netfilter.h 10.0 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 50 51 52 53 54 55 56

struct nf_hook_state {
	unsigned int hook;
	int thresh;
	u_int8_t pf;
	struct net_device *in;
	struct net_device *out;
	int (*okfn)(struct sk_buff *);
};

57
typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
58
			       struct sk_buff *skb,
L
Linus Torvalds 已提交
59 60 61 62
			       const struct net_device *in,
			       const struct net_device *out,
			       int (*okfn)(struct sk_buff *));

E
Eric Dumazet 已提交
63
struct nf_hook_ops {
L
Linus Torvalds 已提交
64 65 66
	struct list_head list;

	/* User fills in from here down. */
P
Patrick McHardy 已提交
67 68 69 70 71
	nf_hookfn	*hook;
	struct module	*owner;
	void		*priv;
	u_int8_t	pf;
	unsigned int	hooknum;
L
Linus Torvalds 已提交
72
	/* Hooks are ordered in ascending priority. */
P
Patrick McHardy 已提交
73
	int		priority;
L
Linus Torvalds 已提交
74 75
};

E
Eric Dumazet 已提交
76
struct nf_sockopt_ops {
L
Linus Torvalds 已提交
77 78
	struct list_head list;

79
	u_int8_t pf;
L
Linus Torvalds 已提交
80 81 82 83 84

	/* 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);
85
#ifdef CONFIG_COMPAT
86 87
	int (*compat_set)(struct sock *sk, int optval,
			void __user *user, unsigned int len);
88
#endif
L
Linus Torvalds 已提交
89 90 91
	int get_optmin;
	int get_optmax;
	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
92
#ifdef CONFIG_COMPAT
93 94
	int (*compat_get)(struct sock *sk, int optval,
			void __user *user, int *len);
95
#endif
96 97
	/* Use the module struct to lock set/get code in place */
	struct module *owner;
L
Linus Torvalds 已提交
98 99 100 101 102
};

/* Function to register/unregister hook points. */
int nf_register_hook(struct nf_hook_ops *reg);
void nf_unregister_hook(struct nf_hook_ops *reg);
103 104
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 已提交
105 106 107 108 109 110

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

111
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
L
Linus Torvalds 已提交
112

113
#ifdef HAVE_JUMP_LABEL
114
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
115

116 117 118 119
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
{
	if (__builtin_constant_p(pf) &&
	    __builtin_constant_p(hook))
120
		return static_key_false(&nf_hooks_needed[pf][hook]);
121 122 123 124 125 126 127 128 129 130

	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

131
int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
132 133 134 135 136 137 138 139

/**
 *	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.
 */
140
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
141
				 struct sk_buff *skb,
142 143
				 struct net_device *indev,
				 struct net_device *outdev,
144
				 int (*okfn)(struct sk_buff *), int thresh)
145
{
146 147 148 149 150 151 152 153 154 155 156 157
	if (nf_hooks_active(pf, hook)) {
		struct nf_hook_state state = {
			.hook = hook,
			.thresh = thresh,
			.pf = pf,
			.in = indev,
			.out = outdev,
			.okfn = okfn
		};

		return nf_hook_slow(skb, &state);
	}
158
	return 1;
159 160
}

161
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
162 163 164
			  struct net_device *indev, struct net_device *outdev,
			  int (*okfn)(struct sk_buff *))
{
165
	return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
166
}
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                   
/* 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 :)
*/

185 186 187 188 189 190 191 192 193 194
static inline int
NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
	       struct net_device *in, struct net_device *out,
	       int (*okfn)(struct sk_buff *), int thresh)
{
	int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
	if (ret == 1)
		ret = okfn(skb);
	return ret;
}
195

196 197 198 199 200
static inline int
NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
	     struct net_device *in, struct net_device *out,
	     int (*okfn)(struct sk_buff *), bool cond)
{
201 202 203
	int ret;

	if (!cond ||
204
	    ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
205 206 207
		ret = okfn(skb);
	return ret;
}
L
Linus Torvalds 已提交
208

209 210 211 212 213 214 215
static inline int
NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
	struct net_device *in, struct net_device *out,
	int (*okfn)(struct sk_buff *))
{
	return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
}
L
Linus Torvalds 已提交
216 217

/* Call setsockopt() */
218
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
219
		  unsigned int len);
220
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
L
Linus Torvalds 已提交
221
		  int *len);
222
#ifdef CONFIG_COMPAT
223
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
224
		char __user *opt, unsigned int len);
225
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
226
		char __user *opt, int *len);
227
#endif
228

229 230 231
/* 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. */
232
int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
233

234
struct flowi;
235
struct nf_queue_entry;
236

237 238
struct nf_afinfo {
	unsigned short	family;
239
	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
240
				    unsigned int dataoff, u_int8_t protocol);
241 242 243 244 245
	__sum16		(*checksum_partial)(struct sk_buff *skb,
					    unsigned int hook,
					    unsigned int dataoff,
					    unsigned int len,
					    u_int8_t protocol);
246
	int		(*route)(struct net *net, struct dst_entry **dst,
247
				 struct flowi *fl, bool strict);
248
	void		(*saveroute)(const struct sk_buff *skb,
249
				     struct nf_queue_entry *entry);
250
	int		(*reroute)(struct sk_buff *skb,
251
				   const struct nf_queue_entry *entry);
252
	int		route_key_size;
253 254
};

E
Eric Dumazet 已提交
255
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
256
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
257 258 259
{
	return rcu_dereference(nf_afinfo[family]);
}
260

261
static inline __sum16
262 263 264
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
	    u_int8_t protocol, unsigned short family)
{
265
	const struct nf_afinfo *afinfo;
266
	__sum16 csum = 0;
267 268 269 270 271 272 273 274 275

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

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
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;
}

293 294
int nf_register_afinfo(const struct nf_afinfo *afinfo);
void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
295

296
#include <net/flow.h>
297
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
298 299

static inline void
300
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
301
{
302
#ifdef CONFIG_NF_NAT_NEEDED
303 304
	void (*decodefn)(struct sk_buff *, struct flowi *);

305 306 307 308 309
	rcu_read_lock();
	decodefn = rcu_dereference(nf_nat_decode_session_hook);
	if (decodefn)
		decodefn(skb, fl);
	rcu_read_unlock();
310 311 312
#endif
}

L
Linus Torvalds 已提交
313 314
#else /* !CONFIG_NETFILTER */
#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
315
#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
316
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
317
				 struct sk_buff *skb,
318 319
				 struct net_device *indev,
				 struct net_device *outdev,
320
				 int (*okfn)(struct sk_buff *), int thresh)
321
{
322
	return okfn(skb);
323
}
324
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
325 326 327
			  struct net_device *indev, struct net_device *outdev,
			  int (*okfn)(struct sk_buff *))
{
328
	return 1;
329 330
}
struct flowi;
331
static inline void
332 333 334
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
{
}
L
Linus Torvalds 已提交
335 336
#endif /*CONFIG_NETFILTER*/

337
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
338
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
339
void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
E
Eric Dumazet 已提交
340
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
341 342

struct nf_conn;
343
enum ip_conntrack_info;
344 345 346 347 348 349
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);
350 351
	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
			     u32 portid, u32 report);
352
	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
353
			   enum ip_conntrack_info ctinfo, s32 off);
354
};
355
extern struct nfq_ct_hook __rcu *nfq_ct_hook;
356 357 358 359
#else
static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
#endif

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