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