netfilter.h 10.7 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 <uapi/linux/netfilter.h>
L
Linus Torvalds 已提交
14
#ifdef CONFIG_NETFILTER
15 16 17 18
static inline int NF_DROP_GETERR(int verdict)
{
	return -(verdict >> NF_VERDICT_QBITS);
}
L
Linus Torvalds 已提交
19

20 21 22 23 24 25 26 27 28
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];
}

29 30 31 32 33 34 35 36 37 38
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];
}

39
int netfilter_init(void);
L
Linus Torvalds 已提交
40 41 42 43 44 45

/* Largest hook number + 1 */
#define NF_MAX_HOOKS 8

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 list_head *hook_list;
58
	int (*okfn)(struct sock *, struct sk_buff *);
59 60
};

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

80
typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
81
			       struct sk_buff *skb,
82
			       const struct nf_hook_state *state);
L
Linus Torvalds 已提交
83

E
Eric Dumazet 已提交
84
struct nf_hook_ops {
85
	struct list_head 	list;
L
Linus Torvalds 已提交
86 87

	/* User fills in from here down. */
88 89 90 91 92
	nf_hookfn		*hook;
	struct module		*owner;
	void			*priv;
	u_int8_t		pf;
	unsigned int		hooknum;
L
Linus Torvalds 已提交
93
	/* Hooks are ordered in ascending priority. */
94
	int			priority;
L
Linus Torvalds 已提交
95 96
};

E
Eric Dumazet 已提交
97
struct nf_sockopt_ops {
L
Linus Torvalds 已提交
98 99
	struct list_head list;

100
	u_int8_t pf;
L
Linus Torvalds 已提交
101 102 103 104 105

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

/* Function to register/unregister hook points. */
int nf_register_hook(struct nf_hook_ops *reg);
void nf_unregister_hook(struct nf_hook_ops *reg);
124 125
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 已提交
126 127 128 129 130 131

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

132
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
L
Linus Torvalds 已提交
133

134
#ifdef HAVE_JUMP_LABEL
135
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
136

137 138 139 140
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
{
	if (__builtin_constant_p(pf) &&
	    __builtin_constant_p(hook))
141
		return static_key_false(&nf_hooks_needed[pf][hook]);
142 143 144 145 146 147 148 149 150 151

	return !list_empty(&nf_hooks[pf][hook]);
}
#else
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
{
	return !list_empty(&nf_hooks[pf][hook]);
}
#endif

152
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
153 154 155 156 157 158 159 160

/**
 *	nf_hook_thresh - call a netfilter hook
 *	
 *	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.
 */
161
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
162
				 struct sock *sk,
163
				 struct sk_buff *skb,
164 165
				 struct net_device *indev,
				 struct net_device *outdev,
166 167
				 int (*okfn)(struct sock *, struct sk_buff *),
				 int thresh)
168
{
169
	if (nf_hooks_active(pf, hook)) {
170
		struct nf_hook_state state;
171

172 173
		nf_hook_state_init(&state, &nf_hooks[pf][hook], hook, thresh,
				   pf, indev, outdev, sk, okfn);
174 175
		return nf_hook_slow(skb, &state);
	}
176
	return 1;
177 178
}

179 180 181 182
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
			  struct sk_buff *skb, struct net_device *indev,
			  struct net_device *outdev,
			  int (*okfn)(struct sock *, struct sk_buff *))
183
{
184
	return nf_hook_thresh(pf, hook, sk, skb, indev, outdev, okfn, INT_MIN);
185
}
L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                   
/* 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 :)
*/

204
static inline int
205 206 207 208
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sock *sk,
	       struct sk_buff *skb, struct net_device *in,
	       struct net_device *out,
	       int (*okfn)(struct sock *, struct sk_buff *), int thresh)
209
{
210
	int ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, thresh);
211
	if (ret == 1)
212
		ret = okfn(sk, skb);
213 214
	return ret;
}
215

216
static inline int
217 218 219
NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sock *sk,
	     struct sk_buff *skb, struct net_device *in, struct net_device *out,
	     int (*okfn)(struct sock *, struct sk_buff *), bool cond)
