]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/netfilter.h
mm/hotplug: invalid PFNs from pfn_to_online_page()
[mirror_ubuntu-bionic-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
960632ec
AC
80struct nf_hook_entries {
81 u16 num_hook_entries;
82 /* padding */
83 struct nf_hook_entry hooks[];
84
85 /* trailer: pointers to original orig_ops of each hook.
86 *
87 * This is not part of struct nf_hook_entry since its only
88 * needed in slow path (hook register/unregister).
89 *
90 * const struct nf_hook_ops *orig_ops[]
91 */
92};
0aa8c57a 93
960632ec 94static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
0aa8c57a 95{
960632ec
AC
96 unsigned int n = e->num_hook_entries;
97 const void *hook_end;
98
99 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
100
101 return (struct nf_hook_ops **)hook_end;
0aa8c57a
AC
102}
103
104static inline int
105nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
106 struct nf_hook_state *state)
107{
d415b9eb 108 return entry->hook(entry->priv, skb, state);
0aa8c57a
AC
109}
110
107a9f4d
DM
111static inline void nf_hook_state_init(struct nf_hook_state *p,
112 unsigned int hook,
1610a73c 113 u_int8_t pf,
107a9f4d
DM
114 struct net_device *indev,
115 struct net_device *outdev,
1c984f8a 116 struct sock *sk,
b11b1f65 117 struct net *net,
0c4b51f0 118 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
107a9f4d
DM
119{
120 p->hook = hook;
107a9f4d
DM
121 p->pf = pf;
122 p->in = indev;
123 p->out = outdev;
1c984f8a 124 p->sk = sk;
b11b1f65 125 p->net = net;
107a9f4d
DM
126 p->okfn = okfn;
127}
128
1da177e4 129
1da177e4 130
d94d9fee 131struct nf_sockopt_ops {
1da177e4
LT
132 struct list_head list;
133
76108cea 134 u_int8_t pf;
1da177e4
LT
135
136 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
137 int set_optmin;
138 int set_optmax;
139 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 140#ifdef CONFIG_COMPAT
3fdadf7d
DM
141 int (*compat_set)(struct sock *sk, int optval,
142 void __user *user, unsigned int len);
c30f540b 143#endif
1da177e4
LT
144 int get_optmin;
145 int get_optmax;
146 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 147#ifdef CONFIG_COMPAT
3fdadf7d
DM
148 int (*compat_get)(struct sock *sk, int optval,
149 void __user *user, int *len);
c30f540b 150#endif
16fcec35
NH
151 /* Use the module struct to lock set/get code in place */
152 struct module *owner;
1da177e4
LT
153};
154
1da177e4 155/* Function to register/unregister hook points. */
085db2c0
EB
156int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
157void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
158int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
159 unsigned int n);
160void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
161 unsigned int n);
162
1da177e4
LT
163/* Functions to register get/setsockopt ranges (non-inclusive). You
164 need to check permissions yourself! */
165int nf_register_sockopt(struct nf_sockopt_ops *reg);
166void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
167
d1c85c2e 168#ifdef HAVE_JUMP_LABEL
c5905afb 169extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58
ED
170#endif
171
01886bd9 172int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
960632ec 173 const struct nf_hook_entries *e, unsigned int i);
16a6677f
PM
174
175/**
1610a73c 176 * nf_hook - call a netfilter hook
b8d0aad0 177 *
16a6677f
PM
178 * Returns 1 if the hook has allowed the packet to pass. The function
179 * okfn must be invoked by the caller in this case. Any other return
180 * value indicates the packet has been consumed by the hook.
181 */
1610a73c
PNA
182static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
183 struct sock *sk, struct sk_buff *skb,
184 struct net_device *indev, struct net_device *outdev,
185 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
16a6677f 186{
960632ec 187 struct nf_hook_entries *hook_head;
e3b37f11 188 int ret = 1;
af4610c3
FW
189
190#ifdef HAVE_JUMP_LABEL
191 if (__builtin_constant_p(pf) &&
192 __builtin_constant_p(hook) &&
193 !static_key_false(&nf_hooks_needed[pf][hook]))
194 return 1;
195#endif
196
e3b37f11
AC
197 rcu_read_lock();
198 hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
199 if (hook_head) {
107a9f4d 200 struct nf_hook_state state;
cfdfab31 201
01886bd9 202 nf_hook_state_init(&state, hook, pf, indev, outdev,
1610a73c 203 sk, net, okfn);
fe72926b 204
960632ec 205 ret = nf_hook_slow(skb, &state, hook_head, 0);
cfdfab31 206 }
e3b37f11
AC
207 rcu_read_unlock();
208
209 return ret;
16a6677f
PM
210}
211
1da177e4
LT
212/* Activate hook; either okfn or kfree_skb called, unless a hook
213 returns NF_STOLEN (in which case, it's up to the hook to deal with
214 the consequences).
215
216 Returns -ERRNO if packet dropped. Zero means queued, stolen or
217 accepted.
218*/
219
220/* RR:
221 > I don't want nf_hook to return anything because people might forget
222 > about async and trust the return value to mean "packet was ok".
223
224 AK:
225 Just document it clearly, then you can expect some sense from kernel
226 coders :)
227*/
228
2249065f 229static inline int
29a26a56 230NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd 231 struct sk_buff *skb, struct net_device *in, struct net_device *out,
0c4b51f0
EB
232 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
233 bool cond)
2249065f 234{
4bac6b18
PM
235 int ret;
236
237 if (!cond ||
1610a73c 238 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
0c4b51f0 239 ret = okfn(net, sk, skb);
2249065f
JE
240 return ret;
241}
1da177e4 242
2249065f 243static inline int
29a26a56 244NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
2249065f 245 struct net_device *in, struct net_device *out,
0c4b51f0 246 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
2249065f 247{
1610a73c
PNA
248 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
249 if (ret == 1)
250 ret = okfn(net, sk, skb);
251 return ret;
2249065f 252}
1da177e4
LT
253
254/* Call setsockopt() */
76108cea 255int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 256 unsigned int len);
76108cea 257int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 258 int *len);
c30f540b 259#ifdef CONFIG_COMPAT
76108cea 260int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 261 char __user *opt, unsigned int len);
76108cea 262int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 263 char __user *opt, int *len);
c30f540b 264#endif
3fdadf7d 265
089af26c
HW
266/* Call this before modifying an existing packet: ensures it is
267 modifiable and linear to the point you care about (writable_len).
268 Returns true or false. */
a0f4ecf3 269int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 270
1841a4c7 271struct flowi;
02f014d8 272struct nf_queue_entry;
c01cd429 273
bce8032e
PM
274struct nf_afinfo {
275 unsigned short family;
b51655b9 276 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 277 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
278 __sum16 (*checksum_partial)(struct sk_buff *skb,
279 unsigned int hook,
280 unsigned int dataoff,
281 unsigned int len,
282 u_int8_t protocol);
31ad3dd6 283 int (*route)(struct net *net, struct dst_entry **dst,
0fae2e77 284 struct flowi *fl, bool strict);
bce8032e 285 void (*saveroute)(const struct sk_buff *skb,
02f014d8 286 struct nf_queue_entry *entry);
d815d90b 287 int (*reroute)(struct net *net, struct sk_buff *skb,
02f014d8 288 const struct nf_queue_entry *entry);
bce8032e 289 int route_key_size;
2cc7d573
HW
290};
291
0e60ebe0 292extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 293static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
294{
295 return rcu_dereference(nf_afinfo[family]);
296}
2cc7d573 297
b51655b9 298static inline __sum16
422c346f
PM
299nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
300 u_int8_t protocol, unsigned short family)
301{
1e796fda 302 const struct nf_afinfo *afinfo;
b51655b9 303 __sum16 csum = 0;
422c346f
PM
304
305 rcu_read_lock();
306 afinfo = nf_get_afinfo(family);
307 if (afinfo)
308 csum = afinfo->checksum(skb, hook, dataoff, protocol);
309 rcu_read_unlock();
310 return csum;
311}
312
d63a6507
PM
313static inline __sum16
314nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
315 unsigned int dataoff, unsigned int len,
316 u_int8_t protocol, unsigned short family)
317{
318 const struct nf_afinfo *afinfo;
319 __sum16 csum = 0;
320
321 rcu_read_lock();
322 afinfo = nf_get_afinfo(family);
323 if (afinfo)
324 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
325 protocol);
326 rcu_read_unlock();
327 return csum;
328}
329
a0f4ecf3
JP
330int nf_register_afinfo(const struct nf_afinfo *afinfo);
331void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 332
eb9c7ebe 333#include <net/flow.h>
c7232c99 334extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
eb9c7ebe
PM
335
336static inline void
76108cea 337nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 338{
051578cc 339#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
340 void (*decodefn)(struct sk_buff *, struct flowi *);
341
c7232c99
PM
342 rcu_read_lock();
343 decodefn = rcu_dereference(nf_nat_decode_session_hook);
344 if (decodefn)
345 decodefn(skb, fl);
346 rcu_read_unlock();
eb9c7ebe
PM
347#endif
348}
349
1da177e4 350#else /* !CONFIG_NETFILTER */
008027c3
AB
351static inline int
352NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
353 struct sk_buff *skb, struct net_device *in, struct net_device *out,
354 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
355 bool cond)
356{
357 return okfn(net, sk, skb);
358}
359
360static inline int
361NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
362 struct sk_buff *skb, struct net_device *in, struct net_device *out,
363 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
364{
365 return okfn(net, sk, skb);
366}
367
29a26a56
EB
368static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
369 struct sock *sk, struct sk_buff *skb,
370 struct net_device *indev, struct net_device *outdev,
0c4b51f0 371 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
f53b61d8 372{
9c92d348 373 return 1;
f53b61d8 374}
f53b61d8 375struct flowi;
eb9c7ebe 376static inline void
76108cea
JE
377nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
378{
379}
1da177e4
LT
380#endif /*CONFIG_NETFILTER*/
381
5f79e0f9 382#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
62da9865
DB
383#include <linux/netfilter/nf_conntrack_zones_common.h>
384
312a0c16 385extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 386void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
0e60ebe0 387extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
b7bd1809
PNA
388#else
389static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
390#endif
9cb01766
PNA
391
392struct nf_conn;
41d73ec0 393enum ip_conntrack_info;
9cb01766
PNA
394struct nlattr;
395
a4b4766c 396struct nfnl_ct_hook {
224a0597 397 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
b7bd1809 398 enum ip_conntrack_info *ctinfo);
9cb01766 399 size_t (*build_size)(const struct nf_conn *ct);
b7bd1809
PNA
400 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
401 enum ip_conntrack_info ctinfo,
402 u_int16_t ct_attr, u_int16_t ct_info_attr);
9cb01766 403 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
404 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
405 u32 portid, u32 report);
8c88f87c 406 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 407 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 408};
a4b4766c 409extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
5f79e0f9 410
e7c8899f
FW
411/**
412 * nf_skb_duplicated - TEE target has sent a packet
413 *
414 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
415 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
416 *
417 * This is used by xtables TEE target to prevent the duplicated skb from
418 * being duplicated again.
419 */
420DECLARE_PER_CPU(bool, nf_skb_duplicated);
421
1da177e4 422#endif /*__LINUX_NETFILTER_H*/