blob: c41f6438095d5ab6ca69e0fb56ec43af69c2636a [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>
10#include <linux/wait.h>
11#include <linux/list.h>
12#endif
13#include <linux/compiler.h>
14
15/* Responses from hook functions. */
16#define NF_DROP 0
17#define NF_ACCEPT 1
18#define NF_STOLEN 2
19#define NF_QUEUE 3
20#define NF_REPEAT 4
21#define NF_STOP 5
22#define NF_MAX_VERDICT NF_STOP
23
Harald Welte0ab43f82005-08-09 19:43:44 -070024/* we overload the higher bits for encoding auxiliary data such as the queue
25 * number. Not nice, but better than additional function arguments. */
26#define NF_VERDICT_MASK 0x0000ffff
27#define NF_VERDICT_BITS 16
28
29#define NF_VERDICT_QMASK 0xffff0000
30#define NF_VERDICT_QBITS 16
31
Harald Welteb766b302005-08-12 11:36:44 -070032#define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
Harald Welte0ab43f82005-08-09 19:43:44 -070033
Harald Welte6869c4d2005-08-09 19:24:19 -070034/* only for userspace compatibility */
35#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Generic cache responses from hook functions.
37 <= 0x2000 is used for protocol-flags. */
38#define NFC_UNKNOWN 0x4000
39#define NFC_ALTERED 0x8000
Harald Welte6869c4d2005-08-09 19:24:19 -070040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Patrick McHardy6e23ae22007-11-19 18:53:30 -080042enum nf_inet_hooks {
43 NF_INET_PRE_ROUTING,
44 NF_INET_LOCAL_IN,
45 NF_INET_FORWARD,
46 NF_INET_LOCAL_OUT,
47 NF_INET_POST_ROUTING,
48 NF_INET_NUMHOOKS
49};
50
Jan Engelhardt643a2c12007-12-17 22:43:50 -080051union nf_inet_addr {
52 u_int32_t all[4];
53 __be32 ip;
54 __be32 ip6[4];
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#ifdef CONFIG_NETFILTER
59
60extern void netfilter_init(void);
61
62/* Largest hook number + 1 */
63#define NF_MAX_HOOKS 8
64
65struct sk_buff;
66struct net_device;
67
68typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070069 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 const struct net_device *in,
71 const struct net_device *out,
72 int (*okfn)(struct sk_buff *));
73
74struct nf_hook_ops
75{
76 struct list_head list;
77
78 /* User fills in from here down. */
79 nf_hookfn *hook;
80 struct module *owner;
81 int pf;
82 int hooknum;
83 /* Hooks are ordered in ascending priority. */
84 int priority;
85};
86
87struct nf_sockopt_ops
88{
89 struct list_head list;
90
91 int pf;
92
93 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
94 int set_optmin;
95 int set_optmax;
96 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080097 int (*compat_set)(struct sock *sk, int optval,
98 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 int get_optmin;
101 int get_optmax;
102 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800103 int (*compat_get)(struct sock *sk, int optval,
104 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Neil Horman16fcec32007-09-11 11:28:26 +0200106 /* Use the module struct to lock set/get code in place */
107 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Function to register/unregister hook points. */
111int nf_register_hook(struct nf_hook_ops *reg);
112void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700113int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
114void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* Functions to register get/setsockopt ranges (non-inclusive). You
117 need to check permissions yourself! */
118int nf_register_sockopt(struct nf_sockopt_ops *reg);
119void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
120
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100121#ifdef CONFIG_SYSCTL
122/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800123extern struct ctl_path nf_net_netfilter_sysctl_path[];
124extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100125#endif /* CONFIG_SYSCTL */
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
128
Herbert Xu3db05fe2007-10-15 00:53:15 -0700129int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800130 struct net_device *indev, struct net_device *outdev,
131 int (*okfn)(struct sk_buff *), int thresh);
132
133/**
134 * nf_hook_thresh - call a netfilter hook
135 *
136 * Returns 1 if the hook has allowed the packet to pass. The function
137 * okfn must be invoked by the caller in this case. Any other return
138 * value indicates the packet has been consumed by the hook.
139 */
140static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700141 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800142 struct net_device *indev,
143 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800144 int (*okfn)(struct sk_buff *), int thresh,
145 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800146{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800147 if (!cond)
148 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800149#ifndef CONFIG_NETFILTER_DEBUG
150 if (list_empty(&nf_hooks[pf][hook]))
151 return 1;
152#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700153 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800154}
155
Herbert Xu3db05fe2007-10-15 00:53:15 -0700156static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800157 struct net_device *indev, struct net_device *outdev,
158 int (*okfn)(struct sk_buff *))
159{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700160 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/* Activate hook; either okfn or kfree_skb called, unless a hook
164 returns NF_STOLEN (in which case, it's up to the hook to deal with
165 the consequences).
166
167 Returns -ERRNO if packet dropped. Zero means queued, stolen or
168 accepted.
169*/
170
171/* RR:
172 > I don't want nf_hook to return anything because people might forget
173 > about async and trust the return value to mean "packet was ok".
174
175 AK:
176 Just document it clearly, then you can expect some sense from kernel
177 coders :)
178*/
179
180/* This is gross, but inline doesn't cut it for avoiding the function
181 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Patrick McHardy16a66772006-01-06 23:01:48 -0800183/* HX: It's slightly less gross now. */
184
185#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
186({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700187if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800188 __ret = (okfn)(skb); \
189__ret;})
190
191#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
192({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700193if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800194 __ret = (okfn)(skb); \
195__ret;})
196
197#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
198 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Call setsockopt() */
201int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
202 int len);
203int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
204 int *len);
205
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800206int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
207 char __user *opt, int len);
208int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
209 char __user *opt, int *len);
210
Harald Welte089af262005-08-09 19:37:23 -0700211/* Call this before modifying an existing packet: ensures it is
212 modifiable and linear to the point you care about (writable_len).
213 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700214extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700215
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800216struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800217struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800218
Patrick McHardybce80322006-04-06 14:18:09 -0700219struct nf_afinfo {
220 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800221 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700222 unsigned int dataoff, u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800223 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700224 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800225 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700226 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800227 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700228 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700229};
230
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800231extern const struct nf_afinfo *nf_afinfo[NPROTO];
232static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700233{
234 return rcu_dereference(nf_afinfo[family]);
235}
Harald Welte2cc7d572005-08-09 19:42:34 -0700236
Al Virob51655b2006-11-14 21:40:42 -0800237static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700238nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
239 u_int8_t protocol, unsigned short family)
240{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800241 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800242 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700243
244 rcu_read_lock();
245 afinfo = nf_get_afinfo(family);
246 if (afinfo)
247 csum = afinfo->checksum(skb, hook, dataoff, protocol);
248 rcu_read_unlock();
249 return csum;
250}
251
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800252extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
253extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700254
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800255#include <net/flow.h>
256extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
257
258static inline void
259nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
260{
Patrick McHardy051578c2007-12-17 22:42:51 -0800261#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800262 void (*decodefn)(struct sk_buff *, struct flowi *);
263
Patrick McHardy051578c2007-12-17 22:42:51 -0800264 if (family == AF_INET) {
265 rcu_read_lock();
266 decodefn = rcu_dereference(ip_nat_decode_session);
267 if (decodefn)
268 decodefn(skb, fl);
269 rcu_read_unlock();
270 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800271#endif
272}
273
Harald Welte608c8e42005-08-09 19:58:27 -0700274#ifdef CONFIG_PROC_FS
275#include <linux/proc_fs.h>
276extern struct proc_dir_entry *proc_net_netfilter;
277#endif
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279#else /* !CONFIG_NETFILTER */
280#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800281#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800282static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700283 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800284 struct net_device *indev,
285 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800286 int (*okfn)(struct sk_buff *), int thresh,
287 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800288{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700289 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800290}
Herbert Xu3db05fe2007-10-15 00:53:15 -0700291static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800292 struct net_device *indev, struct net_device *outdev,
293 int (*okfn)(struct sk_buff *))
294{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800295 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800296}
David S. Millerf53b61d2006-01-07 12:50:27 -0800297struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800298static inline void
299nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#endif /*CONFIG_NETFILTER*/
301
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700302#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
303extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
304extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700305extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700306#else
307static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
308#endif
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif /*__KERNEL__*/
311#endif /*__LINUX_NETFILTER_H*/