netfilter.h 11.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
#ifndef __LINUX_NETFILTER_H
#define __LINUX_NETFILTER_H

#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/net.h>
#include <linux/if.h>
8
9
#include <linux/in.h>
#include <linux/in6.h>
Linus Torvalds's avatar
Linus Torvalds committed
10
11
#include <linux/wait.h>
#include <linux/list.h>
12
#include <linux/static_key.h>
13
#include <linux/netfilter_defs.h>
14
15
#include <linux/netdevice.h>
#include <net/net_namespace.h>
16

Linus Torvalds's avatar
Linus Torvalds committed
17
#ifdef CONFIG_NETFILTER
18
19
20
21
static inline int NF_DROP_GETERR(int verdict)
{
	return -(verdict >> NF_VERDICT_QBITS);
}
Linus Torvalds's avatar
Linus Torvalds committed
22

23
24
25
26
27
28
29
30
31
static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
				   const union nf_inet_addr *a2)
{
	return a1->all[0] == a2->all[0] &&
	       a1->all[1] == a2->all[1] &&
	       a1->all[2] == a2->all[2] &&
	       a1->all[3] == a2->all[3];
}

32
33
34
35
36
37
38
39
40
41
static inline void nf_inet_addr_mask(const union nf_inet_addr *a1,
				     union nf_inet_addr *result,
				     const union nf_inet_addr *mask)
{
	result->all[0] = a1->all[0] & mask->all[0];
	result->all[1] = a1->all[1] & mask->all[1];
	result->all[2] = a1->all[2] & mask->all[2];
	result->all[3] = a1->all[3] & mask->all[3];
}

42
int netfilter_init(void);
Linus Torvalds's avatar
Linus Torvalds committed
43
44
45

struct sk_buff;

46
struct nf_hook_ops;
47

48
49
struct sock;

50
51
52
53
54
55
struct nf_hook_state {
	unsigned int hook;
	int thresh;
	u_int8_t pf;
	struct net_device *in;
	struct net_device *out;
56
	struct sock *sk;
57
	struct net *net;
58
	struct list_head *hook_list;
59
	int (*okfn)(struct sock *, struct sk_buff *);
60
61
};

62
static inline void nf_hook_state_init(struct nf_hook_state *p,
63
				      struct list_head *hook_list,
64
65
66
67
				      unsigned int hook,
				      int thresh, u_int8_t pf,
				      struct net_device *indev,
				      struct net_device *outdev,
68
				      struct sock *sk,
69
				      struct net *net,
70
				      int (*okfn)(struct sock *, struct sk_buff *))
71
72
73
74
75
76
{
	p->hook = hook;
	p->thresh = thresh;
	p->pf = pf;
	p->in = indev;
	p->out = outdev;
77
	p->sk = sk;
78
	p->net = net;
79
	p->hook_list = hook_list;
80
81
82
	p->okfn = okfn;
}

83
typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
84
			       struct sk_buff *skb,
85
			       const struct nf_hook_state *state);
Linus Torvalds's avatar
Linus Torvalds committed
86

Eric Dumazet's avatar
Eric Dumazet committed
87
struct nf_hook_ops {
88
	struct list_head 	list;
Linus Torvalds's avatar
Linus Torvalds committed
89
90

	/* User fills in from here down. */
91
	nf_hookfn		*hook;
92
	struct net_device	*dev;
93
94
95
96
	struct module		*owner;
	void			*priv;
	u_int8_t		pf;
	unsigned int		hooknum;
Linus Torvalds's avatar
Linus Torvalds committed
97
	/* Hooks are ordered in ascending priority. */
98
	int			priority;
Linus Torvalds's avatar
Linus Torvalds committed
99
100
};

Eric Dumazet's avatar
Eric Dumazet committed
101
struct nf_sockopt_ops {
Linus Torvalds's avatar
Linus Torvalds committed
102
103
	struct list_head list;

104
	u_int8_t pf;
Linus Torvalds's avatar
Linus Torvalds committed
105
106
107
108
109

	/* Non-inclusive ranges: use 0/0/NULL to never get called. */
	int set_optmin;
	int set_optmax;
	int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
110
#ifdef CONFIG_COMPAT
111
112
	int (*compat_set)(struct sock *sk, int optval,
			void __user *user, unsigned int len);
113
#endif
Linus Torvalds's avatar
Linus Torvalds committed
114
115
116
	int get_optmin;
	int get_optmax;
	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
117
#ifdef CONFIG_COMPAT
118
119
	int (*compat_get)(struct sock *sk, int optval,
			void __user *user, int *len);
120
#endif
121
122
	/* Use the module struct to lock set/get code in place */
	struct module *owner;
Linus Torvalds's avatar
Linus Torvalds committed
123
124
125
};

