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