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

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

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

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

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

struct sk_buff;

typedef unsigned int nf_hookfn(unsigned int hooknum,
46
			       struct sk_buff *skb,
L
Linus Torvalds 已提交
47 48 49 50
			       const struct net_device *in,
			       const struct net_device *out,
			       int (*okfn)(struct sk_buff *));

E
Eric Dumazet 已提交
51
struct nf_hook_ops {
L
Linus Torvalds 已提交
52 53 54 55 56
	struct list_head list;

	/* User fills in from here down. */
	nf_hookfn *hook;
	struct module *owner;
57 58
	u_int8_t pf;
	unsigned int hooknum;
L
Linus Torvalds 已提交
59 60 61 62
	/* Hooks are ordered in ascending priority. */
	int priority;
};

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

66
	u_int8_t pf;
L
Linus Torvalds 已提交
67 68 69 70 71

	/* 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);
72
#ifdef CONFIG_COMPAT
73 74
	int (*compat_set)(struct sock *sk, int optval,
			void __user *user, unsigned int len);
75
#endif
L
Linus Torvalds 已提交
76 77 78
	int get_optmin;
	int get_optmax;
	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
79
#ifdef CONFIG_COMPAT
80 81
	int (*compat_get)(struct sock *sk, int optval,
			void __user *user, int *len);
82
#endif
83 84
	/* Use the module struct to lock set/get code in place */
	struct module *owner;
L
Linus Torvalds 已提交
85 86 87 88 89
};

/* Function to register/unregister hook points. */
int nf_register_hook(struct nf_hook_ops *reg);
void nf_unregister_hook(struct nf_hook_ops *reg);
90 91
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 已提交
92 93 94 95 96 97

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

98
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
L
Linus Torvalds 已提交
99

100
#if defined(CONFIG_JUMP_LABEL)
101 102
#include <linux/static_key.h>
extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
103 104 105 106
static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
{
	if (__builtin_constant_p(pf) &&
	    __builtin_constant_p(hook))
107
		return static_key_false(&nf_hooks_needed[pf][hook]);
108 109 110 111 112 113 114 115 116 117

	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

118
int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
119 120 121 122 123 124 125 126 127 128
		 struct net_device *indev, struct net_device *outdev,
		 int (*okfn)(struct sk_buff *), int thresh);

/**
 *	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.
 */
129
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
130
				 struct sk_buff *skb,
131 132
				 struct net_device *indev,
				 struct net_device *outdev,
133
				 int (*okfn)(struct sk_buff *), int thresh)
134
{
135 136 137
	if (nf_hooks_active(pf, hook))
		return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
	return 1;
138 139
}

140
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
141 142 143
			  struct net_device *indev, struct net_device *outdev,
			  int (*okfn)(struct sk_buff *))
{
144
	return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
145
}
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                   
/* 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 :)
*/

164 165 166 167 168 169 170 171 172 173
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;
}
174

175 176 177 178 179
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)
{
180 181 182
	int ret;

	if (!cond ||
183
	    ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
184 185 186
		ret = okfn(skb);
	return ret;
}
L
Linus Torvalds 已提交
187

188 189 190 191 192 193 194
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 已提交
195 196

/* Call setsockopt() */
197
int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
198
		  unsigned int len);
199
int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
L
Linus Torvalds 已提交
200
		  int *len);
201
#ifdef CONFIG_COMPAT
202
int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
203
		char __user *opt, unsigned int len);
204
int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
205
		char __user *opt, int *len);
206
#endif
207

208 209 210
/* 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. */
211
extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
212

213
struct flowi;
214
struct nf_queue_entry;
215

216 217
struct nf_afinfo {
	unsigned short	family;
218
	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
219
				    unsigned int dataoff, u_int8_t protocol);
220 221 222 223 224
	__sum16		(*checksum_partial)(struct sk_buff *skb,
					    unsigned int hook,
					    unsigned int dataoff,
					    unsigned int len,
					    u_int8_t protocol);
225
	int		(*route)(struct net *net, struct dst_entry **dst,
226
				 struct flowi *fl, bool strict);
227
	void		(*saveroute)(const struct sk_buff *skb,
228
				     struct nf_queue_entry *entry);
229
	int		(*reroute)(struct sk_buff *skb,
230
				   const struct nf_queue_entry *entry);
231
	int		route_key_size;
232 233
};

E
Eric Dumazet 已提交
234
extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
235
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
236 237 238
{
	return rcu_dereference(nf_afinfo[family]);
}
239

240
static inline __sum16
241 242 243
nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
	    u_int8_t protocol, unsigned short family)
{
244
	const struct nf_afinfo *afinfo;
245
	__sum16 csum = 0;
246 247 248 249 250 251 252 253 254

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

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
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;
}

272 273
extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
274

275
#include <net/flow.h>
276
extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
277 278

static inline void
279
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
280
{
281
#ifdef CONFIG_NF_NAT_NEEDED
282 283
	void (*decodefn)(struct sk_buff *, struct flowi *);

284 285 286 287 288
	rcu_read_lock();
	decodefn = rcu_dereference(nf_nat_decode_session_hook);
	if (decodefn)
		decodefn(skb, fl);
	rcu_read_unlock();
289 290 291
#endif
}

L
Linus Torvalds 已提交
292 293
#else /* !CONFIG_NETFILTER */
#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
294
#define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
295
static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
296
				 struct sk_buff *skb,
297 298
				 struct net_device *indev,
				 struct net_device *outdev,
299
				 int (*okfn)(struct sk_buff *), int thresh)
300
{
301
	return okfn(skb);
302
}
303
static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
304 305 306
			  struct net_device *indev, struct net_device *outdev,
			  int (*okfn)(struct sk_buff *))
{
307
	return 1;
308 309
}
struct flowi;
310
static inline void
311 312 313
nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
{
}
L
Linus Torvalds 已提交
314 315
#endif /*CONFIG_NETFILTER*/

316
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
317 318
extern void (*ip_ct_attach)(struct sk_buff *, const struct sk_buff *) __rcu;
extern void nf_ct_attach(struct sk_buff *, const struct sk_buff *);
E
Eric Dumazet 已提交
319
extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
320 321 322 323 324 325 326 327

struct nf_conn;
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);
328 329 330 331
};
extern struct nfq_ct_hook __rcu *nfq_ct_hook;

struct nfq_ct_nat_hook {
332 333
	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
			   u32 ctinfo, int off);
334
};
335
extern struct nfq_ct_nat_hook __rcu *nfq_ct_nat_hook;
336 337 338 339
#else
static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
#endif

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