blob: bf3afb0844f78d76f10da9461c72b7fb9b88cb1c [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>
Alexey Dobriyan666953df2008-04-14 09:56:02 +02008#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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>
Alexey Dobriyan666953df2008-04-14 09:56:02 +020014#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#endif
Patrick McHardyc8942f12008-05-21 14:08:38 -070016#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 Engelhardt7e9c6ee2008-10-08 11:35:00 +020055enum {
56 NFPROTO_UNSPEC = 0,
57 NFPROTO_IPV4 = 2,
58 NFPROTO_ARP = 3,
59 NFPROTO_BRIDGE = 7,
60 NFPROTO_IPV6 = 10,
61 NFPROTO_DECNET = 12,
62 NFPROTO_NUMPROTO,
63};
64
Jan Engelhardt643a2c12007-12-17 22:43:50 -080065union nf_inet_addr {
Patrick McHardy7b33ed22008-02-19 17:20:33 -080066 __u32 all[4];
Jan Engelhardt643a2c12007-12-17 22:43:50 -080067 __be32 ip;
68 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080069 struct in_addr in;
70 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#ifdef CONFIG_NETFILTER
75
Patrick McHardyb8beedd2008-03-25 20:09:33 -070076static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
77 const union nf_inet_addr *a2)
78{
79 return a1->all[0] == a2->all[0] &&
80 a1->all[1] == a2->all[1] &&
81 a1->all[2] == a2->all[2] &&
82 a1->all[3] == a2->all[3];
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085extern void netfilter_init(void);
86
87/* Largest hook number + 1 */
88#define NF_MAX_HOOKS 8
89
90struct sk_buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070093 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *));
97
98struct nf_hook_ops
99{
100 struct list_head list;
101
102 /* User fills in from here down. */
103 nf_hookfn *hook;
104 struct module *owner;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200105 u_int8_t pf;
106 unsigned int hooknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Hooks are ordered in ascending priority. */
108 int priority;
109};
110
111struct nf_sockopt_ops
112{
113 struct list_head list;
114
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200115 u_int8_t pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
118 int set_optmin;
119 int set_optmax;
120 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800121 int (*compat_set)(struct sock *sk, int optval,
122 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 int get_optmin;
125 int get_optmax;
126 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800127 int (*compat_get)(struct sock *sk, int optval,
128 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Neil Horman16fcec32007-09-11 11:28:26 +0200130 /* Use the module struct to lock set/get code in place */
131 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134/* Function to register/unregister hook points. */
135int nf_register_hook(struct nf_hook_ops *reg);
136void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700137int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
138void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* Functions to register get/setsockopt ranges (non-inclusive). You
141 need to check permissions yourself! */
142int nf_register_sockopt(struct nf_sockopt_ops *reg);
143void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
144
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100145#ifdef CONFIG_SYSCTL
146/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800147extern struct ctl_path nf_net_netfilter_sysctl_path[];
148extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100149#endif /* CONFIG_SYSCTL */
150
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200151extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200153int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800154 struct net_device *indev, struct net_device *outdev,
155 int (*okfn)(struct sk_buff *), int thresh);
156
157/**
158 * nf_hook_thresh - call a netfilter hook
159 *
160 * Returns 1 if the hook has allowed the packet to pass. The function
161 * okfn must be invoked by the caller in this case. Any other return
162 * value indicates the packet has been consumed by the hook.
163 */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200164static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700165 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800166 struct net_device *indev,
167 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800168 int (*okfn)(struct sk_buff *), int thresh,
169 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800170{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800171 if (!cond)
172 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800173#ifndef CONFIG_NETFILTER_DEBUG
174 if (list_empty(&nf_hooks[pf][hook]))
175 return 1;
176#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700177 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800178}
179
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200180static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800181 struct net_device *indev, struct net_device *outdev,
182 int (*okfn)(struct sk_buff *))
183{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700184 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800185}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/* Activate hook; either okfn or kfree_skb called, unless a hook
188 returns NF_STOLEN (in which case, it's up to the hook to deal with
189 the consequences).
190
191 Returns -ERRNO if packet dropped. Zero means queued, stolen or
192 accepted.
193*/
194
195/* RR:
196 > I don't want nf_hook to return anything because people might forget
197 > about async and trust the return value to mean "packet was ok".
198
199 AK:
200 Just document it clearly, then you can expect some sense from kernel
201 coders :)
202*/
203
204/* This is gross, but inline doesn't cut it for avoiding the function
205 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Patrick McHardy16a66772006-01-06 23:01:48 -0800207/* HX: It's slightly less gross now. */
208
209#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
210({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700211if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800212 __ret = (okfn)(skb); \
213__ret;})
214
215#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
216({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700217if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800218 __ret = (okfn)(skb); \
219__ret;})
220
221#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
222 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224/* Call setsockopt() */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200225int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200227int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int *len);
229
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200230int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800231 char __user *opt, int len);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200232int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800233 char __user *opt, int *len);
234
Harald Welte089af262005-08-09 19:37:23 -0700235/* Call this before modifying an existing packet: ensures it is
236 modifiable and linear to the point you care about (writable_len).
237 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700238extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700239
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800240struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800241struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800242
Patrick McHardybce80322006-04-06 14:18:09 -0700243struct nf_afinfo {
244 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800245 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700246 unsigned int dataoff, u_int8_t protocol);
Patrick McHardyd63a6502008-03-20 15:15:53 +0100247 __sum16 (*checksum_partial)(struct sk_buff *skb,
248 unsigned int hook,
249 unsigned int dataoff,
250 unsigned int len,
251 u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800252 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700253 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800254 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700255 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800256 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700257 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700258};
259
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200260extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800261static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700262{
263 return rcu_dereference(nf_afinfo[family]);
264}
Harald Welte2cc7d572005-08-09 19:42:34 -0700265
Al Virob51655b2006-11-14 21:40:42 -0800266static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700267nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
268 u_int8_t protocol, unsigned short family)
269{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800270 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800271 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700272
273 rcu_read_lock();
274 afinfo = nf_get_afinfo(family);
275 if (afinfo)
276 csum = afinfo->checksum(skb, hook, dataoff, protocol);
277 rcu_read_unlock();
278 return csum;
279}
280
Patrick McHardyd63a6502008-03-20 15:15:53 +0100281static inline __sum16
282nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
283 unsigned int dataoff, unsigned int len,
284 u_int8_t protocol, unsigned short family)
285{
286 const struct nf_afinfo *afinfo;
287 __sum16 csum = 0;
288
289 rcu_read_lock();
290 afinfo = nf_get_afinfo(family);
291 if (afinfo)
292 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
293 protocol);
294 rcu_read_unlock();
295 return csum;
296}
297
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800298extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
299extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700300
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800301#include <net/flow.h>
302extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
303
304static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200305nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800306{
Patrick McHardy051578c2007-12-17 22:42:51 -0800307#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800308 void (*decodefn)(struct sk_buff *, struct flowi *);
309
Patrick McHardy051578c2007-12-17 22:42:51 -0800310 if (family == AF_INET) {
311 rcu_read_lock();
312 decodefn = rcu_dereference(ip_nat_decode_session);
313 if (decodefn)
314 decodefn(skb, fl);
315 rcu_read_unlock();
316 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800317#endif
318}
319
Harald Welte608c8e42005-08-09 19:58:27 -0700320#ifdef CONFIG_PROC_FS
321#include <linux/proc_fs.h>
322extern struct proc_dir_entry *proc_net_netfilter;
323#endif
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#else /* !CONFIG_NETFILTER */
326#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800327#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200328static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700329 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800330 struct net_device *indev,
331 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800332 int (*okfn)(struct sk_buff *), int thresh,
333 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800334{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700335 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800336}
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200337static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800338 struct net_device *indev, struct net_device *outdev,
339 int (*okfn)(struct sk_buff *))
340{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800341 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800342}
David S. Millerf53b61d2006-01-07 12:50:27 -0800343struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800344static inline void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200345nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
346{
347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif /*CONFIG_NETFILTER*/
349
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700350#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
351extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
352extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700353extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700354#else
355static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
356#endif
357
Alexey Dobriyan666953df2008-04-14 09:56:02 +0200358static inline struct net *nf_pre_routing_net(const struct net_device *in,
359 const struct net_device *out)
360{
361#ifdef CONFIG_NET_NS
362 return in->nd_net;
363#else
364 return &init_net;
365#endif
366}
367
368static inline struct net *nf_local_in_net(const struct net_device *in,
369 const struct net_device *out)
370{
371#ifdef CONFIG_NET_NS
372 return in->nd_net;
373#else
374 return &init_net;
375#endif
376}
377
378static inline struct net *nf_forward_net(const struct net_device *in,
379 const struct net_device *out)
380{
381#ifdef CONFIG_NET_NS
382 BUG_ON(in->nd_net != out->nd_net);
383 return in->nd_net;
384#else
385 return &init_net;
386#endif
387}
388
389static inline struct net *nf_local_out_net(const struct net_device *in,
390 const struct net_device *out)
391{
392#ifdef CONFIG_NET_NS
393 return out->nd_net;
394#else
395 return &init_net;
396#endif
397}
398
399static inline struct net *nf_post_routing_net(const struct net_device *in,
400 const struct net_device *out)
401{
402#ifdef CONFIG_NET_NS
403 return out->nd_net;
404#else
405 return &init_net;
406#endif
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#endif /*__KERNEL__*/
410#endif /*__LINUX_NETFILTER_H*/