Merge tag 'xfs-for-linus-4.2-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / include / linux / netfilter.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
1da177e4 4#include <linux/init.h>
1da177e4
LT
5#include <linux/skbuff.h>
6#include <linux/net.h>
7#include <linux/if.h>
2e3075a2
JE
8#include <linux/in.h>
9#include <linux/in6.h>
1da177e4
LT
10#include <linux/wait.h>
11#include <linux/list.h>
d1c85c2e 12#include <linux/static_key.h>
a263653e
PNA
13#include <linux/netfilter_defs.h>
14
1da177e4 15#ifdef CONFIG_NETFILTER
f615df76
FW
16static inline int NF_DROP_GETERR(int verdict)
17{
18 return -(verdict >> NF_VERDICT_QBITS);
19}
1da177e4 20
b8beedd2
PM
21static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
22 const union nf_inet_addr *a2)
23{
24 return a1->all[0] == a2->all[0] &&
25 a1->all[1] == a2->all[1] &&
26 a1->all[2] == a2->all[2] &&
27 a1->all[3] == a2->all[3];
28}
29
efdedd54
DF
30static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
31 union nf_inet_addr *result,
32 const union nf_inet_addr *mask)
33{
34 result->all[0] = a1->all[0] & mask->all[0];
35 result->all[1] = a1->all[1] & mask->all[1];
36 result->all[2] = a1->all[2] & mask->all[2];
37 result->all[3] = a1->all[3] & mask->all[3];
38}
39
a0f4ecf3 40int netfilter_init(void);
1da177e4 41
1da177e4 42struct sk_buff;
1da177e4 43
795aa6ef 44struct nf_hook_ops;
cfdfab31 45
1c984f8a
DM
46struct sock;
47
cfdfab31
DM
48struct nf_hook_state {
49 unsigned int hook;
50 int thresh;
51 u_int8_t pf;
52 struct net_device *in;
53 struct net_device *out;
1c984f8a 54 struct sock *sk;
f7191483 55 struct list_head *hook_list;
7026b1dd 56 int (*okfn)(struct sock *, struct sk_buff *);
cfdfab31
DM
57};
58
107a9f4d 59static inline void nf_hook_state_init(struct nf_hook_state *p,
f7191483 60 struct list_head *hook_list,
107a9f4d
DM
61 unsigned int hook,
62 int thresh, u_int8_t pf,
63 struct net_device *indev,
64 struct net_device *outdev,
1c984f8a 65 struct sock *sk,
7026b1dd 66 int (*okfn)(struct sock *, struct sk_buff *))
107a9f4d
DM
67{
68 p->hook = hook;
69 p->thresh = thresh;
70 p->pf = pf;
71 p->in = indev;
72 p->out = outdev;
1c984f8a 73 p->sk = sk;
f7191483 74 p->hook_list = hook_list;
107a9f4d
DM
75 p->okfn = okfn;
76}
77
795aa6ef 78typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
3db05fea 79 struct sk_buff *skb,
238e54c9 80 const struct nf_hook_state *state);
1da177e4 81
d94d9fee 82struct nf_hook_ops {
87d5c18c 83 struct list_head list;
1da177e4
LT
84
85 /* User fills in from here down. */
87d5c18c 86 nf_hookfn *hook;
e687ad60 87 struct net_device *dev;
87d5c18c
PN
88 struct module *owner;
89 void *priv;
90 u_int8_t pf;
91 unsigned int hooknum;
1da177e4 92 /* Hooks are ordered in ascending priority. */
87d5c18c 93 int priority;
1da177e4
LT
94};
95
d94d9fee 96struct nf_sockopt_ops {
1da177e4
LT
97 struct list_head list;
98
76108cea 99 u_int8_t pf;
1da177e4
LT
100
101 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
102 int set_optmin;
103 int set_optmax;
104 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 105#ifdef CONFIG_COMPAT
3fdadf7d
DM
106 int (*compat_set)(struct sock *sk, int optval,
107 void __user *user, unsigned int len);
c30f540b 108#endif
1da177e4
LT
109 int get_optmin;
110 int get_optmax;
111 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 112#ifdef CONFIG_COMPAT
3fdadf7d
DM
113 int (*compat_get)(struct sock *sk, int optval,
114 void __user *user, int *len);
c30f540b 115#endif
16fcec35
NH
116 /* Use the module struct to lock set/get code in place */
117 struct module *owner;
1da177e4
LT
118};
119
1da177e4
LT
120/* Function to register/unregister hook points. */
121int nf_register_hook(struct nf_hook_ops *reg);
122void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
123int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
124void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
125
126/* Functions to register get/setsockopt ranges (non-inclusive). You
127 need to check permissions yourself! */
128int nf_register_sockopt(struct nf_sockopt_ops *reg);
129void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
130
7e9c6eeb 131extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
1da177e4 132
d1c85c2e 133#ifdef HAVE_JUMP_LABEL
c5905afb 134extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
d1c85c2e 135
b8d0aad0
PN
136static inline bool nf_hook_list_active(struct list_head *nf_hook_list,
137 u_int8_t pf, unsigned int hook)
a2d7ec58
ED
138{
139 if (__builtin_constant_p(pf) &&
140 __builtin_constant_p(hook))
c5905afb 141 return static_key_false(&nf_hooks_needed[pf][hook]);
a2d7ec58 142
b8d0aad0 143 return !list_empty(nf_hook_list);
a2d7ec58
ED
144}
145#else
b8d0aad0
PN
146static inline bool nf_hook_list_active(struct list_head *nf_hook_list,
147 u_int8_t pf, unsigned int hook)
a2d7ec58 148{
b8d0aad0 149 return !list_empty(nf_hook_list);
a2d7ec58
ED
150}
151#endif
152
b8d0aad0
PN
153static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
154{
155 return nf_hook_list_active(&nf_hooks[pf][hook], pf, hook);
156}
157
cfdfab31 158int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
16a6677f
PM
159
160/**
161 * nf_hook_thresh - call a netfilter hook
b8d0aad0 162 *
16a6677f
PM
163 * Returns 1 if the hook has allowed the packet to pass. The function
164 * okfn must be invoked by the caller in this case. Any other return
165 * value indicates the packet has been consumed by the hook.
166 */
76108cea 167static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
7026b1dd 168 struct sock *sk,
3db05fea 169 struct sk_buff *skb,
16a6677f
PM
170 struct net_device *indev,
171 struct net_device *outdev,
7026b1dd
DM
172 int (*okfn)(struct sock *, struct sk_buff *),
173 int thresh)
16a6677f 174{
cfdfab31 175 if (nf_hooks_active(pf, hook)) {
107a9f4d 176 struct nf_hook_state state;
cfdfab31 177
f7191483
PN
178 nf_hook_state_init(&state, &nf_hooks[pf][hook], hook, thresh,
179 pf, indev, outdev, sk, okfn);
cfdfab31
DM
180 return nf_hook_slow(skb, &state);
181 }
a2d7ec58 182 return 1;
16a6677f
PM
183}
184
7026b1dd
DM
185static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
186 struct sk_buff *skb, struct net_device *indev,
187 struct net_device *outdev,
188 int (*okfn)(struct sock *, struct sk_buff *))
16a6677f 189{
7026b1dd 190 return nf_hook_thresh(pf, hook, sk, skb, indev, outdev, okfn, INT_MIN);
16a6677f 191}
1da177e4
LT
192
193/* Activate hook; either okfn or kfree_skb called, unless a hook
194 returns NF_STOLEN (in which case, it's up to the hook to deal with
195 the consequences).
196
197 Returns -ERRNO if packet dropped. Zero means queued, stolen or
198 accepted.
199*/
200
201/* RR:
202 > I don't want nf_hook to return anything because people might forget
203 > about async and trust the return value to mean "packet was ok".
204
205 AK:
206 Just document it clearly, then you can expect some sense from kernel
207 coders :)
208*/
209
2249065f 210static inline int
7026b1dd
DM
211NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sock *sk,
212 struct sk_buff *skb, struct net_device *in,
213 struct net_device *out,
214 int (*okfn)(struct sock *, struct sk_buff *), int thresh)
2249065f 215{
7026b1dd 216 int ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, thresh);
2249065f 217 if (ret == 1)
7026b1dd 218 ret = okfn(sk, skb);
2249065f
JE
219 return ret;
220}
48d5cad8 221
2249065f 222static inline int
7026b1dd
DM
223NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sock *sk,
224 struct sk_buff *skb, struct net_device *in, struct net_device *out,
225 int (*okfn)(struct sock *, struct sk_buff *), bool cond)
2249065f 226{
4bac6b18
PM
227 int ret;
228
229 if (!cond ||
7026b1dd
DM
230 ((ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, INT_MIN)) == 1))
231 ret = okfn(sk, skb);
2249065f
JE
232 return ret;
233}
1da177e4 234
2249065f 235static inline int
7026b1dd 236NF_HOOK(uint8_t pf, unsigned int hook, struct sock *sk, struct sk_buff *skb,
2249065f 237 struct net_device *in, struct net_device *out,
7026b1dd 238 int (*okfn)(struct sock *, struct sk_buff *))
2249065f 239{
7026b1dd 240 return NF_HOOK_THRESH(pf, hook, sk, skb, in, out, okfn, INT_MIN);
2249065f 241}
1da177e4
LT
242
243/* Call setsockopt() */
76108cea 244int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 245 unsigned int len);
76108cea 246int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 247 int *len);
c30f540b 248#ifdef CONFIG_COMPAT
76108cea 249int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 250 char __user *opt, unsigned int len);
76108cea 251int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 252 char __user *opt, int *len);
c30f540b 253#endif
3fdadf7d 254
089af26c
HW
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. */
a0f4ecf3 258int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 259
1841a4c7 260struct flowi;
02f014d8 261struct nf_queue_entry;
c01cd429 262
bce8032e
PM
263struct nf_afinfo {
264 unsigned short family;
b51655b9 265 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 266 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
267 __sum16 (*checksum_partial)(struct sk_buff *skb,
268 unsigned int hook,
269 unsigned int dataoff,
270 unsigned int len,
271 u_int8_t protocol);
31ad3dd6 272 int (*route)(struct net *net, struct dst_entry **dst,
0fae2e77 273 struct flowi *fl, bool strict);
bce8032e 274 void (*saveroute)(const struct sk_buff *skb,
02f014d8 275 struct nf_queue_entry *entry);
3db05fea 276 int (*reroute)(struct sk_buff *skb,
02f014d8 277 const struct nf_queue_entry *entry);
bce8032e 278 int route_key_size;
2cc7d573
HW
279};
280
0e60ebe0 281extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 282static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
283{
284 return rcu_dereference(nf_afinfo[family]);
285}
2cc7d573 286
b51655b9 287static inline __sum16
422c346f
PM
288nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
289 u_int8_t protocol, unsigned short family)
290{
1e796fda 291 const struct nf_afinfo *afinfo;
b51655b9 292 __sum16 csum = 0;
422c346f
PM
293
294 rcu_read_lock();
295 afinfo = nf_get_afinfo(family);
296 if (afinfo)
297 csum = afinfo->checksum(skb, hook, dataoff, protocol);
298 rcu_read_unlock();
299 return csum;
300}
301
d63a6507
PM
302static inline __sum16
303nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
304 unsigned int dataoff, unsigned int len,
305 u_int8_t protocol, unsigned short family)
306{
307 const struct nf_afinfo *afinfo;
308 __sum16 csum = 0;
309
310 rcu_read_lock();
311 afinfo = nf_get_afinfo(family);
312 if (afinfo)
313 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
314 protocol);
315 rcu_read_unlock();
316 return csum;
317}
318
a0f4ecf3
JP
319int nf_register_afinfo(const struct nf_afinfo *afinfo);
320void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 321
eb9c7ebe 322#include <net/flow.h>
c7232c99 323extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
eb9c7ebe
PM
324
325static inline void
76108cea 326nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 327{
051578cc 328#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
329 void (*decodefn)(struct sk_buff *, struct flowi *);
330
c7232c99
PM
331 rcu_read_lock();
332 decodefn = rcu_dereference(nf_nat_decode_session_hook);
333 if (decodefn)
334 decodefn(skb, fl);
335 rcu_read_unlock();
eb9c7ebe
PM
336#endif
337}
338
1da177e4 339#else /* !CONFIG_NETFILTER */
7026b1dd
DM
340#define NF_HOOK(pf, hook, sk, skb, indev, outdev, okfn) (okfn)(sk, skb)
341#define NF_HOOK_COND(pf, hook, sk, skb, indev, outdev, okfn, cond) (okfn)(sk, skb)
76108cea 342static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
7026b1dd 343 struct sock *sk,
3db05fea 344 struct sk_buff *skb,
f53b61d8
DM
345 struct net_device *indev,
346 struct net_device *outdev,
7026b1dd 347 int (*okfn)(struct sock *sk, struct sk_buff *), int thresh)
f53b61d8 348{
7026b1dd 349 return okfn(sk, skb);
f53b61d8 350}
7026b1dd
DM
351static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
352 struct sk_buff *skb, struct net_device *indev,
353 struct net_device *outdev,
354 int (*okfn)(struct sock *, struct sk_buff *))
f53b61d8 355{
9c92d348 356 return 1;
f53b61d8 357}
f53b61d8 358struct flowi;
eb9c7ebe 359static inline void
76108cea
JE
360nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
361{
362}
1da177e4
LT
363#endif /*CONFIG_NETFILTER*/
364
5f79e0f9 365#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
312a0c16 366extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 367void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
0e60ebe0 368extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
9cb01766
PNA
369
370struct nf_conn;
41d73ec0 371enum ip_conntrack_info;
9cb01766
PNA
372struct nlattr;
373
374struct nfq_ct_hook {
375 size_t (*build_size)(const struct nf_conn *ct);
376 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
377 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
378 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
379 u32 portid, u32 report);
8c88f87c 380 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 381 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 382};
41d73ec0 383extern struct nfq_ct_hook __rcu *nfq_ct_hook;
5f79e0f9
YK
384#else
385static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
386#endif
387
1da177e4 388#endif /*__LINUX_NETFILTER_H*/