/* Function to register/unregister hook points. */
126
127
128
129
130
131
132
int nf_register_net_hook(struct net *net, const struct nf_hook_ops *ops);
void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *ops);
int nf_register_net_hooks(struct net *net, const struct nf_hook_ops *reg,
			  unsigned int n);
void nf_unregister_net_hooks(struct net *net, const struct nf_hook_ops *reg,
			     unsigned int n);

Linus Torvalds's avatar
Linus Torvalds committed
133
134
int nf_register_hook(struct nf_hook_ops *reg);
void nf_unregister_hook(struct nf_hook_ops *reg);
135
136
int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
Linus Torvalds's avatar
Linus Torvalds committed
137
138
139
140
141
142

/* Functions to register get/setsockopt ranges (non-inclusive).  You
   need to check permissions yourself! */
int nf_register_sockopt(struct nf_sockopt_ops *reg);
void nf_unregister_sockopt(struct nf_sockopt_ops *reg);

143
#ifdef HAVE_JUMP_LABEL
144
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
145

146
static inline bool nf_hook_list_active(struct list_head *hook_list,
147
				       u_int8_t pf, unsigned int hook)
148
149
150
{
	if (__builtin_constant_p(pf) &&
	    __builtin_constant_p(hook))
151
		return static_key_false(&nf_hooks_needed[pf][hook]);
152

153
	return !list_empty(hook_list);
154
155
}
#else
156
static inline bool nf_hook_list_active(struct list_head *hook_list,
157
				       u_int8_t pf, unsigned int hook)
158
{
159
	return !list_empty(hook_list);
160
161
162
}
#endif

163
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
164
165
166

/**
 *	nf_hook_thresh - call a netfilter hook
167
 *
168
169
170
171
 *	Returns 1 if the hook has allowed the packet to pass.  The function
 *	okfn must be invoked by the caller in this case.  Any other return
 *	value indicates the packet has been consumed by the hook.
 */
172
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
173
				 struct net *net,
174
				 struct sock *sk,
175
				 struct sk_buff *skb,
176
177
				 struct net_device *indev,
				 struct net_device *outdev,
178
179
				 int (*okfn)(struct sock *, struct sk_buff *),
				 int thresh)
180
{
181
	struct list_head *hook_list = &net->nf.hooks[pf][hook];
182

183
	if (nf_hook_list_active(hook_list, pf, hook)) {
184
		struct nf_hook_state state;
185

186
		nf_hook_state_init(&state, hook_list, hook, thresh,
187
				   pf, indev, outdev, sk, net, okfn);
188
189
		return nf_hook_slow(skb, &state);
	}
190
	return 1;
191
192
}

193
194
195
static inline int 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,
196
			  int (*okfn)(struct sock *, struct sk_buff *))
197
{
198
	return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN);
199
}
Linus Torvalds's avatar
Linus Torvalds committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
                   
/* Activate hook; either okfn or kfree_skb called, unless a hook
   returns NF_STOLEN (in which case, it's up to the hook to deal with
   the consequences).

   Returns -ERRNO if packet dropped.  Zero means queued, stolen or
   accepted.
*/

/* RR:
   > I don't want nf_hook to return anything because people might forget
   > about async and trust the return value to mean "packet was ok".

   AK:
   Just document it clearly, then you can expect some sense from kernel
   coders :)
*/

218
static inline int
219
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
220
221
222
	       struct sk_buff *skb, struct net_device *in,
	       struct net_device *out,
	       int (*okfn)(struct sock *, struct sk_buff *), int thresh)
223
{
224
	int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh);
225
	if (ret == 1)
226
		ret = okfn(sk, skb);
227
228
	return ret;
}
229

230
static inline int
231
NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
232
233
	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
	     int (*okfn)(struct sock *, struct sk_buff *), bool cond)
234
{
235
236
237
	int ret;

	if (!cond ||
238
	    ((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1))
239
		ret = okfn(sk, skb);
240
241
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
242

243
static inline int
244
NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
245
	struct net_device *in, struct net_device *out,
246
	int (*okfn)(struct sock *, struct sk_buff *))
247
{
248
	return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN);
249
}
Linus Torvalds's avatar
Linus Torvalds committed
250
251

/* Call setsockopt() */
252
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
253
		  unsigned int len);
254
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
Linus Torvalds's avatar
Linus Torvalds committed
255
		  int *len);
256
#ifdef CONFIG_COMPAT
257
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
258
		char __user *opt, unsigned int len);