220
{
221 222 223
	int ret;

	if (!cond ||
224 225
	    ((ret = nf_hook_thresh(pf, hook, sk, skb, in, out, okfn, INT_MIN)) == 1))
		ret = okfn(sk, skb);
226 227
	return ret;
}
L
Linus Torvalds 已提交
228

229
static inline int
230
NF_HOOK(uint8_t pf, unsigned int hook, struct sock *sk, struct sk_buff *skb,
231
	struct net_device *in, struct net_device *out,
232
	int (*okfn)(struct sock *, struct sk_buff *))
233
{
234
	return NF_HOOK_THRESH(pf, hook, sk, skb, in, out, okfn, INT_MIN);
235
}
L
Linus Torvalds 已提交
236 237

/* Call setsockopt() */
238
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
239
		  unsigned int len);
240
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
L
Linus Torvalds 已提交
241
		  int *len);
242
#ifdef CONFIG_COMPAT
243
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
244
		char __user *opt, unsigned int len);
245
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
246
		char __user *opt, int *len);
247
#endif
248

249 250 251
/* 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. */
252
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
253

254
struct flowi;
255
struct nf_queue_entry;
256

257 258
struct nf_afinfo {
	unsigned short	family;
259
	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
260
				    unsigned int dataoff, u_int8_t protocol);
261 262 263 264 265
	__sum16		(*checksum_partial)(struct sk_buff *skb,
					    unsigned int hook,
					    unsigned int dataoff,
					    unsigned int len,
					    u_int8_t protocol);
266
	int		(*route)(struct net *net, struct dst_entry **dst,
267
				 struct flowi *fl, bool strict);
268
	void		(*saveroute)(const struct sk_buff *skb,
269
				     struct nf_queue_entry *entry);
270
	int		(*reroute)(struct sk_buff *skb,
271
				   const struct nf_queue_entry *entry);
272
	int		route_key_size;
273 274
};

E
Eric Dumazet 已提交
275
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
276
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
277 278 279
{
	return rcu_dereference(nf_afinfo[family]);
}
280

281
static inline __sum16
282 283 284
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
	    u_int8_t protocol, unsigned short family)
{
285
	const struct nf_afinfo *afinfo;
286
	__sum16 csum = 0;
287 288 289 290 291 292 293 294 295

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

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
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;
}

313 314
int nf_register_afinfo(const struct nf_afinfo *afinfo);
void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
315

316
#include <net/flow.h>
317
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
318 319

static inline void
320
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
321
{
322
#ifdef CONFIG_NF_NAT_NEEDED
323 324
	void (*decodefn)(struct sk_buff *, struct flowi *);

325 326 327 328 329
	rcu_read_lock();
	decodefn = rcu_dereference(nf_nat_decode_session_hook);
	if (decodefn)
		decodefn(skb, fl);
	rcu_read_unlock();
330 331 332
#endif
}

L
Linus Torvalds 已提交
333
#else /* !CONFIG_NETFILTER */
334 335
#define NF_HOOK(pf, hook, sk, skb, indev, outdev, okfn) (okfn)(sk, skb)
#define NF_HOOK_COND(pf, hook, sk, skb, indev, outdev, okfn, cond) (okfn)(sk, skb)
336
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
337
				 struct sock *sk,
338
				 struct sk_buff *skb,
339 340
				 struct net_device *indev,
				 struct net_device *outdev,
341
				 int (*okfn)(struct sock *sk, struct sk_buff *), int thresh)
342
{
343
	return okfn(sk, skb);
344
}
345 346 347 348
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sock *sk,
			  struct sk_buff *skb, struct net_device *indev,
			  struct net_device *outdev,
			  int (*okfn)(struct sock *, struct sk_buff *))
349
{
350
	return 1;
351 352
}
struct flowi;
353
static inline void
354 355 356
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
{
}
L
Linus Torvalds 已提交
357 358
#endif /*CONFIG_NETFILTER*/

359
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
360
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
361
void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
E
Eric Dumazet 已提交
362
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
363 364

struct nf_conn;
365
enum ip_conntrack_info;
366 367 368 369 370 371
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);
372 373
	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
			     u32 portid, u32 report);
374
	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
375
			   enum ip_conntrack_info ctinfo, s32 off);
376
};
377
extern struct nfq_ct_hook __rcu *nfq_ct_hook;
378 379 380 381
#else
static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
#endif

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