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>
16 #include <linux/sysctl.h>
18 /* Responses from hook functions. */
25 #define NF_MAX_VERDICT NF_STOP
27 /* we overload the higher bits for encoding auxiliary data such as the queue
28 * number or errno values. Not nice, but better than additional function
30 #define NF_VERDICT_MASK 0x000000ff
32 /* extra verdict flags have mask 0x0000ff00 */
33 #define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
35 /* queue number (NF_QUEUE) or errno (NF_DROP) */
36 #define NF_VERDICT_QMASK 0xffff0000
37 #define NF_VERDICT_QBITS 16
39 #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
41 #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
43 /* only for userspace compatibility */
45 /* Generic cache responses from hook functions.
46 <= 0x2000 is used for protocol-flags. */
47 #define NFC_UNKNOWN 0x4000
48 #define NFC_ALTERED 0x8000
50 /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
51 #define NF_VERDICT_BITS 16
82 #ifdef CONFIG_NETFILTER
83 static inline int NF_DROP_GETERR(int verdict
)
85 return -(verdict
>> NF_VERDICT_QBITS
);
88 static inline int nf_inet_addr_cmp(const union nf_inet_addr
*a1
,
89 const union nf_inet_addr
*a2
)
91 return a1
->all
[0] == a2
->all
[0] &&
92 a1
->all
[1] == a2
->all
[1] &&
93 a1
->all
[2] == a2
->all
[2] &&
94 a1
->all
[3] == a2
->all
[3];
97 extern void netfilter_init(void);
99 /* Largest hook number + 1 */
100 #define NF_MAX_HOOKS 8
104 typedef unsigned int nf_hookfn(unsigned int hooknum
,
106 const struct net_device
*in
,
107 const struct net_device
*out
,
108 int (*okfn
)(struct sk_buff
*));
111 struct list_head list
;
113 /* User fills in from here down. */
115 struct module
*owner
;
117 unsigned int hooknum
;
118 /* Hooks are ordered in ascending priority. */
122 struct nf_sockopt_ops
{
123 struct list_head list
;
127 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
130 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
132 int (*compat_set
)(struct sock
*sk
, int optval
,
133 void __user
*user
, unsigned int len
);
137 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
139 int (*compat_get
)(struct sock
*sk
, int optval
,
140 void __user
*user
, int *len
);
142 /* Use the module struct to lock set/get code in place */
143 struct module
*owner
;
146 /* Function to register/unregister hook points. */
147 int nf_register_hook(struct nf_hook_ops
*reg
);
148 void nf_unregister_hook(struct nf_hook_ops
*reg
);
149 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
150 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
152 /* Functions to register get/setsockopt ranges (non-inclusive). You
153 need to check permissions yourself! */
154 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
155 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
157 extern struct list_head nf_hooks
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
159 #if defined(CONFIG_JUMP_LABEL)
160 #include <linux/static_key.h>
161 extern struct static_key nf_hooks_needed
[NFPROTO_NUMPROTO
][NF_MAX_HOOKS
];
162 static inline bool nf_hooks_active(u_int8_t pf
, unsigned int hook
)
164 if (__builtin_constant_p(pf
) &&
165 __builtin_constant_p(hook
))
166 return static_key_false(&nf_hooks_needed
[pf
][hook
]);
168 return !list_empty(&nf_hooks
[pf
][hook
]);
171 static inline bool nf_hooks_active(u_int8_t pf
, unsigned int hook
)
173 return !list_empty(&nf_hooks
[pf
][hook
]);
177 int nf_hook_slow(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
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(u_int8_t pf
, unsigned int hook
,
190 struct net_device
*indev
,
191 struct net_device
*outdev
,
192 int (*okfn
)(struct sk_buff
*), int thresh
)
194 if (nf_hooks_active(pf
, hook
))
195 return nf_hook_slow(pf
, hook
, skb
, indev
, outdev
, okfn
, thresh
);
199 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
200 struct net_device
*indev
, struct net_device
*outdev
,
201 int (*okfn
)(struct sk_buff
*))
203 return nf_hook_thresh(pf
, hook
, skb
, indev
, outdev
, okfn
, INT_MIN
);
206 /* Activate hook; either okfn or kfree_skb called, unless a hook
207 returns NF_STOLEN (in which case, it's up to the hook to deal with
210 Returns -ERRNO if packet dropped. Zero means queued, stolen or
215 > I don't want nf_hook to return anything because people might forget
216 > about async and trust the return value to mean "packet was ok".
219 Just document it clearly, then you can expect some sense from kernel
224 NF_HOOK_THRESH(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
*), int thresh
)
228 int ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, thresh
);
235 NF_HOOK_COND(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
236 struct net_device
*in
, struct net_device
*out
,
237 int (*okfn
)(struct sk_buff
*), bool cond
)
242 ((ret
= nf_hook_thresh(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
)) == 1))
248 NF_HOOK(uint8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
249 struct net_device
*in
, struct net_device
*out
,
250 int (*okfn
)(struct sk_buff
*))
252 return NF_HOOK_THRESH(pf
, hook
, skb
, in
, out
, okfn
, INT_MIN
);
255 /* Call setsockopt() */
256 int nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
258 int nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
, char __user
*opt
,
261 int compat_nf_setsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
262 char __user
*opt
, unsigned int len
);
263 int compat_nf_getsockopt(struct sock
*sk
, u_int8_t pf
, int optval
,
264 char __user
*opt
, int *len
);
267 /* Call this before modifying an existing packet: ensures it is
268 modifiable and linear to the point you care about (writable_len).
269 Returns true or false. */
270 extern int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
273 struct nf_queue_entry
;
276 unsigned short family
;
277 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
278 unsigned int dataoff
, u_int8_t protocol
);
279 __sum16 (*checksum_partial
)(struct sk_buff
*skb
,
281 unsigned int dataoff
,
284 int (*route
)(struct net
*net
, struct dst_entry
**dst
,
285 struct flowi
*fl
, bool strict
);
286 void (*saveroute
)(const struct sk_buff
*skb
,
287 struct nf_queue_entry
*entry
);
288 int (*reroute
)(struct sk_buff
*skb
,
289 const struct nf_queue_entry
*entry
);
293 extern const struct nf_afinfo __rcu
*nf_afinfo
[NFPROTO_NUMPROTO
];
294 static inline const struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
296 return rcu_dereference(nf_afinfo
[family
]);
299 static inline __sum16
300 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
301 u_int8_t protocol
, unsigned short family
)
303 const struct nf_afinfo
*afinfo
;
307 afinfo
= nf_get_afinfo(family
);
309 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
314 static inline __sum16
315 nf_checksum_partial(struct sk_buff
*skb
, unsigned int hook
,
316 unsigned int dataoff
, unsigned int len
,
317 u_int8_t protocol
, unsigned short family
)
319 const struct nf_afinfo
*afinfo
;
323 afinfo
= nf_get_afinfo(family
);
325 csum
= afinfo
->checksum_partial(skb
, hook
, dataoff
, len
,
331 extern int nf_register_afinfo(const struct nf_afinfo
*afinfo
);
332 extern void nf_unregister_afinfo(const struct nf_afinfo
*afinfo
);
334 #include <net/flow.h>
335 extern void (*ip_nat_decode_session
)(struct sk_buff
*, struct flowi
*);
338 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
340 #ifdef CONFIG_NF_NAT_NEEDED
341 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
343 if (family
== AF_INET
) {
345 decodefn
= rcu_dereference(ip_nat_decode_session
);
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(u_int8_t pf
, unsigned int hook
,
363 struct net_device
*indev
,
364 struct net_device
*outdev
,
365 int (*okfn
)(struct sk_buff
*), int thresh
)
369 static inline int nf_hook(u_int8_t pf
, unsigned int hook
, struct sk_buff
*skb
,
370 struct net_device
*indev
, struct net_device
*outdev
,
371 int (*okfn
)(struct sk_buff
*))
377 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, u_int8_t family
)
380 #endif /*CONFIG_NETFILTER*/
382 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
383 extern void (*ip_ct_attach
)(struct sk_buff
*, struct sk_buff
*) __rcu
;
384 extern void nf_ct_attach(struct sk_buff
*, struct sk_buff
*);
385 extern void (*nf_ct_destroy
)(struct nf_conntrack
*) __rcu
;
387 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
390 #endif /*__KERNEL__*/
391 #endif /*__LINUX_NETFILTER_H*/