blob: 78b73cc10216065df1de954692bac2020e7dc57a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/skbuff.h>
7#include <linux/net.h>
8#include <linux/if.h>
Jan Engelhardt2e3075a2008-01-14 23:40:34 -08009#include <linux/in.h>
10#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/wait.h>
12#include <linux/list.h>
13#endif
Patrick McHardyc8942f12008-05-21 14:08:38 -070014#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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
Florian Westphalf615df72011-01-18 15:52:14 +010027 * number or errno values. Not nice, but better than additional function
28 * arguments. */
29#define NF_VERDICT_MASK 0x000000ff
Harald Welte0ab43f82005-08-09 19:43:44 -070030
Florian Westphalf615df72011-01-18 15:52:14 +010031/* extra verdict flags have mask 0x0000ff00 */
32
33/* queue number (NF_QUEUE) or errno (NF_DROP) */
Harald Welte0ab43f82005-08-09 19:43:44 -070034#define NF_VERDICT_QMASK 0xffff0000
35#define NF_VERDICT_QBITS 16
36
Florian Westphalf615df72011-01-18 15:52:14 +010037#define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
Harald Welte0ab43f82005-08-09 19:43:44 -070038
Florian Westphalf615df72011-01-18 15:52:14 +010039#define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
Eric Parisda683652010-11-16 11:52:38 +000040
Harald Welte6869c4d2005-08-09 19:24:19 -070041/* only for userspace compatibility */
42#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Generic cache responses from hook functions.
44 <= 0x2000 is used for protocol-flags. */
45#define NFC_UNKNOWN 0x4000
46#define NFC_ALTERED 0x8000
Florian Westphalf615df72011-01-18 15:52:14 +010047
48/* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
49#define NF_VERDICT_BITS 16
Harald Welte6869c4d2005-08-09 19:24:19 -070050#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Patrick McHardy6e23ae22007-11-19 18:53:30 -080052enum nf_inet_hooks {
53 NF_INET_PRE_ROUTING,
54 NF_INET_LOCAL_IN,
55 NF_INET_FORWARD,
56 NF_INET_LOCAL_OUT,
57 NF_INET_POST_ROUTING,
58 NF_INET_NUMHOOKS
59};
60
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020061enum {
62 NFPROTO_UNSPEC = 0,
63 NFPROTO_IPV4 = 2,
64 NFPROTO_ARP = 3,
65 NFPROTO_BRIDGE = 7,
66 NFPROTO_IPV6 = 10,
67 NFPROTO_DECNET = 12,
68 NFPROTO_NUMPROTO,
69};
70
Jan Engelhardt643a2c12007-12-17 22:43:50 -080071union nf_inet_addr {
Patrick McHardy7b33ed22008-02-19 17:20:33 -080072 __u32 all[4];
Jan Engelhardt643a2c12007-12-17 22:43:50 -080073 __be32 ip;
74 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080075 struct in_addr in;
76 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080077};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#ifdef CONFIG_NETFILTER
Florian Westphalf615df72011-01-18 15:52:14 +010081static inline int NF_DROP_GETERR(int verdict)
82{
83 return -(verdict >> NF_VERDICT_QBITS);
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Patrick McHardyb8beedd2008-03-25 20:09:33 -070086static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
87 const union nf_inet_addr *a2)
88{
89 return a1->all[0] == a2->all[0] &&
90 a1->all[1] == a2->all[1] &&
91 a1->all[2] == a2->all[2] &&
92 a1->all[3] == a2->all[3];
93}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095extern void netfilter_init(void);
96
97/* Largest hook number + 1 */
98#define NF_MAX_HOOKS 8
99
100struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700103 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 const struct net_device *in,
105 const struct net_device *out,
106 int (*okfn)(struct sk_buff *));
107
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800108struct nf_hook_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct list_head list;
110
111 /* User fills in from here down. */
112 nf_hookfn *hook;
113 struct module *owner;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200114 u_int8_t pf;
115 unsigned int hooknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* Hooks are ordered in ascending priority. */
117 int priority;
118};
119
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800120struct nf_sockopt_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct list_head list;
122
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200123 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
126 int set_optmin;
127 int set_optmax;
128 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100129#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800130 int (*compat_set)(struct sock *sk, int optval,
131 void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int get_optmin;
134 int get_optmax;
135 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100136#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800137 int (*compat_get)(struct sock *sk, int optval,
138 void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100139#endif
Neil Horman16fcec32007-09-11 11:28:26 +0200140 /* Use the module struct to lock set/get code in place */
141 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Function to register/unregister hook points. */
145int nf_register_hook(struct nf_hook_ops *reg);
146void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700147int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
148void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/* Functions to register get/setsockopt ranges (non-inclusive). You
151 need to check permissions yourself! */
152int nf_register_sockopt(struct nf_sockopt_ops *reg);
153void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
154
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100155#ifdef CONFIG_SYSCTL
156/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800157extern struct ctl_path nf_net_netfilter_sysctl_path[];
158extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100159#endif /* CONFIG_SYSCTL */
160
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200161extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200163int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800164 struct net_device *indev, struct net_device *outdev,
165 int (*okfn)(struct sk_buff *), int thresh);
166
167/**
168 * nf_hook_thresh - call a netfilter hook
169 *
170 * Returns 1 if the hook has allowed the packet to pass. The function
171 * okfn must be invoked by the caller in this case. Any other return
172 * value indicates the packet has been consumed by the hook.
173 */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200174static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700175 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800176 struct net_device *indev,
177 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200178 int (*okfn)(struct sk_buff *), int thresh)
Patrick McHardy16a66772006-01-06 23:01:48 -0800179{
180#ifndef CONFIG_NETFILTER_DEBUG
181 if (list_empty(&nf_hooks[pf][hook]))
182 return 1;
183#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800185}
186
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200187static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800188 struct net_device *indev, struct net_device *outdev,
189 int (*okfn)(struct sk_buff *))
190{
Jan Engelhardt23f37332009-06-05 17:31:46 +0200191 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
Patrick McHardy16a66772006-01-06 23:01:48 -0800192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* Activate hook; either okfn or kfree_skb called, unless a hook
195 returns NF_STOLEN (in which case, it's up to the hook to deal with
196 the consequences).
197
198 Returns -ERRNO if packet dropped. Zero means queued, stolen or
199 accepted.
200*/
201
202/* RR:
203 > I don't want nf_hook to return anything because people might forget
204 > about async and trust the return value to mean "packet was ok".
205
206 AK:
207 Just document it clearly, then you can expect some sense from kernel
208 coders :)
209*/
210
Jan Engelhardt22490652009-06-13 04:13:26 +0200211static inline int
212NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
213 struct net_device *in, struct net_device *out,
214 int (*okfn)(struct sk_buff *), int thresh)
215{
216 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
217 if (ret == 1)
218 ret = okfn(skb);
219 return ret;
220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jan Engelhardt22490652009-06-13 04:13:26 +0200222static inline int
223NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
224 struct net_device *in, struct net_device *out,
225 int (*okfn)(struct sk_buff *), bool cond)
226{
Patrick McHardy4bac6b12010-02-19 08:03:28 +0100227 int ret;
228
229 if (!cond ||
Eric Parisac5aa2e2010-11-12 08:26:06 +0100230 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
Jan Engelhardt22490652009-06-13 04:13:26 +0200231 ret = okfn(skb);
232 return ret;
233}
Patrick McHardy16a66772006-01-06 23:01:48 -0800234
Jan Engelhardt22490652009-06-13 04:13:26 +0200235static inline int
236NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
237 struct net_device *in, struct net_device *out,
238 int (*okfn)(struct sk_buff *))
239{
240 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/* Call setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200244int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
David S. Millerb7058842009-09-30 16:12:20 -0700245 unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200246int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100248#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200249int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
David S. Millerb7058842009-09-30 16:12:20 -0700250 char __user *opt, unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200251int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800252 char __user *opt, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100253#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800254
Harald Welte089af262005-08-09 19:37:23 -0700255/* Call this before modifying an existing packet: ensures it is
256 modifiable and linear to the point you care about (writable_len).
257 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700258extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700259
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800260struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800261struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800262
Patrick McHardybce80322006-04-06 14:18:09 -0700263struct nf_afinfo {
264 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800265 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700266 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100267 __sum16 (*checksum_partial)(struct sk_buff *skb,
268 unsigned int hook,
269 unsigned int dataoff,
270 unsigned int len,
271 u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800272 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700273 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800274 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700275 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800276 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700277 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700278};
279
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100280extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800281static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700282{
283 return rcu_dereference(nf_afinfo[family]);
284}
Harald Welte2cc7d572005-08-09 19:42:34 -0700285
Al Virob51655b2006-11-14 21:40:42 -0800286static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700287nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
288 u_int8_t protocol, unsigned short family)
289{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800290 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800291 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700292
293 rcu_read_lock();
294 afinfo = nf_get_afinfo(family);
295 if (afinfo)
296 csum = afinfo->checksum(skb, hook, dataoff, protocol);
297 rcu_read_unlock();
298 return csum;
299}
300
Patrick McHardyd63a6502008-03-20 15:15:53 +0100301static inline __sum16
302nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
303 unsigned int dataoff, unsigned int len,
304 u_int8_t protocol, unsigned short family)
305{
306 const struct nf_afinfo *afinfo;
307 __sum16 csum = 0;
308
309 rcu_read_lock();
310 afinfo = nf_get_afinfo(family);
311 if (afinfo)
312 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
313 protocol);
314 rcu_read_unlock();
315 return csum;
316}
317
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800318extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
319extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700320
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800321#include <net/flow.h>
322extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
323
324static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200325nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800326{
Patrick McHardy051578c2007-12-17 22:42:51 -0800327#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800328 void (*decodefn)(struct sk_buff *, struct flowi *);
329
Patrick McHardy051578c2007-12-17 22:42:51 -0800330 if (family == AF_INET) {
331 rcu_read_lock();
332 decodefn = rcu_dereference(ip_nat_decode_session);
333 if (decodefn)
334 decodefn(skb, fl);
335 rcu_read_unlock();
336 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800337#endif
338}
339
Harald Welte608c8e42005-08-09 19:58:27 -0700340#ifdef CONFIG_PROC_FS
341#include <linux/proc_fs.h>
342extern struct proc_dir_entry *proc_net_netfilter;
343#endif
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#else /* !CONFIG_NETFILTER */
346#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800347#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200348static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700349 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800350 struct net_device *indev,
351 struct net_device *outdev,
Jan Engelhardt23f37332009-06-05 17:31:46 +0200352 int (*okfn)(struct sk_buff *), int thresh)
David S. Millerf53b61d2006-01-07 12:50:27 -0800353{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700354 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800355}
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200356static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800357 struct net_device *indev, struct net_device *outdev,
358 int (*okfn)(struct sk_buff *))
359{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800360 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800361}
David S. Millerf53b61d2006-01-07 12:50:27 -0800362struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800363static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200364nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
365{
366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#endif /*CONFIG_NETFILTER*/
368
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700369#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100370extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700371extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Eric Dumazet0e60ebe2010-11-15 18:17:21 +0100372extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700373#else
374static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
375#endif
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif /*__KERNEL__*/
378#endif /*__LINUX_NETFILTER_H*/