259
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
260
		char __user *opt, int *len);
261
#endif
262

263
264
265
/* Call this before modifying an existing packet: ensures it is
   modifiable and linear to the point you care about (writable_len).
   Returns true or false. */
266
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
267

268
struct flowi;
269
struct nf_queue_entry;
270

271
272
struct nf_afinfo {
	unsigned short	family;
273
	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
274
				    unsigned int dataoff, u_int8_t protocol);
275
276
277
278
279
	__sum16		(*checksum_partial)(struct sk_buff *skb,
					    unsigned int hook,
					    unsigned int dataoff,
					    unsigned int len,
					    u_int8_t protocol);
280
	int		(*route)(struct net *net, struct dst_entry **dst,
281
				 struct flowi *fl, bool strict);
282
	void		(*saveroute)(const struct sk_buff *skb,
283
				     struct nf_queue_entry *entry);
284
	int		(*reroute)(struct sk_buff *skb,
285
				   const struct nf_queue_entry *entry);
286
	int		route_key_size;
287
288
};

289
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
290
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
291
292
293
{
	return rcu_dereference(nf_afinfo[family]);
}
294

295
static inline __sum16
296
297
298
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
	    u_int8_t protocol, unsigned short family)
{
299
	const struct nf_afinfo *afinfo;
300
	__sum16 csum = 0;
301
302
303
304
305
306
307
308
309

	rcu_read_lock();
	afinfo = nf_get_afinfo(family);
	if (afinfo)
		csum = afinfo->checksum(skb, hook, dataoff, protocol);
	rcu_read_unlock();
	return csum;
}

310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
static inline __sum16
nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
		    unsigned int dataoff, unsigned int len,
		    u_int8_t protocol, unsigned short family)
{
	const struct nf_afinfo *afinfo;
	__sum16 csum = 0;

	rcu_read_lock();
	afinfo = nf_get_afinfo(family);
	if (afinfo)
		csum = afinfo->checksum_partial(skb, hook, dataoff, len,
						protocol);
	rcu_read_unlock();
	return csum;
}

327
328
int nf_register_afinfo(const struct nf_afinfo *afinfo);
void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
329

330
#include <net/flow.h>
331
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
332
333

static inline void
334
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
335
{
336
#ifdef CONFIG_NF_NAT_NEEDED
337
338
	void (*decodefn)(struct sk_buff *, struct flowi *);

339
340
341
342
343
	rcu_read_lock();
	decodefn = rcu_dereference(nf_nat_decode_session_hook);
	if (decodefn)
		decodefn(skb, fl);
	rcu_read_unlock();
344
345
346
#endif
}

Linus Torvalds's avatar
Linus Torvalds committed
347
#else /* !CONFIG_NETFILTER */
348
349
350
351
352
#define NF_HOOK(pf, hook, net, sk, skb, indev, outdev, okfn) (okfn)(sk, skb)
#define NF_HOOK_COND(pf, hook, net, sk, skb, indev, outdev, okfn, cond) (okfn)(sk, skb)
static inline int 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,
353
			  int (*okfn)(struct sock *, struct sk_buff *))
354
{
355
	return 1;
356
357
}
struct flowi;
358
static inline void
359
360
361
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
{
}
Linus Torvalds's avatar
Linus Torvalds committed
362
363
#endif /*CONFIG_NETFILTER*/

364
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
365
366
#include <linux/netfilter/nf_conntrack_zones_common.h>

367
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
368
void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
369
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
370
371

struct nf_conn;
372
enum ip_conntrack_info;
373
374
375
376
377
378
struct nlattr;

struct nfq_ct_hook {
	size_t (*build_size)(const struct nf_conn *ct);
	int (*build)(struct sk_buff *skb, struct nf_conn *ct);
	int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
379
380
	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
			     u32 portid, u32 report);
381
	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
382
			   enum ip_conntrack_info ctinfo, s32 off);
383
};
384
extern struct nfq_ct_hook __rcu *nfq_ct_hook;
385
386
387
388
#else
static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
#endif

389
390
391
392
393
394
395
396
397
398
399
/**
 * nf_skb_duplicated - TEE target has sent a packet
 *
 * When a xtables target sends a packet, the OUTPUT and POSTROUTING
 * hooks are traversed again, i.e. nft and xtables are invoked recursively.
 *
 * This is used by xtables TEE target to prevent the duplicated skb from
 * being duplicated again.
 */
DECLARE_PER_CPU(bool, nf_skb_duplicated);

Linus Torvalds's avatar
Linus Torvalds committed
400
#endif /*__LINUX_NETFILTER_H*/