]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/netfilter.h
checkpatch: toughen trailing if statement checks and extend them to while and for
[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>
1da177e4
LT
6#include <linux/skbuff.h>
7#include <linux/net.h>
666953df 8#include <linux/netdevice.h>
1da177e4 9#include <linux/if.h>
2e3075a2
JE
10#include <linux/in.h>
11#include <linux/in6.h>
1da177e4
LT
12#include <linux/wait.h>
13#include <linux/list.h>
666953df 14#include <net/net_namespace.h>
1da177e4 15#endif
c8942f1f 16#include <linux/types.h>
1da177e4
LT
17#include <linux/compiler.h>
18
19/* Responses from hook functions. */
20#define NF_DROP 0
21#define NF_ACCEPT 1
22#define NF_STOLEN 2
23#define NF_QUEUE 3
24#define NF_REPEAT 4
25#define NF_STOP 5
26#define NF_MAX_VERDICT NF_STOP
27
0ab43f84
HW
28/* we overload the higher bits for encoding auxiliary data such as the queue
29 * number. Not nice, but better than additional function arguments. */
30#define NF_VERDICT_MASK 0x0000ffff
31#define NF_VERDICT_BITS 16
32
33#define NF_VERDICT_QMASK 0xffff0000
34#define NF_VERDICT_QBITS 16
35
fbabbed8 36#define NF_QUEUE_NR(x) ((((x) << NF_VERDICT_BITS) & NF_VERDICT_QMASK) | NF_QUEUE)
0ab43f84 37
6869c4d8
HW
38/* only for userspace compatibility */
39#ifndef __KERNEL__
1da177e4
LT
40/* Generic cache responses from hook functions.
41 <= 0x2000 is used for protocol-flags. */
42#define NFC_UNKNOWN 0x4000
43#define NFC_ALTERED 0x8000
6869c4d8 44#endif
1da177e4 45
6e23ae2a
PM
46enum nf_inet_hooks {
47 NF_INET_PRE_ROUTING,
48 NF_INET_LOCAL_IN,
49 NF_INET_FORWARD,
50 NF_INET_LOCAL_OUT,
51 NF_INET_POST_ROUTING,
52 NF_INET_NUMHOOKS
53};
54
643a2c15 55union nf_inet_addr {
7b33ed22 56 __u32 all[4];
643a2c15
JE
57 __be32 ip;
58 __be32 ip6[4];
2e3075a2
JE
59 struct in_addr in;
60 struct in6_addr in6;
643a2c15
JE
61};
62
1da177e4 63#ifdef __KERNEL__
1da177e4
LT
64#ifdef CONFIG_NETFILTER
65
b8beedd2
PM
66static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
67 const union nf_inet_addr *a2)
68{
69 return a1->all[0] == a2->all[0] &&
70 a1->all[1] == a2->all[1] &&
71 a1->all[2] == a2->all[2] &&
72 a1->all[3] == a2->all[3];
73}
74
1da177e4
LT
75extern void netfilter_init(void);
76
77/* Largest hook number + 1 */
78#define NF_MAX_HOOKS 8
79
80struct sk_buff;
1da177e4
LT
81
82typedef unsigned int nf_hookfn(unsigned int hooknum,
3db05fea 83 struct sk_buff *skb,
1da177e4
LT
84 const struct net_device *in,
85 const struct net_device *out,
86 int (*okfn)(struct sk_buff *));
87
88struct nf_hook_ops
89{
90 struct list_head list;
91
92 /* User fills in from here down. */
93 nf_hookfn *hook;
94 struct module *owner;
95 int pf;
96 int hooknum;
97 /* Hooks are ordered in ascending priority. */
98 int priority;
99};
100
101struct nf_sockopt_ops
102{
103 struct list_head list;
104
105 int pf;
106
107 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
108 int set_optmin;
109 int set_optmax;
110 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
3fdadf7d
DM
111 int (*compat_set)(struct sock *sk, int optval,
112 void __user *user, unsigned int len);
1da177e4
LT
113
114 int get_optmin;
115 int get_optmax;
116 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
3fdadf7d
DM
117 int (*compat_get)(struct sock *sk, int optval,
118 void __user *user, int *len);
1da177e4 119
16fcec35
NH
120 /* Use the module struct to lock set/get code in place */
121 struct module *owner;
1da177e4
LT
122};
123
1da177e4
LT
124/* Function to register/unregister hook points. */
125int nf_register_hook(struct nf_hook_ops *reg);
126void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
127int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
128void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
129
130/* Functions to register get/setsockopt ranges (non-inclusive). You
131 need to check permissions yourself! */
132int nf_register_sockopt(struct nf_sockopt_ops *reg);
133void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
134
d62f9ed4
PM
135#ifdef CONFIG_SYSCTL
136/* Sysctl registration */
b3fd3ffe
PE
137extern struct ctl_path nf_net_netfilter_sysctl_path[];
138extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
d62f9ed4
PM
139#endif /* CONFIG_SYSCTL */
140
1da177e4
LT
141extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
142
3db05fea 143int nf_hook_slow(int pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
144 struct net_device *indev, struct net_device *outdev,
145 int (*okfn)(struct sk_buff *), int thresh);
146
147/**
148 * nf_hook_thresh - call a netfilter hook
149 *
150 * Returns 1 if the hook has allowed the packet to pass. The function
151 * okfn must be invoked by the caller in this case. Any other return
152 * value indicates the packet has been consumed by the hook.
153 */
154static inline int nf_hook_thresh(int pf, unsigned int hook,
3db05fea 155 struct sk_buff *skb,
16a6677f
PM
156 struct net_device *indev,
157 struct net_device *outdev,
48d5cad8
PM
158 int (*okfn)(struct sk_buff *), int thresh,
159 int cond)
16a6677f 160{
48d5cad8
PM
161 if (!cond)
162 return 1;
16a6677f
PM
163#ifndef CONFIG_NETFILTER_DEBUG
164 if (list_empty(&nf_hooks[pf][hook]))
165 return 1;
166#endif
3db05fea 167 return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
16a6677f
PM
168}
169
3db05fea 170static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
16a6677f
PM
171 struct net_device *indev, struct net_device *outdev,
172 int (*okfn)(struct sk_buff *))
173{
3db05fea 174 return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN, 1);
16a6677f 175}
1da177e4
LT
176
177/* Activate hook; either okfn or kfree_skb called, unless a hook
178 returns NF_STOLEN (in which case, it's up to the hook to deal with
179 the consequences).
180
181 Returns -ERRNO if packet dropped. Zero means queued, stolen or
182 accepted.
183*/
184
185/* RR:
186 > I don't want nf_hook to return anything because people might forget
187 > about async and trust the return value to mean "packet was ok".
188
189 AK:
190 Just document it clearly, then you can expect some sense from kernel
191 coders :)
192*/
193
194/* This is gross, but inline doesn't cut it for avoiding the function
195 call in fast path: gcc doesn't inline (needs value tracking?). --RR */
16a6677f
PM
196
197/* HX: It's slightly less gross now. */
198
1da177e4
LT
199#define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
200({int __ret; \
3db05fea 201if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
48d5cad8
PM
202 __ret = (okfn)(skb); \
203__ret;})
204
205#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
206({int __ret; \
3db05fea 207if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
1da177e4
LT
208 __ret = (okfn)(skb); \
209__ret;})
1da177e4 210
16a6677f
PM
211#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
212 NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
1da177e4
LT
213
214/* Call setsockopt() */
215int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
216 int len);
217int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
218 int *len);
219
3fdadf7d
DM
220int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
221 char __user *opt, int len);
222int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
223 char __user *opt, int *len);
224
089af26c
HW
225/* Call this before modifying an existing packet: ensures it is
226 modifiable and linear to the point you care about (writable_len).
227 Returns true or false. */
37d41879 228extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 229
1841a4c7 230struct flowi;
02f014d8 231struct nf_queue_entry;
c01cd429 232
bce8032e
PM
233struct nf_afinfo {
234 unsigned short family;
b51655b9 235 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 236 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
237 __sum16 (*checksum_partial)(struct sk_buff *skb,
238 unsigned int hook,
239 unsigned int dataoff,
240 unsigned int len,
241 u_int8_t protocol);
1841a4c7 242 int (*route)(struct dst_entry **dst, struct flowi *fl);
bce8032e 243 void (*saveroute)(const struct sk_buff *skb,
02f014d8 244 struct nf_queue_entry *entry);
3db05fea 245 int (*reroute)(struct sk_buff *skb,
02f014d8 246 const struct nf_queue_entry *entry);
bce8032e 247 int route_key_size;
2cc7d573
HW
248};
249
1e796fda
PM
250extern const struct nf_afinfo *nf_afinfo[NPROTO];
251static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
252{
253 return rcu_dereference(nf_afinfo[family]);
254}
2cc7d573 255
b51655b9 256static inline __sum16
422c346f
PM
257nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
258 u_int8_t protocol, unsigned short family)
259{
1e796fda 260 const struct nf_afinfo *afinfo;
b51655b9 261 __sum16 csum = 0;
422c346f
PM
262
263 rcu_read_lock();
264 afinfo = nf_get_afinfo(family);
265 if (afinfo)
266 csum = afinfo->checksum(skb, hook, dataoff, protocol);
267 rcu_read_unlock();
268 return csum;
269}
270
d63a6507
PM
271static inline __sum16
272nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
273 unsigned int dataoff, unsigned int len,
274 u_int8_t protocol, unsigned short family)
275{
276 const struct nf_afinfo *afinfo;
277 __sum16 csum = 0;
278
279 rcu_read_lock();
280 afinfo = nf_get_afinfo(family);
281 if (afinfo)
282 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
283 protocol);
284 rcu_read_unlock();
285 return csum;
286}
287
1e796fda
PM
288extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
289extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 290
eb9c7ebe
PM
291#include <net/flow.h>
292extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
293
294static inline void
295nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
296{
051578cc 297#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
298 void (*decodefn)(struct sk_buff *, struct flowi *);
299
051578cc
PM
300 if (family == AF_INET) {
301 rcu_read_lock();
302 decodefn = rcu_dereference(ip_nat_decode_session);
303 if (decodefn)
304 decodefn(skb, fl);
305 rcu_read_unlock();
306 }
eb9c7ebe
PM
307#endif
308}
309
608c8e4f
HW
310#ifdef CONFIG_PROC_FS
311#include <linux/proc_fs.h>
312extern struct proc_dir_entry *proc_net_netfilter;
313#endif
314
1da177e4
LT
315#else /* !CONFIG_NETFILTER */
316#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
48d5cad8 317#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
f53b61d8 318static inline int nf_hook_thresh(int pf, unsigned int hook,
3db05fea 319 struct sk_buff *skb,
f53b61d8
DM
320 struct net_device *indev,
321 struct net_device *outdev,
48d5cad8
PM
322 int (*okfn)(struct sk_buff *), int thresh,
323 int cond)
f53b61d8 324{
3db05fea 325 return okfn(skb);
f53b61d8 326}
3db05fea 327static inline int nf_hook(int pf, unsigned int hook, struct sk_buff *skb,
f53b61d8
DM
328 struct net_device *indev, struct net_device *outdev,
329 int (*okfn)(struct sk_buff *))
330{
9c92d348 331 return 1;
f53b61d8 332}
f53b61d8 333struct flowi;
eb9c7ebe
PM
334static inline void
335nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
1da177e4
LT
336#endif /*CONFIG_NETFILTER*/
337
5f79e0f9
YK
338#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
339extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
340extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
de6e05c4 341extern void (*nf_ct_destroy)(struct nf_conntrack *);
5f79e0f9
YK
342#else
343static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
344#endif
345
666953df
AD
346static inline struct net *nf_pre_routing_net(const struct net_device *in,
347 const struct net_device *out)
348{
349#ifdef CONFIG_NET_NS
350 return in->nd_net;
351#else
352 return &init_net;
353#endif
354}
355
356static inline struct net *nf_local_in_net(const struct net_device *in,
357 const struct net_device *out)
358{
359#ifdef CONFIG_NET_NS
360 return in->nd_net;
361#else
362 return &init_net;
363#endif
364}
365
366static inline struct net *nf_forward_net(const struct net_device *in,
367 const struct net_device *out)
368{
369#ifdef CONFIG_NET_NS
370 BUG_ON(in->nd_net != out->nd_net);
371 return in->nd_net;
372#else
373 return &init_net;
374#endif
375}
376
377static inline struct net *nf_local_out_net(const struct net_device *in,
378 const struct net_device *out)
379{
380#ifdef CONFIG_NET_NS
381 return out->nd_net;
382#else
383 return &init_net;
384#endif
385}
386
387static inline struct net *nf_post_routing_net(const struct net_device *in,
388 const struct net_device *out)
389{
390#ifdef CONFIG_NET_NS
391 return out->nd_net;
392#else
393 return &init_net;
394#endif
395}
396
1da177e4
LT
397#endif /*__KERNEL__*/
398#endif /*__LINUX_NETFILTER_H*/