RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / include / linux / netfilter.h
blob380c3c299f456e490f657d1dd6542319610b2b2c
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/types.h>
16 #include <linux/compiler.h>
18 /* Responses from hook functions. */
19 #define NF_DROP 0
20 #define NF_ACCEPT 1
21 #define NF_STOLEN 2
22 #define NF_QUEUE 3
23 #define NF_REPEAT 4
24 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
25 #define NF_FAST_NAT 5
26 #define NF_STOP 6
27 #else
28 #define NF_STOP 5
29 #endif
30 #define NF_MAX_VERDICT NF_STOP
32 /* we overload the higher bits for encoding auxiliary data such as the queue
33 * number. Not nice, but better than additional function arguments. */
34 #define NF_VERDICT_MASK 0x0000ffff
35 #define NF_VERDICT_BITS 16
37 #define NF_VERDICT_QMASK 0xffff0000
38 #define NF_VERDICT_QBITS 16
40 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
42 /* only for userspace compatibility */
43 #ifndef __KERNEL__
44 /* Generic cache responses from hook functions.
45 <= 0x2000 is used for protocol-flags. */
46 #define NFC_UNKNOWN 0x4000
47 #define NFC_ALTERED 0x8000
48 #endif
50 union nf_inet_addr {
51 __u32 all[4];
52 __be32 ip;
53 __be32 ip6[4];
54 struct in_addr in;
55 struct in6_addr in6;
58 #ifdef __KERNEL__
59 #ifdef CONFIG_NETFILTER
61 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
62 const union nf_inet_addr *a2)
64 return a1->all[0] == a2->all[0] &&
65 a1->all[1] == a2->all[1] &&
66 a1->all[2] == a2->all[2] &&
67 a1->all[3] == a2->all[3];
70 extern void netfilter_init(void);
72 /* Largest hook number + 1 */
73 #define NF_MAX_HOOKS 8
75 struct sk_buff;
76 struct net_device;
78 typedef unsigned int nf_hookfn(unsigned int hooknum,
79 struct sk_buff *skb,
80 const struct net_device *in,
81 const struct net_device *out,
82 int (*okfn)(struct sk_buff *));
84 struct nf_hook_ops
86 struct list_head list;
88 /* User fills in from here down. */
89 nf_hookfn *hook;
90 struct module *owner;
91 int pf;
92 int hooknum;
93 /* Hooks are ordered in ascending priority. */
94 int priority;
97 struct nf_sockopt_ops
99 struct list_head list;
101 int pf;
103 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
104 int set_optmin;
105 int set_optmax;
106 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
107 int (*compat_set)(struct sock *sk, int optval,
108 void __user *user, unsigned int len);
110 int get_optmin;
111 int get_optmax;
112 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
113 int (*compat_get)(struct sock *sk, int optval,
114 void __user *user, int *len);
116 /* Number of users inside set() or get(). */
117 unsigned int use;
118 struct task_struct *cleanup_task;
121 /* Each queued (to userspace) skbuff has one of these. */
122 struct nf_info
124 /* The ops struct which sent us to userspace. */
125 struct nf_hook_ops *elem;
127 /* If we're sent to userspace, this keeps housekeeping info */
128 int pf;
129 unsigned int hook;
130 struct net_device *indev, *outdev;
131 int (*okfn)(struct sk_buff *);
134 /* Function to register/unregister hook points. */
135 int nf_register_hook(struct nf_hook_ops *reg);
136 void nf_unregister_hook(struct nf_hook_ops *reg);
137 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
138 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
140 /* Functions to register get/setsockopt ranges (non-inclusive). You
141 need to check permissions yourself! */
142 int nf_register_sockopt(struct nf_sockopt_ops *reg);
143 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
145 #ifdef CONFIG_SYSCTL
146 /* Sysctl registration */
147 struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
148 struct ctl_table *table);
149 void nf_unregister_sysctl_table(struct ctl_table_header *header,
150 struct ctl_table *table);
151 extern struct ctl_table nf_net_netfilter_sysctl_path[];
152 extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
153 #endif /* CONFIG_SYSCTL */
155 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
157 /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
158 * disappear once iptables is replaced with pkttables. Please DO NOT use them
159 * for any new code! */
160 #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
161 #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
162 #define NF_LOG_IPOPT 0x04 /* Log IP options */
163 #define NF_LOG_UID 0x08 /* Log UID owning local socket */
164 #define NF_LOG_MASK 0x0f
166 #define NF_LOG_TYPE_LOG 0x01
167 #define NF_LOG_TYPE_ULOG 0x02
169 struct nf_loginfo {
170 u_int8_t type;
171 union {
172 struct {
173 u_int32_t copy_len;
174 u_int16_t group;
175 u_int16_t qthreshold;
176 } ulog;
177 struct {
178 u_int8_t level;
179 u_int8_t logflags;
180 } log;
181 } u;
184 typedef void nf_logfn(unsigned int pf,
185 unsigned int hooknum,
186 const struct sk_buff *skb,
187 const struct net_device *in,
188 const struct net_device *out,
189 const struct nf_loginfo *li,
190 const char *prefix);
192 struct nf_logger {
193 struct module *me;
194 nf_logfn *logfn;
195 char *name;
198 /* Function to register/unregister log function. */
199 int nf_log_register(int pf, struct nf_logger *logger);
200 void nf_log_unregister(struct nf_logger *logger);
201 void nf_log_unregister_pf(int pf);
203 /* Calls the registered backend logging function */
204 void nf_log_packet(int pf,
205 unsigned int hooknum,
206 const struct sk_buff *skb,
207 const struct net_device *in,
208 const struct net_device *out,
209 struct nf_loginfo *li,
210 const char *fmt, ...);
212 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
213 struct net_device *indev, struct net_device *outdev,
214 int (*okfn)(struct sk_buff *), int thresh);
217 * nf_hook_thresh - call a netfilter hook
219 * Returns 1 if the hook has allowed the packet to pass. The function
220 * okfn must be invoked by the caller in this case. Any other return
221 * value indicates the packet has been consumed by the hook.
223 static inline int nf_hook_thresh(int pf, unsigned int hook,
224 struct sk_buff *skb,
225 struct net_device *indev,
226 struct net_device *outdev,
227 int (*okfn)(struct sk_buff *), int thresh,
228 int cond)
230 if (!cond)
231 return 1;
232 #ifndef CONFIG_NETFILTER_DEBUG
233 if (list_empty(&nf_hooks[pf][hook]))
234 return 1;
235 #endif
236 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
239 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
240 struct net_device *indev, struct net_device *outdev,
241 int (*okfn)(struct sk_buff *))
243 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
246 /* Activate hook; either okfn or kfree_skb called, unless a hook
247 returns NF_STOLEN (in which case, it's up to the hook to deal with
248 the consequences).
250 Returns -ERRNO if packet dropped. Zero means queued, stolen or
251 accepted.
254 /* RR:
255 > I don't want nf_hook to return anything because people might forget
256 > about async and trust the return value to mean "packet was ok".
259 Just document it clearly, then you can expect some sense from kernel
260 coders :)
263 /* This is gross, but inline doesn't cut it for avoiding the function
264 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
266 /* HX: It's slightly less gross now. */
268 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
269 ({int __ret; \
270 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
271 __ret = (okfn)(skb); \
272 __ret;})
274 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
275 ({int __ret; \
276 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
277 __ret = (okfn)(skb); \
278 __ret;})
280 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
281 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
283 /* Call setsockopt() */
284 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
285 int len);
286 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
287 int *len);
289 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
290 char __user *opt, int len);
291 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
292 char __user *opt, int *len);
294 /* Packet queuing */
295 struct nf_queue_handler {
296 int (*outfn)(struct sk_buff *skb, struct nf_info *info,
297 unsigned int queuenum, void *data);
298 void *data;
299 char *name;
301 extern int nf_register_queue_handler(int pf,
302 struct nf_queue_handler *qh);
303 extern int nf_unregister_queue_handler(int pf);
304 extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
305 extern void nf_reinject(struct sk_buff *skb,
306 struct nf_info *info,
307 unsigned int verdict);
309 /* FIXME: Before cache is ever used, this must be implemented for real. */
310 extern void nf_invalidate_cache(int pf);
312 /* Call this before modifying an existing packet: ensures it is
313 modifiable and linear to the point you care about (writable_len).
314 Returns true or false. */
315 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
317 static inline void nf_csum_replace4(__sum16 *sum, __be32 from, __be32 to)
319 __be32 diff[] = { ~from, to };
321 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), ~csum_unfold(*sum)));
324 static inline void nf_csum_replace2(__sum16 *sum, __be16 from, __be16 to)
326 nf_csum_replace4(sum, (__force __be32)from, (__force __be32)to);
329 extern void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
330 __be32 from, __be32 to, int pseudohdr);
332 static inline void nf_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
333 __be16 from, __be16 to, int pseudohdr)
335 nf_proto_csum_replace4(sum, skb, (__force __be32)from,
336 (__force __be32)to, pseudohdr);
339 struct flowi;
341 struct nf_afinfo {
342 unsigned short family;
343 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
344 unsigned int dataoff, u_int8_t protocol);
345 int (*route)(struct dst_entry **dst, struct flowi *fl);
346 void (*saveroute)(const struct sk_buff *skb,
347 struct nf_info *info);
348 int (*reroute)(struct sk_buff *skb,
349 const struct nf_info *info);
350 int route_key_size;
353 extern struct nf_afinfo *nf_afinfo[];
354 static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
356 return rcu_dereference(nf_afinfo[family]);
359 static inline __sum16
360 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
361 u_int8_t protocol, unsigned short family)
363 struct nf_afinfo *afinfo;
364 __sum16 csum = 0;
366 rcu_read_lock();
367 afinfo = nf_get_afinfo(family);
368 if (afinfo)
369 csum = afinfo->checksum(skb, hook, dataoff, protocol);
370 rcu_read_unlock();
371 return csum;
374 extern int nf_register_afinfo(struct nf_afinfo *afinfo);
375 extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
377 #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
379 #include <net/flow.h>
380 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
382 static inline void
383 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
385 #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
386 void (*decodefn)(struct sk_buff *, struct flowi *);
388 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
389 decodefn(skb, fl);
390 #endif
393 #ifdef CONFIG_PROC_FS
394 #include <linux/proc_fs.h>
395 extern struct proc_dir_entry *proc_net_netfilter;
396 #endif
398 #else /* !CONFIG_NETFILTER */
399 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
400 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
401 static inline int nf_hook_thresh(int pf, unsigned int hook,
402 struct sk_buff *skb,
403 struct net_device *indev,
404 struct net_device *outdev,
405 int (*okfn)(struct sk_buff *), int thresh,
406 int cond)
408 return okfn(skb);
410 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
411 struct net_device *indev, struct net_device *outdev,
412 int (*okfn)(struct sk_buff *))
414 return 1;
416 struct flowi;
417 static inline void
418 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
419 #endif /*CONFIG_NETFILTER*/
421 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
422 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
423 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
424 extern void (*nf_ct_destroy)(struct nf_conntrack *);
425 #else
426 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
427 #endif
429 #endif /*__KERNEL__*/
430 #endif /*__LINUX_NETFILTER_H*/