1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_NETFILTER_H
3 #define __LINUX_NETFILTER_H
4
5 #include <linux/init.h>
6 #include <linux/skbuff.h>
7 #include <linux/net.h>
8 #include <linux/if.h>
9 #include <linux/in.h>
10 #include <linux/in6.h>
11 #include <linux/wait.h>
12 #include <linux/list.h>
13 #include <linux/static_key.h>
14 #include <linux/netfilter_defs.h>
15 #include <linux/netdevice.h>
16 #include <linux/sockptr.h>
17 #include <net/net_namespace.h>
18
NF_DROP_GETERR(int verdict)19 static inline int NF_DROP_GETERR(int verdict)
20 {
21 return -(verdict >> NF_VERDICT_QBITS);
22 }
23
nf_inet_addr_cmp(const union nf_inet_addr * a1,const union nf_inet_addr * a2)24 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
25 const union nf_inet_addr *a2)
26 {
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
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];
37 #endif
38 }
39
nf_inet_addr_mask(const union nf_inet_addr * a1,union nf_inet_addr * result,const union nf_inet_addr * mask)40 static 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 {
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
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];
56 #endif
57 }
58
59 int netfilter_init(void);
60
61 struct sk_buff;
62
63 struct nf_hook_ops;
64
65 struct sock;
66
67 struct nf_hook_state {
68 unsigned int hook;
69 u_int8_t pf;
70 struct net_device *in;
71 struct net_device *out;
72 struct sock *sk;
73 struct net *net;
74 int (*okfn)(struct net *, struct sock *, struct sk_buff *);
75 };
76
77 typedef unsigned int nf_hookfn(void *priv,
78 struct sk_buff *skb,
79 const struct nf_hook_state *state);
80 struct nf_hook_ops {
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
91 struct nf_hook_entry {
92 nf_hookfn *hook;
93 void *priv;
94 };
95
96 struct nf_hook_entries_rcu_head {
97 struct rcu_head head;
98 void *allocation;
99 };
100
101 struct nf_hook_entries {
102 u16 num_hook_entries;
103 /* padding */
104 struct nf_hook_entry hooks[];
105
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.
109 *
110 * This is not part of struct nf_hook_entry since its only
111 * needed in slow path (hook register/unregister):
112 * const struct nf_hook_ops *orig_ops[]
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
118 */
119 };
120
121 #ifdef CONFIG_NETFILTER
nf_hook_entries_get_hook_ops(const struct nf_hook_entries * e)122 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
123 {
124 unsigned int n = e->num_hook_entries;
125 const void *hook_end;
126
127 hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
128
129 return (struct nf_hook_ops **)hook_end;
130 }
131
132 static inline int
nf_hook_entry_hookfn(const struct nf_hook_entry * entry,struct sk_buff * skb,struct nf_hook_state * state)133 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
134 struct nf_hook_state *state)
135 {
136 return entry->hook(entry->priv, skb, state);
137 }
138
nf_hook_state_init(struct nf_hook_state * p,unsigned int hook,u_int8_t pf,struct net_device * indev,struct net_device * outdev,struct sock * sk,struct net * net,int (* okfn)(struct net *,struct sock *,struct sk_buff *))139 static inline void nf_hook_state_init(struct nf_hook_state *p,
140 unsigned int hook,
141 u_int8_t pf,
142 struct net_device *indev,
143 struct net_device *outdev,
144 struct sock *sk,
145 struct net *net,
146 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
147 {
148 p->hook = hook;
149 p->pf = pf;
150 p->in = indev;
151 p->out = outdev;
152 p->sk = sk;
153 p->net = net;
154 p->okfn = okfn;
155 }
156
157
158
159 struct nf_sockopt_ops {
160 struct list_head list;
161
162 u_int8_t pf;
163
164 /* Non-inclusive ranges: use 0/0/NULL to never get called. */
165 int set_optmin;
166 int set_optmax;
167 int (*set)(struct sock *sk, int optval, sockptr_t arg,
168 unsigned int len);
169 int get_optmin;
170 int get_optmax;
171 int (*get)(struct sock *sk, int optval, void __user *user, int *len);
172 /* Use the module struct to lock set/get code in place */
173 struct module *owner;
174 };
175
176 /* Function to register/unregister hook points. */
177 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
178 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
179 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
180 unsigned int n);
181 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
182 unsigned int n);
183
184 /* Functions to register get/setsockopt ranges (non-inclusive). You
185 need to check permissions yourself! */
186 int nf_register_sockopt(struct nf_sockopt_ops *reg);
187 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
188
189 #ifdef CONFIG_JUMP_LABEL
190 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
191 #endif
192
193 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
194 const struct nf_hook_entries *e, unsigned int i);
195
196 void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
197 const struct nf_hook_entries *e);
198 /**
199 * nf_hook - call a netfilter hook
200 *
201 * Returns 1 if the hook has allowed the packet to pass. The function
202 * okfn must be invoked by the caller in this case. Any other return
203 * value indicates the packet has been consumed by the hook.
204 */
nf_hook(u_int8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * indev,struct net_device * outdev,int (* okfn)(struct net *,struct sock *,struct sk_buff *))205 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
206 struct sock *sk, struct sk_buff *skb,
207 struct net_device *indev, struct net_device *outdev,
208 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
209 {
210 struct nf_hook_entries *hook_head = NULL;
211 int ret = 1;
212
213 #ifdef CONFIG_JUMP_LABEL
214 if (__builtin_constant_p(pf) &&
215 __builtin_constant_p(hook) &&
216 !static_key_false(&nf_hooks_needed[pf][hook]))
217 return 1;
218 #endif
219
220 rcu_read_lock();
221 switch (pf) {
222 case NFPROTO_IPV4:
223 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
224 break;
225 case NFPROTO_IPV6:
226 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
227 break;
228 case NFPROTO_ARP:
229 #ifdef CONFIG_NETFILTER_FAMILY_ARP
230 if (WARN_ON_ONCE(hook >= ARRAY_SIZE(net->nf.hooks_arp)))
231 break;
232 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
233 #endif
234 break;
235 case NFPROTO_BRIDGE:
236 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
237 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
238 #endif
239 break;
240 default:
241 WARN_ON_ONCE(1);
242 break;
243 }
244
245 if (hook_head) {
246 struct nf_hook_state state;
247
248 nf_hook_state_init(&state, hook, pf, indev, outdev,
249 sk, net, okfn);
250
251 ret = nf_hook_slow(skb, &state, hook_head, 0);
252 }
253 rcu_read_unlock();
254
255 return ret;
256 }
257
258 /* Activate hook; either okfn or kfree_skb called, unless a hook
259 returns NF_STOLEN (in which case, it's up to the hook to deal with
260 the consequences).
261
262 Returns -ERRNO if packet dropped. Zero means queued, stolen or
263 accepted.
264 */
265
266 /* RR:
267 > I don't want nf_hook to return anything because people might forget
268 > about async and trust the return value to mean "packet was ok".
269
270 AK:
271 Just document it clearly, then you can expect some sense from kernel
272 coders :)
273 */
274
275 static inline int
NF_HOOK_COND(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *),bool cond)276 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
277 struct sk_buff *skb, struct net_device *in, struct net_device *out,
278 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
279 bool cond)
280 {
281 int ret;
282
283 if (!cond ||
284 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
285 ret = okfn(net, sk, skb);
286 return ret;
287 }
288
289 static inline int
NF_HOOK(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *))290 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
291 struct net_device *in, struct net_device *out,
292 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
293 {
294 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
295 if (ret == 1)
296 ret = okfn(net, sk, skb);
297 return ret;
298 }
299
300 static inline void
NF_HOOK_LIST(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct list_head * head,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *))301 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
302 struct list_head *head, struct net_device *in, struct net_device *out,
303 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
304 {
305 struct nf_hook_entries *hook_head = NULL;
306
307 #ifdef CONFIG_JUMP_LABEL
308 if (__builtin_constant_p(pf) &&
309 __builtin_constant_p(hook) &&
310 !static_key_false(&nf_hooks_needed[pf][hook]))
311 return;
312 #endif
313
314 rcu_read_lock();
315 switch (pf) {
316 case NFPROTO_IPV4:
317 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
318 break;
319 case NFPROTO_IPV6:
320 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
321 break;
322 default:
323 WARN_ON_ONCE(1);
324 break;
325 }
326
327 if (hook_head) {
328 struct nf_hook_state state;
329
330 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
331
332 nf_hook_slow_list(head, &state, hook_head);
333 }
334 rcu_read_unlock();
335 }
336
337 /* Call setsockopt() */
338 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt,
339 unsigned int len);
340 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
341 int *len);
342
343 struct flowi;
344 struct nf_queue_entry;
345
346 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
347 unsigned int dataoff, u_int8_t protocol,
348 unsigned short family);
349
350 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
351 unsigned int dataoff, unsigned int len,
352 u_int8_t protocol, unsigned short family);
353 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
354 bool strict, unsigned short family);
355 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
356
357 #include <net/flow.h>
358
359 struct nf_conn;
360 enum nf_nat_manip_type;
361 struct nlattr;
362 enum ip_conntrack_dir;
363
364 struct nf_nat_hook {
365 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
366 const struct nlattr *attr);
367 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
368 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
369 enum nf_nat_manip_type mtype,
370 enum ip_conntrack_dir dir);
371 };
372
373 extern struct nf_nat_hook __rcu *nf_nat_hook;
374
375 static inline void
nf_nat_decode_session(struct sk_buff * skb,struct flowi * fl,u_int8_t family)376 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
377 {
378 #if IS_ENABLED(CONFIG_NF_NAT)
379 struct nf_nat_hook *nat_hook;
380
381 rcu_read_lock();
382 nat_hook = rcu_dereference(nf_nat_hook);
383 if (nat_hook && nat_hook->decode_session)
384 nat_hook->decode_session(skb, fl);
385 rcu_read_unlock();
386 #endif
387 }
388
389 #else /* !CONFIG_NETFILTER */
390 static inline int
NF_HOOK_COND(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *),bool cond)391 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
392 struct sk_buff *skb, struct net_device *in, struct net_device *out,
393 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
394 bool cond)
395 {
396 return okfn(net, sk, skb);
397 }
398
399 static inline int
NF_HOOK(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *))400 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
401 struct sk_buff *skb, struct net_device *in, struct net_device *out,
402 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
403 {
404 return okfn(net, sk, skb);
405 }
406
407 static inline void
NF_HOOK_LIST(uint8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct list_head * head,struct net_device * in,struct net_device * out,int (* okfn)(struct net *,struct sock *,struct sk_buff *))408 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
409 struct list_head *head, struct net_device *in, struct net_device *out,
410 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
411 {
412 /* nothing to do */
413 }
414
nf_hook(u_int8_t pf,unsigned int hook,struct net * net,struct sock * sk,struct sk_buff * skb,struct net_device * indev,struct net_device * outdev,int (* okfn)(struct net *,struct sock *,struct sk_buff *))415 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
416 struct sock *sk, struct sk_buff *skb,
417 struct net_device *indev, struct net_device *outdev,
418 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
419 {
420 return 1;
421 }
422 struct flowi;
423 static inline void
nf_nat_decode_session(struct sk_buff * skb,struct flowi * fl,u_int8_t family)424 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
425 {
426 }
427 #endif /*CONFIG_NETFILTER*/
428
429 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
430 #include <linux/netfilter/nf_conntrack_zones_common.h>
431
432 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
433 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
434 struct nf_conntrack_tuple;
435 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
436 const struct sk_buff *skb);
437 #else
nf_ct_attach(struct sk_buff * new,struct sk_buff * skb)438 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
439 struct nf_conntrack_tuple;
nf_ct_get_tuple_skb(struct nf_conntrack_tuple * dst_tuple,const struct sk_buff * skb)440 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
441 const struct sk_buff *skb)
442 {
443 return false;
444 }
445 #endif
446
447 struct nf_conn;
448 enum ip_conntrack_info;
449
450 struct nf_ct_hook {
451 int (*update)(struct net *net, struct sk_buff *skb);
452 void (*destroy)(struct nf_conntrack *);
453 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
454 const struct sk_buff *);
455 };
456 extern struct nf_ct_hook __rcu *nf_ct_hook;
457
458 struct nlattr;
459
460 struct nfnl_ct_hook {
461 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
462 enum ip_conntrack_info *ctinfo);
463 size_t (*build_size)(const struct nf_conn *ct);
464 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
465 enum ip_conntrack_info ctinfo,
466 u_int16_t ct_attr, u_int16_t ct_info_attr);
467 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
468 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
469 u32 portid, u32 report);
470 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
471 enum ip_conntrack_info ctinfo, s32 off);
472 };
473 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
474
475 /**
476 * nf_skb_duplicated - TEE target has sent a packet
477 *
478 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
479 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
480 *
481 * This is used by xtables TEE target to prevent the duplicated skb from
482 * being duplicated again.
483 */
484 DECLARE_PER_CPU(bool, nf_skb_duplicated);
485
486 #endif /*__LINUX_NETFILTER_H*/
487