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