blob: 89e6c72ad2959411523648fc53ec76f58cb557aa [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>
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>
14#endif
15#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 Engelhardt643a2c12007-12-17 22:43:50 -080053union nf_inet_addr {
Patrick McHardy7b33ed22008-02-19 17:20:33 -080054 __u32 all[4];
Jan Engelhardt643a2c12007-12-17 22:43:50 -080055 __be32 ip;
56 __be32 ip6[4];
Jan Engelhardt2e3075a2008-01-14 23:40:34 -080057 struct in_addr in;
58 struct in6_addr in6;
Jan Engelhardt643a2c12007-12-17 22:43:50 -080059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_NETFILTER
63
Patrick McHardyb8beedd2008-03-25 20:09:33 -070064static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
65 const union nf_inet_addr *a2)
66{
67 return a1->all[0] == a2->all[0] &&
68 a1->all[1] == a2->all[1] &&
69 a1->all[2] == a2->all[2] &&
70 a1->all[3] == a2->all[3];
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073extern void netfilter_init(void);
74
75/* Largest hook number + 1 */
76#define NF_MAX_HOOKS 8
77
78struct sk_buff;
79struct net_device;
80
81typedef unsigned int nf_hookfn(unsigned int hooknum,
Herbert Xu3db05fe2007-10-15 00:53:15 -070082 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 const struct net_device *in,
84 const struct net_device *out,
85 int (*okfn)(struct sk_buff *));
86
87struct nf_hook_ops
88{
89 struct list_head list;
90
91 /* User fills in from here down. */
92 nf_hookfn *hook;
93 struct module *owner;
94 int pf;
95 int hooknum;
96 /* Hooks are ordered in ascending priority. */
97 int priority;
98};
99
100struct nf_sockopt_ops
101{
102 struct list_head list;
103
104 int pf;
105
106 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
107 int set_optmin;
108 int set_optmax;
109 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800110 int (*compat_set)(struct sock *sk, int optval,
111 void __user *user, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 int get_optmin;
114 int get_optmax;
115 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800116 int (*compat_get)(struct sock *sk, int optval,
117 void __user *user, int *len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Neil Horman16fcec32007-09-11 11:28:26 +0200119 /* Use the module struct to lock set/get code in place */
120 struct module *owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* Function to register/unregister hook points. */
124int nf_register_hook(struct nf_hook_ops *reg);
125void nf_unregister_hook(struct nf_hook_ops *reg);
Patrick McHardy972d1cb2006-04-06 14:09:12 -0700126int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
127void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* Functions to register get/setsockopt ranges (non-inclusive). You
130 need to check permissions yourself! */
131int nf_register_sockopt(struct nf_sockopt_ops *reg);
132void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
133
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100134#ifdef CONFIG_SYSCTL
135/* Sysctl registration */
Pavel Emelyanovb3fd3ff2008-01-09 00:34:02 -0800136extern struct ctl_path nf_net_netfilter_sysctl_path[];
137extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
Patrick McHardyd62f9ed2006-11-29 02:35:17 +0100138#endif /* CONFIG_SYSCTL */
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
141
Herbert Xu3db05fe2007-10-15 00:53:15 -0700142int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800143 struct net_device *indev, struct net_device *outdev,
144 int (*okfn)(struct sk_buff *), int thresh);
145
146/**
147 * nf_hook_thresh - call a netfilter hook
148 *
149 * Returns 1 if the hook has allowed the packet to pass. The function
150 * okfn must be invoked by the caller in this case. Any other return
151 * value indicates the packet has been consumed by the hook.
152 */
153static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700154 struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800155 struct net_device *indev,
156 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800157 int (*okfn)(struct sk_buff *), int thresh,
158 int cond)
Patrick McHardy16a66772006-01-06 23:01:48 -0800159{
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800160 if (!cond)
161 return 1;
Patrick McHardy16a66772006-01-06 23:01:48 -0800162#ifndef CONFIG_NETFILTER_DEBUG
163 if (list_empty(&nf_hooks[pf][hook]))
164 return 1;
165#endif
Herbert Xu3db05fe2007-10-15 00:53:15 -0700166 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
Patrick McHardy16a66772006-01-06 23:01:48 -0800167}
168
Herbert Xu3db05fe2007-10-15 00:53:15 -0700169static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
Patrick McHardy16a66772006-01-06 23:01:48 -0800170 struct net_device *indev, struct net_device *outdev,
171 int (*okfn)(struct sk_buff *))
172{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700173 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
Patrick McHardy16a66772006-01-06 23:01:48 -0800174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176/* Activate hook; either okfn or kfree_skb called, unless a hook
177 returns NF_STOLEN (in which case, it's up to the hook to deal with
178 the consequences).
179
180 Returns -ERRNO if packet dropped. Zero means queued, stolen or
181 accepted.
182*/
183
184/* RR:
185 > I don't want nf_hook to return anything because people might forget
186 > about async and trust the return value to mean "packet was ok".
187
188 AK:
189 Just document it clearly, then you can expect some sense from kernel
190 coders :)
191*/
192
193/* This is gross, but inline doesn't cut it for avoiding the function
194 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Patrick McHardy16a66772006-01-06 23:01:48 -0800196/* HX: It's slightly less gross now. */
197
198#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
199({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700200if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800201 __ret = (okfn)(skb); \
202__ret;})
203
204#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
205({int __ret; \
Herbert Xu3db05fe2007-10-15 00:53:15 -0700206if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
Patrick McHardy16a66772006-01-06 23:01:48 -0800207 __ret = (okfn)(skb); \
208__ret;})
209
210#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
211 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/* Call setsockopt() */
214int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
215 int len);
216int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
217 int *len);
218
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800219int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
220 char __user *opt, int len);
221int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
222 char __user *opt, int *len);
223
Harald Welte089af262005-08-09 19:37:23 -0700224/* Call this before modifying an existing packet: ensures it is
225 modifiable and linear to the point you care about (writable_len).
226 Returns true or false. */
Herbert Xu37d41872007-10-14 00:39:18 -0700227extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
Harald Welte089af262005-08-09 19:37:23 -0700228
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800229struct flowi;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800230struct nf_queue_entry;
Patrick McHardyc01cd422007-12-05 01:24:48 -0800231
Patrick McHardybce80322006-04-06 14:18:09 -0700232struct nf_afinfo {
233 unsigned short family;
Al Virob51655b2006-11-14 21:40:42 -0800234 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
Patrick McHardy422c3462006-04-06 14:18:43 -0700235 unsigned int dataoff, u_int8_t protocol);
Patrick McHardy1841a4c2007-12-05 01:22:05 -0800236 int (*route)(struct dst_entry **dst, struct flowi *fl);
Patrick McHardybce80322006-04-06 14:18:09 -0700237 void (*saveroute)(const struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800238 struct nf_queue_entry *entry);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700239 int (*reroute)(struct sk_buff *skb,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800240 const struct nf_queue_entry *entry);
Patrick McHardybce80322006-04-06 14:18:09 -0700241 int route_key_size;
Harald Welte2cc7d572005-08-09 19:42:34 -0700242};
243
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800244extern const struct nf_afinfo *nf_afinfo[NPROTO];
245static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
Patrick McHardybce80322006-04-06 14:18:09 -0700246{
247 return rcu_dereference(nf_afinfo[family]);
248}
Harald Welte2cc7d572005-08-09 19:42:34 -0700249
Al Virob51655b2006-11-14 21:40:42 -0800250static inline __sum16
Patrick McHardy422c3462006-04-06 14:18:43 -0700251nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
252 u_int8_t protocol, unsigned short family)
253{
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800254 const struct nf_afinfo *afinfo;
Al Virob51655b2006-11-14 21:40:42 -0800255 __sum16 csum = 0;
Patrick McHardy422c3462006-04-06 14:18:43 -0700256
257 rcu_read_lock();
258 afinfo = nf_get_afinfo(family);
259 if (afinfo)
260 csum = afinfo->checksum(skb, hook, dataoff, protocol);
261 rcu_read_unlock();
262 return csum;
263}
264
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800265extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
266extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
Patrick McHardybce80322006-04-06 14:18:09 -0700267
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800268#include <net/flow.h>
269extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
270
271static inline void
272nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
273{
Patrick McHardy051578c2007-12-17 22:42:51 -0800274#ifdef CONFIG_NF_NAT_NEEDED
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800275 void (*decodefn)(struct sk_buff *, struct flowi *);
276
Patrick McHardy051578c2007-12-17 22:42:51 -0800277 if (family == AF_INET) {
278 rcu_read_lock();
279 decodefn = rcu_dereference(ip_nat_decode_session);
280 if (decodefn)
281 decodefn(skb, fl);
282 rcu_read_unlock();
283 }
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800284#endif
285}
286
Harald Welte608c8e42005-08-09 19:58:27 -0700287#ifdef CONFIG_PROC_FS
288#include <linux/proc_fs.h>
289extern struct proc_dir_entry *proc_net_netfilter;
290#endif
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292#else /* !CONFIG_NETFILTER */
293#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800294#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
David S. Millerf53b61d2006-01-07 12:50:27 -0800295static inline int nf_hook_thresh(int pf, unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700296 struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800297 struct net_device *indev,
298 struct net_device *outdev,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800299 int (*okfn)(struct sk_buff *), int thresh,
300 int cond)
David S. Millerf53b61d2006-01-07 12:50:27 -0800301{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700302 return okfn(skb);
David S. Millerf53b61d2006-01-07 12:50:27 -0800303}
Herbert Xu3db05fe2007-10-15 00:53:15 -0700304static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
David S. Millerf53b61d2006-01-07 12:50:27 -0800305 struct net_device *indev, struct net_device *outdev,
306 int (*okfn)(struct sk_buff *))
307{
Patrick McHardy9c92d342006-02-15 15:18:19 -0800308 return 1;
David S. Millerf53b61d2006-01-07 12:50:27 -0800309}
David S. Millerf53b61d2006-01-07 12:50:27 -0800310struct flowi;
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -0800311static inline void
312nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif /*CONFIG_NETFILTER*/
314
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700315#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
316extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
317extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
Yasuyuki Kozakaide6e05c2007-03-23 11:17:27 -0700318extern void (*nf_ct_destroy)(struct nf_conntrack *);
Yasuyuki Kozakai5f79e0f2007-03-23 11:17:07 -0700319#else
320static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
321#endif
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif /*__KERNEL__*/
324#endif /*__LINUX_NETFILTER_H*/