netfilter.h 11.9 KB
Newer Older
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
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

L
Linus Torvalds 已提交
17
#ifdef CONFIG_NETFILTER
18 19 20 21
static inline int NF_DROP_GETERR(int verdict)
{
	return -(verdict >> NF_VERDICT_QBITS);
}
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
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 net *, 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 net *, 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(void *priv,
84
			       struct sk_buff *skb,
85
			       const struct nf_hook_state *state);
L
Linus Torvalds 已提交
86

E
Eric Dumazet 已提交
87
struct nf_hook_ops {
88
	struct list_head 	list;
L
Linus Torvalds 已提交
89 90

	/* User fills in from here down. */
91
	nf_hookfn		*hook;
92
	struct net_device	*dev;
93 94 95
	void			*priv;
	u_int8_t		pf;
	unsigned int		hooknum;
L
Linus Torvalds 已提交
96
	/* Hooks are ordered in ascending priority. */
97
	int			priority;
L
Linus Torvalds 已提交
98 99
};

E
Eric Dumazet 已提交
100
struct nf_sockopt_ops {
L
Linus Torvalds 已提交
101 102
	struct list_head list;

103
	u_int8_t pf;
L
Linus Torvalds 已提交
104 105 106 107 108

	/* 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);
109
#ifdef CONFIG_COMPAT
110 111
	int (*compat_set)(struct sock *sk, int optval,
			void __user *user, unsigned int len);
112
#endif
L
Linus Torvalds 已提交
113 114 115
	int get_optmin;
	int get_optmax;
	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
116
#ifdef CONFIG_COMPAT
117 118
	int (*compat_get)(struct sock *sk, int optval,
			void __user *user, int *len);
119
#endif
120 121
	/* Use the module struct to lock set/get code in place */
	struct module *owner;
L
Linus Torvalds 已提交
122 123 124
};

/* Function to register/unregister hook points. */
125 126 127 128 129 130 131
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);

L
Linus Torvalds 已提交
132 133
int nf_register_hook(struct nf_hook_ops *reg);
void nf_unregister_hook(struct nf_hook_ops *reg);
134 135
int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
L
Linus Torvalds 已提交
136 137 138 139 140 141

/* 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);

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

146
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
147 148 149

/**
 *	nf_hook_thresh - call a netfilter hook
150
 *
151 152 153 154
 *	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.
 */
155
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
156
				 struct net *net,
157
				 struct sock *sk,
158
				 struct sk_buff *skb,
159 160
				 struct net_device *indev,
				 struct net_device *outdev,
161
				 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
162
				 int thresh)
163
{
164 165 166 167 168 169 170 171 172 173
	struct list_head *hook_list;

#ifdef HAVE_JUMP_LABEL
	if (__builtin_constant_p(pf) &&
	    __builtin_constant_p(hook) &&
	    !static_key_false(&nf_hooks_needed[pf][hook]))
		return 1;
#endif

	hook_list = &net->nf.hooks[pf][hook];
174

175
	if (!list_empty(hook_list)) {
176
		struct nf_hook_state state;
177

178
		nf_hook_state_init(&state, hook_list, hook, thresh,
179
				   pf, indev, outdev, sk, net, okfn);
180 181
		return nf_hook_slow(skb, &state);
	}
182
	return 1;
183 184
}

185 186 187
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,
188
			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
189
{
190
	return nf_hook_thresh(pf, hook, net, sk, skb, indev, outdev, okfn, INT_MIN);
191
}
L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
                   
/* 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 :)
*/

210
static inline int
211
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
212 213
	       struct sk_buff *skb, struct net_device *in,
	       struct net_device *out,
214 215
	       int (*okfn)(struct net *, struct sock *, struct sk_buff *),
	       int thresh)
216
{
217
	int ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, thresh);
218
	if (ret == 1)
219
		ret = okfn(net, sk, skb);
220 221
	return ret;
}
222

