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/types.h>
16 #include <linux/compiler.h>
18 /* Responses from hook functions. */
24 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE)
30 #define NF_MAX_VERDICT NF_STOP
32 /* we overload the higher bits for encoding auxiliary data such as the queue
33 * number. Not nice, but better than additional function arguments. */
34 #define NF_VERDICT_MASK 0x0000ffff
35 #define NF_VERDICT_BITS 16
37 #define NF_VERDICT_QMASK 0xffff0000
38 #define NF_VERDICT_QBITS 16
40 #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
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
59 #ifdef CONFIG_NETFILTER
61 static inline int nf_inet_addr_cmp(const union nf_inet_addr
*a1
,
62 const union nf_inet_addr
*a2
)
64 return a1
->all
[0] == a2
->all
[0] &&
65 a1
->all
[1] == a2
->all
[1] &&
66 a1
->all
[2] == a2
->all
[2] &&
67 a1
->all
[3] == a2
->all
[3];
70 extern void netfilter_init(void);
72 /* Largest hook number + 1 */
73 #define NF_MAX_HOOKS 8
78 typedef unsigned int nf_hookfn(unsigned int hooknum
,
80 const struct net_device
*in
,
81 const struct net_device
*out
,
82 int (*okfn
)(struct sk_buff
*));
86 struct list_head list
;
88 /* User fills in from here down. */
93 /* Hooks are ordered in ascending priority. */
99 struct list_head list
;
103 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
106 int (*set
)(struct sock
*sk
, int optval
, void __user
*user
, unsigned int len
);
107 int (*compat_set
)(struct sock
*sk
, int optval
,
108 void __user
*user
, unsigned int len
);
112 int (*get
)(struct sock
*sk
, int optval
, void __user
*user
, int *len
);
113 int (*compat_get
)(struct sock
*sk
, int optval
,
114 void __user
*user
, int *len
);
116 /* Number of users inside set() or get(). */
118 struct task_struct
*cleanup_task
;
121 /* Each queued (to userspace) skbuff has one of these. */
124 /* The ops struct which sent us to userspace. */
125 struct nf_hook_ops
*elem
;
127 /* If we're sent to userspace, this keeps housekeeping info */
130 struct net_device
*indev
, *outdev
;
131 int (*okfn
)(struct sk_buff
*);
134 /* Function to register/unregister hook points. */
135 int nf_register_hook(struct nf_hook_ops
*reg
);
136 void nf_unregister_hook(struct nf_hook_ops
*reg
);
137 int nf_register_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
138 void nf_unregister_hooks(struct nf_hook_ops
*reg
, unsigned int n
);
140 /* Functions to register get/setsockopt ranges (non-inclusive). You
141 need to check permissions yourself! */
142 int nf_register_sockopt(struct nf_sockopt_ops
*reg
);
143 void nf_unregister_sockopt(struct nf_sockopt_ops
*reg
);
146 /* Sysctl registration */
147 struct ctl_table_header
*nf_register_sysctl_table(struct ctl_table
*path
,
148 struct ctl_table
*table
);
149 void nf_unregister_sysctl_table(struct ctl_table_header
*header
,
150 struct ctl_table
*table
);
151 extern struct ctl_table nf_net_netfilter_sysctl_path
[];
152 extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path
[];
153 #endif /* CONFIG_SYSCTL */
155 extern struct list_head nf_hooks
[NPROTO
][NF_MAX_HOOKS
];
157 /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
158 * disappear once iptables is replaced with pkttables. Please DO NOT use them
159 * for any new code! */
160 #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
161 #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
162 #define NF_LOG_IPOPT 0x04 /* Log IP options */
163 #define NF_LOG_UID 0x08 /* Log UID owning local socket */
164 #define NF_LOG_MASK 0x0f
166 #define NF_LOG_TYPE_LOG 0x01
167 #define NF_LOG_TYPE_ULOG 0x02
175 u_int16_t qthreshold
;
184 typedef void nf_logfn(unsigned int pf
,
185 unsigned int hooknum
,
186 const struct sk_buff
*skb
,
187 const struct net_device
*in
,
188 const struct net_device
*out
,
189 const struct nf_loginfo
*li
,
198 /* Function to register/unregister log function. */
199 int nf_log_register(int pf
, struct nf_logger
*logger
);
200 void nf_log_unregister(struct nf_logger
*logger
);
201 void nf_log_unregister_pf(int pf
);
203 /* Calls the registered backend logging function */
204 void nf_log_packet(int pf
,
205 unsigned int hooknum
,
206 const struct sk_buff
*skb
,
207 const struct net_device
*in
,
208 const struct net_device
*out
,
209 struct nf_loginfo
*li
,
210 const char *fmt
, ...);
212 int nf_hook_slow(int pf
, unsigned int hook
, struct sk_buff
*skb
,
213 struct net_device
*indev
, struct net_device
*outdev
,
214 int (*okfn
)(struct sk_buff
*), int thresh
);
217 * nf_hook_thresh - call a netfilter hook
219 * Returns 1 if the hook has allowed the packet to pass. The function
220 * okfn must be invoked by the caller in this case. Any other return
221 * value indicates the packet has been consumed by the hook.
223 static inline int nf_hook_thresh(int pf
, unsigned int hook
,
225 struct net_device
*indev
,
226 struct net_device
*outdev
,
227 int (*okfn
)(struct sk_buff
*), int thresh
,
232 #ifndef CONFIG_NETFILTER_DEBUG
233 if (list_empty(&nf_hooks
[pf
][hook
]))
236 return nf_hook_slow(pf
, hook
, skb
, indev
, outdev
, okfn
, thresh
);
239 static inline int nf_hook(int pf
, unsigned int hook
, struct sk_buff
*skb
,
240 struct net_device
*indev
, struct net_device
*outdev
,
241 int (*okfn
)(struct sk_buff
*))
243 return nf_hook_thresh(pf
, hook
, skb
, indev
, outdev
, okfn
, INT_MIN
, 1);
246 /* Activate hook; either okfn or kfree_skb called, unless a hook
247 returns NF_STOLEN (in which case, it's up to the hook to deal with
250 Returns -ERRNO if packet dropped. Zero means queued, stolen or
255 > I don't want nf_hook to return anything because people might forget
256 > about async and trust the return value to mean "packet was ok".
259 Just document it clearly, then you can expect some sense from kernel
263 /* This is gross, but inline doesn't cut it for avoiding the function
264 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
266 /* HX: It's slightly less gross now. */
268 #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
270 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
271 __ret = (okfn)(skb); \
274 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
276 if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
277 __ret = (okfn)(skb); \
280 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
281 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
283 /* Call setsockopt() */
284 int nf_setsockopt(struct sock
*sk
, int pf
, int optval
, char __user
*opt
,
286 int nf_getsockopt(struct sock
*sk
, int pf
, int optval
, char __user
*opt
,
289 int compat_nf_setsockopt(struct sock
*sk
, int pf
, int optval
,
290 char __user
*opt
, int len
);
291 int compat_nf_getsockopt(struct sock
*sk
, int pf
, int optval
,
292 char __user
*opt
, int *len
);
295 struct nf_queue_handler
{
296 int (*outfn
)(struct sk_buff
*skb
, struct nf_info
*info
,
297 unsigned int queuenum
, void *data
);
301 extern int nf_register_queue_handler(int pf
,
302 struct nf_queue_handler
*qh
);
303 extern int nf_unregister_queue_handler(int pf
);
304 extern void nf_unregister_queue_handlers(struct nf_queue_handler
*qh
);
305 extern void nf_reinject(struct sk_buff
*skb
,
306 struct nf_info
*info
,
307 unsigned int verdict
);
309 /* FIXME: Before cache is ever used, this must be implemented for real. */
310 extern void nf_invalidate_cache(int pf
);
312 /* Call this before modifying an existing packet: ensures it is
313 modifiable and linear to the point you care about (writable_len).
314 Returns true or false. */
315 extern int skb_make_writable(struct sk_buff
*skb
, unsigned int writable_len
);
317 static inline void nf_csum_replace4(__sum16
*sum
, __be32 from
, __be32 to
)
319 __be32 diff
[] = { ~from
, to
};
321 *sum
= csum_fold(csum_partial((char *)diff
, sizeof(diff
), ~csum_unfold(*sum
)));
324 static inline void nf_csum_replace2(__sum16
*sum
, __be16 from
, __be16 to
)
326 nf_csum_replace4(sum
, (__force __be32
)from
, (__force __be32
)to
);
329 extern void nf_proto_csum_replace4(__sum16
*sum
, struct sk_buff
*skb
,
330 __be32 from
, __be32 to
, int pseudohdr
);
332 static inline void nf_proto_csum_replace2(__sum16
*sum
, struct sk_buff
*skb
,
333 __be16 from
, __be16 to
, int pseudohdr
)
335 nf_proto_csum_replace4(sum
, skb
, (__force __be32
)from
,
336 (__force __be32
)to
, pseudohdr
);
342 unsigned short family
;
343 __sum16 (*checksum
)(struct sk_buff
*skb
, unsigned int hook
,
344 unsigned int dataoff
, u_int8_t protocol
);
345 int (*route
)(struct dst_entry
**dst
, struct flowi
*fl
);
346 void (*saveroute
)(const struct sk_buff
*skb
,
347 struct nf_info
*info
);
348 int (*reroute
)(struct sk_buff
*skb
,
349 const struct nf_info
*info
);
353 extern struct nf_afinfo
*nf_afinfo
[];
354 static inline struct nf_afinfo
*nf_get_afinfo(unsigned short family
)
356 return rcu_dereference(nf_afinfo
[family
]);
359 static inline __sum16
360 nf_checksum(struct sk_buff
*skb
, unsigned int hook
, unsigned int dataoff
,
361 u_int8_t protocol
, unsigned short family
)
363 struct nf_afinfo
*afinfo
;
367 afinfo
= nf_get_afinfo(family
);
369 csum
= afinfo
->checksum(skb
, hook
, dataoff
, protocol
);
374 extern int nf_register_afinfo(struct nf_afinfo
*afinfo
);
375 extern void nf_unregister_afinfo(struct nf_afinfo
*afinfo
);
377 #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
379 #include <net/flow.h>
380 extern void (*ip_nat_decode_session
)(struct sk_buff
*, struct flowi
*);
383 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, int family
)
385 #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
386 void (*decodefn
)(struct sk_buff
*, struct flowi
*);
388 if (family
== AF_INET
&& (decodefn
= ip_nat_decode_session
) != NULL
)
393 #ifdef CONFIG_PROC_FS
394 #include <linux/proc_fs.h>
395 extern struct proc_dir_entry
*proc_net_netfilter
;
398 #else /* !CONFIG_NETFILTER */
399 #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
400 #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
401 static inline int nf_hook_thresh(int pf
, unsigned int hook
,
403 struct net_device
*indev
,
404 struct net_device
*outdev
,
405 int (*okfn
)(struct sk_buff
*), int thresh
,
410 static inline int nf_hook(int pf
, unsigned int hook
, struct sk_buff
*skb
,
411 struct net_device
*indev
, struct net_device
*outdev
,
412 int (*okfn
)(struct sk_buff
*))
418 nf_nat_decode_session(struct sk_buff
*skb
, struct flowi
*fl
, int family
) {}
419 #endif /*CONFIG_NETFILTER*/
421 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
422 extern void (*ip_ct_attach
)(struct sk_buff
*, struct sk_buff
*);
423 extern void nf_ct_attach(struct sk_buff
*, struct sk_buff
*);
424 extern void (*nf_ct_destroy
)(struct nf_conntrack
*);
426 static inline void nf_ct_attach(struct sk_buff
*new, struct sk_buff
*skb
) {}
429 #endif /*__KERNEL__*/
430 #endif /*__LINUX_NETFILTER_H*/