1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
10 #include <linux/in6.h>
11 #include <linux/wait.h>
12 #include <linux/list.h>
14 #include <linux/types.h>
15 #include <linux/compiler.h>
17 /* Responses from hook functions. */
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_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
36 /* only for userspace compatibility */
38 /* Generic cache responses from hook functions.
39 <= 0x2000 is used for protocol-flags. */
40 #define NFC_UNKNOWN 0x4000
41 #define NFC_ALTERED 0x8000
72 #ifdef CONFIG_NETFILTER
74 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
75 const union nf_inet_addr *a2)
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];
83 extern void netfilter_init(void);
85 /* Largest hook number + 1 */
86 #define NF_MAX_HOOKS 8
90 typedef unsigned int nf_hookfn(unsigned int hooknum,
92 const struct net_device *in,
93 const struct net_device *out,
94 int (*okfn)(struct sk_buff *));
97 struct list_head list;
99 /* User fills in from here down. */
101 struct module *owner;
103 unsigned int hooknum;
104 /* Hooks are ordered in ascending priority. */
108 struct nf_sockopt_ops {
109 struct list_head list;
113 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
116 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
118 int (*compat_set)(struct sock *sk, int optval,
119 void __user *user, unsigned int len);
123 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
125 int (*compat_get)(struct sock *sk, int optval,
126 void __user *user, int *len);
128 /* Use the module struct to lock set/get code in place */
129 struct module *owner;
132 /* Function to register/unregister hook points. */
133 int nf_register_hook(struct nf_hook_ops *reg);
134 void nf_unregister_hook(struct nf_hook_ops *reg);
135 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
136 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
138 /* Functions to register get/setsockopt ranges (non-inclusive). You
139 need to check permissions yourself! */
140 int nf_register_sockopt(struct nf_sockopt_ops *reg);
141 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
144 /* Sysctl registration */
145 extern struct ctl_path nf_net_netfilter_sysctl_path[];
146 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
147 #endif /* CONFIG_SYSCTL */
149 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
151 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
152 struct net_device *indev, struct net_device *outdev,
153 int (*okfn)(struct sk_buff *), int thresh);
156 * nf_hook_thresh - call a netfilter hook
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.
162 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
164 struct net_device *indev,
165 struct net_device *outdev,
166 int (*okfn)(struct sk_buff *), int thresh,
171 #ifndef CONFIG_NETFILTER_DEBUG
172 if (list_empty(&nf_hooks[pf][hook]))
175 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
178 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
179 struct net_device *indev, struct net_device *outdev,
180 int (*okfn)(struct sk_buff *))
182 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
185 /* Activate hook; either okfn or kfree_skb called, unless a hook
186 returns NF_STOLEN (in which case, it's up to the hook to deal with
189 Returns -ERRNO if packet dropped. Zero means queued, stolen or
194 > I don't want nf_hook to return anything because people might forget
195 > about async and trust the return value to mean "packet was ok".
198 Just document it clearly, then you can expect some sense from kernel
202 /* This is gross, but inline doesn't cut it for avoiding the function
203 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
205 /* HX: It's slightly less gross now. */
207 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
209 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
210 __ret = (okfn)(skb); \
213 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
215 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
216 __ret = (okfn)(skb); \
219 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
220 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
222 /* Call setsockopt() */
223 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
225 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
228 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
229 char __user *opt, unsigned int len);
230 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
231 char __user *opt, int *len);
234 /* Call this before modifying an existing packet: ensures it is
235 modifiable and linear to the point you care about (writable_len).
236 Returns true or false. */
237 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
240 struct nf_queue_entry;
243 unsigned short family;
244 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
245 unsigned int dataoff, u_int8_t protocol);
246 __sum16 (*checksum_partial)(struct sk_buff *skb,
248 unsigned int dataoff,
251 int (*route)(struct dst_entry **dst, struct flowi *fl);
252 void (*saveroute)(const struct sk_buff *skb,
253 struct nf_queue_entry *entry);
254 int (*reroute)(struct sk_buff *skb,
255 const struct nf_queue_entry *entry);
259 extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
260 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
262 return rcu_dereference(nf_afinfo[family]);
265 static inline __sum16
266 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
267 u_int8_t protocol, unsigned short family)
269 const struct nf_afinfo *afinfo;
273 afinfo = nf_get_afinfo(family);
275 csum = afinfo->checksum(skb, hook, dataoff, protocol);
280 static inline __sum16
281 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
282 unsigned int dataoff, unsigned int len,
283 u_int8_t protocol, unsigned short family)
285 const struct nf_afinfo *afinfo;
289 afinfo = nf_get_afinfo(family);
291 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
297 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
298 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
300 #include <net/flow.h>
301 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
304 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
306 #ifdef CONFIG_NF_NAT_NEEDED
307 void (*decodefn)(struct sk_buff *, struct flowi *);
309 if (family == AF_INET) {
311 decodefn = rcu_dereference(ip_nat_decode_session);
319 #ifdef CONFIG_PROC_FS
320 #include <linux/proc_fs.h>
321 extern struct proc_dir_entry *proc_net_netfilter;
324 #else /* !CONFIG_NETFILTER */
325 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
326 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
327 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
329 struct net_device *indev,
330 struct net_device *outdev,
331 int (*okfn)(struct sk_buff *), int thresh,
336 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
337 struct net_device *indev, struct net_device *outdev,
338 int (*okfn)(struct sk_buff *))
344 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
347 #endif /*CONFIG_NETFILTER*/
349 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
350 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
351 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
352 extern void (*nf_ct_destroy)(struct nf_conntrack *);
354 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
357 #endif /*__KERNEL__*/
358 #endif /*__LINUX_NETFILTER_H*/