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>
11 #include <linux/in6.h>
12 #include <linux/wait.h>
13 #include <linux/list.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_QBITS) & 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
62 #ifdef CONFIG_NETFILTER
64 extern void netfilter_init(void);
66 /* Largest hook number + 1 */
67 #define NF_MAX_HOOKS 8
72 typedef unsigned int nf_hookfn(unsigned int hooknum
,
74 const struct net_device
*in
,
75 const struct net_device
*out
,
76 int (*okfn
)(struct sk_buff
*));
80 struct list_head list
;
82 /* User fills in from here down. */
87 /* Hooks are ordered in ascending priority. */
93 struct list_head list
;
97 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
100 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
101 int (*compat_set
)(struct sock
*sk
, int optval
,
102 void __user
*user
, unsigned int len
);
106 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
107 int (*compat_get
)(struct sock
*sk
, int optval
,
108 void __user
*user
, int *len
);
110 /* Use the module struct to lock set/get code in place */
111 struct module
*owner
;
114 /* Function to register/unregister hook points. */
115 int nf_register_hook(struct nf_hook_ops
*reg
);
116 void nf_unregister_hook(struct nf_hook_ops
*reg
);
117 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
118 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
120 /* Functions to register get/setsockopt ranges (non-inclusive). You
121 need to check permissions yourself! */
122 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
123 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
126 /* Sysctl registration */
127 extern struct ctl_path nf_net_netfilter_sysctl_path
[];
128 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path
[];
129 #endif /* CONFIG_SYSCTL */
131 extern struct list_head nf_hooks
[NPROTO
][NF_MAX_HOOKS
];
133 int nf_hook_slow(int pf
, unsigned int hook
, struct sk_buff
*skb
,
134 struct net_device
*indev
, struct net_device
*outdev
,
135 int (*okfn
)(struct sk_buff
*), int thresh
);
138 * nf_hook_thresh - call a netfilter hook
140 * Returns 1 if the hook has allowed the packet to pass. The function
141 * okfn must be invoked by the caller in this case. Any other return
142 * value indicates the packet has been consumed by the hook.
144 static inline int nf_hook_thresh(int pf
, unsigned int hook
,
146 struct net_device
*indev
,
147 struct net_device
*outdev
,
148 int (*okfn
)(struct sk_buff
*), int thresh
,
153 #ifndef CONFIG_NETFILTER_DEBUG
154 if (list_empty(&nf_hooks
[pf
][hook
]))
157 return nf_hook_slow(pf
, hook
, skb
, indev
, outdev
, okfn
, thresh
);
160 static inline int nf_hook(int pf
, unsigned int hook
, struct sk_buff
*skb
,
161 struct net_device
*indev
, struct net_device
*outdev
,
162 int (*okfn
)(struct sk_buff
*))
164 return nf_hook_thresh(pf
, hook
, skb
, indev
, outdev
, okfn
, INT_MIN
, 1);
167 /* Activate hook; either okfn or kfree_skb called, unless a hook
168 returns NF_STOLEN (in which case, it's up to the hook to deal with
171 Returns -ERRNO if packet dropped. Zero means queued, stolen or
176 > I don't want nf_hook to return anything because people might forget
177 > about async and trust the return value to mean "packet was ok".
180 Just document it clearly, then you can expect some sense from kernel
184 /* This is gross, but inline doesn't cut it for avoiding the function
185 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
187 /* HX: It's slightly less gross now. */
189 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
191 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
192 __ret = (okfn)(skb); \
195 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
197 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
198 __ret = (okfn)(skb); \
201 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
202 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
204 /* Call setsockopt() */
205 int nf_setsockopt(struct sock
*sk
, int pf
, int optval
, char __user
*opt
,
207 int nf_getsockopt(struct sock
*sk
, int pf
, int optval
, char __user
*opt
,
210 int compat_nf_setsockopt(struct sock
*sk
, int pf
, int optval
,
211 char __user
*opt
, int len
);
212 int compat_nf_getsockopt(struct sock
*sk
, int pf
, int optval
,
213 char __user
*opt
, int *len
);
215 /* Call this before modifying an existing packet: ensures it is
216 modifiable and linear to the point you care about (writable_len).
217 Returns true or false. */
218 extern int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
221 struct nf_queue_entry
;
224 unsigned short family
;
225 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
226 unsigned int dataoff
, u_int8_t protocol
);
227 int (*route
)(struct dst_entry
**dst
, struct flowi
*fl
);
228 void (*saveroute
)(const struct sk_buff
*skb
,
229 struct nf_queue_entry
*entry
);
230 int (*reroute
)(struct sk_buff
*skb
,
231 const struct nf_queue_entry
*entry
);
235 extern const struct nf_afinfo
*nf_afinfo
[NPROTO
];
236 static inline const struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
238 return rcu_dereference(nf_afinfo
[family
]);
241 static inline __sum16
242 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
243 u_int8_t protocol
, unsigned short family
)
245 const struct nf_afinfo
*afinfo
;
249 afinfo
= nf_get_afinfo(family
);
251 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
256 extern int nf_register_afinfo(const struct nf_afinfo
*afinfo
);
257 extern void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
);
259 #include <net/flow.h>
260 extern void (*ip_nat_decode_session
)(struct sk_buff
*, struct flowi
*);
263 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, int family
)
265 #ifdef CONFIG_NF_NAT_NEEDED
266 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
268 if (family
== AF_INET
) {
270 decodefn
= rcu_dereference(ip_nat_decode_session
);
278 #ifdef CONFIG_PROC_FS
279 #include <linux/proc_fs.h>
280 extern struct proc_dir_entry
*proc_net_netfilter
;
283 #else /* !CONFIG_NETFILTER */
284 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
285 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
286 static inline int nf_hook_thresh(int pf
, unsigned int hook
,
288 struct net_device
*indev
,
289 struct net_device
*outdev
,
290 int (*okfn
)(struct sk_buff
*), int thresh
,
295 static inline int nf_hook(int pf
, unsigned int hook
, struct sk_buff
*skb
,
296 struct net_device
*indev
, struct net_device
*outdev
,
297 int (*okfn
)(struct sk_buff
*))
303 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, int family
) {}
304 #endif /*CONFIG_NETFILTER*/
306 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
307 extern void (*ip_ct_attach
)(struct sk_buff
*, struct sk_buff
*);
308 extern void nf_ct_attach(struct sk_buff
*, struct sk_buff
*);
309 extern void (*nf_ct_destroy
)(struct nf_conntrack
*);
311 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
314 #endif /*__KERNEL__*/
315 #endif /*__LINUX_NETFILTER_H*/