blob: 78f33d22368058f92e3d98bdc52e1a0f3567a244 [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
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
Patrick McHardyfbabbed2008-02-27 12:21:18 -080034#define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & 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 Engelhardt7e9c6ee2008-10-08 11:35:00 +020053enum {
54 NFPROTO_UNSPEC = 0,
55 NFPROTO_IPV4 = 2,
56 NFPROTO_ARP = 3,
57 NFPROTO_BRIDGE = 7,
58 NFPROTO_IPV6 = 10,
59 NFPROTO_DECNET = 12,
60 NFPROTO_NUMPROTO,
61};
62
Jan Engelhardt643a2c12007-12-17 22:43:50 -080063union nf_inet_addr {
Patrick McHardy7b33ed22008-02-19 17:20:33 -080064 __u32 all[4];
Jan Engelhardt643a2c12007-12-17 22:43:50 -080065 __be32 ip;
66 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080067 struct in_addr in;
68 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#ifdef CONFIG_NETFILTER
73
Patrick McHardyb8beedd2008-03-25 20:09:33 -070074static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
75 const union nf_inet_addr *a2)
76{
77 return a1->all[0] == a2->all[0] &&
78 a1->all[1] == a2->all[1] &&
79 a1->all[2] == a2->all[2] &&
80 a1->all[3] == a2->all[3];
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083extern void netfilter_init(void);
84
85/* Largest hook number + 1 */
86#define NF_MAX_HOOKS 8
87
88struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070091 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 const struct net_device *in,
93 const struct net_device *out,
94 int (*okfn)(struct sk_buff *));
95
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080096struct nf_hook_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct list_head list;
98
99 /* User fills in from here down. */
100 nf_hookfn *hook;
101 struct module *owner;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200102 u_int8_t pf;
103 unsigned int hooknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* Hooks are ordered in ascending priority. */
105 int priority;
106};
107
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800108struct nf_sockopt_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct list_head list;
110
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200111 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
114 int set_optmin;
115 int set_optmax;
116 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100117#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800118 int (*compat_set)(struct sock *sk, int optval,
119 void __user *user, unsigned int len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int get_optmin;
122 int get_optmax;
123 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100124#ifdef CONFIG_COMPAT
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800125 int (*compat_get)(struct sock *sk, int optval,
126 void __user *user, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100127#endif
Neil Horman16fcec32007-09-11 11:28:26 +0200128 /* Use the module struct to lock set/get code in place */
129 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/* Function to register/unregister hook points. */
133int nf_register_hook(struct nf_hook_ops *reg);
134void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700135int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
136void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* Functions to register get/setsockopt ranges (non-inclusive). You
139 need to check permissions yourself! */
140int nf_register_sockopt(struct nf_sockopt_ops *reg);
141void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
142
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100143#ifdef CONFIG_SYSCTL
144/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800145extern struct ctl_path nf_net_netfilter_sysctl_path[];
146extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100147#endif /* CONFIG_SYSCTL */
148
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200149extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200151int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800152 struct net_device *indev, struct net_device *outdev,
153 int (*okfn)(struct sk_buff *), int thresh);
154
155/**
156 * nf_hook_thresh - call a netfilter hook
157 *
158 * Returns 1 if the hook has allowed the packet to pass. The function
159 * okfn must be invoked by the caller in this case. Any other return
160 * value indicates the packet has been consumed by the hook.
161 */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200162static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700163 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800164 struct net_device *indev,
165 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800166 int (*okfn)(struct sk_buff *), int thresh,
167 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800168{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800169 if (!cond)
170 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800171#ifndef CONFIG_NETFILTER_DEBUG
172 if (list_empty(&nf_hooks[pf][hook]))
173 return 1;
174#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700175 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800176}
177
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200178static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800179 struct net_device *indev, struct net_device *outdev,
180 int (*okfn)(struct sk_buff *))
181{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700182 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/* Activate hook; either okfn or kfree_skb called, unless a hook
186 returns NF_STOLEN (in which case, it's up to the hook to deal with
187 the consequences).
188
189 Returns -ERRNO if packet dropped. Zero means queued, stolen or
190 accepted.
191*/
192
193/* RR:
194 > I don't want nf_hook to return anything because people might forget
195 > about async and trust the return value to mean "packet was ok".
196
197 AK:
198 Just document it clearly, then you can expect some sense from kernel
199 coders :)
200*/
201
202/* This is gross, but inline doesn't cut it for avoiding the function
203 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Patrick McHardy16a66772006-01-06 23:01:48 -0800205/* HX: It's slightly less gross now. */
206
207#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
208({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700209if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800210 __ret = (okfn)(skb); \
211__ret;})
212
213#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
214({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700215if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800216 __ret = (okfn)(skb); \
217__ret;})
218
219#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
220 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/* Call setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200223int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
David S. Millerb7058842009-09-30 16:12:20 -0700224 unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200225int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100227#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200228int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
David S. Millerb7058842009-09-30 16:12:20 -0700229 char __user *opt, unsigned int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200230int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800231 char __user *opt, int *len);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100232#endif
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800233
Harald Welte089af262005-08-09 19:37:23 -0700234/* Call this before modifying an existing packet: ensures it is
235 modifiable and linear to the point you care about (writable_len).
236 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700237extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700238
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800239struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800240struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800241
Patrick McHardybce80322006-04-06 14:18:09 -0700242struct nf_afinfo {
243 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800244 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700245 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100246 __sum16 (*checksum_partial)(struct sk_buff *skb,
247 unsigned int hook,
248 unsigned int dataoff,
249 unsigned int len,
250 u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800251 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700252 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800253 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700254 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800255 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700256 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700257};
258
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200259extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800260static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700261{
262 return rcu_dereference(nf_afinfo[family]);
263}
Harald Welte2cc7d572005-08-09 19:42:34 -0700264
Al Virob51655b2006-11-14 21:40:42 -0800265static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700266nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
267 u_int8_t protocol, unsigned short family)
268{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800269 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800270 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700271
272 rcu_read_lock();
273 afinfo = nf_get_afinfo(family);
274 if (afinfo)
275 csum = afinfo->checksum(skb, hook, dataoff, protocol);
276 rcu_read_unlock();
277 return csum;
278}
279
Patrick McHardyd63a6502008-03-20 15:15:53 +0100280static inline __sum16
281nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
282 unsigned int dataoff, unsigned int len,
283 u_int8_t protocol, unsigned short family)
284{
285 const struct nf_afinfo *afinfo;
286 __sum16 csum = 0;
287
288 rcu_read_lock();
289 afinfo = nf_get_afinfo(family);
290 if (afinfo)
291 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
292 protocol);
293 rcu_read_unlock();
294 return csum;
295}
296
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800297extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
298extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700299
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800300#include <net/flow.h>
301extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
302
303static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200304nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800305{
Patrick McHardy051578c2007-12-17 22:42:51 -0800306#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800307 void (*decodefn)(struct sk_buff *, struct flowi *);
308
Patrick McHardy051578c2007-12-17 22:42:51 -0800309 if (family == AF_INET) {
310 rcu_read_lock();
311 decodefn = rcu_dereference(ip_nat_decode_session);
312 if (decodefn)
313 decodefn(skb, fl);
314 rcu_read_unlock();
315 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800316#endif
317}
318
Harald Welte608c8e42005-08-09 19:58:27 -0700319#ifdef CONFIG_PROC_FS
320#include <linux/proc_fs.h>
321extern struct proc_dir_entry *proc_net_netfilter;
322#endif
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#else /* !CONFIG_NETFILTER */
325#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800326#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200327static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700328 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800329 struct net_device *indev,
330 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800331 int (*okfn)(struct sk_buff *), int thresh,
332 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800333{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700334 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800335}
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200336static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800337 struct net_device *indev, struct net_device *outdev,
338 int (*okfn)(struct sk_buff *))
339{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800340 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800341}
David S. Millerf53b61d2006-01-07 12:50:27 -0800342struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800343static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200344nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
345{
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif /*CONFIG_NETFILTER*/
348
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700349#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
350extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
351extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700352extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700353#else
354static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
355#endif
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#endif /*__KERNEL__*/
358#endif /*__LINUX_NETFILTER_H*/