Initial commit
[wrt350n-kernel.git] / include / linux / netfilter.h
blobd74e79bacd2d19c240f5b8d27ba6fdd123c8ca9b
1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
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/in.h>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #endif
15 #include <linux/compiler.h>
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
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
31 #define NF_VERDICT_QMASK 0xffff0000
32 #define NF_VERDICT_QBITS 16
34 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
36 /* only for userspace compatibility */
37 #ifndef __KERNEL__
38 /* Generic cache responses from hook functions.
39 <= 0x2000 is used for protocol-flags. */
40 #define NFC_UNKNOWN 0x4000
41 #define NFC_ALTERED 0x8000
42 #endif
44 enum 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
53 union nf_inet_addr {
54 u_int32_t all[4];
55 __be32 ip;
56 __be32 ip6[4];
57 struct in_addr in;
58 struct in6_addr in6;
61 #ifdef __KERNEL__
62 #ifdef CONFIG_NETFILTER
64 extern void netfilter_init(void);
66 /* Largest hook number + 1 */
67 #define NF_MAX_HOOKS 8
69 struct sk_buff;
70 struct net_device;
72 typedef unsigned int nf_hookfn(unsigned int hooknum,
73 struct sk_buff *skb,
74 const struct net_device *in,
75 const struct net_device *out,
76 int (*okfn)(struct sk_buff *));
78 struct nf_hook_ops
80 struct list_head list;
82 /* User fills in from here down. */
83 nf_hookfn *hook;
84 struct module *owner;
85 int pf;
86 int hooknum;
87 /* Hooks are ordered in ascending priority. */
88 int priority;
91 struct nf_sockopt_ops
93 struct list_head list;
95 int pf;
97 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
98 int set_optmin;
99 int set_optmax;
100 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
101 int (*compat_set)(struct sock *sk, int optval,
102 void __user *user, unsigned int len);
104 int get_optmin;
105 int get_optmax;
106 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
107 int (*compat_get)(struct sock *sk, int optval,
108 void __user *user, int *len);
110 /* Use the module struct to lock set/get code in place */
111 struct module *owner;
114 /* Function to register/unregister hook points. */
115 int nf_register_hook(struct nf_hook_ops *reg);
116 void nf_unregister_hook(struct nf_hook_ops *reg);
117 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
118 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
120 /* Functions to register get/setsockopt ranges (non-inclusive). You
121 need to check permissions yourself! */
122 int nf_register_sockopt(struct nf_sockopt_ops *reg);
123 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
125 #ifdef CONFIG_SYSCTL
126 /* Sysctl registration */
127 extern struct ctl_path nf_net_netfilter_sysctl_path[];
128 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
129 #endif /* CONFIG_SYSCTL */
131 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
133 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
134 struct net_device *indev, struct net_device *outdev,
135 int (*okfn)(struct sk_buff *), int thresh);
138 * nf_hook_thresh - call a netfilter hook
140 * Returns 1 if the hook has allowed the packet to pass. The function
141 * okfn must be invoked by the caller in this case. Any other return
142 * value indicates the packet has been consumed by the hook.
144 static inline int nf_hook_thresh(int pf, unsigned int hook,
145 struct sk_buff *skb,
146 struct net_device *indev,
147 struct net_device *outdev,
148 int (*okfn)(struct sk_buff *), int thresh,
149 int cond)
151 if (!cond)
152 return 1;
153 #ifndef CONFIG_NETFILTER_DEBUG
154 if (list_empty(&nf_hooks[pf][hook]))
155 return 1;
156 #endif
157 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
160 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
161 struct net_device *indev, struct net_device *outdev,
162 int (*okfn)(struct sk_buff *))
164 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
167 /* Activate hook; either okfn or kfree_skb called, unless a hook
168 returns NF_STOLEN (in which case, it's up to the hook to deal with
169 the consequences).
171 Returns -ERRNO if packet dropped. Zero means queued, stolen or
172 accepted.
175 /* RR:
176 > I don't want nf_hook to return anything because people might forget
177 > about async and trust the return value to mean "packet was ok".
180 Just document it clearly, then you can expect some sense from kernel
181 coders :)
184 /* This is gross, but inline doesn't cut it for avoiding the function
185 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
187 /* HX: It's slightly less gross now. */
189 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
190 ({int __ret; \
191 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
192 __ret = (okfn)(skb); \
193 __ret;})
195 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
196 ({int __ret; \
197 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
198 __ret = (okfn)(skb); \
199 __ret;})
201 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
202 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
204 /* Call setsockopt() */
205 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
206 int len);
207 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
208 int *len);
210 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
211 char __user *opt, int len);
212 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
213 char __user *opt, int *len);
215 /* Call this before modifying an existing packet: ensures it is
216 modifiable and linear to the point you care about (writable_len).
217 Returns true or false. */
218 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
220 struct flowi;
221 struct nf_queue_entry;
223 struct nf_afinfo {
224 unsigned short family;
225 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
226 unsigned int dataoff, u_int8_t protocol);
227 int (*route)(struct dst_entry **dst, struct flowi *fl);
228 void (*saveroute)(const struct sk_buff *skb,
229 struct nf_queue_entry *entry);
230 int (*reroute)(struct sk_buff *skb,
231 const struct nf_queue_entry *entry);
232 int route_key_size;
235 extern const struct nf_afinfo *nf_afinfo[NPROTO];
236 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
238 return rcu_dereference(nf_afinfo[family]);
241 static inline __sum16
242 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
243 u_int8_t protocol, unsigned short family)
245 const struct nf_afinfo *afinfo;
246 __sum16 csum = 0;
248 rcu_read_lock();
249 afinfo = nf_get_afinfo(family);
250 if (afinfo)
251 csum = afinfo->checksum(skb, hook, dataoff, protocol);
252 rcu_read_unlock();
253 return csum;
256 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
257 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
259 #include <net/flow.h>
260 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
262 static inline void
263 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
265 #ifdef CONFIG_NF_NAT_NEEDED
266 void (*decodefn)(struct sk_buff *, struct flowi *);
268 if (family == AF_INET) {
269 rcu_read_lock();
270 decodefn = rcu_dereference(ip_nat_decode_session);
271 if (decodefn)
272 decodefn(skb, fl);
273 rcu_read_unlock();
275 #endif
278 #ifdef CONFIG_PROC_FS
279 #include <linux/proc_fs.h>
280 extern struct proc_dir_entry *proc_net_netfilter;
281 #endif
283 #else /* !CONFIG_NETFILTER */
284 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
285 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
286 static inline int nf_hook_thresh(int pf, unsigned int hook,
287 struct sk_buff *skb,
288 struct net_device *indev,
289 struct net_device *outdev,
290 int (*okfn)(struct sk_buff *), int thresh,
291 int cond)
293 return okfn(skb);
295 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
296 struct net_device *indev, struct net_device *outdev,
297 int (*okfn)(struct sk_buff *))
299 return 1;
301 struct flowi;
302 static inline void
303 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
304 #endif /*CONFIG_NETFILTER*/
306 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
307 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
308 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
309 extern void (*nf_ct_destroy)(struct nf_conntrack *);
310 #else
311 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
312 #endif
314 #endif /*__KERNEL__*/
315 #endif /*__LINUX_NETFILTER_H*/