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 */
33 /* queue number (NF_QUEUE) or errno (NF_DROP) */
34 #define NF_VERDICT_QMASK 0xffff0000
35 #define NF_VERDICT_QBITS 16
37 #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
39 #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
41 /* only for userspace compatibility */
43 /* Generic cache responses from hook functions.
44 <= 0x2000 is used for protocol-flags. */
45 #define NFC_UNKNOWN 0x4000
46 #define NFC_ALTERED 0x8000
48 /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
49 #define NF_VERDICT_BITS 16
80 #ifdef CONFIG_NETFILTER
81 static inline int NF_DROP_GETERR(int verdict
)
83 return -(verdict
>> NF_VERDICT_QBITS
);
86 static inline int nf_inet_addr_cmp(const union nf_inet_addr
*a1
,
87 const union nf_inet_addr
*a2
)
89 return a1
->all
[0] == a2
->all
[0] &&
90 a1
->all
[1] == a2
->all
[1] &&
91 a1
->all
[2] == a2
->all
[2] &&
92 a1
->all
[3] == a2
->all
[3];
95 extern void netfilter_init(void);
97 /* Largest hook number + 1 */
98 #define NF_MAX_HOOKS 8
102 typedef unsigned int nf_hookfn(unsigned int hooknum
,
104 const struct net_device
*in
,
105 const struct net_device
*out
,
106 int (*okfn
)(struct sk_buff
*));
109 struct list_head list
;
111 /* User fills in from here down. */
113 struct module
*owner
;
115 unsigned int hooknum
;
116 /* Hooks are ordered in ascending priority. */
120 struct nf_sockopt_ops
{
121 struct list_head list
;
125 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
128 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
130 int (*compat_set
)(struct sock
*sk
, int optval
,
131 void __user
*user
, unsigned int len
);
135 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
137 int (*compat_get
)(struct sock
*sk
, int optval
,
138 void __user
*user
, int *len
);
140 /* Use the module struct to lock set/get code in place */
141 struct module
*owner
;
144 /* Function to register/unregister hook points. */
145 int nf_register_hook(struct nf_hook_ops
*reg
);
146 void nf_unregister_hook(struct nf_hook_ops
*reg
);
147 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
148 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
150 /* Functions to register get/setsockopt ranges (non-inclusive). You
151 need to check permissions yourself! */
152 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
153 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
156 /* Sysctl registration */
157 extern struct ctl_path nf_net_netfilter_sysctl_path
[];
158 extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path
[];
159 #endif /* CONFIG_SYSCTL */
161 extern struct list_head nf_hooks
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
163 int nf_hook_slow(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
164 struct net_device
*indev
, struct net_device
*outdev
,
165 int (*okfn
)(struct sk_buff
*), int thresh
);
168 * nf_hook_thresh - call a netfilter hook
170 * Returns 1 if the hook has allowed the packet to pass. The function
171 * okfn must be invoked by the caller in this case. Any other return
172 * value indicates the packet has been consumed by the hook.
174 static inline int nf_hook_thresh(u_int8_t pf
, unsigned int hook
,
176 struct net_device
*indev
,
177 struct net_device
*outdev
,
178 int (*okfn
)(struct sk_buff
*), int thresh
)
180 #ifndef CONFIG_NETFILTER_DEBUG
181 if (list_empty(&nf_hooks
[pf
][hook
]))
184 return nf_hook_slow(pf
, hook
, skb
, indev
, outdev
, okfn
, thresh
);
187 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
188 struct net_device
*indev
, struct net_device
*outdev
,
189 int (*okfn
)(struct sk_buff
*))
191 return nf_hook_thresh(pf
, hook
, skb
, indev
, outdev
, okfn
, INT_MIN
);
194 /* Activate hook; either okfn or kfree_skb called, unless a hook
195 returns NF_STOLEN (in which case, it's up to the hook to deal with
198 Returns -ERRNO if packet dropped. Zero means queued, stolen or
203 > I don't want nf_hook to return anything because people might forget
204 > about async and trust the return value to mean "packet was ok".
207 Just document it clearly, then you can expect some sense from kernel
212 NF_HOOK_THRESH(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
213 struct net_device
*in
, struct net_device
*out
,
214 int (*okfn
)(struct sk_buff
*), int thresh
)
216 int ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, thresh
);
223 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
224 struct net_device
*in
, struct net_device
*out
,
225 int (*okfn
)(struct sk_buff
*), bool cond
)
230 ((ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
)) == 1))
236 NF_HOOK(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
237 struct net_device
*in
, struct net_device
*out
,
238 int (*okfn
)(struct sk_buff
*))
240 return NF_HOOK_THRESH(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
);
243 /* Call setsockopt() */
244 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
246 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
249 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
250 char __user
*opt
, unsigned int len
);
251 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
252 char __user
*opt
, int *len
);
255 /* Call this before modifying an existing packet: ensures it is
256 modifiable and linear to the point you care about (writable_len).
257 Returns true or false. */
258 extern int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
261 struct nf_queue_entry
;
264 unsigned short family
;
265 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
266 unsigned int dataoff
, u_int8_t protocol
);
267 __sum16 (*checksum_partial
)(struct sk_buff
*skb
,
269 unsigned int dataoff
,
272 int (*route
)(struct dst_entry
**dst
, struct flowi
*fl
);
273 void (*saveroute
)(const struct sk_buff
*skb
,
274 struct nf_queue_entry
*entry
);
275 int (*reroute
)(struct sk_buff
*skb
,
276 const struct nf_queue_entry
*entry
);
280 extern const struct nf_afinfo __rcu
*nf_afinfo
[NFPROTO_NUMPROTO
];
281 static inline const struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
283 return rcu_dereference(nf_afinfo
[family
]);
286 static inline __sum16
287 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
288 u_int8_t protocol
, unsigned short family
)
290 const struct nf_afinfo
*afinfo
;
294 afinfo
= nf_get_afinfo(family
);
296 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
301 static inline __sum16
302 nf_checksum_partial(struct sk_buff
*skb
, unsigned int hook
,
303 unsigned int dataoff
, unsigned int len
,
304 u_int8_t protocol
, unsigned short family
)
306 const struct nf_afinfo
*afinfo
;
310 afinfo
= nf_get_afinfo(family
);
312 csum
= afinfo
->checksum_partial(skb
, hook
, dataoff
, len
,
318 extern int nf_register_afinfo(const struct nf_afinfo
*afinfo
);
319 extern void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
);
321 #include <net/flow.h>
322 extern void (*ip_nat_decode_session
)(struct sk_buff
*, struct flowi
*);
325 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
327 #ifdef CONFIG_NF_NAT_NEEDED
328 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
330 if (family
== AF_INET
) {
332 decodefn
= rcu_dereference(ip_nat_decode_session
);
340 #ifdef CONFIG_PROC_FS
341 #include <linux/proc_fs.h>
342 extern struct proc_dir_entry
*proc_net_netfilter
;
345 #else /* !CONFIG_NETFILTER */
346 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
347 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
348 static inline int nf_hook_thresh(u_int8_t pf
, unsigned int hook
,
350 struct net_device
*indev
,
351 struct net_device
*outdev
,
352 int (*okfn
)(struct sk_buff
*), int thresh
)
356 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
357 struct net_device
*indev
, struct net_device
*outdev
,
358 int (*okfn
)(struct sk_buff
*))
364 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
367 #endif /*CONFIG_NETFILTER*/
369 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
370 extern void (*ip_ct_attach
)(struct sk_buff
*, struct sk_buff
*) __rcu
;
371 extern void nf_ct_attach(struct sk_buff
*, struct sk_buff
*);
372 extern void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu
;
374 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
377 #endif /*__KERNEL__*/
378 #endif /*__LINUX_NETFILTER_H*/