Blame view

include/linux/netfilter.h 9.83 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  #ifndef __LINUX_NETFILTER_H
  #define __LINUX_NETFILTER_H
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
  #include <linux/skbuff.h>
  #include <linux/net.h>
  #include <linux/if.h>
2e3075a2c   Jan Engelhardt   [NETFILTER]: Exte...
7
8
  #include <linux/in.h>
  #include <linux/in6.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
  #include <linux/wait.h>
  #include <linux/list.h>
607ca46e9   David Howells   UAPI: (Scripted) ...
11
  #include <uapi/linux/netfilter.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
  #ifdef CONFIG_NETFILTER
f615df76e   Florian Westphal   netfilter: reduce...
13
14
15
16
  static inline int NF_DROP_GETERR(int verdict)
  {
  	return -(verdict >> NF_VERDICT_QBITS);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17

b8beedd25   Patrick McHardy   [NETFILTER]: Add ...
18
19
20
21
22
23
24
25
  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];
  }
efdedd542   Denys Fedoryshchenko   netfilter: xt_rec...
26
27
28
29
30
31
32
33
34
  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];
  }
6d11cfdba   Pablo Neira Ayuso   netfilter: don't ...
35
  extern int netfilter_init(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
39
40
  
  /* Largest hook number + 1 */
  #define NF_MAX_HOOKS 8
  
  struct sk_buff;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
  
  typedef unsigned int nf_hookfn(unsigned int hooknum,
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
43
  			       struct sk_buff *skb,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
  			       const struct net_device *in,
  			       const struct net_device *out,
  			       int (*okfn)(struct sk_buff *));
d94d9fee9   Eric Dumazet   net: cleanup incl...
47
  struct nf_hook_ops {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
51
52
  	struct list_head list;
  
  	/* User fills in from here down. */
  	nf_hookfn *hook;
  	struct module *owner;
76108cea0   Jan Engelhardt   netfilter: Use un...
53
54
  	u_int8_t pf;
  	unsigned int hooknum;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
  	/* Hooks are ordered in ascending priority. */
  	int priority;
  };
d94d9fee9   Eric Dumazet   net: cleanup incl...
58
  struct nf_sockopt_ops {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  	struct list_head list;
76108cea0   Jan Engelhardt   netfilter: Use un...
60
  	u_int8_t pf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
65
  
  	/* 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);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
66
  #ifdef CONFIG_COMPAT
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
67
68
  	int (*compat_set)(struct sock *sk, int optval,
  			void __user *user, unsigned int len);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
69
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
71
72
  	int get_optmin;
  	int get_optmax;
  	int (*get)(struct sock *sk, int optval, void __user *user, int *len);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
73
  #ifdef CONFIG_COMPAT
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
74
75
  	int (*compat_get)(struct sock *sk, int optval,
  			void __user *user, int *len);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
76
  #endif
16fcec35e   Neil Horman   [NETFILTER]: Fix/...
77
78
  	/* Use the module struct to lock set/get code in place */
  	struct module *owner;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
  /* Function to register/unregister hook points. */
  int nf_register_hook(struct nf_hook_ops *reg);
  void nf_unregister_hook(struct nf_hook_ops *reg);
972d1cb14   Patrick McHardy   [NETFILTER]: Add ...
83
84
  int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
88
89
  
  /* 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);
7e9c6eeb1   Jan Engelhardt   netfilter: Introd...
90
  extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

a2d7ec58a   Eric Dumazet   netfilter: use ju...
92
  #if defined(CONFIG_JUMP_LABEL)
c5905afb0   Ingo Molnar   static keys: Intr...
93
94
  #include <linux/static_key.h>
  extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
a2d7ec58a   Eric Dumazet   netfilter: use ju...
95
96
97
98
  static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  {
  	if (__builtin_constant_p(pf) &&
  	    __builtin_constant_p(hook))
c5905afb0   Ingo Molnar   static keys: Intr...
99
  		return static_key_false(&nf_hooks_needed[pf][hook]);
a2d7ec58a   Eric Dumazet   netfilter: use ju...
100
101
102
103
104
105
106
107
108
  
  	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
76108cea0   Jan Engelhardt   netfilter: Use un...
109
  int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
110
111
112
113
114
115
116
117
118
119
  		 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.
   */
76108cea0   Jan Engelhardt   netfilter: Use un...
120
  static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
121
  				 struct sk_buff *skb,
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
122
123
  				 struct net_device *indev,
  				 struct net_device *outdev,
23f3733d4   Jan Engelhardt   netfilter: reduce...
124
  				 int (*okfn)(struct sk_buff *), int thresh)
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
125
  {
a2d7ec58a   Eric Dumazet   netfilter: use ju...
126
127
128
  	if (nf_hooks_active(pf, hook))
  		return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  	return 1;
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
129
  }
76108cea0   Jan Engelhardt   netfilter: Use un...
130
  static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
131
132
133
  			  struct net_device *indev, struct net_device *outdev,
  			  int (*okfn)(struct sk_buff *))
  {
23f3733d4   Jan Engelhardt   netfilter: reduce...
134
  	return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
16a6677fd   Patrick McHardy   [XFRM]: Netfilter...
135
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                     
  /* 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 :)
  */
2249065f4   Jan Engelhardt   netfilter: get ri...
153
154
155
156
157
158
159
160
161
162
  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;
  }
