blob: e4c66593b5c697f32d140cdc34a121c1173425d9 [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>
Alexey Dobriyan666953df2008-04-14 09:56:02 +02009#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/if.h>
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080011#include <linux/in.h>
12#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/wait.h>
14#include <linux/list.h>
Alexey Dobriyan666953df2008-04-14 09:56:02 +020015#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#endif
17#include <linux/compiler.h>
18
19/* Responses from hook functions. */
20#define NF_DROP 0
21#define NF_ACCEPT 1
22#define NF_STOLEN 2
23#define NF_QUEUE 3
24#define NF_REPEAT 4
25#define NF_STOP 5
26#define NF_MAX_VERDICT NF_STOP
27
Harald Welte0ab43f82005-08-09 19:43:44 -070028/* we overload the higher bits for encoding auxiliary data such as the queue
29 * number. Not nice, but better than additional function arguments. */
30#define NF_VERDICT_MASK 0x0000ffff
31#define NF_VERDICT_BITS 16
32
33#define NF_VERDICT_QMASK 0xffff0000
34#define NF_VERDICT_QBITS 16
35
Patrick McHardyfbabbed2008-02-27 12:21:18 -080036#define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
Harald Welte0ab43f82005-08-09 19:43:44 -070037
Harald Welte6869c4d2005-08-09 19:24:19 -070038/* only for userspace compatibility */
39#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/* Generic cache responses from hook functions.
41 <= 0x2000 is used for protocol-flags. */
42#define NFC_UNKNOWN 0x4000
43#define NFC_ALTERED 0x8000
Harald Welte6869c4d2005-08-09 19:24:19 -070044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Patrick McHardy6e23ae22007-11-19 18:53:30 -080046enum nf_inet_hooks {
47 NF_INET_PRE_ROUTING,
48 NF_INET_LOCAL_IN,
49 NF_INET_FORWARD,
50 NF_INET_LOCAL_OUT,
51 NF_INET_POST_ROUTING,
52 NF_INET_NUMHOOKS
53};
54
Jan Engelhardt643a2c12007-12-17 22:43:50 -080055union nf_inet_addr {
Patrick McHardy7b33ed22008-02-19 17:20:33 -080056 __u32 all[4];
Jan Engelhardt643a2c12007-12-17 22:43:50 -080057 __be32 ip;
58 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080059 struct in_addr in;
60 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080061};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef CONFIG_NETFILTER
65
Patrick McHardyb8beedd2008-03-25 20:09:33 -070066static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
67 const union nf_inet_addr *a2)
68{
69 return a1->all[0] == a2->all[0] &&
70 a1->all[1] == a2->all[1] &&
71 a1->all[2] == a2->all[2] &&
72 a1->all[3] == a2->all[3];
73}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075extern void netfilter_init(void);
76
77/* Largest hook number + 1 */
78#define NF_MAX_HOOKS 8
79
80struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070083 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 const struct net_device *in,
85 const struct net_device *out,
86 int (*okfn)(struct sk_buff *));
87
88struct nf_hook_ops
89{
90 struct list_head list;
91
92 /* User fills in from here down. */
93 nf_hookfn *hook;
94 struct module *owner;
95 int pf;
96 int hooknum;
97 /* Hooks are ordered in ascending priority. */
98 int priority;
99};
100
101struct nf_sockopt_ops
102{
103 struct list_head list;
104
105 int pf;
106
107 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
108 int set_optmin;
109 int set_optmax;
110 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800111 int (*compat_set)(struct sock *sk, int optval,
112 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 int get_optmin;
115 int get_optmax;
116 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800117 int (*compat_get)(struct sock *sk, int optval,
118 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Neil Horman16fcec32007-09-11 11:28:26 +0200120 /* Use the module struct to lock set/get code in place */
121 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* Function to register/unregister hook points. */
125int nf_register_hook(struct nf_hook_ops *reg);
126void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700127int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
128void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* Functions to register get/setsockopt ranges (non-inclusive). You
131 need to check permissions yourself! */
132int nf_register_sockopt(struct nf_sockopt_ops *reg);
133void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
134
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100135#ifdef CONFIG_SYSCTL
136/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800137extern struct ctl_path nf_net_netfilter_sysctl_path[];
138extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100139#endif /* CONFIG_SYSCTL */
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
142
Herbert Xu3db05fe2007-10-15 00:53:15 -0700143int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800144 struct net_device *indev, struct net_device *outdev,
145 int (*okfn)(struct sk_buff *), int thresh);
146
147/**
148 * nf_hook_thresh - call a netfilter hook
149 *
150 * Returns 1 if the hook has allowed the packet to pass. The function
151 * okfn must be invoked by the caller in this case. Any other return
152 * value indicates the packet has been consumed by the hook.
153 */
154static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700155 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800156 struct net_device *indev,
157 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800158 int (*okfn)(struct sk_buff *), int thresh,
159 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800160{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800161 if (!cond)
162 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800163#ifndef CONFIG_NETFILTER_DEBUG
164 if (list_empty(&nf_hooks[pf][hook]))
165 return 1;
166#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700167 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800168}
169
Herbert Xu3db05fe2007-10-15 00:53:15 -0700170static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800171 struct net_device *indev, struct net_device *outdev,
172 int (*okfn)(struct sk_buff *))
173{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700174 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/* Activate hook; either okfn or kfree_skb called, unless a hook
178 returns NF_STOLEN (in which case, it's up to the hook to deal with
179 the consequences).
180
181 Returns -ERRNO if packet dropped. Zero means queued, stolen or
182 accepted.
183*/
184
185/* RR:
186 > I don't want nf_hook to return anything because people might forget
187 > about async and trust the return value to mean "packet was ok".
188
189 AK:
190 Just document it clearly, then you can expect some sense from kernel
191 coders :)
192*/
193
194/* This is gross, but inline doesn't cut it for avoiding the function
195 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Patrick McHardy16a66772006-01-06 23:01:48 -0800197/* HX: It's slightly less gross now. */
198
199#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
200({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700201if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800202 __ret = (okfn)(skb); \
203__ret;})
204
205#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
206({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700207if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800208 __ret = (okfn)(skb); \
209__ret;})
210
211#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
212 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214/* Call setsockopt() */
215int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
216 int len);
217int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
218 int *len);
219
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800220int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
221 char __user *opt, int len);
222int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
223 char __user *opt, int *len);
224
Harald Welte089af262005-08-09 19:37:23 -0700225/* Call this before modifying an existing packet: ensures it is
226 modifiable and linear to the point you care about (writable_len).
227 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700228extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700229
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800230struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800231struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800232
Patrick McHardybce80322006-04-06 14:18:09 -0700233struct nf_afinfo {
234 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800235 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700236 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100237 __sum16 (*checksum_partial)(struct sk_buff *skb,
238 unsigned int hook,
239 unsigned int dataoff,
240 unsigned int len,
241 u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800242 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700243 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800244 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700245 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800246 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700247 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700248};
249
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800250extern const struct nf_afinfo *nf_afinfo[NPROTO];
251static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700252{
253 return rcu_dereference(nf_afinfo[family]);
254}
Harald Welte2cc7d572005-08-09 19:42:34 -0700255
Al Virob51655b2006-11-14 21:40:42 -0800256static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700257nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
258 u_int8_t protocol, unsigned short family)
259{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800260 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800261 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700262
263 rcu_read_lock();
264 afinfo = nf_get_afinfo(family);
265 if (afinfo)
266 csum = afinfo->checksum(skb, hook, dataoff, protocol);
267 rcu_read_unlock();
268 return csum;
269}
270
Patrick McHardyd63a6502008-03-20 15:15:53 +0100271static inline __sum16
272nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
273 unsigned int dataoff, unsigned int len,
274 u_int8_t protocol, unsigned short family)
275{
276 const struct nf_afinfo *afinfo;
277 __sum16 csum = 0;
278
279 rcu_read_lock();
280 afinfo = nf_get_afinfo(family);
281 if (afinfo)
282 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
283 protocol);
284 rcu_read_unlock();
285 return csum;
286}
287
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800288extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
289extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700290
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800291#include <net/flow.h>
292extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
293
294static inline void
295nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
296{
Patrick McHardy051578c2007-12-17 22:42:51 -0800297#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800298 void (*decodefn)(struct sk_buff *, struct flowi *);
299
Patrick McHardy051578c2007-12-17 22:42:51 -0800300 if (family == AF_INET) {
301 rcu_read_lock();
302 decodefn = rcu_dereference(ip_nat_decode_session);
303 if (decodefn)
304 decodefn(skb, fl);
305 rcu_read_unlock();
306 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800307#endif
308}
309
Harald Welte608c8e42005-08-09 19:58:27 -0700310#ifdef CONFIG_PROC_FS
311#include <linux/proc_fs.h>
312extern struct proc_dir_entry *proc_net_netfilter;
313#endif
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#else /* !CONFIG_NETFILTER */
316#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800317#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800318static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700319 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800320 struct net_device *indev,
321 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800322 int (*okfn)(struct sk_buff *), int thresh,
323 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800324{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700325 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800326}
Herbert Xu3db05fe2007-10-15 00:53:15 -0700327static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800328 struct net_device *indev, struct net_device *outdev,
329 int (*okfn)(struct sk_buff *))
330{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800331 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800332}
David S. Millerf53b61d2006-01-07 12:50:27 -0800333struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800334static inline void
335nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#endif /*CONFIG_NETFILTER*/
337
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700338#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
339extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
340extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700341extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700342#else
343static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
344#endif
345
Alexey Dobriyan666953df2008-04-14 09:56:02 +0200346static inline struct net *nf_pre_routing_net(const struct net_device *in,
347 const struct net_device *out)
348{
349#ifdef CONFIG_NET_NS
350 return in->nd_net;
351#else
352 return &init_net;
353#endif
354}
355
356static inline struct net *nf_local_in_net(const struct net_device *in,
357 const struct net_device *out)
358{
359#ifdef CONFIG_NET_NS
360 return in->nd_net;
361#else
362 return &init_net;
363#endif
364}
365
366static inline struct net *nf_forward_net(const struct net_device *in,
367 const struct net_device *out)
368{
369#ifdef CONFIG_NET_NS
370 BUG_ON(in->nd_net != out->nd_net);
371 return in->nd_net;
372#else
373 return &init_net;
374#endif
375}
376
377static inline struct net *nf_local_out_net(const struct net_device *in,
378 const struct net_device *out)
379{
380#ifdef CONFIG_NET_NS
381 return out->nd_net;
382#else
383 return &init_net;
384#endif
385}
386
387static inline struct net *nf_post_routing_net(const struct net_device *in,
388 const struct net_device *out)
389{
390#ifdef CONFIG_NET_NS
391 return out->nd_net;
392#else
393 return &init_net;
394#endif
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif /*__KERNEL__*/
398#endif /*__LINUX_NETFILTER_H*/