xref: /openbmc/linux/include/linux/netfilter.h (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
1 #ifndef __LINUX_NETFILTER_H
2 #define __LINUX_NETFILTER_H
3 
4 #include <linux/init.h>
5 #include <linux/skbuff.h>
6 #include <linux/net.h>
7 #include <linux/if.h>
8 #include <linux/in.h>
9 #include <linux/in6.h>
10 #include <linux/wait.h>
11 #include <linux/list.h>
12 #include <linux/static_key.h>
13 #include <linux/netfilter_defs.h>
14 #include <linux/netdevice.h>
15 #include <net/net_namespace.h>
16 
17 #ifdef CONFIG_NETFILTER
18 static inline int NF_DROP_GETERR(int verdict)
19 {
20 	return -(verdict >> NF_VERDICT_QBITS);
21 }
22 
23 static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
24 				   const union nf_inet_addr *a2)
25 {
26 	return a1->all[0] == a2->all[0] &&
27 	       a1->all[1] == a2->all[1] &&
28 	       a1->all[2] == a2->all[2] &&
29 	       a1->all[3] == a2->all[3];
30 }
31 
32 static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
33 				     union nf_inet_addr *result,
34 				     const union nf_inet_addr *mask)
35 {
36 	result->all[0] = a1->all[0] & mask->all[0];
37 	result->all[1] = a1->all[1] & mask->all[1];
38 	result->all[2] = a1->all[2] & mask->all[2];
39 	result->all[3] = a1->all[3] & mask->all[3];
40 }
41 
42 int netfilter_init(void);
43 
44 struct sk_buff;
45 
46 struct nf_hook_ops;
47 
48 struct sock;
49 
50 struct nf_hook_state {
51 	unsigned int hook;
52 	u_int8_t pf;
53 	struct net_device *in;
54 	struct net_device *out;
55 	struct sock *sk;
56 	struct net *net;
57 	int (*okfn)(struct net *, struct sock *, struct sk_buff *);
58 };
59 
60 typedef unsigned int nf_hookfn(void *priv,
61 			       struct sk_buff *skb,
62 			       const struct nf_hook_state *state);
63 struct nf_hook_ops {
64 	/* User fills in from here down. */
65 	nf_hookfn		*hook;
66 	struct net_device	*dev;
67 	void			*priv;
68 	u_int8_t		pf;
69 	unsigned int		hooknum;
70 	/* Hooks are ordered in ascending priority. */
71 	int			priority;
72 };
73 
74 struct nf_hook_entry {
75 	nf_hookfn			*hook;
76 	void				*priv;
77 };
78 
79 struct nf_hook_entries {
80 	u16				num_hook_entries;
81 	/* padding */
82 	struct nf_hook_entry		hooks[];
83 
84 	/* trailer: pointers to original orig_ops of each hook.
85 	 *
86 	 * This is not part of struct nf_hook_entry since its only
87 	 * needed in slow path (hook register/unregister).
88 	 *
89 	 * const struct nf_hook_ops     *orig_ops[]
90 	 */
91 };
92 
93 static inline struct nf_hook_ops **nf_hook_entries_get_hook_ops(const struct nf_hook_entries *e)
94 {
95 	unsigned int n = e->num_hook_entries;
96 	const void *hook_end;
97 
98 	hook_end = &e->hooks[n]; /* this is *past* ->hooks[]! */
99 
100 	return (struct nf_hook_ops **)hook_end;
101 }
102 
103 static inline int
104 nf_hook_entry_hookfn(const struct nf_hook_entry *entry, struct sk_buff *skb,
105 		     struct nf_hook_state *state)
106 {
107 	return entry->hook(entry->priv, skb, state);
108 }
109 
110 static inline void nf_hook_state_init(struct nf_hook_state *p,
111 				      unsigned int hook,
112 				      u_int8_t pf,
113 				      struct net_device *indev,
114 				      struct net_device *outdev,
115 				      struct sock *sk,
116 				      struct net *net,
117 				      int (*okfn)(struct net *, struct sock *, struct sk_buff *))
118 {
119 	p->hook = hook;
120 	p->pf = pf;
121 	p->in = indev;
122 	p->out = outdev;
123 	p->sk = sk;
124 	p->net = net;
125 	p->okfn = okfn;
126 }
127 
128 
129 
130 struct nf_sockopt_ops {
131 	struct list_head list;
132 
133 	u_int8_t pf;
134 
135 	/* Non-inclusive ranges: use 0/0/NULL to never get called. */
136 	int set_optmin;
137 	int set_optmax;
138 	int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
139 #ifdef CONFIG_COMPAT
140 	int (*compat_set)(struct sock *sk, int optval,
141 			void __user *user, unsigned int len);
142 #endif
143 	int get_optmin;
144 	int get_optmax;
145 	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
146 #ifdef CONFIG_COMPAT
147 	int (*compat_get)(struct sock *sk, int optval,
148 			void __user *user, int *len);
149 #endif
150 	/* Use the module struct to lock set/get code in place */
151 	struct module *owner;
152 };
153 
154 /* Function to register/unregister hook points. */
155 int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
156 void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
157 int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
158 			  unsigned int n);
159 void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
160 			     unsigned int n);
161 
162 /* Functions to register get/setsockopt ranges (non-inclusive).  You
163    need to check permissions yourself! */
164 int nf_register_sockopt(struct nf_sockopt_ops *reg);
165 void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
166 
167 #ifdef HAVE_JUMP_LABEL
168 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
169 #endif
170 
171 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
172 		 const struct nf_hook_entries *e, unsigned int i);
173 
174 /**
175  *	nf_hook - call a netfilter hook
176  *
177  *	Returns 1 if the hook has allowed the packet to pass.  The function
178  *	okfn must be invoked by the caller in this case.  Any other return
179  *	value indicates the packet has been consumed by the hook.
180  */
181 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
182 			  struct sock *sk, struct sk_buff *skb,
183 			  struct net_device *indev, struct net_device *outdev,
184 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
185 {
186 	struct nf_hook_entries *hook_head;
187 	int ret = 1;
188 
189 #ifdef HAVE_JUMP_LABEL
190 	if (__builtin_constant_p(pf) &&
191 	    __builtin_constant_p(hook) &&
192 	    !static_key_false(&nf_hooks_needed[pf][hook]))
193 		return 1;
194 #endif
195 
196 	rcu_read_lock();
197 	hook_head = rcu_dereference(net->nf.hooks[pf][hook]);
198 	if (hook_head) {
199 		struct nf_hook_state state;
200 
201 		nf_hook_state_init(&state, hook, pf, indev, outdev,
202 				   sk, net, okfn);
203 
204 		ret = nf_hook_slow(skb, &state, hook_head, 0);
205 	}
206 	rcu_read_unlock();
207 
208 	return ret;
209 }
210 
211 /* Activate hook; either okfn or kfree_skb called, unless a hook
212    returns NF_STOLEN (in which case, it's up to the hook to deal with
213    the consequences).
214 
215    Returns -ERRNO if packet dropped.  Zero means queued, stolen or
216    accepted.
217 */
218 
219 /* RR:
220    > I don't want nf_hook to return anything because people might forget
221    > about async and trust the return value to mean "packet was ok".
222 
223    AK:
224    Just document it clearly, then you can expect some sense from kernel
225    coders :)
226 */
227 
228 static inline int
229 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
230 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
231 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
232 	     bool cond)
233 {
234 	int ret;
235 
236 	if (!cond ||
237 	    ((ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn)) == 1))
238 		ret = okfn(net, sk, skb);
239 	return ret;
240 }
241 
242 static inline int
243 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
244 	struct net_device *in, struct net_device *out,
245 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
246 {
247 	int ret = nf_hook(pf, hook, net, sk, skb, in, out, okfn);
248 	if (ret == 1)
249 		ret = okfn(net, sk, skb);
250 	return ret;
251 }
252 
253 /* Call setsockopt() */
254 int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
255 		  unsigned int len);
256 int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
257 		  int *len);
258 #ifdef CONFIG_COMPAT
259 int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
260 		char __user *opt, unsigned int len);
261 int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
262 		char __user *opt, int *len);
263 #endif
264 
265 /* Call this before modifying an existing packet: ensures it is
266    modifiable and linear to the point you care about (writable_len).
267    Returns true or false. */
268 int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
269 
270 struct flowi;
271 struct nf_queue_entry;
272 
273 struct nf_afinfo {
274 	unsigned short	family;
275 	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
276 				    unsigned int dataoff, u_int8_t protocol);
277 	__sum16		(*checksum_partial)(struct sk_buff *skb,
278 					    unsigned int hook,
279 					    unsigned int dataoff,
280 					    unsigned int len,
281 					    u_int8_t protocol);
282 	int		(*route)(struct net *net, struct dst_entry **dst,
283 				 struct flowi *fl, bool strict);
284 	void		(*saveroute)(const struct sk_buff *skb,
285 				     struct nf_queue_entry *entry);
286 	int		(*reroute)(struct net *net, struct sk_buff *skb,
287 				   const struct nf_queue_entry *entry);
288 	int		route_key_size;
289 };
290 
291 extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
292 static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
293 {
294 	return rcu_dereference(nf_afinfo[family]);
295 }
296 
297 static inline __sum16
298 nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
299 	    u_int8_t protocol, unsigned short family)
300 {
301 	const struct nf_afinfo *afinfo;
302 	__sum16 csum = 0;
303 
304 	rcu_read_lock();
305 	afinfo = nf_get_afinfo(family);
306 	if (afinfo)
307 		csum = afinfo->checksum(skb, hook, dataoff, protocol);
308 	rcu_read_unlock();
309 	return csum;
310 }
311 
312 static inline __sum16
313 nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
314 		    unsigned int dataoff, unsigned int len,
315 		    u_int8_t protocol, unsigned short family)
316 {
317 	const struct nf_afinfo *afinfo;
318 	__sum16 csum = 0;
319 
320 	rcu_read_lock();
321 	afinfo = nf_get_afinfo(family);
322 	if (afinfo)
323 		csum = afinfo->checksum_partial(skb, hook, dataoff, len,
324 						protocol);
325 	rcu_read_unlock();
326 	return csum;
327 }
328 
329 int nf_register_afinfo(const struct nf_afinfo *afinfo);
330 void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
331 
332 #include <net/flow.h>
333 extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
334 
335 static inline void
336 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
337 {
338 #ifdef CONFIG_NF_NAT_NEEDED
339 	void (*decodefn)(struct sk_buff *, struct flowi *);
340 
341 	rcu_read_lock();
342 	decodefn = rcu_dereference(nf_nat_decode_session_hook);
343 	if (decodefn)
344 		decodefn(skb, fl);
345 	rcu_read_unlock();
346 #endif
347 }
348 
349 #else /* !CONFIG_NETFILTER */
350 static inline int
351 NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
352 	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
353 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
354 	     bool cond)
355 {
356 	return okfn(net, sk, skb);
357 }
358 
359 static inline int
360 NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
361 	struct sk_buff *skb, struct net_device *in, struct net_device *out,
362 	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
363 {
364 	return okfn(net, sk, skb);
365 }
366 
367 static inline int nf_hook(u_int8_t pf, unsigned int hook, struct net *net,
368 			  struct sock *sk, struct sk_buff *skb,
369 			  struct net_device *indev, struct net_device *outdev,
370 			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
371 {
372 	return 1;
373 }
374 struct flowi;
375 static inline void
376 nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
377 {
378 }
379 #endif /*CONFIG_NETFILTER*/
380 
381 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
382 #include <linux/netfilter/nf_conntrack_zones_common.h>
383 
384 extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
385 void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
386 extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
387 #else
388 static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
389 #endif
390 
391 struct nf_conn;
392 enum ip_conntrack_info;
393 struct nlattr;
394 
395 struct nfnl_ct_hook {
396 	struct nf_conn *(*get_ct)(const struct sk_buff *skb,
397 				  enum ip_conntrack_info *ctinfo);
398 	size_t (*build_size)(const struct nf_conn *ct);
399 	int (*build)(struct sk_buff *skb, struct nf_conn *ct,
400 		     enum ip_conntrack_info ctinfo,
401 		     u_int16_t ct_attr, u_int16_t ct_info_attr);
402 	int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
403 	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
404 			     u32 portid, u32 report);
405 	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
406 			   enum ip_conntrack_info ctinfo, s32 off);
407 };
408 extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
409 
410 /**
411  * nf_skb_duplicated - TEE target has sent a packet
412  *
413  * When a xtables target sends a packet, the OUTPUT and POSTROUTING
414  * hooks are traversed again, i.e. nft and xtables are invoked recursively.
415  *
416  * This is used by xtables TEE target to prevent the duplicated skb from
417  * being duplicated again.
418  */
419 DECLARE_PER_CPU(bool, nf_skb_duplicated);
420 
421 #endif /*__LINUX_NETFILTER_H*/
422