48d5cad87   Patrick McHardy   [XFRM]: Fix SNAT-...
163

2249065f4   Jan Engelhardt   netfilter: get ri...
164
165
166
167
168
  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)
  {
4bac6b180   Patrick McHardy   netfilter: restor...
169
170
171
  	int ret;
  
  	if (!cond ||
ac5aa2e33   Eric Paris   netfilter: NF_HOO...
172
  	    ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
2249065f4   Jan Engelhardt   netfilter: get ri...
173
174
175
  		ret = okfn(skb);
  	return ret;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176

2249065f4   Jan Engelhardt   netfilter: get ri...
177
178
179
180
181
182
183
  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);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
  
  /* Call setsockopt() */
76108cea0   Jan Engelhardt   netfilter: Use un...
186
  int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
b7058842c   David S. Miller   net: Make setsock...
187
  		  unsigned int len);
76108cea0   Jan Engelhardt   netfilter: Use un...
188
  int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  		  int *len);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
190
  #ifdef CONFIG_COMPAT
76108cea0   Jan Engelhardt   netfilter: Use un...
191
  int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
b7058842c   David S. Miller   net: Make setsock...
192
  		char __user *opt, unsigned int len);
76108cea0   Jan Engelhardt   netfilter: Use un...
193
  int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
194
  		char __user *opt, int *len);
c30f540b6   Alexey Dobriyan   netfilter: xtable...
195
  #endif
3fdadf7d2   Dmitry Mishin   [NET]: {get|set}s...
196

089af26c7   Harald Welte   [NETFILTER]: Rena...
197
198
199
  /* 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. */
37d418792   Herbert Xu   [NETFILTER]: Do n...
200
  extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
089af26c7   Harald Welte   [NETFILTER]: Rena...
201

1841a4c7a   Patrick McHardy   [NETFILTER]: nf_c...
202
  struct flowi;
02f014d88   Patrick McHardy   [NETFILTER]: nf_q...
203
  struct nf_queue_entry;
c01cd429f   Patrick McHardy   [NETFILTER]: nf_q...
204

bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
205
206
  struct nf_afinfo {
  	unsigned short	family;
b51655b95   Al Viro   [NET]: Annotate _...
207
  	__sum16		(*checksum)(struct sk_buff *skb, unsigned int hook,
422c346fa   Patrick McHardy   [NETFILTER]: Add ...
208
  				    unsigned int dataoff, u_int8_t protocol);
d63a65073   Patrick McHardy   [NETFILTER]: Add ...
209
210
211
212
213
  	__sum16		(*checksum_partial)(struct sk_buff *skb,
  					    unsigned int hook,
  					    unsigned int dataoff,
  					    unsigned int len,
  					    u_int8_t protocol);
31ad3dd64   Florian Westphal   netfilter: af_inf...
214
  	int		(*route)(struct net *net, struct dst_entry **dst,
0fae2e774   Florian Westphal   netfilter: af_inf...
215
  				 struct flowi *fl, bool strict);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
216
  	void		(*saveroute)(const struct sk_buff *skb,
02f014d88   Patrick McHardy   [NETFILTER]: nf_q...
217
  				     struct nf_queue_entry *entry);
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
218
  	int		(*reroute)(struct sk_buff *skb,
02f014d88   Patrick McHardy   [NETFILTER]: nf_q...
219
  				   const struct nf_queue_entry *entry);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
220
  	int		route_key_size;
2cc7d5730   Harald Welte   [NETFILTER]: Move...
221
  };
0e60ebe04   Eric Dumazet   netfilter: add __...
222
  extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
1e796fda0   Patrick McHardy   [NETFILTER]: cons...
223
  static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
224
225
226
  {
  	return rcu_dereference(nf_afinfo[family]);
  }
2cc7d5730   Harald Welte   [NETFILTER]: Move...
227

b51655b95   Al Viro   [NET]: Annotate _...
228
  static inline __sum16
