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
52 #ifdef CONFIG_NETFILTER
54 extern void netfilter_init(void);
56 /* Largest hook number + 1 */
57 #define NF_MAX_HOOKS 8
62 typedef unsigned int nf_hookfn(unsigned int hooknum,
64 const struct net_device *in,
65 const struct net_device *out,
66 int (*okfn)(struct sk_buff *));
70 struct list_head list;
72 /* User fills in from here down. */
77 /* Hooks are ordered in ascending priority. */
83 struct list_head list;
87 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
90 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
91 int (*compat_set)(struct sock *sk, int optval,
92 void __user *user, unsigned int len);
96 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
97 int (*compat_get)(struct sock *sk, int optval,
98 void __user *user, int *len);
100 /* Use the module struct to lock set/get code in place */
101 struct module *owner;
104 /* Each queued (to userspace) skbuff has one of these. */
107 /* The ops struct which sent us to userspace. */
108 struct nf_hook_ops *elem;
110 /* If we're sent to userspace, this keeps housekeeping info */
113 struct net_device *indev, *outdev;
114 int (*okfn)(struct sk_buff *);
117 /* Function to register/unregister hook points. */
118 int nf_register_hook(struct nf_hook_ops *reg);
119 void nf_unregister_hook(struct nf_hook_ops *reg);
120 int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
121 void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
123 /* Functions to register get/setsockopt ranges (non-inclusive). You
124 need to check permissions yourself! */
125 int nf_register_sockopt(struct nf_sockopt_ops *reg);
126 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
129 /* Sysctl registration */
130 struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
131 struct ctl_table *table);
132 void nf_unregister_sysctl_table(struct ctl_table_header *header,
133 struct ctl_table *table);
134 extern struct ctl_table nf_net_netfilter_sysctl_path[];
135 extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
136 #endif /* CONFIG_SYSCTL */
138 extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
140 /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
141 * disappear once iptables is replaced with pkttables. Please DO NOT use them
142 * for any new code! */
143 #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
144 #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
145 #define NF_LOG_IPOPT 0x04 /* Log IP options */
146 #define NF_LOG_UID 0x08 /* Log UID owning local socket */
147 #define NF_LOG_MASK 0x0f
149 #define NF_LOG_TYPE_LOG 0x01
150 #define NF_LOG_TYPE_ULOG 0x02
158 u_int16_t qthreshold;
167 typedef void nf_logfn(unsigned int pf,
168 unsigned int hooknum,
169 const struct sk_buff *skb,
170 const struct net_device *in,
171 const struct net_device *out,
172 const struct nf_loginfo *li,
181 /* Function to register/unregister log function. */
182 int nf_log_register(int pf, struct nf_logger *logger);
183 void nf_log_unregister(struct nf_logger *logger);
184 void nf_log_unregister_pf(int pf);
186 /* Calls the registered backend logging function */
187 void nf_log_packet(int pf,
188 unsigned int hooknum,
189 const struct sk_buff *skb,
190 const struct net_device *in,
191 const struct net_device *out,
192 struct nf_loginfo *li,
193 const char *fmt, ...);
195 int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
196 struct net_device *indev, struct net_device *outdev,
197 int (*okfn)(struct sk_buff *), int thresh);
200 * nf_hook_thresh - call a netfilter hook
202 * Returns 1 if the hook has allowed the packet to pass. The function
203 * okfn must be invoked by the caller in this case. Any other return
204 * value indicates the packet has been consumed by the hook.
206 static inline int nf_hook_thresh(int pf, unsigned int hook,
208 struct net_device *indev,
209 struct net_device *outdev,
210 int (*okfn)(struct sk_buff *), int thresh,
215 #ifndef CONFIG_NETFILTER_DEBUG
216 if (list_empty(&nf_hooks[pf][hook]))
219 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
222 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
223 struct net_device *indev, struct net_device *outdev,
224 int (*okfn)(struct sk_buff *))
226 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
229 /* Activate hook; either okfn or kfree_skb called, unless a hook
230 returns NF_STOLEN (in which case, it's up to the hook to deal with
233 Returns -ERRNO if packet dropped. Zero means queued, stolen or
238 > I don't want nf_hook to return anything because people might forget
239 > about async and trust the return value to mean "packet was ok".
242 Just document it clearly, then you can expect some sense from kernel
246 /* This is gross, but inline doesn't cut it for avoiding the function
247 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
249 /* HX: It's slightly less gross now. */
251 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
253 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
254 __ret = (okfn)(skb); \
257 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
259 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
260 __ret = (okfn)(skb); \
263 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
264 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
266 /* Call setsockopt() */
267 int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
269 int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
272 int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
273 char __user *opt, int len);
274 int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
275 char __user *opt, int *len);
278 struct nf_queue_handler {
279 int (*outfn)(struct sk_buff *skb, struct nf_info *info,
280 unsigned int queuenum);
283 extern int nf_register_queue_handler(int pf,
284 const struct nf_queue_handler *qh);
285 extern int nf_unregister_queue_handler(int pf,
286 const struct nf_queue_handler *qh);
287 extern void nf_unregister_queue_handlers(const struct nf_queue_handler *qh);
288 extern void nf_reinject(struct sk_buff *skb,
289 struct nf_info *info,
290 unsigned int verdict);
292 /* FIXME: Before cache is ever used, this must be implemented for real. */
293 extern void nf_invalidate_cache(int pf);
295 /* Call this before modifying an existing packet: ensures it is
296 modifiable and linear to the point you care about (writable_len).
297 Returns true or false. */
298 extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
302 unsigned short family;
303 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
304 unsigned int dataoff, u_int8_t protocol);
305 int (*route)(struct dst_entry **dst, struct flowi *fl);
306 void (*saveroute)(const struct sk_buff *skb,
307 struct nf_info *info);
308 int (*reroute)(struct sk_buff *skb,
309 const struct nf_info *info);
313 extern struct nf_afinfo *nf_afinfo[];
314 static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
316 return rcu_dereference(nf_afinfo[family]);
319 static inline __sum16
320 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
321 u_int8_t protocol, unsigned short family)
323 struct nf_afinfo *afinfo;
327 afinfo = nf_get_afinfo(family);
329 csum = afinfo->checksum(skb, hook, dataoff, protocol);
334 extern int nf_register_afinfo(struct nf_afinfo *afinfo);
335 extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
337 #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
339 #include <net/flow.h>
340 extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
343 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
345 #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
346 void (*decodefn)(struct sk_buff *, struct flowi *);
348 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
353 #ifdef CONFIG_PROC_FS
354 #include <linux/proc_fs.h>
355 extern struct proc_dir_entry *proc_net_netfilter;
358 #else /* !CONFIG_NETFILTER */
359 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
360 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
361 static inline int nf_hook_thresh(int pf, unsigned int hook,
363 struct net_device *indev,
364 struct net_device *outdev,
365 int (*okfn)(struct sk_buff *), int thresh,
370 static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
371 struct net_device *indev, struct net_device *outdev,
372 int (*okfn)(struct sk_buff *))
378 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
379 #endif /*CONFIG_NETFILTER*/
381 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
382 extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
383 extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
384 extern void (*nf_ct_destroy)(struct nf_conntrack *);
386 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
389 #endif /*__KERNEL__*/
390 #endif /*__LINUX_NETFILTER_H*/