Merge tag 'wireless-drivers-next-for-davem-2019-06-26' of git://git.kernel.org/pub...
[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
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{
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
DM
67struct nf_hook_state {
68 unsigned int hook;
cfdfab31
DM
69 u_int8_t pf;
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);
80struct nf_hook_ops {
e3b37f11
AC
81 /* User fills in from here down. */
82 nf_hookfn *hook;
83 struct net_device *dev;
84 void *priv;
85 u_int8_t pf;
86 unsigned int hooknum;
87 /* Hooks are ordered in ascending priority. */
88 int priority;
89};
90
91struct nf_hook_entry {
d415b9eb
AC
92 nf_hookfn *hook;
93 void *priv;
e3b37f11
AC
94};
95
8c873e21
FW
96struct nf_hook_entries_rcu_head {
97 struct rcu_head head;
98 void *allocation;
99};
100
960632ec
AC
101struct nf_hook_entries {
102 u16 num_hook_entries;
103 /* padding */
104 struct nf_hook_entry hooks[];
105
8c873e21
FW
106 /* trailer: pointers to original orig_ops of each hook,
107 * followed by rcu_head and scratch space used for freeing
108 * the structure via call_rcu.
960632ec 109 *
8c873e21
FW
110 * This is not part of struct nf_hook_entry since its only
111 * needed in slow path (hook register/unregister):
960632ec 112 * const struct nf_hook_ops *orig_ops[]
8c873e21
FW
113 *
114 * For the same reason, we store this at end -- its
115 * only needed when a hook is deleted, not during
116 * packet path processing:
117 * struct nf_hook_entries_rcu_head head
960632ec
AC
118 */
119};
0aa8c57a 120
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
PM
201
202/**
1610a73c 203 * nf_hook - call a netfilter hook
b8d0aad0 204 *
16a6677f
PM
205 * Returns 1 if the hook has allowed the packet to pass. The function
206 * okfn must be invoked by the caller in this case. Any other return
207 * value indicates the packet has been consumed by the hook.
208 */
1610a73c
PNA
209static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
210 struct sock *sk, struct sk_buff *skb,
211 struct net_device *indev, struct net_device *outdev,
212 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
16a6677f 213{
b0f38338 214 struct nf_hook_entries *hook_head = NULL;
e3b37f11 215 int ret = 1;
af4610c3 216
e9666d10 217#ifdef CONFIG_JUMP_LABEL
af4610c3
FW
218 if (__builtin_constant_p(pf) &&
219 __builtin_constant_p(hook) &&
220 !static_key_false(&nf_hooks_needed[pf][hook]))
221 return 1;
222#endif
223
e3b37f11 224 rcu_read_lock();
b0f38338
FW
225 switch (pf) {
226 case NFPROTO_IPV4:
227 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
228 break;
229 case NFPROTO_IPV6:
230 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
231 break;
232 case NFPROTO_ARP:
2a95183a 233#ifdef CONFIG_NETFILTER_FAMILY_ARP
421c119f
FW
234 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
235 break;
b0f38338 236 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
2a95183a 237#endif
b0f38338
FW
238 break;
239 case NFPROTO_BRIDGE:
2a95183a 240#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
b0f38338 241 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
2a95183a 242#endif
b0f38338 243 break;
bb4badf3 244#if IS_ENABLED(CONFIG_DECNET)
b0f38338
FW
245 case NFPROTO_DECNET:
246 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
247 break;
bb4badf3 248#endif
b0f38338
FW
249 default:
250 WARN_ON_ONCE(1);
251 break;
252 }
253
e3b37f11 254 if (hook_head) {
107a9f4d 255 struct nf_hook_state state;
cfdfab31 256
01886bd9 257 nf_hook_state_init(&state, hook, pf, indev, outdev,
1610a73c 258 sk, net, okfn);
fe72926b 259
960632ec 260 ret = nf_hook_slow(skb, &state, hook_head, 0);
cfdfab31 261 }
e3b37f11
AC
262 rcu_read_unlock();
263
264 return ret;
16a6677f
PM
265}
266
1da177e4
LT
267/* Activate hook; either okfn or kfree_skb called, unless a hook
268 returns NF_STOLEN (in which case, it's up to the hook to deal with
269 the consequences).
270
271 Returns -ERRNO if packet dropped. Zero means queued, stolen or
272 accepted.
273*/
274
275/* RR:
276 > I don't want nf_hook to return anything because people might forget
277 > about async and trust the return value to mean "packet was ok".
278
279 AK:
280 Just document it clearly, then you can expect some sense from kernel
281 coders :)
282*/
283
2249065f 284static inline int
29a26a56 285NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd 286 struct sk_buff *skb, struct net_device *in, struct net_device *out,
0c4b51f0
EB
287 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
288 bool cond)
2249065f 289{
4bac6b18
PM
290 int ret;
291
292 if (!cond ||
1610a73c 293 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
0c4b51f0 294 ret = okfn(net, sk, skb);
2249065f
JE
295 return ret;
296}
1da177e4 297
2249065f 298static inline int
29a26a56 299NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
2249065f 300 struct net_device *in, struct net_device *out,
0c4b51f0 301 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
2249065f 302{
1610a73c
PNA
303 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
304 if (ret == 1)
305 ret = okfn(net, sk, skb);
306 return ret;
2249065f 307}
1da177e4 308
17266ee9
EC
309static inline void
310NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
311 struct list_head *head, struct net_device *in, struct net_device *out,
312 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
313{
314 struct sk_buff *skb, *next;
9f17dbf0 315 struct list_head sublist;
17266ee9 316
9f17dbf0 317 INIT_LIST_HEAD(&sublist);
17266ee9 318 list_for_each_entry_safe(skb, next, head, list) {
9f17dbf0
EC
319 list_del(&skb->list);
320 if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
321 list_add_tail(&skb->list, &sublist);
17266ee9 322 }
9f17dbf0
EC
323 /* Put passed packets back on main list */
324 list_splice(&sublist, head);
17266ee9
EC
325}
326
1da177e4 327/* Call setsockopt() */
76108cea 328int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 329 unsigned int len);
76108cea 330int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 331 int *len);
c30f540b 332#ifdef CONFIG_COMPAT
76108cea 333int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 334 char __user *opt, unsigned int len);
76108cea 335int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 336 char __user *opt, int *len);
c30f540b 337#endif
3fdadf7d 338
1841a4c7 339struct flowi;
02f014d8 340struct nf_queue_entry;
c01cd429 341
ef71fe27
PNA
342__sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
343 unsigned int dataoff, u_int8_t protocol,
344 unsigned short family);
422c346f 345
f7dcbe2f
PNA
346__sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
347 unsigned int dataoff, unsigned int len,
348 u_int8_t protocol, unsigned short family);
3f87c08c
PNA
349int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
350 bool strict, unsigned short family);
ce388f45 351int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
d63a6507 352
eb9c7ebe 353#include <net/flow.h>
2c205dd3
PNA
354
355struct nf_conn;
356enum nf_nat_manip_type;
357struct nlattr;
368982cd 358enum ip_conntrack_dir;
2c205dd3
PNA
359
360struct nf_nat_hook {
361 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
362 const struct nlattr *attr);
363 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
368982cd
PNA
364 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
365 enum nf_nat_manip_type mtype,
366 enum ip_conntrack_dir dir);
2c205dd3
PNA
367};
368
369extern struct nf_nat_hook __rcu *nf_nat_hook;
eb9c7ebe
PM
370
371static inline void
76108cea 372nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 373{
4806e975 374#if IS_ENABLED(CONFIG_NF_NAT)
2c205dd3 375 struct nf_nat_hook *nat_hook;
eb9c7ebe 376
c7232c99 377 rcu_read_lock();
2c205dd3 378 nat_hook = rcu_dereference(nf_nat_hook);
155fb5c5 379 if (nat_hook && nat_hook->decode_session)
2c205dd3 380 nat_hook->decode_session(skb, fl);
c7232c99 381 rcu_read_unlock();
eb9c7ebe
PM
382#endif
383}
384
1da177e4 385#else /* !CONFIG_NETFILTER */
008027c3
AB
386static inline int
387NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
388 struct sk_buff *skb, struct net_device *in, struct net_device *out,
389 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
390 bool cond)
391{
392 return okfn(net, sk, skb);
393}
394
395static inline int
396NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
397 struct sk_buff *skb, struct net_device *in, struct net_device *out,
398 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
399{
400 return okfn(net, sk, skb);
401}
402
17266ee9
EC
403static inline void
404NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
405 struct list_head *head, struct net_device *in, struct net_device *out,
406 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
407{
408 /* nothing to do */
409}
410
29a26a56
EB
411static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
412 struct sock *sk, struct sk_buff *skb,
413 struct net_device *indev, struct net_device *outdev,
0c4b51f0 414 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
f53b61d8 415{
9c92d348 416 return 1;
f53b61d8 417}
f53b61d8 418struct flowi;
eb9c7ebe 419static inline void
76108cea
JE
420nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
421{
422}
1da177e4
LT
423#endif /*CONFIG_NETFILTER*/
424
5f79e0f9 425#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
62da9865
DB
426#include <linux/netfilter/nf_conntrack_zones_common.h>
427
312a0c16 428extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 429void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
b60a6040
THJ
430struct nf_conntrack_tuple;
431bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
432 const struct sk_buff *skb);
b7bd1809
PNA
433#else
434static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
b60a6040
THJ
435struct nf_conntrack_tuple;
436static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
437 const struct sk_buff *skb)
438{
439 return false;
440}
b7bd1809 441#endif
9cb01766
PNA
442
443struct nf_conn;
41d73ec0 444enum ip_conntrack_info;
1f4b2439
PNA
445
446struct nf_ct_hook {
368982cd 447 int (*update)(struct net *net, struct sk_buff *skb);
1f4b2439 448 void (*destroy)(struct nf_conntrack *);
b60a6040
THJ
449 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
450 const struct sk_buff *);
1f4b2439
PNA
451};
452extern struct nf_ct_hook __rcu *nf_ct_hook;
453
9cb01766
PNA
454struct nlattr;
455
a4b4766c 456struct nfnl_ct_hook {
224a0597 457 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
b7bd1809 458 enum ip_conntrack_info *ctinfo);
9cb01766 459 size_t (*build_size)(const struct nf_conn *ct);
b7bd1809
PNA
460 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
461 enum ip_conntrack_info ctinfo,
462 u_int16_t ct_attr, u_int16_t ct_info_attr);
9cb01766 463 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
464 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
465 u32 portid, u32 report);
8c88f87c 466 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 467 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 468};
a4b4766c 469extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
5f79e0f9 470
e7c8899f
FW
471/**
472 * nf_skb_duplicated - TEE target has sent a packet
473 *
474 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
475 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
476 *
477 * This is used by xtables TEE target to prevent the duplicated skb from
478 * being duplicated again.
479 */
480DECLARE_PER_CPU(bool, nf_skb_duplicated);
481
1da177e4 482#endif /*__LINUX_NETFILTER_H*/