]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/netfilter.h
PM / OPP: Support updating performance state of device's power domain
[mirror_ubuntu-bionic-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>
d1c85c2e 12#include <linux/static_key.h>
a263653e 13#include <linux/netfilter_defs.h>
085db2c0
EB
14#include <linux/netdevice.h>
15#include <net/net_namespace.h>
a263653e 16
1da177e4 17#ifdef CONFIG_NETFILTER
f615df76
FW
18static inline int NF_DROP_GETERR(int verdict)
19{
20 return -(verdict >> NF_VERDICT_QBITS);
21}
1da177e4 22
b8beedd2
PM
23static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
24 const union nf_inet_addr *a2)
25{
26 return a1->all[0] == a2->all[0] &&
27 a1->all[1] == a2->all[1] &&
28 a1->all[2] == a2->all[2] &&
29 a1->all[3] == a2->all[3];
30}
31
efdedd54
DF
32static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
33 union nf_inet_addr *result,
34 const union nf_inet_addr *mask)
35{
36 result->all[0] = a1->all[0] & mask->all[0];
37 result->all[1] = a1->all[1] & mask->all[1];
38 result->all[2] = a1->all[2] & mask->all[2];
39 result->all[3] = a1->all[3] & mask->all[3];
40}
41
a0f4ecf3 42int netfilter_init(void);
1da177e4 43
1da177e4 44struct sk_buff;
1da177e4 45
795aa6ef 46struct nf_hook_ops;
cfdfab31 47
1c984f8a
DM
48struct sock;
49
cfdfab31
DM
50struct nf_hook_state {
51 unsigned int hook;
cfdfab31
DM
52 u_int8_t pf;
53 struct net_device *in;
54 struct net_device *out;
1c984f8a 55 struct sock *sk;
b11b1f65 56 struct net *net;
0c4b51f0 57 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
cfdfab31
DM
58};
59
e3b37f11
AC
60typedef unsigned int nf_hookfn(void *priv,
61 struct sk_buff *skb,
62 const struct nf_hook_state *state);
63struct nf_hook_ops {
e3b37f11
AC
64 /* User fills in from here down. */
65 nf_hookfn *hook;
66 struct net_device *dev;
67 void *priv;
68 u_int8_t pf;
69 unsigned int hooknum;
70 /* Hooks are ordered in ascending priority. */
71 int priority;
72};
73
74struct nf_hook_entry {
d415b9eb
AC
75 nf_hookfn *hook;
76 void *priv;
e3b37f11
AC
77};
78
960632ec
AC
79struct nf_hook_entries {
80 u16 num_hook_entries;
81 /* padding */
82 struct nf_hook_entry hooks[];
83
84 /* trailer: pointers to original orig_ops of each hook.
85 *
86 * This is not part of struct nf_hook_entry since its only
87 * needed in slow path (hook register/unregister).
88 *
89 * const struct nf_hook_ops *orig_ops[]
90 */
91};
0aa8c57a 92
960632ec 93static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
0aa8c57a 94{
960632ec
AC
95 unsigned int n = e->num_hook_entries;
96 const void *hook_end;
97
98 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
99
100 return (struct nf_hook_ops **)hook_end;
0aa8c57a
AC
101}
102
103static inline int
104nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
105 struct nf_hook_state *state)
106{
d415b9eb 107 return entry->hook(entry->priv, skb, state);
0aa8c57a
AC
108}
109
107a9f4d
DM
110static inline void nf_hook_state_init(struct nf_hook_state *p,
111 unsigned int hook,
1610a73c 112 u_int8_t pf,
107a9f4d
DM
113 struct net_device *indev,
114 struct net_device *outdev,
1c984f8a 115 struct sock *sk,
b11b1f65 116 struct net *net,
0c4b51f0 117 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
107a9f4d
DM
118{
119 p->hook = hook;
107a9f4d
DM
120 p->pf = pf;
121 p->in = indev;
122 p->out = outdev;
1c984f8a 123 p->sk = sk;
b11b1f65 124 p->net = net;
107a9f4d
DM
125 p->okfn = okfn;
126}
127
1da177e4 128
1da177e4 129
d94d9fee 130struct nf_sockopt_ops {
1da177e4
LT
131 struct list_head list;
132
76108cea 133 u_int8_t pf;
1da177e4
LT
134
135 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
136 int set_optmin;
137 int set_optmax;
138 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 139#ifdef CONFIG_COMPAT
3fdadf7d
DM
140 int (*compat_set)(struct sock *sk, int optval,
141 void __user *user, unsigned int len);
c30f540b 142#endif
1da177e4
LT
143 int get_optmin;
144 int get_optmax;
145 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 146#ifdef CONFIG_COMPAT
3fdadf7d
DM
147 int (*compat_get)(struct sock *sk, int optval,
148 void __user *user, int *len);
c30f540b 149#endif
16fcec35
NH
150 /* Use the module struct to lock set/get code in place */
151 struct module *owner;
1da177e4
LT
152};
153
1da177e4 154/* Function to register/unregister hook points. */
085db2c0
EB
155int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
156void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
157int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
158 unsigned int n);
159void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
160 unsigned int n);
161
1da177e4
LT
162/* Functions to register get/setsockopt ranges (non-inclusive). You
163 need to check permissions yourself! */
164int nf_register_sockopt(struct nf_sockopt_ops *reg);
165void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
166
d1c85c2e 167#ifdef HAVE_JUMP_LABEL
c5905afb 168extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58
ED
169#endif
170
01886bd9 171int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
960632ec 172 const struct nf_hook_entries *e, unsigned int i);
16a6677f
PM
173
174/**
1610a73c 175 * nf_hook - call a netfilter hook
b8d0aad0 176 *
16a6677f
PM
177 * Returns 1 if the hook has allowed the packet to pass. The function
178 * okfn must be invoked by the caller in this case. Any other return
179 * value indicates the packet has been consumed by the hook.
180 */
1610a73c
PNA
181static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
182 struct sock *sk, struct sk_buff *skb,
183 struct net_device *indev, struct net_device *outdev,
184 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
16a6677f 185{
960632ec 186 struct nf_hook_entries *hook_head;
e3b37f11 187 int ret = 1;
af4610c3
FW
188
189#ifdef HAVE_JUMP_LABEL
190 if (__builtin_constant_p(pf) &&
191 __builtin_constant_p(hook) &&
192 !static_key_false(&nf_hooks_needed[pf][hook]))
193 return 1;
194#endif
195
e3b37f11
AC
196 rcu_read_lock();
197 hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
198 if (hook_head) {
107a9f4d 199 struct nf_hook_state state;
cfdfab31 200
01886bd9 201 nf_hook_state_init(&state, hook, pf, indev, outdev,
1610a73c 202 sk, net, okfn);
fe72926b 203
960632ec 204 ret = nf_hook_slow(skb, &state, hook_head, 0);
cfdfab31 205 }
e3b37f11
AC
206 rcu_read_unlock();
207
208 return ret;
16a6677f
PM
209}
210
1da177e4
LT
211/* Activate hook; either okfn or kfree_skb called, unless a hook
212 returns NF_STOLEN (in which case, it's up to the hook to deal with
213 the consequences).
214
215 Returns -ERRNO if packet dropped. Zero means queued, stolen or
216 accepted.
217*/
218
219/* RR:
220 > I don't want nf_hook to return anything because people might forget
221 > about async and trust the return value to mean "packet was ok".
222
223 AK:
224 Just document it clearly, then you can expect some sense from kernel
225 coders :)
226*/
227
2249065f 228static inline int
29a26a56 229NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd 230 struct sk_buff *skb, struct net_device *in, struct net_device *out,
0c4b51f0
EB
231 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
232 bool cond)
2249065f 233{
4bac6b18
PM
234 int ret;
235
236 if (!cond ||
1610a73c 237 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
0c4b51f0 238 ret = okfn(net, sk, skb);
2249065f
JE
239 return ret;
240}
1da177e4 241
2249065f 242static inline int
29a26a56 243NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
2249065f 244 struct net_device *in, struct net_device *out,
0c4b51f0 245 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
2249065f 246{
1610a73c
PNA
247 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
248 if (ret == 1)
249 ret = okfn(net, sk, skb);
250 return ret;
2249065f 251}
1da177e4
LT
252
253/* Call setsockopt() */
76108cea 254int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 255 unsigned int len);
76108cea 256int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 257 int *len);
c30f540b 258#ifdef CONFIG_COMPAT
76108cea 259int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 260 char __user *opt, unsigned int len);
76108cea 261int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 262 char __user *opt, int *len);
c30f540b 263#endif
3fdadf7d 264
089af26c
HW
265/* Call this before modifying an existing packet: ensures it is
266 modifiable and linear to the point you care about (writable_len).
267 Returns true or false. */
a0f4ecf3 268int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 269
1841a4c7 270struct flowi;
02f014d8 271struct nf_queue_entry;
c01cd429 272
bce8032e
PM
273struct nf_afinfo {
274 unsigned short family;
b51655b9 275 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 276 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
277 __sum16 (*checksum_partial)(struct sk_buff *skb,
278 unsigned int hook,
279 unsigned int dataoff,
280 unsigned int len,
281 u_int8_t protocol);
31ad3dd6 282 int (*route)(struct net *net, struct dst_entry **dst,
0fae2e77 283 struct flowi *fl, bool strict);
bce8032e 284 void (*saveroute)(const struct sk_buff *skb,
02f014d8 285 struct nf_queue_entry *entry);
d815d90b 286 int (*reroute)(struct net *net, struct sk_buff *skb,
02f014d8 287 const struct nf_queue_entry *entry);
bce8032e 288 int route_key_size;
2cc7d573
HW
289};
290
0e60ebe0 291extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 292static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
293{
294 return rcu_dereference(nf_afinfo[family]);
295}
2cc7d573 296
b51655b9 297static inline __sum16
422c346f
PM
298nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
299 u_int8_t protocol, unsigned short family)
300{
1e796fda 301 const struct nf_afinfo *afinfo;
b51655b9 302 __sum16 csum = 0;
422c346f
PM
303
304 rcu_read_lock();
305 afinfo = nf_get_afinfo(family);
306 if (afinfo)
307 csum = afinfo->checksum(skb, hook, dataoff, protocol);
308 rcu_read_unlock();
309 return csum;
310}
311
d63a6507
PM
312static inline __sum16
313nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
314 unsigned int dataoff, unsigned int len,
315 u_int8_t protocol, unsigned short family)
316{
317 const struct nf_afinfo *afinfo;
318 __sum16 csum = 0;
319
320 rcu_read_lock();
321 afinfo = nf_get_afinfo(family);
322 if (afinfo)
323 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
324 protocol);
325 rcu_read_unlock();
326 return csum;
327}
328
a0f4ecf3
JP
329int nf_register_afinfo(const struct nf_afinfo *afinfo);
330void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 331
eb9c7ebe 332#include <net/flow.h>
c7232c99 333extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
eb9c7ebe
PM
334
335static inline void
76108cea 336nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 337{
051578cc 338#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
339 void (*decodefn)(struct sk_buff *, struct flowi *);
340
c7232c99
PM
341 rcu_read_lock();
342 decodefn = rcu_dereference(nf_nat_decode_session_hook);
343 if (decodefn)
344 decodefn(skb, fl);
345 rcu_read_unlock();
eb9c7ebe
PM
346#endif
347}
348
1da177e4 349#else /* !CONFIG_NETFILTER */
008027c3
AB
350static inline int
351NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
352 struct sk_buff *skb, struct net_device *in, struct net_device *out,
353 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
354 bool cond)
355{
356 return okfn(net, sk, skb);
357}
358
359static inline int
360NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
361 struct sk_buff *skb, struct net_device *in, struct net_device *out,
362 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
363{
364 return okfn(net, sk, skb);
365}
366
29a26a56
EB
367static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
368 struct sock *sk, struct sk_buff *skb,
369 struct net_device *indev, struct net_device *outdev,
0c4b51f0 370 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
f53b61d8 371{
9c92d348 372 return 1;
f53b61d8 373}
f53b61d8 374struct flowi;
eb9c7ebe 375static inline void
76108cea
JE
376nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
377{
378}
1da177e4
LT
379#endif /*CONFIG_NETFILTER*/
380
5f79e0f9 381#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
62da9865
DB
382#include <linux/netfilter/nf_conntrack_zones_common.h>
383
312a0c16 384extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 385void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
0e60ebe0 386extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
b7bd1809
PNA
387#else
388static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
389#endif
9cb01766
PNA
390
391struct nf_conn;
41d73ec0 392enum ip_conntrack_info;
9cb01766
PNA
393struct nlattr;
394
a4b4766c 395struct nfnl_ct_hook {
224a0597 396 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
b7bd1809 397 enum ip_conntrack_info *ctinfo);
9cb01766 398 size_t (*build_size)(const struct nf_conn *ct);
b7bd1809
PNA
399 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
400 enum ip_conntrack_info ctinfo,
401 u_int16_t ct_attr, u_int16_t ct_info_attr);
9cb01766 402 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
403 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
404 u32 portid, u32 report);
8c88f87c 405 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 406 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 407};
a4b4766c 408extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
5f79e0f9 409
e7c8899f
FW
410/**
411 * nf_skb_duplicated - TEE target has sent a packet
412 *
413 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
414 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
415 *
416 * This is used by xtables TEE target to prevent the duplicated skb from
417 * being duplicated again.
418 */
419DECLARE_PER_CPU(bool, nf_skb_duplicated);
420
1da177e4 421#endif /*__LINUX_NETFILTER_H*/