41ae8456f8345dbaa376e8568766e0fd1d868814
[ipfw.git] / dummynet2 / include / netinet / ipfw / ip_fw_private.h
1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: head/sys/netinet/ipfw/ip_fw_private.h 200601 2009-12-16 10:48:40Z luigi $
26  */
27
28 #ifndef _IPFW2_PRIVATE_H
29 #define _IPFW2_PRIVATE_H
30
31 /*
32  * Internal constants and data structures used by ipfw components
33  * and not meant to be exported outside the kernel.
34  */
35
36 #ifdef _KERNEL
37
38 #define MTAG_IPFW       1148380143      /* IPFW-tagged cookie */
39 #define MTAG_IPFW_RULE  1262273568      /* rule reference */
40
41 /* Return values from ipfw_chk() */
42 enum {
43         IP_FW_PASS = 0,
44         IP_FW_DENY,
45         IP_FW_DIVERT,
46         IP_FW_TEE,
47         IP_FW_DUMMYNET,
48         IP_FW_NETGRAPH,
49         IP_FW_NGTEE,
50         IP_FW_NAT,
51         IP_FW_REASS,
52 };
53
54 /*
55  * Structure for collecting parameters to dummynet for ip6_output forwarding
56  */
57 struct _ip6dn_args {
58        struct ip6_pktopts *opt_or;
59        struct route_in6 ro_or;
60        int flags_or;
61        struct ip6_moptions *im6o_or;
62        struct ifnet *origifp_or;
63        struct ifnet *ifp_or;
64        struct sockaddr_in6 dst_or;
65        u_long mtu_or;
66        struct route_in6 ro_pmtu_or;
67 };
68
69 /*
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.
74  *
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.
82  */
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                    */
89 };
90
91 enum {
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 */
100 };
101
102 /*
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.
106  */
107 struct ip_fw_args {
108         struct mbuf     *m;             /* the mbuf chain               */
109         struct ifnet    *oif;           /* output interface             */
110         struct sockaddr_in *next_hop;   /* forward address              */
111
112         /*
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
118          */
119         struct ipfw_rule_ref rule;      /* match/restart info           */
120
121         struct ether_header *eh;        /* for bridged packets          */
122
123         struct ipfw_flow_id f_id;       /* grabbed from IP header       */
124         //uint32_t      cookie;         /* a cookie depending on rule action */
125         struct inpcb    *inp;
126
127         struct _ip6dn_args      dummypar; /* dummynet->ip6_output */
128         struct sockaddr_in hopstore;    /* store here if cannot use a pointer */
129 };
130
131 MALLOC_DECLARE(M_IPFW);
132
133 /*
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
138  * specific protocol
139  * indicating the protocol (if necessary)
140  */
141 enum {
142         DIR_MASK =      0x3,
143         DIR_OUT =       0,
144         DIR_IN =        1,
145         DIR_FWD =       2,
146         DIR_DROP =      3,
147         PROTO_LAYER2 =  0x4, /* set for layer 2 */
148         /* PROTO_DEFAULT = 0, */
149         PROTO_IPV4 =    0x08,
150         PROTO_IPV6 =    0x10,
151         PROTO_IFB =     0x0c, /* layer2 + ifbridge */
152     /*  PROTO_OLDBDG =  0x14, unused, old bridge */
153 };
154
155 /* wrapper for freeing a packet, in case we need to do more work */
156 #ifdef __linux__
157 #define FREE_PKT(m)     netisr_dispatch(-1, m)
158 #else
159 #define FREE_PKT(m)     m_freem(m)
160 #endif
161
162 /*
163  * Function definitions.
164  */
165
166 /* attach (arg = 1) or detach (arg = 0) hooks */
167 int ipfw_attach_hooks(int);
168 #ifdef NOTYET
169 void ipfw_nat_destroy(void);
170 #endif
171
172 /* In ip_fw_log.c */
173 struct ip;
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,
177         struct ip *ip);
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)
182
183 /* In ip_fw_dynamic.c */
184
185 enum { /* result for matching dynamic rules */
186         MATCH_REVERSE = 0,
187         MATCH_FORWARD,
188         MATCH_NONE,
189         MATCH_UNKNOWN,
190 };
191
192 /*
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.
196  */
197 void ipfw_dyn_unlock(void);
198
199 struct tcphdr;
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);
208
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);
214
215 /* common variables */
216 VNET_DECLARE(int, fw_one_pass);
217 #define V_fw_one_pass           VNET(fw_one_pass)
218
219 VNET_DECLARE(int, fw_verbose);
220 #define V_fw_verbose            VNET(fw_verbose)
221
222 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
223 #define V_layer3_chain          VNET(layer3_chain)
224
225 VNET_DECLARE(u_int32_t, set_disable);
226 #define V_set_disable           VNET(set_disable)
227
228 VNET_DECLARE(int, autoinc_step);
229 #define V_autoinc_step          VNET(autoinc_step)
230
231 struct ip_fw_chain {
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 )
241         spinlock_t rwmtx;
242         spinlock_t uh_lock;
243 #else
244         struct rwlock   rwmtx;
245         struct rwlock   uh_lock;        /* lock for upper half */
246 #endif
247         uint32_t        id;             /* ruleset id */
248 };
249
250 struct sockopt; /* used by tcp_var.h */
251
252 /*
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.
255  */
256
257 #define IPFW_LOCK_INIT(_chain) do {                     \
258         rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
259         rw_init(&(_chain)->uh_lock, "IPFW UH lock");    \
260         } while (0)
261
262 #define IPFW_LOCK_DESTROY(_chain) do {                  \
263         rw_destroy(&(_chain)->rwmtx);                   \
264         rw_destroy(&(_chain)->uh_lock);                 \
265         } while (0)
266
267 #define IPFW_WLOCK_ASSERT(_chain)       rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
268
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)
273
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)
278
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);
285
286 /* In ip_fw_table.c */
287 struct radix_node;
288 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
289     uint32_t *val);
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,
297     uint8_t mlen);
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);
300
301 /* hooks for divert */
302 extern void (*ip_divert_ptr)(struct mbuf *m, int incoming);
303
304 /* In ip_fw_nat.c */
305
306 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
307
308 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
309 typedef int ipfw_nat_cfg_t(struct sockopt *);
310
311 extern ipfw_nat_t *ipfw_nat_ptr;
312 #define IPFW_NAT_LOADED (ipfw_nat_ptr != NULL)
313
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;
318
319 /* netgraph prototypes */
320
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)
324
325 #define TAGSIZ  (sizeof(struct ng_ipfw_tag) - sizeof(struct m_tag))
326
327
328 #endif /* _KERNEL */
329 #endif /* _IPFW2_PRIVATE_H */