Merge tag 'mt76-for-kvalo-2020-06-07' of https://github.com/nbd168/wireless
[linux-2.6-block.git] / include / linux / netfilter.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __LINUX_NETFILTER_H
3#define __LINUX_NETFILTER_H
4
1da177e4 5#include <linux/init.h>
1da177e4
LT
6#include <linux/skbuff.h>
7#include <linux/net.h>
8#include <linux/if.h>
2e3075a2
JE
9#include <linux/in.h>
10#include <linux/in6.h>
1da177e4
LT
11#include <linux/wait.h>
12#include <linux/list.h>
d1c85c2e 13#include <linux/static_key.h>
a263653e 14#include <linux/netfilter_defs.h>
085db2c0
EB
15#include <linux/netdevice.h>
16#include <net/net_namespace.h>
a263653e 17
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{
01902f8c
LR
26#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
27 const unsigned long *ul1 = (const unsigned long *)a1;
28 const unsigned long *ul2 = (const unsigned long *)a2;
29
30 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
31#else
b8beedd2
PM
32 return a1->all[0] == a2->all[0] &&
33 a1->all[1] == a2->all[1] &&
34 a1->all[2] == a2->all[2] &&
35 a1->all[3] == a2->all[3];
01902f8c 36#endif
b8beedd2
PM
37}
38
efdedd54
DF
39static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
40 union nf_inet_addr *result,
41 const union nf_inet_addr *mask)
42{
522e4077
LR
43#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
44 const unsigned long *ua = (const unsigned long *)a1;
45 unsigned long *ur = (unsigned long *)result;
46 const unsigned long *um = (const unsigned long *)mask;
47
48 ur[0] = ua[0] & um[0];
49 ur[1] = ua[1] & um[1];
50#else
efdedd54
DF
51 result->all[0] = a1->all[0] & mask->all[0];
52 result->all[1] = a1->all[1] & mask->all[1];
53 result->all[2] = a1->all[2] & mask->all[2];
54 result->all[3] = a1->all[3] & mask->all[3];
522e4077 55#endif
efdedd54
DF
56}
57
a0f4ecf3 58int netfilter_init(void);
1da177e4 59
1da177e4 60struct sk_buff;
1da177e4 61
795aa6ef 62struct nf_hook_ops;
cfdfab31 63
1c984f8a
DM
64struct sock;
65
cfdfab31
DM
66struct nf_hook_state {
67 unsigned int hook;
cfdfab31
DM
68 u_int8_t pf;
69 struct net_device *in;
70 struct net_device *out;
1c984f8a 71 struct sock *sk;
b11b1f65 72 struct net *net;
0c4b51f0 73 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
cfdfab31
DM
74};
75
e3b37f11
AC
76typedef unsigned int nf_hookfn(void *priv,
77 struct sk_buff *skb,
78 const struct nf_hook_state *state);
79struct nf_hook_ops {
e3b37f11
AC
80 /* User fills in from here down. */
81 nf_hookfn *hook;
82 struct net_device *dev;
83 void *priv;
84 u_int8_t pf;
85 unsigned int hooknum;
86 /* Hooks are ordered in ascending priority. */
87 int priority;
88};
89
90struct nf_hook_entry {
d415b9eb
AC
91 nf_hookfn *hook;
92 void *priv;
e3b37f11
AC
93};
94
8c873e21
FW
95struct nf_hook_entries_rcu_head {
96 struct rcu_head head;
97 void *allocation;
98};
99
960632ec
AC
100struct nf_hook_entries {
101 u16 num_hook_entries;
102 /* padding */
103 struct nf_hook_entry hooks[];
104
8c873e21
FW
105 /* trailer: pointers to original orig_ops of each hook,
106 * followed by rcu_head and scratch space used for freeing
107 * the structure via call_rcu.
960632ec 108 *
8c873e21
FW
109 * This is not part of struct nf_hook_entry since its only
110 * needed in slow path (hook register/unregister):
960632ec 111 * const struct nf_hook_ops *orig_ops[]
8c873e21
FW
112 *
113 * For the same reason, we store this at end -- its
114 * only needed when a hook is deleted, not during
115 * packet path processing:
116 * struct nf_hook_entries_rcu_head head
960632ec
AC
117 */
118};
0aa8c57a 119
f19438bd 120#ifdef CONFIG_NETFILTER
960632ec 121static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
0aa8c57a 122{
960632ec
AC
123 unsigned int n = e->num_hook_entries;
124 const void *hook_end;
125
126 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
127
128 return (struct nf_hook_ops **)hook_end;
0aa8c57a
AC
129}
130
131static inline int
132nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
133 struct nf_hook_state *state)
134{
d415b9eb 135 return entry->hook(entry->priv, skb, state);
0aa8c57a
AC
136}
137
107a9f4d
DM
138static inline void nf_hook_state_init(struct nf_hook_state *p,
139 unsigned int hook,
1610a73c 140 u_int8_t pf,
107a9f4d
DM
141 struct net_device *indev,
142 struct net_device *outdev,
1c984f8a 143 struct sock *sk,
b11b1f65 144 struct net *net,
0c4b51f0 145 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
107a9f4d
DM
146{
147 p->hook = hook;
107a9f4d
DM
148 p->pf = pf;
149 p->in = indev;
150 p->out = outdev;
1c984f8a 151 p->sk = sk;
b11b1f65 152 p->net = net;
107a9f4d
DM
153 p->okfn = okfn;
154}
155
1da177e4 156
1da177e4 157
d94d9fee 158struct nf_sockopt_ops {
1da177e4
LT
159 struct list_head list;
160
76108cea 161 u_int8_t pf;
1da177e4
LT
162
163 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
164 int set_optmin;
165 int set_optmax;
166 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 167#ifdef CONFIG_COMPAT
3fdadf7d
DM
168 int (*compat_set)(struct sock *sk, int optval,
169 void __user *user, unsigned int len);
c30f540b 170#endif
1da177e4
LT
171 int get_optmin;
172 int get_optmax;
173 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 174#ifdef CONFIG_COMPAT
3fdadf7d
DM
175 int (*compat_get)(struct sock *sk, int optval,
176 void __user *user, int *len);
c30f540b 177#endif
16fcec35
NH
178 /* Use the module struct to lock set/get code in place */
179 struct module *owner;
1da177e4
LT
180};
181
1da177e4 182/* Function to register/unregister hook points. */
085db2c0
EB
183int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
184void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
185int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
186 unsigned int n);
187void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
188 unsigned int n);
189
1da177e4
LT
190/* Functions to register get/setsockopt ranges (non-inclusive). You
191 need to check permissions yourself! */
192int nf_register_sockopt(struct nf_sockopt_ops *reg);
193void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
194
e9666d10 195#ifdef CONFIG_JUMP_LABEL
c5905afb 196extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58
ED
197#endif
198
01886bd9 199int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
960632ec 200 const struct nf_hook_entries *e, unsigned int i);
16a6677f 201
ca58fbe0
FW
202void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
203 const struct nf_hook_entries *e);
16a6677f 204/**
1610a73c 205 * nf_hook - call a netfilter hook
b8d0aad0 206 *
16a6677f
PM
207 * Returns 1 if the hook has allowed the packet to pass. The function
208 * okfn must be invoked by the caller in this case. Any other return
209 * value indicates the packet has been consumed by the hook.
210 */
1610a73c
PNA
211static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
212 struct sock *sk, struct sk_buff *skb,
213 struct net_device *indev, struct net_device *outdev,
214 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
16a6677f 215{
b0f38338 216 struct nf_hook_entries *hook_head = NULL;
e3b37f11 217 int ret = 1;
af4610c3 218
e9666d10 219#ifdef CONFIG_JUMP_LABEL
af4610c3
FW
220 if (__builtin_constant_p(pf) &&
221 __builtin_constant_p(hook) &&
222 !static_key_false(&nf_hooks_needed[pf][hook]))
223 return 1;
224#endif
225
e3b37f11 226 rcu_read_lock();
b0f38338
FW
227 switch (pf) {
228 case NFPROTO_IPV4:
229 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
230 break;
231 case NFPROTO_IPV6:
232 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
233 break;
234 case NFPROTO_ARP:
2a95183a 235#ifdef CONFIG_NETFILTER_FAMILY_ARP
421c119f
FW
236 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
237 break;
b0f38338 238 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
2a95183a 239#endif
b0f38338
FW
240 break;
241 case NFPROTO_BRIDGE:
2a95183a 242#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
b0f38338 243 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
2a95183a 244#endif
b0f38338 245 break;
bb4badf3 246#if IS_ENABLED(CONFIG_DECNET)
b0f38338
FW
247 case NFPROTO_DECNET:
248 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
249 break;
bb4badf3 250#endif
b0f38338
FW
251 default:
252 WARN_ON_ONCE(1);
253 break;
254 }
255
e3b37f11 256 if (hook_head) {
107a9f4d 257 struct nf_hook_state state;
cfdfab31 258
01886bd9 259 nf_hook_state_init(&state, hook, pf, indev, outdev,
1610a73c 260 sk, net, okfn);
fe72926b 261
960632ec 262 ret = nf_hook_slow(skb, &state, hook_head, 0);
cfdfab31 263 }
e3b37f11
AC
264 rcu_read_unlock();
265
266 return ret;
16a6677f
PM
267}
268
1da177e4
LT
269/* Activate hook; either okfn or kfree_skb called, unless a hook
270 returns NF_STOLEN (in which case, it's up to the hook to deal with
271 the consequences).
272
273 Returns -ERRNO if packet dropped. Zero means queued, stolen or
274 accepted.
275*/
276
277/* RR:
278 > I don't want nf_hook to return anything because people might forget
279 > about async and trust the return value to mean "packet was ok".
280
281 AK:
282 Just document it clearly, then you can expect some sense from kernel
283 coders :)
284*/
285
2249065f 286static inline int
29a26a56 287NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd 288 struct sk_buff *skb, struct net_device *in, struct net_device *out,
0c4b51f0
EB
289 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
290 bool cond)
2249065f 291{
4bac6b18
PM
292 int ret;
293
294 if (!cond ||
1610a73c 295 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
0c4b51f0 296 ret = okfn(net, sk, skb);
2249065f
JE
297 return ret;
298}
1da177e4 299
2249065f 300static inline int
29a26a56 301NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
2249065f 302 struct net_device *in, struct net_device *out,
0c4b51f0 303 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
2249065f 304{
1610a73c
PNA
305 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
306 if (ret == 1)
307 ret = okfn(net, sk, skb);
308 return ret;
2249065f 309}
1da177e4 310
17266ee9
EC
311static inline void
312NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
313 struct list_head *head, struct net_device *in, struct net_device *out,
314 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
315{
ca58fbe0
FW
316 struct nf_hook_entries *hook_head = NULL;
317
318#ifdef CONFIG_JUMP_LABEL
319 if (__builtin_constant_p(pf) &&
320 __builtin_constant_p(hook) &&
321 !static_key_false(&nf_hooks_needed[pf][hook]))
322 return;
323#endif
324
325 rcu_read_lock();
326 switch (pf) {
327 case NFPROTO_IPV4:
328 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
329 break;
330 case NFPROTO_IPV6:
331 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
332 break;
333 default:
334 WARN_ON_ONCE(1);
335 break;
17266ee9 336 }
ca58fbe0
FW
337
338 if (hook_head) {
339 struct nf_hook_state state;
340
341 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
342
343 nf_hook_slow_list(head, &state, hook_head);
344 }
345 rcu_read_unlock();
17266ee9
EC
346}
347
1da177e4 348/* Call setsockopt() */
76108cea 349int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 350 unsigned int len);
76108cea 351int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 352 int *len);
c30f540b 353#ifdef CONFIG_COMPAT
76108cea 354int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 355 char __user *opt, unsigned int len);
76108cea 356int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 357 char __user *opt, int *len);
c30f540b 358#endif
3fdadf7d 359
1841a4c7 360struct flowi;
02f014d8 361struct nf_queue_entry;
c01cd429 362
ef71fe27
PNA
363__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
364 unsigned int dataoff, u_int8_t protocol,
365 unsigned short family);
422c346f 366
f7dcbe2f
PNA
367__sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
368 unsigned int dataoff, unsigned int len,
369 u_int8_t protocol, unsigned short family);
3f87c08c
PNA
370int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
371 bool strict, unsigned short family);
ce388f45 372int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
d63a6507 373
eb9c7ebe 374#include <net/flow.h>
2c205dd3
PNA
375
376struct nf_conn;
377enum nf_nat_manip_type;
378struct nlattr;
368982cd 379enum ip_conntrack_dir;
2c205dd3
PNA
380
381struct nf_nat_hook {
382 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
383 const struct nlattr *attr);
384 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
368982cd
PNA
385 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
386 enum nf_nat_manip_type mtype,
387 enum ip_conntrack_dir dir);
2c205dd3
PNA
388};
389
390extern struct nf_nat_hook __rcu *nf_nat_hook;
eb9c7ebe
PM
391
392static inline void
76108cea 393nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 394{
4806e975 395#if IS_ENABLED(CONFIG_NF_NAT)
2c205dd3 396 struct nf_nat_hook *nat_hook;
eb9c7ebe 397
c7232c99 398 rcu_read_lock();
2c205dd3 399 nat_hook = rcu_dereference(nf_nat_hook);
155fb5c5 400 if (nat_hook && nat_hook->decode_session)
2c205dd3 401 nat_hook->decode_session(skb, fl);
c7232c99 402 rcu_read_unlock();
eb9c7ebe
PM
403#endif
404}
405
1da177e4 406#else /* !CONFIG_NETFILTER */
008027c3
AB
407static inline int
408NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
409 struct sk_buff *skb, struct net_device *in, struct net_device *out,
410 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
411 bool cond)
412{
413 return okfn(net, sk, skb);
414}
415
416static inline int
417NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
418 struct sk_buff *skb, struct net_device *in, struct net_device *out,
419 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
420{
421 return okfn(net, sk, skb);
422}
423
17266ee9
EC
424static inline void
425NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
426 struct list_head *head, struct net_device *in, struct net_device *out,
427 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
428{
429 /* nothing to do */
430}
431
29a26a56
EB
432static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
433 struct sock *sk, struct sk_buff *skb,
434 struct net_device *indev, struct net_device *outdev,
0c4b51f0 435 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
f53b61d8 436{
9c92d348 437 return 1;
f53b61d8 438}
f53b61d8 439struct flowi;
eb9c7ebe 440static inline void
76108cea
JE
441nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
442{
443}
1da177e4
LT
444#endif /*CONFIG_NETFILTER*/
445
25d7cbcd 446#if IS_ENABLED(CONFIG_NF_CONNTRACK)
62da9865
DB
447#include <linux/netfilter/nf_conntrack_zones_common.h>
448
312a0c16 449extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 450void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
b60a6040
THJ
451struct nf_conntrack_tuple;
452bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
453 const struct sk_buff *skb);
b7bd1809
PNA
454#else
455static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
b60a6040
THJ
456struct nf_conntrack_tuple;
457static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
458 const struct sk_buff *skb)
459{
460 return false;
461}
b7bd1809 462#endif
9cb01766
PNA
463
464struct nf_conn;
41d73ec0 465enum ip_conntrack_info;
1f4b2439
PNA
466
467struct nf_ct_hook {
368982cd 468 int (*update)(struct net *net, struct sk_buff *skb);
1f4b2439 469 void (*destroy)(struct nf_conntrack *);
b60a6040
THJ
470 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
471 const struct sk_buff *);
1f4b2439
PNA
472};
473extern struct nf_ct_hook __rcu *nf_ct_hook;
474
9cb01766
PNA
475struct nlattr;
476
a4b4766c 477struct nfnl_ct_hook {
224a0597 478 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
b7bd1809 479 enum ip_conntrack_info *ctinfo);
9cb01766 480 size_t (*build_size)(const struct nf_conn *ct);
b7bd1809
PNA
481 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
482 enum ip_conntrack_info ctinfo,
483 u_int16_t ct_attr, u_int16_t ct_info_attr);
9cb01766 484 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
485 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
486 u32 portid, u32 report);
8c88f87c 487 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 488 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 489};
a4b4766c 490extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
5f79e0f9 491
e7c8899f
FW
492/**
493 * nf_skb_duplicated - TEE target has sent a packet
494 *
495 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
496 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
497 *
498 * This is used by xtables TEE target to prevent the duplicated skb from
499 * being duplicated again.
500 */
501DECLARE_PER_CPU(bool, nf_skb_duplicated);
502
1da177e4 503#endif /*__LINUX_NETFILTER_H*/