]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/netfilter.h
[NETFILTER]: nf_conntrack: don't use nfct in skb if conntrack is disabled
[mirror_ubuntu-artful-kernel.git] / include / linux / netfilter.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
4#ifdef __KERNEL__
5#include <linux/init.h>
6#include <linux/types.h>
7#include <linux/skbuff.h>
8#include <linux/net.h>
9#include <linux/if.h>
10#include <linux/wait.h>
11#include <linux/list.h>
12#endif
13#include <linux/compiler.h>
14
15/* Responses from hook functions. */
16#define NF_DROP 0
17#define NF_ACCEPT 1
18#define NF_STOLEN 2
19#define NF_QUEUE 3
20#define NF_REPEAT 4
21#define NF_STOP 5
22#define NF_MAX_VERDICT NF_STOP
23
0ab43f84
HW
24/* we overload the higher bits for encoding auxiliary data such as the queue
25 * number. Not nice, but better than additional function arguments. */
26#define NF_VERDICT_MASK 0x0000ffff
27#define NF_VERDICT_BITS 16
28
29#define NF_VERDICT_QMASK 0xffff0000
30#define NF_VERDICT_QBITS 16
31
b766b305 32#define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
0ab43f84 33
6869c4d8
HW
34/* only for userspace compatibility */
35#ifndef __KERNEL__
1da177e4
LT
36/* Generic cache responses from hook functions.
37 <= 0x2000 is used for protocol-flags. */
38#define NFC_UNKNOWN 0x4000
39#define NFC_ALTERED 0x8000
6869c4d8 40#endif
1da177e4
LT
41
42#ifdef __KERNEL__
1da177e4
LT
43#ifdef CONFIG_NETFILTER
44
45extern void netfilter_init(void);
46
47/* Largest hook number + 1 */
48#define NF_MAX_HOOKS 8
49
50struct sk_buff;
51struct net_device;
52
53typedef unsigned int nf_hookfn(unsigned int hooknum,
54 struct sk_buff **skb,
55 const struct net_device *in,
56 const struct net_device *out,
57 int (*okfn)(struct sk_buff *));
58
59struct nf_hook_ops
60{
61 struct list_head list;
62
63 /* User fills in from here down. */
64 nf_hookfn *hook;
65 struct module *owner;
66 int pf;
67 int hooknum;
68 /* Hooks are ordered in ascending priority. */
69 int priority;
70};
71
72struct nf_sockopt_ops
73{
74 struct list_head list;
75
76 int pf;
77
78 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
79 int set_optmin;
80 int set_optmax;
81 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
3fdadf7d
DM
82 int (*compat_set)(struct sock *sk, int optval,
83 void __user *user, unsigned int len);
1da177e4
LT
84
85 int get_optmin;
86 int get_optmax;
87 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
3fdadf7d
DM
88 int (*compat_get)(struct sock *sk, int optval,
89 void __user *user, int *len);
1da177e4
LT
90
91 /* Number of users inside set() or get(). */
92 unsigned int use;
93 struct task_struct *cleanup_task;
94};
95
96/* Each queued (to userspace) skbuff has one of these. */
97struct nf_info
98{
99 /* The ops struct which sent us to userspace. */
100 struct nf_hook_ops *elem;
101
102 /* If we're sent to userspace, this keeps housekeeping info */
103 int pf;
104 unsigned int hook;
105 struct net_device *indev, *outdev;
106 int (*okfn)(struct sk_buff *);
107};
108
109/* Function to register/unregister hook points. */
110int nf_register_hook(struct nf_hook_ops *reg);
111void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
112int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
113void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
114
115/* Functions to register get/setsockopt ranges (non-inclusive). You
116 need to check permissions yourself! */
117int nf_register_sockopt(struct nf_sockopt_ops *reg);
118void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
119
d62f9ed4
PM
120#ifdef CONFIG_SYSCTL
121/* Sysctl registration */
122struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
123 struct ctl_table *table);
124void nf_unregister_sysctl_table(struct ctl_table_header *header,
125 struct ctl_table *table);
126extern struct ctl_table nf_net_netfilter_sysctl_path[];
127extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
128#endif /* CONFIG_SYSCTL */
129
1da177e4
LT
130extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
131
608c8e4f
HW
132/* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
133 * disappear once iptables is replaced with pkttables. Please DO NOT use them
134 * for any new code! */
135#define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
136#define NF_LOG_TCPOPT 0x02 /* Log TCP options */
137#define NF_LOG_IPOPT 0x04 /* Log IP options */
138#define NF_LOG_UID 0x08 /* Log UID owning local socket */
139#define NF_LOG_MASK 0x0f
140
141#define NF_LOG_TYPE_LOG 0x01
142#define NF_LOG_TYPE_ULOG 0x02
143
144struct nf_loginfo {
145 u_int8_t type;
146 union {
147 struct {
148 u_int32_t copy_len;
149 u_int16_t group;
150 u_int16_t qthreshold;
151 } ulog;
152 struct {
153 u_int8_t level;
154 u_int8_t logflags;
155 } log;
156 } u;
157};
158
159typedef void nf_logfn(unsigned int pf,
160 unsigned int hooknum,
1da177e4
LT
161 const struct sk_buff *skb,
162 const struct net_device *in,
163 const struct net_device *out,
608c8e4f 164 const struct nf_loginfo *li,
1da177e4
LT
165 const char *prefix);
166
608c8e4f
HW
167struct nf_logger {
168 struct module *me;
169 nf_logfn *logfn;
170 char *name;
171};
172
1da177e4 173/* Function to register/unregister log function. */
608c8e4f 174int nf_log_register(int pf, struct nf_logger *logger);
e92ad99c 175void nf_log_unregister(struct nf_logger *logger);
9dc6aa5f 176void nf_log_unregister_pf(int pf);
1da177e4
LT
177
178/* Calls the registered backend logging function */
179void nf_log_packet(int pf,
180 unsigned int hooknum,
181 const struct sk_buff *skb,
182 const struct net_device *in,
183 const struct net_device *out,
608c8e4f 184 struct nf_loginfo *li,
1da177e4 185 const char *fmt, ...);
16a6677f
PM
186
187int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
188 struct net_device *indev, struct net_device *outdev,
189 int (*okfn)(struct sk_buff *), int thresh);
190
191/**
192 * nf_hook_thresh - call a netfilter hook
193 *
194 * Returns 1 if the hook has allowed the packet to pass. The function
195 * okfn must be invoked by the caller in this case. Any other return
196 * value indicates the packet has been consumed by the hook.
197 */
198static inline int nf_hook_thresh(int pf, unsigned int hook,
199 struct sk_buff **pskb,
200 struct net_device *indev,
201 struct net_device *outdev,
48d5cad8
PM
202 int (*okfn)(struct sk_buff *), int thresh,
203 int cond)
16a6677f 204{
48d5cad8
PM
205 if (!cond)
206 return 1;
16a6677f
PM
207#ifndef CONFIG_NETFILTER_DEBUG
208 if (list_empty(&nf_hooks[pf][hook]))
209 return 1;
210#endif
211 return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
212}
213
214static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
215 struct net_device *indev, struct net_device *outdev,
216 int (*okfn)(struct sk_buff *))
217{
48d5cad8 218 return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
16a6677f 219}
1da177e4
LT
220
221/* Activate hook; either okfn or kfree_skb called, unless a hook
222 returns NF_STOLEN (in which case, it's up to the hook to deal with
223 the consequences).
224
225 Returns -ERRNO if packet dropped. Zero means queued, stolen or
226 accepted.
227*/
228
229/* RR:
230 > I don't want nf_hook to return anything because people might forget
231 > about async and trust the return value to mean "packet was ok".
232
233 AK:
234 Just document it clearly, then you can expect some sense from kernel
235 coders :)
236*/
237
238/* This is gross, but inline doesn't cut it for avoiding the function
239 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
16a6677f
PM
240
241/* HX: It's slightly less gross now. */
242
1da177e4
LT
243#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
244({int __ret; \
48d5cad8
PM
245if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
246 __ret = (okfn)(skb); \
247__ret;})
248
249#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
250({int __ret; \
251if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
1da177e4
LT
252 __ret = (okfn)(skb); \
253__ret;})
1da177e4 254
16a6677f
PM
255#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
256 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
1da177e4
LT
257
258/* Call setsockopt() */
259int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
260 int len);
261int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
262 int *len);
263
3fdadf7d
DM
264int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
265 char __user *opt, int len);
266int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
267 char __user *opt, int *len);
268
1da177e4 269/* Packet queuing */
bbd86b9f
HW
270struct nf_queue_handler {
271 int (*outfn)(struct sk_buff *skb, struct nf_info *info,
272 unsigned int queuenum, void *data);
273 void *data;
274 char *name;
275};
1da177e4 276extern int nf_register_queue_handler(int pf,
bbd86b9f 277 struct nf_queue_handler *qh);
1da177e4 278extern int nf_unregister_queue_handler(int pf);
bbd86b9f 279extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
1da177e4
LT
280extern void nf_reinject(struct sk_buff *skb,
281 struct nf_info *info,
282 unsigned int verdict);
283
1da177e4
LT
284/* FIXME: Before cache is ever used, this must be implemented for real. */
285extern void nf_invalidate_cache(int pf);
286
089af26c
HW
287/* Call this before modifying an existing packet: ensures it is
288 modifiable and linear to the point you care about (writable_len).
289 Returns true or false. */
290extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
291
43bc0ca7
AV
292static inline void nf_csum_replace4(__sum16 *sum, __be32 from, __be32 to)
293{
294 __be32 diff[] = { ~from, to };
295
296 *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), ~csum_unfold(*sum)));
297}
298
299static inline void nf_csum_replace2(__sum16 *sum, __be16 from, __be16 to)
300{
301 nf_csum_replace4(sum, (__force __be32)from, (__force __be32)to);
302}
303
304extern void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
305 __be32 from, __be32 to, int pseudohdr);
306
307static inline void nf_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
308 __be16 from, __be16 to, int pseudohdr)
309{
310 nf_proto_csum_replace4(sum, skb, (__force __be32)from,
311 (__force __be32)to, pseudohdr);
312}
4cf411de 313
bce8032e
PM
314struct nf_afinfo {
315 unsigned short family;
b51655b9 316 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 317 unsigned int dataoff, u_int8_t protocol);
bce8032e
PM
318 void (*saveroute)(const struct sk_buff *skb,
319 struct nf_info *info);
320 int (*reroute)(struct sk_buff **skb,
321 const struct nf_info *info);
322 int route_key_size;
2cc7d573
HW
323};
324
bce8032e
PM
325extern struct nf_afinfo *nf_afinfo[];
326static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
327{
328 return rcu_dereference(nf_afinfo[family]);
329}
2cc7d573 330
b51655b9 331static inline __sum16
422c346f
PM
332nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
333 u_int8_t protocol, unsigned short family)
334{
335 struct nf_afinfo *afinfo;
b51655b9 336 __sum16 csum = 0;
422c346f
PM
337
338 rcu_read_lock();
339 afinfo = nf_get_afinfo(family);
340 if (afinfo)
341 csum = afinfo->checksum(skb, hook, dataoff, protocol);
342 rcu_read_unlock();
343 return csum;
344}
345
bce8032e
PM
346extern int nf_register_afinfo(struct nf_afinfo *afinfo);
347extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
348
349#define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
2cc7d573 350
eb9c7ebe
PM
351#include <net/flow.h>
352extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
353
354static inline void
355nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
356{
5b1158e9 357#if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
eb9c7ebe
PM
358 void (*decodefn)(struct sk_buff *, struct flowi *);
359
360 if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
361 decodefn(skb, fl);
362#endif
363}
364
608c8e4f
HW
365#ifdef CONFIG_PROC_FS
366#include <linux/proc_fs.h>
367extern struct proc_dir_entry *proc_net_netfilter;
368#endif
369
1da177e4
LT
370#else /* !CONFIG_NETFILTER */
371#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
48d5cad8 372#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
f53b61d8
DM
373static inline int nf_hook_thresh(int pf, unsigned int hook,
374 struct sk_buff **pskb,
375 struct net_device *indev,
376 struct net_device *outdev,
48d5cad8
PM
377 int (*okfn)(struct sk_buff *), int thresh,
378 int cond)
f53b61d8
DM
379{
380 return okfn(*pskb);
381}
382static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
383 struct net_device *indev, struct net_device *outdev,
384 int (*okfn)(struct sk_buff *))
385{
9c92d348 386 return 1;
f53b61d8 387}
f53b61d8 388struct flowi;
eb9c7ebe
PM
389static inline void
390nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
1da177e4
LT
391#endif /*CONFIG_NETFILTER*/
392
5f79e0f9
YK
393#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
394extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
395extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
396#else
397static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
398#endif
399
1da177e4
LT
400#endif /*__KERNEL__*/
401#endif /*__LINUX_NETFILTER_H*/