1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <net/net_namespace.h>
16 #include <linux/types.h>
17 #include <linux/compiler.h>
19 /* Responses from hook functions. */
26 #define NF_MAX_VERDICT NF_STOP
28 /* we overload the higher bits for encoding auxiliary data such as the queue
29 * number. Not nice, but better than additional function arguments. */
30 #define NF_VERDICT_MASK 0x0000ffff
31 #define NF_VERDICT_BITS 16
33 #define NF_VERDICT_QMASK 0xffff0000
34 #define NF_VERDICT_QBITS 16
36 #define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
38 /* only for userspace compatibility */
40 /* Generic cache responses from hook functions.
41 <= 0x2000 is used for protocol-flags. */
42 #define NFC_UNKNOWN 0x4000
43 #define NFC_ALTERED 0x8000
74 #ifdef CONFIG_NETFILTER
76 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
77 const union nf_inet_addr *a2)
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];
85 extern void netfilter_init(void);
87 /* Largest hook number + 1 */
88 #define NF_MAX_HOOKS 8
92 typedef unsigned int nf_hookfn(unsigned int hooknum,
94 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *));
100 struct list_head list;
102 /* User fills in from here down. */
104 struct module *owner;
106 unsigned int hooknum;
107 /* Hooks are ordered in ascending priority. */
111 struct nf_sockopt_ops
113 struct list_head list;
117 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
120 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
121 int (*compat_set)(struct sock *sk, int optval,
122 void __user *user, unsigned int len);
126 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
127 int (*compat_get)(struct sock *sk, int optval,
128 void __user *user, int *len);
130 /* Use the module struct to lock set/get code in place */
131 struct module *owner;
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);
146 /* Sysctl registration */
147 extern struct ctl_path nf_net_netfilter_sysctl_path[];
148 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
149 #endif /* CONFIG_SYSCTL */
151 extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
153 int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
154 struct net_device *indev, struct net_device *outdev,
155 int (*okfn)(struct sk_buff *), int thresh);
158 * nf_hook_thresh - call a netfilter hook
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.
164 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
166 struct net_device *indev,
167 struct net_device *outdev,
168 int (*okfn)(struct sk_buff *), int thresh,
173 #ifndef CONFIG_NETFILTER_DEBUG
174 if (list_empty(&nf_hooks[pf][hook]))
177 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
180 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
181 struct net_device *indev, struct net_device *outdev,
182 int (*okfn)(struct sk_buff *))
184 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
187 /* Activate hook; either okfn or kfree_skb called, unless a hook
188 returns NF_STOLEN (in which case, it's up to the hook to deal with
191 Returns -ERRNO if packet dropped. Zero means queued, stolen or
196 > I don't want nf_hook to return anything because people might forget
197 > about async and trust the return value to mean "packet was ok".
200 Just document it clearly, then you can expect some sense from kernel
204 /* This is gross, but inline doesn't cut it for avoiding the function
205 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
207 /* HX: It's slightly less gross now. */
209 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
211 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
212 __ret = (okfn)(skb); \
215 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
217 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
218 __ret = (okfn)(skb); \
221 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
222 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
224 /* Call setsockopt() */
225 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
227 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
230 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
231 char __user *opt, int len);
232 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
233 char __user *opt, int *len);
235 /* Call this before modifying an existing packet: ensures it is
236 modifiable and linear to the point you care about (writable_len).
237 Returns true or false. */
238 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
241 struct nf_queue_entry;
244 unsigned short family;
245 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
246 unsigned int dataoff, u_int8_t protocol);
247 __sum16 (*checksum_partial)(struct sk_buff *skb,
249 unsigned int dataoff,
252 int (*route)(struct dst_entry **dst, struct flowi *fl);
253 void (*saveroute)(const struct sk_buff *skb,
254 struct nf_queue_entry *entry);
255 int (*reroute)(struct sk_buff *skb,
256 const struct nf_queue_entry *entry);
260 extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
261 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
263 return rcu_dereference(nf_afinfo[family]);
266 static inline __sum16
267 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
268 u_int8_t protocol, unsigned short family)
270 const struct nf_afinfo *afinfo;
274 afinfo = nf_get_afinfo(family);
276 csum = afinfo->checksum(skb, hook, dataoff, protocol);
281 static inline __sum16
282 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
283 unsigned int dataoff, unsigned int len,
284 u_int8_t protocol, unsigned short family)
286 const struct nf_afinfo *afinfo;
290 afinfo = nf_get_afinfo(family);
292 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
298 extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
299 extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
301 #include <net/flow.h>
302 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
305 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
307 #ifdef CONFIG_NF_NAT_NEEDED
308 void (*decodefn)(struct sk_buff *, struct flowi *);
310 if (family == AF_INET) {
312 decodefn = rcu_dereference(ip_nat_decode_session);
320 #ifdef CONFIG_PROC_FS
321 #include <linux/proc_fs.h>
322 extern struct proc_dir_entry *proc_net_netfilter;
325 #else /* !CONFIG_NETFILTER */
326 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
327 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
328 static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
330 struct net_device *indev,
331 struct net_device *outdev,
332 int (*okfn)(struct sk_buff *), int thresh,
337 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
338 struct net_device *indev, struct net_device *outdev,
339 int (*okfn)(struct sk_buff *))
345 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
348 #endif /*CONFIG_NETFILTER*/
350 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
351 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
352 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
353 extern void (*nf_ct_destroy)(struct nf_conntrack *);
355 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
358 static inline struct net *nf_pre_routing_net(const struct net_device *in,
359 const struct net_device *out)
368 static inline struct net *nf_local_in_net(const struct net_device *in,
369 const struct net_device *out)
378 static inline struct net *nf_forward_net(const struct net_device *in,
379 const struct net_device *out)
382 BUG_ON(in->nd_net != out->nd_net);
389 static inline struct net *nf_local_out_net(const struct net_device *in,
390 const struct net_device *out)
399 static inline struct net *nf_post_routing_net(const struct net_device *in,
400 const struct net_device *out)
409 #endif /*__KERNEL__*/
410 #endif /*__LINUX_NETFILTER_H*/