]> bbs.cooldavid.org Git - net-next-2.6.git/blame - include/linux/netfilter.h
netfilter: allow hooks to pass error code back up the stack
[net-next-2.6.git] / include / linux / netfilter.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
4#ifdef __KERNEL__
5#include <linux/init.h>
1da177e4
LT
6#include <linux/skbuff.h>
7#include <linux/net.h>
8#include <linux/if.h>
2e3075a2
JE
9#include <linux/in.h>
10#include <linux/in6.h>
1da177e4
LT
11#include <linux/wait.h>
12#include <linux/list.h>
13#endif
c8942f1f 14#include <linux/types.h>
1da177e4
LT
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
0ab43f84
HW
26/* 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
fbabbed8 34#define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
0ab43f84 35
da683650
EP
36#define NF_DROP_ERR(x) (((-x) << NF_VERDICT_BITS) | NF_DROP)
37
6869c4d8
HW
38/* only for userspace compatibility */
39#ifndef __KERNEL__
1da177e4
LT
40/* Generic cache responses from hook functions.
41 <= 0x2000 is used for protocol-flags. */
42#define NFC_UNKNOWN 0x4000
43#define NFC_ALTERED 0x8000
6869c4d8 44#endif
1da177e4 45
6e23ae2a
PM
46enum 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
7e9c6eeb
JE
55enum {
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
643a2c15 65union nf_inet_addr {
7b33ed22 66 __u32 all[4];
643a2c15
JE
67 __be32 ip;
68 __be32 ip6[4];
2e3075a2
JE
69 struct in_addr in;
70 struct in6_addr in6;
643a2c15
JE
71};
72
1da177e4 73#ifdef __KERNEL__
1da177e4
LT
74#ifdef CONFIG_NETFILTER
75
b8beedd2
PM
76static 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
1da177e4
LT
85extern void netfilter_init(void);
86
87/* Largest hook number + 1 */
88#define NF_MAX_HOOKS 8
89
90struct sk_buff;
1da177e4
LT
91
92typedef unsigned int nf_hookfn(unsigned int hooknum,
3db05fea 93 struct sk_buff *skb,
1da177e4
LT
94 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *));
97
d94d9fee 98struct nf_hook_ops {
1da177e4
LT
99 struct list_head list;
100
101 /* User fills in from here down. */
102 nf_hookfn *hook;
103 struct module *owner;
76108cea
JE
104 u_int8_t pf;
105 unsigned int hooknum;
1da177e4
LT
106 /* Hooks are ordered in ascending priority. */
107 int priority;
108};
109
d94d9fee 110struct nf_sockopt_ops {
1da177e4
LT
111 struct list_head list;
112
76108cea 113 u_int8_t pf;
1da177e4
LT
114
115 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
116 int set_optmin;
117 int set_optmax;
118 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 119#ifdef CONFIG_COMPAT
3fdadf7d
DM
120 int (*compat_set)(struct sock *sk, int optval,
121 void __user *user, unsigned int len);
c30f540b 122#endif
1da177e4
LT
123 int get_optmin;
124 int get_optmax;
125 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 126#ifdef CONFIG_COMPAT
3fdadf7d
DM
127 int (*compat_get)(struct sock *sk, int optval,
128 void __user *user, int *len);
c30f540b 129#endif
16fcec35
NH
130 /* Use the module struct to lock set/get code in place */
131 struct module *owner;
1da177e4
LT
132};
133
1da177e4
LT
134/* Function to register/unregister hook points. */
135int nf_register_hook(struct nf_hook_ops *reg);
136void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
137int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
138void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
139
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
d62f9ed4
PM
145#ifdef CONFIG_SYSCTL
146/* Sysctl registration */
b3fd3ffe
PE
147extern struct ctl_path nf_net_netfilter_sysctl_path[];
148extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
d62f9ed4
PM
149#endif /* CONFIG_SYSCTL */
150
7e9c6eeb 151extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
1da177e4 152
76108cea 153int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
154 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 */
76108cea 164static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea 165 struct sk_buff *skb,
16a6677f
PM
166 struct net_device *indev,
167 struct net_device *outdev,
23f3733d 168 int (*okfn)(struct sk_buff *), int thresh)
16a6677f
PM
169{
170#ifndef CONFIG_NETFILTER_DEBUG
171 if (list_empty(&nf_hooks[pf][hook]))
172 return 1;
173#endif
3db05fea 174 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
16a6677f
PM
175}
176
76108cea 177static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
178 struct net_device *indev, struct net_device *outdev,
179 int (*okfn)(struct sk_buff *))
180{
23f3733d 181 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
16a6677f 182}
1da177e4
LT
183
184/* Activate hook; either okfn or kfree_skb called, unless a hook
185 returns NF_STOLEN (in which case, it's up to the hook to deal with
186 the consequences).
187
188 Returns -ERRNO if packet dropped. Zero means queued, stolen or
189 accepted.
190*/
191
192/* RR:
193 > I don't want nf_hook to return anything because people might forget
194 > about async and trust the return value to mean "packet was ok".
195
196 AK:
197 Just document it clearly, then you can expect some sense from kernel
198 coders :)
199*/
200
2249065f
JE
201static inline int
202NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
203 struct net_device *in, struct net_device *out,
204 int (*okfn)(struct sk_buff *), int thresh)
205{
206 int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
207 if (ret == 1)
208 ret = okfn(skb);
209 return ret;
210}
48d5cad8 211
2249065f
JE
212static inline int
213NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
214 struct net_device *in, struct net_device *out,
215 int (*okfn)(struct sk_buff *), bool cond)
216{
4bac6b18
PM
217 int ret;
218
219 if (!cond ||
ac5aa2e3 220 ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
2249065f
JE
221 ret = okfn(skb);
222 return ret;
223}
1da177e4 224
2249065f
JE
225static inline int
226NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
227 struct net_device *in, struct net_device *out,
228 int (*okfn)(struct sk_buff *))
229{
230 return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
231}
1da177e4
LT
232
233/* Call setsockopt() */
76108cea 234int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 235 unsigned int len);
76108cea 236int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 237 int *len);
c30f540b 238#ifdef CONFIG_COMPAT
76108cea 239int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 240 char __user *opt, unsigned int len);
76108cea 241int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 242 char __user *opt, int *len);
c30f540b 243#endif
3fdadf7d 244
089af26c
HW
245/* Call this before modifying an existing packet: ensures it is
246 modifiable and linear to the point you care about (writable_len).
247 Returns true or false. */
37d41879 248extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 249
1841a4c7 250struct flowi;
02f014d8 251struct nf_queue_entry;
c01cd429 252
bce8032e
PM
253struct nf_afinfo {
254 unsigned short family;
b51655b9 255 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 256 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
257 __sum16 (*checksum_partial)(struct sk_buff *skb,
258 unsigned int hook,
259 unsigned int dataoff,
260 unsigned int len,
261 u_int8_t protocol);
1841a4c7 262 int (*route)(struct dst_entry **dst, struct flowi *fl);
bce8032e 263 void (*saveroute)(const struct sk_buff *skb,
02f014d8 264 struct nf_queue_entry *entry);
3db05fea 265 int (*reroute)(struct sk_buff *skb,
02f014d8 266 const struct nf_queue_entry *entry);
bce8032e 267 int route_key_size;
2cc7d573
HW
268};
269
7e9c6eeb 270extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 271static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
272{
273 return rcu_dereference(nf_afinfo[family]);
274}
2cc7d573 275
b51655b9 276static inline __sum16
422c346f
PM
277nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
278 u_int8_t protocol, unsigned short family)
279{
1e796fda 280 const struct nf_afinfo *afinfo;
b51655b9 281 __sum16 csum = 0;
422c346f
PM
282
283 rcu_read_lock();
284 afinfo = nf_get_afinfo(family);
285 if (afinfo)
286 csum = afinfo->checksum(skb, hook, dataoff, protocol);
287 rcu_read_unlock();
288 return csum;
289}
290
d63a6507
PM
291static inline __sum16
292nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
293 unsigned int dataoff, unsigned int len,
294 u_int8_t protocol, unsigned short family)
295{
296 const struct nf_afinfo *afinfo;
297 __sum16 csum = 0;
298
299 rcu_read_lock();
300 afinfo = nf_get_afinfo(family);
301 if (afinfo)
302 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
303 protocol);
304 rcu_read_unlock();
305 return csum;
306}
307
1e796fda
PM
308extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
309extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 310
eb9c7ebe
PM
311#include <net/flow.h>
312extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
313
314static inline void
76108cea 315nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 316{
051578cc 317#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
318 void (*decodefn)(struct sk_buff *, struct flowi *);
319
051578cc
PM
320 if (family == AF_INET) {
321 rcu_read_lock();
322 decodefn = rcu_dereference(ip_nat_decode_session);
323 if (decodefn)
324 decodefn(skb, fl);
325 rcu_read_unlock();
326 }
eb9c7ebe
PM
327#endif
328}
329
608c8e4f
HW
330#ifdef CONFIG_PROC_FS
331#include <linux/proc_fs.h>
332extern struct proc_dir_entry *proc_net_netfilter;
333#endif
334
1da177e4
LT
335#else /* !CONFIG_NETFILTER */
336#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
48d5cad8 337#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
76108cea 338static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea 339 struct sk_buff *skb,
f53b61d8
DM
340 struct net_device *indev,
341 struct net_device *outdev,
23f3733d 342 int (*okfn)(struct sk_buff *), int thresh)
f53b61d8 343{
3db05fea 344 return okfn(skb);
f53b61d8 345}
76108cea 346static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
f53b61d8
DM
347 struct net_device *indev, struct net_device *outdev,
348 int (*okfn)(struct sk_buff *))
349{
9c92d348 350 return 1;
f53b61d8 351}
f53b61d8 352struct flowi;
eb9c7ebe 353static inline void
76108cea
JE
354nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
355{
356}
1da177e4
LT
357#endif /*CONFIG_NETFILTER*/
358
5f79e0f9
YK
359#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
360extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
361extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
de6e05c4 362extern void (*nf_ct_destroy)(struct nf_conntrack *);
5f79e0f9
YK
363#else
364static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
365#endif
366
1da177e4
LT
367#endif /*__KERNEL__*/
368#endif /*__LINUX_NETFILTER_H*/