blob: d74e79bacd2d19c240f5b8d27ba6fdd123c8ca9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
4#ifdef __KERNEL__
5#include <linux/init.h>
6#include <linux/types.h>
7#include <linux/skbuff.h>
8#include <linux/net.h>
9#include <linux/if.h>
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080010#include <linux/in.h>
11#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/wait.h>
13#include <linux/list.h>
14#endif
15#include <linux/compiler.h>
16
17/* Responses from hook functions. */
18#define NF_DROP 0
19#define NF_ACCEPT 1
20#define NF_STOLEN 2
21#define NF_QUEUE 3
22#define NF_REPEAT 4
23#define NF_STOP 5
24#define NF_MAX_VERDICT NF_STOP
25
Harald Welte0ab43f82005-08-09 19:43:44 -070026/* we overload the higher bits for encoding auxiliary data such as the queue
27 * number. Not nice, but better than additional function arguments. */
28#define NF_VERDICT_MASK 0x0000ffff
29#define NF_VERDICT_BITS 16
30
31#define NF_VERDICT_QMASK 0xffff0000
32#define NF_VERDICT_QBITS 16
33
Harald Welteb766b302005-08-12 11:36:44 -070034#define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
Harald Welte0ab43f82005-08-09 19:43:44 -070035
Harald Welte6869c4d2005-08-09 19:24:19 -070036/* only for userspace compatibility */
37#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Generic cache responses from hook functions.
39 <= 0x2000 is used for protocol-flags. */
40#define NFC_UNKNOWN 0x4000
41#define NFC_ALTERED 0x8000
Harald Welte6869c4d2005-08-09 19:24:19 -070042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Patrick McHardy6e23ae22007-11-19 18:53:30 -080044enum nf_inet_hooks {
45 NF_INET_PRE_ROUTING,
46 NF_INET_LOCAL_IN,
47 NF_INET_FORWARD,
48 NF_INET_LOCAL_OUT,
49 NF_INET_POST_ROUTING,
50 NF_INET_NUMHOOKS
51};
52
Jan Engelhardt643a2c12007-12-17 22:43:50 -080053union nf_inet_addr {
54 u_int32_t all[4];
55 __be32 ip;
56 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080057 struct in_addr in;
58 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_NETFILTER
63
64extern void netfilter_init(void);
65
66/* Largest hook number + 1 */
67#define NF_MAX_HOOKS 8
68
69struct sk_buff;
70struct net_device;
71
72typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070073 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 const struct net_device *in,
75 const struct net_device *out,
76 int (*okfn)(struct sk_buff *));
77
78struct nf_hook_ops
79{
80 struct list_head list;
81
82 /* User fills in from here down. */
83 nf_hookfn *hook;
84 struct module *owner;
85 int pf;
86 int hooknum;
87 /* Hooks are ordered in ascending priority. */
88 int priority;
89};
90
91struct nf_sockopt_ops
92{
93 struct list_head list;
94
95 int pf;
96
97 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
98 int set_optmin;
99 int set_optmax;
100 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800101 int (*compat_set)(struct sock *sk, int optval,
102 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 int get_optmin;
105 int get_optmax;
106 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800107 int (*compat_get)(struct sock *sk, int optval,
108 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Neil Horman16fcec32007-09-11 11:28:26 +0200110 /* Use the module struct to lock set/get code in place */
111 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Function to register/unregister hook points. */
115int nf_register_hook(struct nf_hook_ops *reg);
116void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700117int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
118void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Functions to register get/setsockopt ranges (non-inclusive). You
121 need to check permissions yourself! */
122int nf_register_sockopt(struct nf_sockopt_ops *reg);
123void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
124
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100125#ifdef CONFIG_SYSCTL
126/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800127extern struct ctl_path nf_net_netfilter_sysctl_path[];
128extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100129#endif /* CONFIG_SYSCTL */
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
132
Herbert Xu3db05fe2007-10-15 00:53:15 -0700133int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800134 struct net_device *indev, struct net_device *outdev,
135 int (*okfn)(struct sk_buff *), int thresh);
136
137/**
138 * nf_hook_thresh - call a netfilter hook
139 *
140 * Returns 1 if the hook has allowed the packet to pass. The function
141 * okfn must be invoked by the caller in this case. Any other return
142 * value indicates the packet has been consumed by the hook.
143 */
144static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700145 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800146 struct net_device *indev,
147 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800148 int (*okfn)(struct sk_buff *), int thresh,
149 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800150{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800151 if (!cond)
152 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800153#ifndef CONFIG_NETFILTER_DEBUG
154 if (list_empty(&nf_hooks[pf][hook]))
155 return 1;
156#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700157 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800158}
159
Herbert Xu3db05fe2007-10-15 00:53:15 -0700160static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800161 struct net_device *indev, struct net_device *outdev,
162 int (*okfn)(struct sk_buff *))
163{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700164 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* Activate hook; either okfn or kfree_skb called, unless a hook
168 returns NF_STOLEN (in which case, it's up to the hook to deal with
169 the consequences).
170
171 Returns -ERRNO if packet dropped. Zero means queued, stolen or
172 accepted.
173*/
174
175/* RR:
176 > I don't want nf_hook to return anything because people might forget
177 > about async and trust the return value to mean "packet was ok".
178
179 AK:
180 Just document it clearly, then you can expect some sense from kernel
181 coders :)
182*/
183
184/* This is gross, but inline doesn't cut it for avoiding the function
185 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Patrick McHardy16a66772006-01-06 23:01:48 -0800187/* HX: It's slightly less gross now. */
188
189#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
190({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700191if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800192 __ret = (okfn)(skb); \
193__ret;})
194
195#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
196({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700197if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800198 __ret = (okfn)(skb); \
199__ret;})
200
201#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
202 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/* Call setsockopt() */
205int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
206 int len);
207int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
208 int *len);
209
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800210int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
211 char __user *opt, int len);
212int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
213 char __user *opt, int *len);
214
Harald Welte089af262005-08-09 19:37:23 -0700215/* Call this before modifying an existing packet: ensures it is
216 modifiable and linear to the point you care about (writable_len).
217 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700218extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700219
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800220struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800221struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800222
Patrick McHardybce80322006-04-06 14:18:09 -0700223struct nf_afinfo {
224 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800225 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700226 unsigned int dataoff, u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800227 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700228 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800229 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700230 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800231 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700232 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700233};
234
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800235extern const struct nf_afinfo *nf_afinfo[NPROTO];
236static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700237{
238 return rcu_dereference(nf_afinfo[family]);
239}
Harald Welte2cc7d572005-08-09 19:42:34 -0700240
Al Virob51655b2006-11-14 21:40:42 -0800241static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700242nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
243 u_int8_t protocol, unsigned short family)
244{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800245 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800246 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700247
248 rcu_read_lock();
249 afinfo = nf_get_afinfo(family);
250 if (afinfo)
251 csum = afinfo->checksum(skb, hook, dataoff, protocol);
252 rcu_read_unlock();
253 return csum;
254}
255
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800256extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
257extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700258
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800259#include <net/flow.h>
260extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
261
262static inline void
263nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
264{
Patrick McHardy051578c2007-12-17 22:42:51 -0800265#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800266 void (*decodefn)(struct sk_buff *, struct flowi *);
267
Patrick McHardy051578c2007-12-17 22:42:51 -0800268 if (family == AF_INET) {
269 rcu_read_lock();
270 decodefn = rcu_dereference(ip_nat_decode_session);
271 if (decodefn)
272 decodefn(skb, fl);
273 rcu_read_unlock();
274 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800275#endif
276}
277
Harald Welte608c8e42005-08-09 19:58:27 -0700278#ifdef CONFIG_PROC_FS
279#include <linux/proc_fs.h>
280extern struct proc_dir_entry *proc_net_netfilter;
281#endif
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#else /* !CONFIG_NETFILTER */
284#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800285#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800286static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700287 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800288 struct net_device *indev,
289 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800290 int (*okfn)(struct sk_buff *), int thresh,
291 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800292{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700293 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800294}
Herbert Xu3db05fe2007-10-15 00:53:15 -0700295static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800296 struct net_device *indev, struct net_device *outdev,
297 int (*okfn)(struct sk_buff *))
298{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800299 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800300}
David S. Millerf53b61d2006-01-07 12:50:27 -0800301struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800302static inline void
303nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#endif /*CONFIG_NETFILTER*/
305
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700306#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
307extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
308extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700309extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700310#else
311static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
312#endif
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif /*__KERNEL__*/
315#endif /*__LINUX_NETFILTER_H*/