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 #if IS_ENABLED(CONFIG_DECNET)
244 case NFPROTO_DECNET:
245 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
246 break;
247 #endif
248 default:
249 WARN_ON_ONCE(1);
250 break;
251 }
252
253 if (hook_head) {
254 struct nf_hook_state state;
255
256 nf_hook_state_init(&state, hook, pf, indev, outdev,
257 sk, net, okfn);
258
259 ret = nf_hook_slow(skb, &state, hook_head, 0);
260 }
261 rcu_read_unlock();
262
263 return ret;
264 }
265
266 /* Activate hook; either okfn or kfree_skb called, unless a hook
267 returns NF_STOLEN (in which case, it's up to the hook to deal with
268 the consequences).
269
270 Returns -ERRNO if packet dropped. Zero means queued, stolen or
271 accepted.
272 */
273
274 /* RR:
275 > I don't want nf_hook to return anything because people might forget
276 > about async and trust the return value to mean "packet was ok".
277
278 AK:
279 Just document it clearly, then you can expect some sense from kernel
280 coders :)
281 */
282
283 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)284 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
285 struct sk_buff *skb, struct net_device *in, struct net_device *out,
286 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
287 bool cond)
288 {
289 int ret;
290
291 if (!cond ||
292 ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
293 ret = okfn(net, sk, skb);
294 return ret;
295 }
296
297 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 *))298 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
299 struct net_device *in, struct net_device *out,
300 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
301 {
302 int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
303 if (ret == 1)
304 ret = okfn(net, sk, skb);
305 return ret;
306 }
307
308 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 *))309 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
310 struct list_head *head, struct net_device *in, struct net_device *out,
311 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
312 {
313 struct nf_hook_entries *hook_head = NULL;
314
315 #ifdef CONFIG_JUMP_LABEL
316 if (__builtin_constant_p(pf) &&
317 __builtin_constant_p(hook) &&
318 !static_key_false(&nf_hooks_needed[pf][hook]))
319 return;
320 #endif
321
322 rcu_read_lock();
323 switch (pf) {
324 case NFPROTO_IPV4:
325 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
326 break;
327 case NFPROTO_IPV6:
328 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
329 break;
330 default:
331 WARN_ON_ONCE(1);
332 break;
333 }
334
335 if (hook_head) {
336 struct nf_hook_state state;
337
338 nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
339
340 nf_hook_slow_list(head, &state, hook_head);
341 }
342 rcu_read_unlock();
343 }
344
345 /* Call setsockopt() */
346 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, sockptr_t opt,
347 unsigned int len);
348 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
349 int *len);
350
351 struct flowi;
352 struct nf_queue_entry;
353
354 __sum16 nf_checksum(struct sk_buff *skb, unsigned int hook,
355 unsigned int dataoff, u_int8_t protocol,
356 unsigned short family);
357
358 __sum16 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
359 unsigned int dataoff, unsigned int len,
360 u_int8_t protocol, unsigned short family);
361 int nf_route(struct net *net, struct dst_entry **dst, struct flowi *fl,
362 bool strict, unsigned short family);
363 int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry);
364
365 #include <net/flow.h>
366
367 struct nf_conn;
368 enum nf_nat_manip_type;
369 struct nlattr;
370 enum ip_conntrack_dir;
371
372 struct nf_nat_hook {
373 int (*parse_nat_setup)(struct nf_conn *ct, enum nf_nat_manip_type manip,
374 const struct nlattr *attr);
375 void (*decode_session)(struct sk_buff *skb, struct flowi *fl);
376 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
377 enum nf_nat_manip_type mtype,
378 enum ip_conntrack_dir dir);
379
380 ANDROID_KABI_RESERVE(1);
381 };
382
383 extern struct nf_nat_hook __rcu *nf_nat_hook;
384
385 static inline void
nf_nat_decode_session(struct sk_buff * skb,struct flowi * fl,u_int8_t family)386 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
387 {
388 #if IS_ENABLED(CONFIG_NF_NAT)
389 struct nf_nat_hook *nat_hook;
390
391 rcu_read_lock();
392 nat_hook = rcu_dereference(nf_nat_hook);
393 if (nat_hook && nat_hook->decode_session)
394 nat_hook->decode_session(skb, fl);
395 rcu_read_unlock();
396 #endif
397 }
398
399 #else /* !CONFIG_NETFILTER */
400 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)401 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
402 struct sk_buff *skb, struct net_device *in, struct net_device *out,
403 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
404 bool cond)
405 {
406 return okfn(net, sk, skb);
407 }
408
409 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 *))410 NF_HOOK(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 {
414 return okfn(net, sk, skb);
415 }
416
417 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 *))418 NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
419 struct list_head *head, struct net_device *in, struct net_device *out,
420 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
421 {
422 /* nothing to do */
423 }
424
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 *))425 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
426 struct sock *sk, struct sk_buff *skb,
427 struct net_device *indev, struct net_device *outdev,
428 int (*okfn)(struct net *, struct sock *, struct sk_buff *))
429 {
430 return 1;
431 }
432 struct flowi;
433 static inline void
nf_nat_decode_session(struct sk_buff * skb,struct flowi * fl,u_int8_t family)434 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
435 {
436 }
437 #endif /*CONFIG_NETFILTER*/
438
439 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
440 #include <linux/netfilter/nf_conntrack_zones_common.h>
441
442 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
443 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
444 struct nf_conntrack_tuple;
445 bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
446 const struct sk_buff *skb);
447 #else
nf_ct_attach(struct sk_buff * new,struct sk_buff * skb)448 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
449 struct nf_conntrack_tuple;
nf_ct_get_tuple_skb(struct nf_conntrack_tuple * dst_tuple,const struct sk_buff * skb)450 static inline bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
451 const struct sk_buff *skb)
452 {
453 return false;
454 }
455 #endif
456
457 struct nf_conn;
458 enum ip_conntrack_info;
459
460 struct nf_ct_hook {
461 int (*update)(struct net *net, struct sk_buff *skb);
462 void (*destroy)(struct nf_conntrack *);
463 bool (*get_tuple_skb)(struct nf_conntrack_tuple *,
464 const struct sk_buff *);
465
466 ANDROID_KABI_RESERVE(1);
467 };
468 extern struct nf_ct_hook __rcu *nf_ct_hook;
469
470 struct nlattr;
471
472 struct nfnl_ct_hook {
473 struct nf_conn *(*get_ct)(const struct sk_buff *skb,
474 enum ip_conntrack_info *ctinfo);
475 size_t (*build_size)(const struct nf_conn *ct);
476 int (*build)(struct sk_buff *skb, struct nf_conn *ct,
477 enum ip_conntrack_info ctinfo,
478 u_int16_t ct_attr, u_int16_t ct_info_attr);
479 int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
480 int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
481 u32 portid, u32 report);
482 void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
483 enum ip_conntrack_info ctinfo, s32 off);
484
485 ANDROID_KABI_RESERVE(1);
486 };
487 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
488
489 /**
490 * nf_skb_duplicated - TEE target has sent a packet
491 *
492 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
493 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
494 *
495 * This is used by xtables TEE target to prevent the duplicated skb from
496 * being duplicated again.
497 */
498 DECLARE_PER_CPU(bool, nf_skb_duplicated);
499
500 #endif /*__LINUX_NETFILTER_H*/
501