223
static inline int
224
NF_HOOK_COND(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
225
	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
226 227
	     int (*okfn)(struct net *, struct sock *, struct sk_buff *),
	     bool cond)
228
{
229 230 231
	int ret;

	if (!cond ||
232
	    ((ret = nf_hook_thresh(pf, hook, net, sk, skb, in, out, okfn, INT_MIN)) == 1))
233
		ret = okfn(net, sk, skb);
234 235
	return ret;
}
L
Linus Torvalds 已提交
236

237
static inline int
238
NF_HOOK(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk, struct sk_buff *skb,
239
	struct net_device *in, struct net_device *out,
240
	int (*okfn)(struct net *, struct sock *, struct sk_buff *))
241
{
242
	return NF_HOOK_THRESH(pf, hook, net, sk, skb, in, out, okfn, INT_MIN);
243
}
L
Linus Torvalds 已提交
244 245

/* Call setsockopt() */
246
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
247
		  unsigned int len);
248
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
L
Linus Torvalds 已提交
249
		  int *len);
250
#ifdef CONFIG_COMPAT
251
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
252
		char __user *opt, unsigned int len);
253
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
254
		char __user *opt, int *len);
255
#endif
256

257 258 259
/* 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. */
260
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
261

262
struct flowi;
263
struct nf_queue_entry;
264

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

E
Eric Dumazet 已提交
283
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
284
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
285 286 287
{
	return rcu_dereference(nf_afinfo[family]);
}
288

289
static inline __sum16
290 291 292
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
	    u_int8_t protocol, unsigned short family)
{
293
	const struct nf_afinfo *afinfo;
294
	__sum16 csum = 0;
295 296 297 298 299 300 301 302 303

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

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
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;
}

321 322
int nf_register_afinfo(const struct nf_afinfo *afinfo);
void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
323

324
#include <net/flow.h>
325
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
326 327

static inline void
328
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
329
{
330
#ifdef CONFIG_NF_NAT_NEEDED
331 332
	void (*decodefn)(struct sk_buff *, struct flowi *);

333 334 335 336 337
	rcu_read_lock();
	decodefn = rcu_dereference(nf_nat_decode_session_hook);
	if (decodefn)
		decodefn(skb, fl);
	rcu_read_unlock();
338 339 340
#endif
}

L
Linus Torvalds 已提交
341
#else /* !CONFIG_NETFILTER */
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
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)
{
	return okfn(net, sk, skb);
}

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 *))
{
	return okfn(net, sk, skb);
}

359 360 361
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,
362
			  int (*okfn)(struct net *, struct sock *, struct sk_buff *))
363
{
364
	return 1;
365 366
}
struct flowi;
367
static inline void
368 369 370
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
{
}
L
Linus Torvalds 已提交
371 372
#endif /*CONFIG_NETFILTER*/

373
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
374 375
#include <linux/netfilter/nf_conntrack_zones_common.h>

376
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
377
void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
E
Eric Dumazet 已提交
378
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
379 380 381
#else
static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
#endif
382 383

struct nf_conn;
384
enum ip_conntrack_info;
385 386
struct nlattr;

387
struct nfnl_ct_hook {
388
	struct nf_conn *(*get_ct)(const struct sk_buff *skb,
389
				  enum ip_conntrack_info *ctinfo);
390
	size_t (*build_size)(const struct nf_conn *ct);
391 392 393
	int (*build)(struct sk_buff *skb, struct nf_conn *ct,
		     enum ip_conntrack_info ctinfo,
		     u_int16_t ct_attr, u_int16_t ct_info_attr);
394
	int (*parse)(const struct nlattr *attr, struct nf_conn *ct);
395 396
	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
			     u32 portid, u32 report);
397
	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
398
			   enum ip_conntrack_info ctinfo, s32 off);
399
};
400
extern struct nfnl_ct_hook __rcu *nfnl_ct_hook;
401

402 403 404 405 406 407 408 409 410 411 412
/**
 * 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);

L
Linus Torvalds 已提交
413
#endif /*__LINUX_NETFILTER_H*/