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