blob: 1a8487325a4f28ee7235ee318522c734c18094dd [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_NETFILTER
53
54extern void netfilter_init(void);
55
56/* Largest hook number + 1 */
57#define NF_MAX_HOOKS 8
58
59struct sk_buff;
60struct net_device;
61
62typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070063 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const struct net_device *in,
65 const struct net_device *out,
66 int (*okfn)(struct sk_buff *));
67
68struct nf_hook_ops
69{
70 struct list_head list;
71
72 /* User fills in from here down. */
73 nf_hookfn *hook;
74 struct module *owner;
75 int pf;
76 int hooknum;
77 /* Hooks are ordered in ascending priority. */
78 int priority;
79};
80
81struct nf_sockopt_ops
82{
83 struct list_head list;
84
85 int pf;
86
87 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
88 int set_optmin;
89 int set_optmax;
90 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080091 int (*compat_set)(struct sock *sk, int optval,
92 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 int get_optmin;
95 int get_optmax;
96 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -080097 int (*compat_get)(struct sock *sk, int optval,
98 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Neil Horman16fcec32007-09-11 11:28:26 +0200100 /* Use the module struct to lock set/get code in place */
101 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* Function to register/unregister hook points. */
105int nf_register_hook(struct nf_hook_ops *reg);
106void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700107int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
108void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/* Functions to register get/setsockopt ranges (non-inclusive). You
111 need to check permissions yourself! */
112int nf_register_sockopt(struct nf_sockopt_ops *reg);
113void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
114
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100115#ifdef CONFIG_SYSCTL
116/* Sysctl registration */
117struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
118 struct ctl_table *table);
119void nf_unregister_sysctl_table(struct ctl_table_header *header,
120 struct ctl_table *table);
121extern struct ctl_table nf_net_netfilter_sysctl_path[];
122extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
123#endif /* CONFIG_SYSCTL */
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
126
Herbert Xu3db05fe2007-10-15 00:53:15 -0700127int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800128 struct net_device *indev, struct net_device *outdev,
129 int (*okfn)(struct sk_buff *), int thresh);
130
131/**
132 * nf_hook_thresh - call a netfilter hook
133 *
134 * Returns 1 if the hook has allowed the packet to pass. The function
135 * okfn must be invoked by the caller in this case. Any other return
136 * value indicates the packet has been consumed by the hook.
137 */
138static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700139 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800140 struct net_device *indev,
141 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800142 int (*okfn)(struct sk_buff *), int thresh,
143 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800144{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800145 if (!cond)
146 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800147#ifndef CONFIG_NETFILTER_DEBUG
148 if (list_empty(&nf_hooks[pf][hook]))
149 return 1;
150#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700151 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800152}
153
Herbert Xu3db05fe2007-10-15 00:53:15 -0700154static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800155 struct net_device *indev, struct net_device *outdev,
156 int (*okfn)(struct sk_buff *))
157{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700158 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/* Activate hook; either okfn or kfree_skb called, unless a hook
162 returns NF_STOLEN (in which case, it's up to the hook to deal with
163 the consequences).
164
165 Returns -ERRNO if packet dropped. Zero means queued, stolen or
166 accepted.
167*/
168
169/* RR:
170 > I don't want nf_hook to return anything because people might forget
171 > about async and trust the return value to mean "packet was ok".
172
173 AK:
174 Just document it clearly, then you can expect some sense from kernel
175 coders :)
176*/
177
178/* This is gross, but inline doesn't cut it for avoiding the function
179 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Patrick McHardy16a66772006-01-06 23:01:48 -0800181/* HX: It's slightly less gross now. */
182
183#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
184({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700185if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800186 __ret = (okfn)(skb); \
187__ret;})
188
189#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
190({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700191if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800192 __ret = (okfn)(skb); \
193__ret;})
194
195#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
196 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/* Call setsockopt() */
199int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
200 int len);
201int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
202 int *len);
203
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800204int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
205 char __user *opt, int len);
206int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
207 char __user *opt, int *len);
208
Harald Welte089af262005-08-09 19:37:23 -0700209/* Call this before modifying an existing packet: ensures it is
210 modifiable and linear to the point you care about (writable_len).
211 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700212extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700213
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800214struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800215struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800216
Patrick McHardybce80322006-04-06 14:18:09 -0700217struct nf_afinfo {
218 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800219 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700220 unsigned int dataoff, u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800221 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700222 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800223 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700224 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800225 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700226 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700227};
228
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800229extern const struct nf_afinfo *nf_afinfo[NPROTO];
230static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700231{
232 return rcu_dereference(nf_afinfo[family]);
233}
Harald Welte2cc7d572005-08-09 19:42:34 -0700234
Al Virob51655b2006-11-14 21:40:42 -0800235static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700236nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
237 u_int8_t protocol, unsigned short family)
238{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800239 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800240 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700241
242 rcu_read_lock();
243 afinfo = nf_get_afinfo(family);
244 if (afinfo)
245 csum = afinfo->checksum(skb, hook, dataoff, protocol);
246 rcu_read_unlock();
247 return csum;
248}
249
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800250extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
251extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700252
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800253#include <net/flow.h>
254extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
255
256static inline void
257nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
258{
Patrick McHardy051578c2007-12-17 22:42:51 -0800259#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800260 void (*decodefn)(struct sk_buff *, struct flowi *);
261
Patrick McHardy051578c2007-12-17 22:42:51 -0800262 if (family == AF_INET) {
263 rcu_read_lock();
264 decodefn = rcu_dereference(ip_nat_decode_session);
265 if (decodefn)
266 decodefn(skb, fl);
267 rcu_read_unlock();
268 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800269#endif
270}
271
Harald Welte608c8e42005-08-09 19:58:27 -0700272#ifdef CONFIG_PROC_FS
273#include <linux/proc_fs.h>
274extern struct proc_dir_entry *proc_net_netfilter;
275#endif
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#else /* !CONFIG_NETFILTER */
278#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800279#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800280static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700281 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800282 struct net_device *indev,
283 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800284 int (*okfn)(struct sk_buff *), int thresh,
285 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800286{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700287 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800288}
Herbert Xu3db05fe2007-10-15 00:53:15 -0700289static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800290 struct net_device *indev, struct net_device *outdev,
291 int (*okfn)(struct sk_buff *))
292{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800293 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800294}
David S. Millerf53b61d2006-01-07 12:50:27 -0800295struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800296static inline void
297nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif /*CONFIG_NETFILTER*/
299
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700300#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
301extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
302extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700303extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700304#else
305static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
306#endif
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#endif /*__KERNEL__*/
309#endif /*__LINUX_NETFILTER_H*/