422c346fa   Patrick McHardy   [NETFILTER]: Add ...
229
230
231
  nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  	    u_int8_t protocol, unsigned short family)
  {
1e796fda0   Patrick McHardy   [NETFILTER]: cons...
232
  	const struct nf_afinfo *afinfo;
b51655b95   Al Viro   [NET]: Annotate _...
233
  	__sum16 csum = 0;
422c346fa   Patrick McHardy   [NETFILTER]: Add ...
234
235
236
237
238
239
240
241
  
  	rcu_read_lock();
  	afinfo = nf_get_afinfo(family);
  	if (afinfo)
  		csum = afinfo->checksum(skb, hook, dataoff, protocol);
  	rcu_read_unlock();
  	return csum;
  }
d63a65073   Patrick McHardy   [NETFILTER]: Add ...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  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;
  }
1e796fda0   Patrick McHardy   [NETFILTER]: cons...
258
259
  extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
bce8032ef   Patrick McHardy   [NETFILTER]: Intr...
260

eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
261
  #include <net/flow.h>
c7232c997   Patrick McHardy   netfilter: add pr...
262
  extern void (*nf_nat_decode_session_hook)(struct sk_buff *, struct flowi *);
eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
263
264
  
  static inline void
76108cea0   Jan Engelhardt   netfilter: Use un...
265
  nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
266
  {
051578ccb   Patrick McHardy   [NETFILTER]: nf_n...
267
  #ifdef CONFIG_NF_NAT_NEEDED
eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
268
  	void (*decodefn)(struct sk_buff *, struct flowi *);
c7232c997   Patrick McHardy   netfilter: add pr...
269
270
271
272
273
  	rcu_read_lock();
  	decodefn = rcu_dereference(nf_nat_decode_session_hook);
  	if (decodefn)
  		decodefn(skb, fl);
  	rcu_read_unlock();
eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
274
275
  #endif
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
276
277
  #else /* !CONFIG_NETFILTER */
  #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
48d5cad87   Patrick McHardy   [XFRM]: Fix SNAT-...
278
  #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
76108cea0   Jan Engelhardt   netfilter: Use un...
279
  static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
280
  				 struct sk_buff *skb,
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
281
282
  				 struct net_device *indev,
  				 struct net_device *outdev,
23f3733d4   Jan Engelhardt   netfilter: reduce...
283
  				 int (*okfn)(struct sk_buff *), int thresh)
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
284
  {
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
285
  	return okfn(skb);
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
286
  }
76108cea0   Jan Engelhardt   netfilter: Use un...
287
  static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
288
289
290
  			  struct net_device *indev, struct net_device *outdev,
  			  int (*okfn)(struct sk_buff *))
  {
9c92d3486   Patrick McHardy   [NETFILTER]: Don'...
291
  	return 1;
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
292
  }
f53b61d8c   David S. Miller   [NETFILTER]: Add ...
293
  struct flowi;
eb9c7ebe6   Patrick McHardy   [NETFILTER]: Hand...
294
  static inline void
76108cea0   Jan Engelhardt   netfilter: Use un...
295
296
297
  nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  {
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
  #endif /*CONFIG_NETFILTER*/
5f79e0f91   Yasuyuki Kozakai   [NETFILTER]: nf_c...
299
  #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
312a0c16c   Patrick McHardy   netfilter: nf_con...
300
301
  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 *);
0e60ebe04   Eric Dumazet   netfilter: add __...
302
  extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
9cb017665   Pablo Neira Ayuso   netfilter: add gl...
303
304
  
  struct nf_conn;
41d73ec05   Patrick McHardy   netfilter: nf_con...
305
  enum ip_conntrack_info;
9cb017665   Pablo Neira Ayuso   netfilter: add gl...
306
307
308
309
310
311
  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);
bd0779370   Pablo Neira Ayuso   netfilter: nfnetl...
312
313
  	int (*attach_expect)(const struct nlattr *attr, struct nf_conn *ct,
  			     u32 portid, u32 report);
8c88f87cb   Pablo Neira Ayuso   netfilter: nfnetl...
314
  	void (*seq_adjust)(struct sk_buff *skb, struct nf_conn *ct,
41d73ec05   Patrick McHardy   netfilter: nf_con...
315
  			   enum ip_conntrack_info ctinfo, s32 off);
9cb017665   Pablo Neira Ayuso   netfilter: add gl...
316
  };
41d73ec05   Patrick McHardy   netfilter: nf_con...
317
  extern struct nfq_ct_hook __rcu *nfq_ct_hook;
5f79e0f91   Yasuyuki Kozakai   [NETFILTER]: nf_c...
318
319
320
  #else
  static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
  #endif /*__LINUX_NETFILTER_H*/