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.
38 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
39 #define MTAG_IPFW_RULE 1262273568 /* rule reference */
41 /* Return values from ipfw_chk() */
55 * Structure for collecting parameters to dummynet for ip6_output forwarding
58 struct ip6_pktopts *opt_or;
59 struct route_in6 ro_or;
61 struct ip6_moptions *im6o_or;
62 struct ifnet *origifp_or;
64 struct sockaddr_in6 dst_or;
66 struct route_in6 ro_pmtu_or;
70 * Reference to an ipfw rule that can be carried outside critical sections.
71 * A rule is identified by rulenum:rule_id which is ordered.
72 * In version chain_id the rule can be found in slot 'slot', so
73 * we don't need a lookup if chain_id == chain->id.
75 * On exit from the firewall this structure refers to the rule after
76 * the matching one (slot points to the new rule; rulenum:rule_id-1
77 * is the matching rule), and additional info (e.g. info often contains
78 * the insn argument or tablearg in the low 16 bits, in host format).
79 * On entry, the structure is valid if slot>0, and refers to the starting
80 * rules. 'info' contains the reason for reinject, e.g. divert port,
81 * divert direction, and so on.
83 struct ipfw_rule_ref {
84 uint32_t slot; /* slot for matching rule */
85 uint32_t rulenum; /* matching rule number */
86 uint32_t rule_id; /* matching rule id */
87 uint32_t chain_id; /* ruleset id */
88 uint32_t info; /* see below */
92 IPFW_INFO_MASK = 0x0000ffff,
93 IPFW_INFO_OUT = 0x00000000, /* outgoing, just for convenience */
94 IPFW_INFO_IN = 0x80000000, /* incoming, overloads dir */
95 IPFW_ONEPASS = 0x40000000, /* One-pass, do not reinject */
96 IPFW_IS_MASK = 0x30000000, /* which source ? */
97 IPFW_IS_DIVERT = 0x20000000,
98 IPFW_IS_DUMMYNET =0x10000000,
99 IPFW_IS_PIPE = 0x08000000, /* pip1=1, queue = 0 */
103 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
104 * all into a structure because this way it is easier and more
105 * efficient to pass variables around and extend the interface.
108 struct mbuf *m; /* the mbuf chain */
109 struct ifnet *oif; /* output interface */
110 struct sockaddr_in *next_hop; /* forward address */
113 * On return, it points to the matching rule.
114 * On entry, rule.slot > 0 means the info is valid and
115 * contains the the starting rule for an ipfw search.
116 * If chain_id == chain->id && slot >0 then jump to that slot.
117 * Otherwise, we locate the first rule >= rulenum:rule_id
119 struct ipfw_rule_ref rule; /* match/restart info */
121 struct ether_header *eh; /* for bridged packets */
123 struct ipfw_flow_id f_id; /* grabbed from IP header */
124 //uint32_t cookie; /* a cookie depending on rule action */
127 struct _ip6dn_args dummypar; /* dummynet->ip6_output */
128 struct sockaddr_in hopstore; /* store here if cannot use a pointer */
131 MALLOC_DECLARE(M_IPFW);
134 * Hooks sometime need to know the direction of the packet
135 * (divert, dummynet, netgraph, ...)
136 * We use a generic definition here, with bit0-1 indicating the
137 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
139 * indicating the protocol (if necessary)
147 PROTO_LAYER2 = 0x4, /* set for layer 2 */
148 /* PROTO_DEFAULT = 0, */
151 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
152 /* PROTO_OLDBDG = 0x14, unused, old bridge */
155 /* wrapper for freeing a packet, in case we need to do more work */
157 #define FREE_PKT(m) netisr_dispatch(-1, m)
159 #define FREE_PKT(m) m_freem(m)
163 * Function definitions.
166 /* attach (arg = 1) or detach (arg = 0) hooks */
167 int ipfw_attach_hooks(int);
169 void ipfw_nat_destroy(void);
174 void ipfw_log_bpf(int);
175 void ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
176 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
178 VNET_DECLARE(u_int64_t, norule_counter);
179 #define V_norule_counter VNET(norule_counter)
180 VNET_DECLARE(int, verbose_limit);
181 #define V_verbose_limit VNET(verbose_limit)
183 /* In ip_fw_dynamic.c */
185 enum { /* result for matching dynamic rules */
193 * The lock for dynamic rules is only used once outside the file,
194 * and only to release the result of lookup_dyn_rule().
195 * Eventually we may implement it with a callback on the function.
197 void ipfw_dyn_unlock(void);
200 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
201 u_int32_t, u_int32_t, int);
202 int ipfw_install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
203 struct ip_fw_args *args, uint32_t tablearg);
204 ipfw_dyn_rule *ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt,
205 int *match_direction, struct tcphdr *tcp);
206 void ipfw_remove_dyn_children(struct ip_fw *rule);
207 void ipfw_get_dynamic(char **bp, const char *ep);
209 void ipfw_dyn_attach(void); /* uma_zcreate .... */
210 void ipfw_dyn_detach(void); /* uma_zdestroy ... */
211 void ipfw_dyn_init(void); /* per-vnet initialization */
212 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
213 int ipfw_dyn_len(void);
215 /* common variables */
216 VNET_DECLARE(int, fw_one_pass);
217 #define V_fw_one_pass VNET(fw_one_pass)
219 VNET_DECLARE(int, fw_verbose);
220 #define V_fw_verbose VNET(fw_verbose)
222 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
223 #define V_layer3_chain VNET(layer3_chain)
225 VNET_DECLARE(u_int32_t, set_disable);
226 #define V_set_disable VNET(set_disable)
228 VNET_DECLARE(int, autoinc_step);
229 #define V_autoinc_step VNET(autoinc_step)
232 struct ip_fw *rules; /* list of rules */
233 struct ip_fw *reap; /* list of rules to reap */
234 struct ip_fw *default_rule;
235 int n_rules; /* number of static rules */
236 int static_len; /* total len of static rules */
237 struct ip_fw **map; /* array of rule ptrs to ease lookup */
238 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
239 struct radix_node_head *tables[IPFW_TABLES_MAX];
240 #if defined( __linux__ ) || defined( _WIN32 )
245 struct rwlock uh_lock; /* lock for upper half */
247 uint32_t id; /* ruleset id */
250 struct sockopt; /* used by tcp_var.h */
253 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
254 * so the variable and the macros must be here.
257 #define IPFW_LOCK_INIT(_chain) do { \
258 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
259 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
262 #define IPFW_LOCK_DESTROY(_chain) do { \
263 rw_destroy(&(_chain)->rwmtx); \
264 rw_destroy(&(_chain)->uh_lock); \
267 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
269 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
270 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
271 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
272 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
274 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
275 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
276 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
277 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
279 /* In ip_fw_sockopt.c */
280 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
281 int ipfw_add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule);
282 int ipfw_ctl(struct sockopt *sopt);
283 int ipfw_chk(struct ip_fw_args *args);
284 void ipfw_reap_rules(struct ip_fw *head);
286 /* In ip_fw_table.c */
288 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
290 int ipfw_init_tables(struct ip_fw_chain *ch);
291 int ipfw_flush_table(struct ip_fw_chain *ch, uint16_t tbl);
292 void ipfw_flush_tables(struct ip_fw_chain *ch);
293 int ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
294 uint8_t mlen, uint32_t value);
295 int ipfw_dump_table_entry(struct radix_node *rn, void *arg);
296 int ipfw_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
298 int ipfw_count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
299 int ipfw_dump_table(struct ip_fw_chain *ch, ipfw_table *tbl);
301 /* hooks for divert */
302 extern void (*ip_divert_ptr)(struct mbuf *m, int incoming);
306 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
308 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
309 typedef int ipfw_nat_cfg_t(struct sockopt *);
311 extern ipfw_nat_t *ipfw_nat_ptr;
312 #define IPFW_NAT_LOADED (ipfw_nat_ptr != NULL)
314 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
315 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
316 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
317 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
319 /* netgraph prototypes */
321 typedef int ng_ipfw_input_t(struct mbuf **, int, struct ip_fw_args *, int);
322 extern ng_ipfw_input_t *ng_ipfw_input_p;
323 #define NG_IPFW_LOADED (ng_ipfw_input_p != NULL)
325 #define TAGSIZ (sizeof(struct ng_ipfw_tag) - sizeof(struct m_tag))
329 #endif /* _IPFW2_PRIVATE_H */