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 or errno values. Not nice, but better than additional function
29 #define NF_VERDICT_MASK 0x000000ff
31 /* extra verdict flags have mask 0x0000ff00 */
32 #define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
34 /* queue number (NF_QUEUE) or errno (NF_DROP) */
35 #define NF_VERDICT_QMASK 0xffff0000
36 #define NF_VERDICT_QBITS 16
38 #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
40 #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
42 /* only for userspace compatibility */
44 /* Generic cache responses from hook functions.
45 <= 0x2000 is used for protocol-flags. */
46 #define NFC_UNKNOWN 0x4000
47 #define NFC_ALTERED 0x8000
49 /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
50 #define NF_VERDICT_BITS 16
81 #ifdef CONFIG_NETFILTER
82 static inline int NF_DROP_GETERR(int verdict
)
84 return -(verdict
>> NF_VERDICT_QBITS
);
87 static inline int nf_inet_addr_cmp(const union nf_inet_addr
*a1
,
88 const union nf_inet_addr
*a2
)
90 return a1
->all
[0] == a2
->all
[0] &&
91 a1
->all
[1] == a2
->all
[1] &&
92 a1
->all
[2] == a2
->all
[2] &&
93 a1
->all
[3] == a2
->all
[3];
96 extern void netfilter_init(void);
98 /* Largest hook number + 1 */
99 #define NF_MAX_HOOKS 8
103 typedef unsigned int nf_hookfn(unsigned int hooknum
,
105 const struct net_device
*in
,
106 const struct net_device
*out
,
107 int (*okfn
)(struct sk_buff
*));
110 struct list_head list
;
112 /* User fills in from here down. */
114 struct module
*owner
;
116 unsigned int hooknum
;
117 /* Hooks are ordered in ascending priority. */
121 struct nf_sockopt_ops
{
122 struct list_head list
;
126 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
129 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
131 int (*compat_set
)(struct sock
*sk
, int optval
,
132 void __user
*user
, unsigned int len
);
136 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
138 int (*compat_get
)(struct sock
*sk
, int optval
,
139 void __user
*user
, int *len
);
141 /* Use the module struct to lock set/get code in place */
142 struct module
*owner
;
145 /* Function to register/unregister hook points. */
146 int nf_register_hook(struct nf_hook_ops
*reg
);
147 void nf_unregister_hook(struct nf_hook_ops
*reg
);
148 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
149 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
151 /* Functions to register get/setsockopt ranges (non-inclusive). You
152 need to check permissions yourself! */
153 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
154 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
157 /* Sysctl registration */
158 extern struct ctl_path nf_net_netfilter_sysctl_path
[];
159 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path
[];
160 #endif /* CONFIG_SYSCTL */
162 extern struct list_head nf_hooks
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
164 int nf_hook_slow(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
165 struct net_device
*indev
, struct net_device
*outdev
,
166 int (*okfn
)(struct sk_buff
*), int thresh
);
169 * nf_hook_thresh - call a netfilter hook
171 * Returns 1 if the hook has allowed the packet to pass. The function
172 * okfn must be invoked by the caller in this case. Any other return
173 * value indicates the packet has been consumed by the hook.
175 static inline int nf_hook_thresh(u_int8_t pf
, unsigned int hook
,
177 struct net_device
*indev
,
178 struct net_device
*outdev
,
179 int (*okfn
)(struct sk_buff
*), int thresh
)
181 #ifndef CONFIG_NETFILTER_DEBUG
182 if (list_empty(&nf_hooks
[pf
][hook
]))
185 return nf_hook_slow(pf
, hook
, skb
, indev
, outdev
, okfn
, thresh
);
188 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
189 struct net_device
*indev
, struct net_device
*outdev
,
190 int (*okfn
)(struct sk_buff
*))
192 return nf_hook_thresh(pf
, hook
, skb
, indev
, outdev
, okfn
, INT_MIN
);
195 /* Activate hook; either okfn or kfree_skb called, unless a hook
196 returns NF_STOLEN (in which case, it's up to the hook to deal with
199 Returns -ERRNO if packet dropped. Zero means queued, stolen or
204 > I don't want nf_hook to return anything because people might forget
205 > about async and trust the return value to mean "packet was ok".
208 Just document it clearly, then you can expect some sense from kernel
213 NF_HOOK_THRESH(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
214 struct net_device
*in
, struct net_device
*out
,
215 int (*okfn
)(struct sk_buff
*), int thresh
)
217 int ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, thresh
);
224 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
225 struct net_device
*in
, struct net_device
*out
,
226 int (*okfn
)(struct sk_buff
*), bool cond
)
231 ((ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
)) == 1))
237 NF_HOOK(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
238 struct net_device
*in
, struct net_device
*out
,
239 int (*okfn
)(struct sk_buff
*))
241 return NF_HOOK_THRESH(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
);
244 /* Call setsockopt() */
245 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
247 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
250 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
251 char __user
*opt
, unsigned int len
);
252 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
253 char __user
*opt
, int *len
);
256 /* Call this before modifying an existing packet: ensures it is
257 modifiable and linear to the point you care about (writable_len).
258 Returns true or false. */
259 extern int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
262 struct nf_queue_entry
;
265 unsigned short family
;
266 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
267 unsigned int dataoff
, u_int8_t protocol
);
268 __sum16 (*checksum_partial
)(struct sk_buff
*skb
,
270 unsigned int dataoff
,
273 int (*route
)(struct dst_entry
**dst
, struct flowi
*fl
);
274 void (*saveroute
)(const struct sk_buff
*skb
,
275 struct nf_queue_entry
*entry
);
276 int (*reroute
)(struct sk_buff
*skb
,
277 const struct nf_queue_entry
*entry
);
281 extern const struct nf_afinfo __rcu
*nf_afinfo
[NFPROTO_NUMPROTO
];
282 static inline const struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
284 return rcu_dereference(nf_afinfo
[family
]);
287 static inline __sum16
288 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
289 u_int8_t protocol
, unsigned short family
)
291 const struct nf_afinfo
*afinfo
;
295 afinfo
= nf_get_afinfo(family
);
297 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
302 static inline __sum16
303 nf_checksum_partial(struct sk_buff
*skb
, unsigned int hook
,
304 unsigned int dataoff
, unsigned int len
,
305 u_int8_t protocol
, unsigned short family
)
307 const struct nf_afinfo
*afinfo
;
311 afinfo
= nf_get_afinfo(family
);
313 csum
= afinfo
->checksum_partial(skb
, hook
, dataoff
, len
,
319 extern int nf_register_afinfo(const struct nf_afinfo
*afinfo
);
320 extern void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
);
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
, u_int8_t family
)
328 #ifdef CONFIG_NF_NAT_NEEDED
329 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
331 if (family
== AF_INET
) {
333 decodefn
= rcu_dereference(ip_nat_decode_session
);
341 #ifdef CONFIG_PROC_FS
342 #include <linux/proc_fs.h>
343 extern struct proc_dir_entry
*proc_net_netfilter
;
346 #else /* !CONFIG_NETFILTER */
347 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
348 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
349 static inline int nf_hook_thresh(u_int8_t pf
, unsigned int hook
,
351 struct net_device
*indev
,
352 struct net_device
*outdev
,
353 int (*okfn
)(struct sk_buff
*), int thresh
)
357 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
358 struct net_device
*indev
, struct net_device
*outdev
,
359 int (*okfn
)(struct sk_buff
*))
365 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
368 #endif /*CONFIG_NETFILTER*/
370 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
371 extern void (*ip_ct_attach
)(struct sk_buff
*, struct sk_buff
*) __rcu
;
372 extern void nf_ct_attach(struct sk_buff
*, struct sk_buff
*);
373 extern void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu
;
375 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
378 #endif /*__KERNEL__*/
379 #endif /*__LINUX_NETFILTER_H*/