]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/netfilter.h
ipv4: Push struct net down into nf_send_reset
[mirror_ubuntu-bionic-kernel.git] / include / linux / netfilter.h
CommitLineData
1da177e4
LT
1#ifndef __LINUX_NETFILTER_H
2#define __LINUX_NETFILTER_H
3
1da177e4 4#include <linux/init.h>
1da177e4
LT
5#include <linux/skbuff.h>
6#include <linux/net.h>
7#include <linux/if.h>
2e3075a2
JE
8#include <linux/in.h>
9#include <linux/in6.h>
1da177e4
LT
10#include <linux/wait.h>
11#include <linux/list.h>
d1c85c2e 12#include <linux/static_key.h>
a263653e 13#include <linux/netfilter_defs.h>
085db2c0
EB
14#include <linux/netdevice.h>
15#include <net/net_namespace.h>
a263653e 16
1da177e4 17#ifdef CONFIG_NETFILTER
f615df76
FW
18static inline int NF_DROP_GETERR(int verdict)
19{
20 return -(verdict >> NF_VERDICT_QBITS);
21}
1da177e4 22
b8beedd2
PM
23static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
24 const union nf_inet_addr *a2)
25{
26 return a1->all[0] == a2->all[0] &&
27 a1->all[1] == a2->all[1] &&
28 a1->all[2] == a2->all[2] &&
29 a1->all[3] == a2->all[3];
30}
31
efdedd54
DF
32static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
33 union nf_inet_addr *result,
34 const union nf_inet_addr *mask)
35{
36 result->all[0] = a1->all[0] & mask->all[0];
37 result->all[1] = a1->all[1] & mask->all[1];
38 result->all[2] = a1->all[2] & mask->all[2];
39 result->all[3] = a1->all[3] & mask->all[3];
40}
41
a0f4ecf3 42int netfilter_init(void);
1da177e4 43
1da177e4 44struct sk_buff;
1da177e4 45
795aa6ef 46struct nf_hook_ops;
cfdfab31 47
1c984f8a
DM
48struct sock;
49
cfdfab31
DM
50struct nf_hook_state {
51 unsigned int hook;
52 int thresh;
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;
f7191483 58 struct list_head *hook_list;
0c4b51f0 59 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
cfdfab31
DM
60};
61
107a9f4d 62static inline void nf_hook_state_init(struct nf_hook_state *p,
f7191483 63 struct list_head *hook_list,
107a9f4d
DM
64 unsigned int hook,
65 int thresh, u_int8_t pf,
66 struct net_device *indev,
67 struct net_device *outdev,
1c984f8a 68 struct sock *sk,
b11b1f65 69 struct net *net,
0c4b51f0 70 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
107a9f4d
DM
71{
72 p->hook = hook;
73 p->thresh = thresh;
74 p->pf = pf;
75 p->in = indev;
76 p->out = outdev;
1c984f8a 77 p->sk = sk;
b11b1f65 78 p->net = net;
f7191483 79 p->hook_list = hook_list;
107a9f4d
DM
80 p->okfn = okfn;
81}
82
06198b34 83typedef unsigned int nf_hookfn(void *priv,
3db05fea 84 struct sk_buff *skb,
238e54c9 85 const struct nf_hook_state *state);
1da177e4 86
d94d9fee 87struct nf_hook_ops {
87d5c18c 88 struct list_head list;
1da177e4
LT
89
90 /* User fills in from here down. */
87d5c18c 91 nf_hookfn *hook;
e687ad60 92 struct net_device *dev;
87d5c18c
PN
93 struct module *owner;
94 void *priv;
95 u_int8_t pf;
96 unsigned int hooknum;
1da177e4 97 /* Hooks are ordered in ascending priority. */
87d5c18c 98 int priority;
1da177e4
LT
99};
100
d94d9fee 101struct nf_sockopt_ops {
1da177e4
LT
102 struct list_head list;
103
76108cea 104 u_int8_t pf;
1da177e4
LT
105
106 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
107 int set_optmin;
108 int set_optmax;
109 int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
c30f540b 110#ifdef CONFIG_COMPAT
3fdadf7d
DM
111 int (*compat_set)(struct sock *sk, int optval,
112 void __user *user, unsigned int len);
c30f540b 113#endif
1da177e4
LT
114 int get_optmin;
115 int get_optmax;
116 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b 117#ifdef CONFIG_COMPAT
3fdadf7d
DM
118 int (*compat_get)(struct sock *sk, int optval,
119 void __user *user, int *len);
c30f540b 120#endif
16fcec35
NH
121 /* Use the module struct to lock set/get code in place */
122 struct module *owner;
1da177e4
LT
123};
124
1da177e4 125/* Function to register/unregister hook points. */
085db2c0
EB
126int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
127void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
128int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
129 unsigned int n);
130void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
131 unsigned int n);
132
1da177e4
LT
133int nf_register_hook(struct nf_hook_ops *reg);
134void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb1
PM
135int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
136void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4
LT
137
138/* Functions to register get/setsockopt ranges (non-inclusive). You
139 need to check permissions yourself! */
140int nf_register_sockopt(struct nf_sockopt_ops *reg);
141void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
142
d1c85c2e 143#ifdef HAVE_JUMP_LABEL
c5905afb 144extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
d1c85c2e 145
3bbd14e0 146static inline bool nf_hook_list_active(struct list_head *hook_list,
b8d0aad0 147 u_int8_t pf, unsigned int hook)
a2d7ec58
ED
148{
149 if (__builtin_constant_p(pf) &&
150 __builtin_constant_p(hook))
c5905afb 151 return static_key_false(&nf_hooks_needed[pf][hook]);
a2d7ec58 152
3bbd14e0 153 return !list_empty(hook_list);
a2d7ec58
ED
154}
155#else
3bbd14e0 156static inline bool nf_hook_list_active(struct list_head *hook_list,
b8d0aad0 157 u_int8_t pf, unsigned int hook)
a2d7ec58 158{
3bbd14e0 159 return !list_empty(hook_list);
a2d7ec58
ED
160}
161#endif
162
cfdfab31 163int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
16a6677f
PM
164
165/**
166 * nf_hook_thresh - call a netfilter hook
b8d0aad0 167 *
16a6677f
PM
168 * Returns 1 if the hook has allowed the packet to pass. The function
169 * okfn must be invoked by the caller in this case. Any other return
170 * value indicates the packet has been consumed by the hook.
171 */
76108cea 172static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
7a773504 173 struct net *net,
7026b1dd 174 struct sock *sk,
3db05fea 175 struct sk_buff *skb,
16a6677f
PM
176 struct net_device *indev,
177 struct net_device *outdev,
0c4b51f0 178 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
7026b1dd 179 int thresh)
16a6677f 180{
3bbd14e0 181 struct list_head *hook_list = &net->nf.hooks[pf][hook];
70aa9966 182
3bbd14e0 183 if (nf_hook_list_active(hook_list, pf, hook)) {
107a9f4d 184 struct nf_hook_state state;
cfdfab31 185
3bbd14e0 186 nf_hook_state_init(&state, hook_list, hook, thresh,
b11b1f65 187 pf, indev, outdev, sk, net, okfn);
cfdfab31
DM
188 return nf_hook_slow(skb, &state);
189 }
a2d7ec58 190 return 1;
16a6677f
PM
191}
192
29a26a56
EB
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,
0c4b51f0 196 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
16a6677f 197{
7a773504 198 return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN);
16a6677f 199}
1da177e4
LT
200
201/* Activate hook; either okfn or kfree_skb called, unless a hook
202 returns NF_STOLEN (in which case, it's up to the hook to deal with
203 the consequences).
204
205 Returns -ERRNO if packet dropped. Zero means queued, stolen or
206 accepted.
207*/
208
209/* RR:
210 > I don't want nf_hook to return anything because people might forget
211 > about async and trust the return value to mean "packet was ok".
212
213 AK:
214 Just document it clearly, then you can expect some sense from kernel
215 coders :)
216*/
217
2249065f 218static inline int
29a26a56 219NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd
DM
220 struct sk_buff *skb, struct net_device *in,
221 struct net_device *out,
0c4b51f0
EB
222 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
223 int thresh)
2249065f 224{
7a773504 225 int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh);
2249065f 226 if (ret == 1)
0c4b51f0 227 ret = okfn(net, sk, skb);
2249065f
JE
228 return ret;
229}
48d5cad8 230
2249065f 231static inline int
29a26a56 232NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
7026b1dd 233 struct sk_buff *skb, struct net_device *in, struct net_device *out,
0c4b51f0
EB
234 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
235 bool cond)
2249065f 236{
4bac6b18
PM
237 int ret;
238
239 if (!cond ||
7a773504 240 ((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1))
0c4b51f0 241 ret = okfn(net, sk, skb);
2249065f
JE
242 return ret;
243}
1da177e4 244
2249065f 245static inline int
29a26a56 246NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
2249065f 247 struct net_device *in, struct net_device *out,
0c4b51f0 248 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
2249065f 249{
29a26a56 250 return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN);
2249065f 251}
1da177e4
LT
252
253/* Call setsockopt() */
76108cea 254int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842 255 unsigned int len);
76108cea 256int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4 257 int *len);
c30f540b 258#ifdef CONFIG_COMPAT
76108cea 259int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842 260 char __user *opt, unsigned int len);
76108cea 261int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d 262 char __user *opt, int *len);
c30f540b 263#endif
3fdadf7d 264
089af26c
HW
265/* Call this before modifying an existing packet: ensures it is
266 modifiable and linear to the point you care about (writable_len).
267 Returns true or false. */
a0f4ecf3 268int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c 269
1841a4c7 270struct flowi;
02f014d8 271struct nf_queue_entry;
c01cd429 272
bce8032e
PM
273struct nf_afinfo {
274 unsigned short family;
b51655b9 275 __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
422c346f 276 unsigned int dataoff, u_int8_t protocol);
d63a6507
PM
277 __sum16 (*checksum_partial)(struct sk_buff *skb,
278 unsigned int hook,
279 unsigned int dataoff,
280 unsigned int len,
281 u_int8_t protocol);
31ad3dd6 282 int (*route)(struct net *net, struct dst_entry **dst,
0fae2e77 283 struct flowi *fl, bool strict);
bce8032e 284 void (*saveroute)(const struct sk_buff *skb,
02f014d8 285 struct nf_queue_entry *entry);
3db05fea 286 int (*reroute)(struct sk_buff *skb,
02f014d8 287 const struct nf_queue_entry *entry);
bce8032e 288 int route_key_size;
2cc7d573
HW
289};
290
0e60ebe0 291extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda 292static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032e
PM
293{
294 return rcu_dereference(nf_afinfo[family]);
295}
2cc7d573 296
b51655b9 297static inline __sum16
422c346f
PM
298nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
299 u_int8_t protocol, unsigned short family)
300{
1e796fda 301 const struct nf_afinfo *afinfo;
b51655b9 302 __sum16 csum = 0;
422c346f
PM
303
304 rcu_read_lock();
305 afinfo = nf_get_afinfo(family);
306 if (afinfo)
307 csum = afinfo->checksum(skb, hook, dataoff, protocol);
308 rcu_read_unlock();
309 return csum;
310}
311
d63a6507
PM
312static inline __sum16
313nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
314 unsigned int dataoff, unsigned int len,
315 u_int8_t protocol, unsigned short family)
316{
317 const struct nf_afinfo *afinfo;
318 __sum16 csum = 0;
319
320 rcu_read_lock();
321 afinfo = nf_get_afinfo(family);
322 if (afinfo)
323 csum = afinfo->checksum_partial(skb, hook, dataoff, len,
324 protocol);
325 rcu_read_unlock();
326 return csum;
327}
328
a0f4ecf3
JP
329int nf_register_afinfo(const struct nf_afinfo *afinfo);
330void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032e 331
eb9c7ebe 332#include <net/flow.h>
c7232c99 333extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
eb9c7ebe
PM
334
335static inline void
76108cea 336nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe 337{
051578cc 338#ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe
PM
339 void (*decodefn)(struct sk_buff *, struct flowi *);
340
c7232c99
PM
341 rcu_read_lock();
342 decodefn = rcu_dereference(nf_nat_decode_session_hook);
343 if (decodefn)
344 decodefn(skb, fl);
345 rcu_read_unlock();
eb9c7ebe
PM
346#endif
347}
348
1da177e4 349#else /* !CONFIG_NETFILTER */
0c4b51f0
EB
350#define NF_HOOK(pf, hook, net, sk, skb, indev, outdev, okfn) (okfn)(net, sk, skb)
351#define NF_HOOK_COND(pf, hook, net, sk, skb, indev, outdev, okfn, cond) (okfn)(net, sk, skb)
29a26a56
EB
352static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
353 struct sock *sk, struct sk_buff *skb,
354 struct net_device *indev, struct net_device *outdev,
0c4b51f0 355 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
f53b61d8 356{
9c92d348 357 return 1;
f53b61d8 358}
f53b61d8 359struct flowi;
eb9c7ebe 360static inline void
76108cea
JE
361nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
362{
363}
1da177e4
LT
364#endif /*CONFIG_NETFILTER*/
365
5f79e0f9 366#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
62da9865
DB
367#include <linux/netfilter/nf_conntrack_zones_common.h>
368
312a0c16 369extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
a0f4ecf3 370void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
0e60ebe0 371extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
9cb01766
PNA
372
373struct nf_conn;
41d73ec0 374enum ip_conntrack_info;
9cb01766
PNA
375struct nlattr;
376
377struct nfq_ct_hook {
378 size_t (*build_size)(const struct nf_conn *ct);
379 int (*build)(struct sk_buff *skb, struct nf_conn *ct);
380 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
bd077937
PNA
381 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
382 u32 portid, u32 report);
8c88f87c 383 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec0 384 enum ip_conntrack_info ctinfo, s32 off);
9cb01766 385};
41d73ec0 386extern struct nfq_ct_hook __rcu *nfq_ct_hook;
5f79e0f9
YK
387#else
388static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
389#endif
390
e7c8899f
FW
391/**
392 * nf_skb_duplicated - TEE target has sent a packet
393 *
394 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
395 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
396 *
397 * This is used by xtables TEE target to prevent the duplicated skb from
398 * being duplicated again.
399 */
400DECLARE_PER_CPU(bool, nf_skb_duplicated);
401
1da177e4 402#endif /*__LINUX_NETFILTER_H*/