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