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