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