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