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