1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
5 #include <linux/init.h>
6 #include <linux/types.h>
7 #include <linux/skbuff.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
13 #include <linux/compiler.h>
15 /* Responses from hook functions. */
22 #define NF_MAX_VERDICT NF_STOP
24 /* we overload the higher bits for encoding auxiliary data such as the queue
25 * number. Not nice, but better than additional function arguments. */
26 #define NF_VERDICT_MASK 0x0000ffff
27 #define NF_VERDICT_BITS 16
29 #define NF_VERDICT_QMASK 0xffff0000
30 #define NF_VERDICT_QBITS 16
32 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
34 /* only for userspace compatibility */
36 /* Generic cache responses from hook functions.
37 <= 0x2000 is used for protocol-flags. */
38 #define NFC_UNKNOWN 0x4000
39 #define NFC_ALTERED 0x8000
43 #ifdef CONFIG_NETFILTER
45 extern void netfilter_init(void);
47 /* Largest hook number + 1 */
48 #define NF_MAX_HOOKS 8
53 typedef unsigned int nf_hookfn(unsigned int hooknum,
55 const struct net_device *in,
56 const struct net_device *out,
57 int (*okfn)(struct sk_buff *));
61 struct list_head list;
63 /* User fills in from here down. */
68 /* Hooks are ordered in ascending priority. */
74 struct list_head list;
78 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
81 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
82 int (*compat_set)(struct sock *sk, int optval,
83 void __user *user, unsigned int len);
87 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
88 int (*compat_get)(struct sock *sk, int optval,
89 void __user *user, int *len);
91 /* Number of users inside set() or get(). */
93 struct task_struct *cleanup_task;
96 /* Each queued (to userspace) skbuff has one of these. */
99 /* The ops struct which sent us to userspace. */
100 struct nf_hook_ops *elem;
102 /* If we're sent to userspace, this keeps housekeeping info */
105 struct net_device *indev, *outdev;
106 int (*okfn)(struct sk_buff *);
109 /* Function to register/unregister hook points. */
110 int nf_register_hook(struct nf_hook_ops *reg);
111 void nf_unregister_hook(struct nf_hook_ops *reg);
112 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
113 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
115 /* Functions to register get/setsockopt ranges (non-inclusive). You
116 need to check permissions yourself! */
117 int nf_register_sockopt(struct nf_sockopt_ops *reg);
118 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
120 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
122 /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
123 * disappear once iptables is replaced with pkttables. Please DO NOT use them
124 * for any new code! */
125 #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
126 #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
127 #define NF_LOG_IPOPT 0x04 /* Log IP options */
128 #define NF_LOG_UID 0x08 /* Log UID owning local socket */
129 #define NF_LOG_MASK 0x0f
131 #define NF_LOG_TYPE_LOG 0x01
132 #define NF_LOG_TYPE_ULOG 0x02
140 u_int16_t qthreshold;
149 typedef void nf_logfn(unsigned int pf,
150 unsigned int hooknum,
151 const struct sk_buff *skb,
152 const struct net_device *in,
153 const struct net_device *out,
154 const struct nf_loginfo *li,
163 /* Function to register/unregister log function. */
164 int nf_log_register(int pf, struct nf_logger *logger);
165 int nf_log_unregister_pf(int pf);
166 void nf_log_unregister_logger(struct nf_logger *logger);
168 /* Calls the registered backend logging function */
169 void nf_log_packet(int pf,
170 unsigned int hooknum,
171 const struct sk_buff *skb,
172 const struct net_device *in,
173 const struct net_device *out,
174 struct nf_loginfo *li,
175 const char *fmt, ...);
177 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
178 struct net_device *indev, struct net_device *outdev,
179 int (*okfn)(struct sk_buff *), int thresh);
182 * nf_hook_thresh - call a netfilter hook
184 * Returns 1 if the hook has allowed the packet to pass. The function
185 * okfn must be invoked by the caller in this case. Any other return
186 * value indicates the packet has been consumed by the hook.
188 static inline int nf_hook_thresh(int pf, unsigned int hook,
189 struct sk_buff **pskb,
190 struct net_device *indev,
191 struct net_device *outdev,
192 int (*okfn)(struct sk_buff *), int thresh,
197 #ifndef CONFIG_NETFILTER_DEBUG
198 if (list_empty(&nf_hooks[pf][hook]))
201 return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
204 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
205 struct net_device *indev, struct net_device *outdev,
206 int (*okfn)(struct sk_buff *))
208 return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
211 /* Activate hook; either okfn or kfree_skb called, unless a hook
212 returns NF_STOLEN (in which case, it's up to the hook to deal with
215 Returns -ERRNO if packet dropped. Zero means queued, stolen or
220 > I don't want nf_hook to return anything because people might forget
221 > about async and trust the return value to mean "packet was ok".
224 Just document it clearly, then you can expect some sense from kernel
228 /* This is gross, but inline doesn't cut it for avoiding the function
229 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
231 /* HX: It's slightly less gross now. */
233 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
235 if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
236 __ret = (okfn)(skb); \
239 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
241 if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
242 __ret = (okfn)(skb); \
245 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
246 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
248 /* Call setsockopt() */
249 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
251 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
254 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
255 char __user *opt, int len);
256 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
257 char __user *opt, int *len);
260 struct nf_queue_handler {
261 int (*outfn)(struct sk_buff *skb, struct nf_info *info,
262 unsigned int queuenum, void *data);
266 extern int nf_register_queue_handler(int pf,
267 struct nf_queue_handler *qh);
268 extern int nf_unregister_queue_handler(int pf);
269 extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
270 extern void nf_reinject(struct sk_buff *skb,
271 struct nf_info *info,
272 unsigned int verdict);
274 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
275 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
277 /* FIXME: Before cache is ever used, this must be implemented for real. */
278 extern void nf_invalidate_cache(int pf);
280 /* Call this before modifying an existing packet: ensures it is
281 modifiable and linear to the point you care about (writable_len).
282 Returns true or false. */
283 extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
286 unsigned short family;
287 unsigned int (*checksum)(struct sk_buff *skb, unsigned int hook,
288 unsigned int dataoff, u_int8_t protocol);
289 void (*saveroute)(const struct sk_buff *skb,
290 struct nf_info *info);
291 int (*reroute)(struct sk_buff **skb,
292 const struct nf_info *info);
296 extern struct nf_afinfo *nf_afinfo[];
297 static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
299 return rcu_dereference(nf_afinfo[family]);
302 static inline unsigned int
303 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
304 u_int8_t protocol, unsigned short family)
306 struct nf_afinfo *afinfo;
307 unsigned int csum = 0;
310 afinfo = nf_get_afinfo(family);
312 csum = afinfo->checksum(skb, hook, dataoff, protocol);
317 extern int nf_register_afinfo(struct nf_afinfo *afinfo);
318 extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
320 #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
322 #include <net/flow.h>
323 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
326 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
328 #ifdef CONFIG_IP_NF_NAT_NEEDED
329 void (*decodefn)(struct sk_buff *, struct flowi *);
331 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
336 #ifdef CONFIG_PROC_FS
337 #include <linux/proc_fs.h>
338 extern struct proc_dir_entry *proc_net_netfilter;
341 #else /* !CONFIG_NETFILTER */
342 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
343 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
344 static inline int nf_hook_thresh(int pf, unsigned int hook,
345 struct sk_buff **pskb,
346 struct net_device *indev,
347 struct net_device *outdev,
348 int (*okfn)(struct sk_buff *), int thresh,
353 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
354 struct net_device *indev, struct net_device *outdev,
355 int (*okfn)(struct sk_buff *))
359 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
362 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
363 #endif /*CONFIG_NETFILTER*/
365 #endif /*__KERNEL__*/
366 #endif /*__LINUX_NETFILTER_H*/