blob: 32cddf78b13e35907c9ee76ee555b86d45d76b34 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001#ifndef _X_TABLES_H
2#define _X_TABLES_H
Alexey Dobriyana79ff732010-04-13 11:21:46 +02003#include <linux/kernel.h>
Arnd Bergmann60c195c2009-02-26 00:51:43 +01004#include <linux/types.h>
5
Harald Welte2e4e6a12006-01-12 13:30:04 -08006#define XT_FUNCTION_MAXNAMELEN 30
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +02007#define XT_EXTENSION_MAXNAMELEN 29
Harald Welte2e4e6a12006-01-12 13:30:04 -08008#define XT_TABLE_MAXNAMELEN 32
9
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080010struct xt_entry_match {
Dmitry Mishin1e30a012006-03-22 13:56:56 -080011 union {
12 struct {
Arnd Bergmann60c195c2009-02-26 00:51:43 +010013 __u16 match_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080014
15 /* Used by userspace */
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +020016 char name[XT_EXTENSION_MAXNAMELEN];
Arnd Bergmann60c195c2009-02-26 00:51:43 +010017 __u8 revision;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080018 } user;
19 struct {
Arnd Bergmann60c195c2009-02-26 00:51:43 +010020 __u16 match_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080021
22 /* Used inside the kernel */
23 struct xt_match *match;
24 } kernel;
25
26 /* Total length */
Arnd Bergmann60c195c2009-02-26 00:51:43 +010027 __u16 match_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080028 } u;
29
30 unsigned char data[0];
31};
32
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080033struct xt_entry_target {
Dmitry Mishin1e30a012006-03-22 13:56:56 -080034 union {
35 struct {
Arnd Bergmann60c195c2009-02-26 00:51:43 +010036 __u16 target_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080037
38 /* Used by userspace */
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +020039 char name[XT_EXTENSION_MAXNAMELEN];
Arnd Bergmann60c195c2009-02-26 00:51:43 +010040 __u8 revision;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080041 } user;
42 struct {
Arnd Bergmann60c195c2009-02-26 00:51:43 +010043 __u16 target_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080044
45 /* Used inside the kernel */
46 struct xt_target *target;
47 } kernel;
48
49 /* Total length */
Arnd Bergmann60c195c2009-02-26 00:51:43 +010050 __u16 target_size;
Dmitry Mishin1e30a012006-03-22 13:56:56 -080051 } u;
52
53 unsigned char data[0];
54};
55
Patrick McHardy3c2ad462007-05-10 14:14:16 -070056#define XT_TARGET_INIT(__name, __size) \
57{ \
58 .target.u.user = { \
59 .target_size = XT_ALIGN(__size), \
60 .name = __name, \
61 }, \
62}
63
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080064struct xt_standard_target {
Dmitry Mishin1e30a012006-03-22 13:56:56 -080065 struct xt_entry_target target;
66 int verdict;
67};
68
Jan Engelhardt75f0a0f2010-10-13 16:37:45 +020069struct xt_error_target {
70 struct xt_entry_target target;
71 char errorname[XT_FUNCTION_MAXNAMELEN];
72};
73
Harald Welte2e4e6a12006-01-12 13:30:04 -080074/* The argument to IPT_SO_GET_REVISION_*. Returns highest revision
75 * kernel supports, if >= revision. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080076struct xt_get_revision {
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +020077 char name[XT_EXTENSION_MAXNAMELEN];
Arnd Bergmann60c195c2009-02-26 00:51:43 +010078 __u8 revision;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079};
80
81/* CONTINUE verdict for targets */
82#define XT_CONTINUE 0xFFFFFFFF
83
84/* For standard target */
85#define XT_RETURN (-NF_REPEAT - 1)
86
David S. Miller6fbfc962006-01-20 11:57:07 -080087/* this is a dummy structure to find out the alignment requirement for a struct
88 * containing all the fundamental data types that are used in ipt_entry,
89 * ip6t_entry and arpt_entry. This sucks, and it is a hack. It will be my
90 * personal pleasure to remove it -HW
91 */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -080092struct _xt_align {
Arnd Bergmann60c195c2009-02-26 00:51:43 +010093 __u8 u8;
94 __u16 u16;
95 __u32 u32;
96 __u64 u64;
David S. Miller6fbfc962006-01-20 11:57:07 -080097};
98
Alexey Dobriyana79ff732010-04-13 11:21:46 +020099#define XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _xt_align))
Harald Welte2e4e6a12006-01-12 13:30:04 -0800100
101/* Standard return verdict, or do jump. */
102#define XT_STANDARD_TARGET ""
103/* Error verdict. */
104#define XT_ERROR_TARGET "ERROR"
105
Harald Welte2e4e6a12006-01-12 13:30:04 -0800106#define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
107#define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
108
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800109struct xt_counters {
Arnd Bergmann60c195c2009-02-26 00:51:43 +0100110 __u64 pcnt, bcnt; /* Packet and byte counters */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800111};
112
113/* The argument to IPT_SO_ADD_COUNTERS. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800114struct xt_counters_info {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800115 /* Which table. */
116 char name[XT_TABLE_MAXNAMELEN];
117
118 unsigned int num_counters;
119
120 /* The counters (actually `number' of these). */
121 struct xt_counters counters[0];
122};
123
124#define XT_INV_PROTO 0x40 /* Invert the sense of PROTO. */
125
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100126#ifndef __KERNEL__
Patrick McHardy89c002d2007-12-17 21:46:59 -0800127/* fn returns 0 to continue iteration */
128#define XT_MATCH_ITERATE(type, e, fn, args...) \
129({ \
130 unsigned int __i; \
131 int __ret = 0; \
132 struct xt_entry_match *__m; \
133 \
134 for (__i = sizeof(type); \
135 __i < (e)->target_offset; \
136 __i += __m->u.match_size) { \
137 __m = (void *)e + __i; \
138 \
139 __ret = fn(__m , ## args); \
140 if (__ret != 0) \
141 break; \
142 } \
143 __ret; \
144})
145
146/* fn returns 0 to continue iteration */
147#define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
148({ \
149 unsigned int __i, __n; \
150 int __ret = 0; \
151 type *__entry; \
152 \
153 for (__i = 0, __n = 0; __i < (size); \
154 __i += __entry->next_offset, __n++) { \
155 __entry = (void *)(entries) + __i; \
156 if (__n < n) \
157 continue; \
158 \
159 __ret = fn(__entry , ## args); \
160 if (__ret != 0) \
161 break; \
162 } \
163 __ret; \
164})
165
166/* fn returns 0 to continue iteration */
167#define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
168 XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
169
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100170#endif /* !__KERNEL__ */
171
172/* pos is normally a struct ipt_entry/ip6t_entry/etc. */
173#define xt_entry_foreach(pos, ehead, esize) \
174 for ((pos) = (typeof(pos))(ehead); \
175 (pos) < (typeof(pos))((char *)(ehead) + (esize)); \
176 (pos) = (typeof(pos))((char *)(pos) + (pos)->next_offset))
177
Jan Engelhardtdcea9922010-02-24 18:34:48 +0100178/* can only be xt_entry_match, so no use of typeof here */
179#define xt_ematch_foreach(pos, entry) \
180 for ((pos) = (struct xt_entry_match *)entry->elems; \
181 (pos) < (struct xt_entry_match *)((char *)(entry) + \
182 (entry)->target_offset); \
183 (pos) = (struct xt_entry_match *)((char *)(pos) + \
184 (pos)->u.match_size))
185
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186#ifdef __KERNEL__
187
188#include <linux/netdevice.h>
189
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200190/**
Jan Engelhardtde74c162009-07-05 18:26:37 +0200191 * struct xt_action_param - parameters for matches/targets
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200192 *
Jan Engelhardtde74c162009-07-05 18:26:37 +0200193 * @match: the match extension
194 * @target: the target extension
195 * @matchinfo: per-match data
196 * @targetinfo: per-target data
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200197 * @in: input netdevice
198 * @out: output netdevice
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200199 * @fragoff: packet is a fragment, this is the data offset
200 * @thoff: position of transport header relative to skb->data
Evgeniy Polyakova5e78822009-06-04 16:54:42 +0200201 * @hook: hook number given packet came from
Jan Engelhardt916a9172008-10-08 11:35:20 +0200202 * @family: Actual NFPROTO_* through which the function is invoked
203 * (helpful when match->family == NFPROTO_UNSPEC)
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200204 *
205 * Fields written to by extensions:
206 *
Evgeniy Polyakova5e78822009-06-04 16:54:42 +0200207 * @hotdrop: drop packet if we had inspection problems
Jan Engelhardt16599782010-03-18 10:30:44 +0100208 * Network namespace obtainable using dev_net(in/out)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200209 */
Jan Engelhardtde74c162009-07-05 18:26:37 +0200210struct xt_action_param {
211 union {
212 const struct xt_match *match;
213 const struct xt_target *target;
214 };
215 union {
216 const void *matchinfo, *targinfo;
217 };
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200218 const struct net_device *in, *out;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200219 int fragoff;
220 unsigned int thoff;
Evgeniy Polyakova5e78822009-06-04 16:54:42 +0200221 unsigned int hooknum;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200222 u_int8_t family;
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200223 bool hotdrop;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200224};
225
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200226/**
227 * struct xt_mtchk_param - parameters for match extensions'
228 * checkentry functions
229 *
Jan Engelhardt16599782010-03-18 10:30:44 +0100230 * @net: network namespace through which the check was invoked
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200231 * @table: table the rule is tried to be inserted into
232 * @entryinfo: the family-specific rule data
Jan Engelhardt16599782010-03-18 10:30:44 +0100233 * (struct ipt_ip, ip6t_ip, arpt_arp or (note) ebt_entry)
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200234 * @match: struct xt_match through which this function was invoked
235 * @matchinfo: per-match data
236 * @hook_mask: via which hooks the new rule is reachable
Jan Engelhardt16599782010-03-18 10:30:44 +0100237 * Other fields as above.
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200238 */
239struct xt_mtchk_param {
Alexey Dobriyana83d8e82010-01-18 08:21:13 +0100240 struct net *net;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200241 const char *table;
242 const void *entryinfo;
243 const struct xt_match *match;
244 void *matchinfo;
245 unsigned int hook_mask;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200246 u_int8_t family;
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200247};
248
Jan Engelhardt16599782010-03-18 10:30:44 +0100249/**
250 * struct xt_mdtor_param - match destructor parameters
251 * Fields as above.
252 */
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200253struct xt_mtdtor_param {
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100254 struct net *net;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200255 const struct xt_match *match;
256 void *matchinfo;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200257 u_int8_t family;
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200258};
259
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200260/**
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200261 * struct xt_tgchk_param - parameters for target extensions'
262 * checkentry functions
263 *
264 * @entryinfo: the family-specific rule data
265 * (struct ipt_entry, ip6t_entry, arpt_entry, ebt_entry)
266 *
267 * Other fields see above.
268 */
269struct xt_tgchk_param {
Patrick McHardyadd67462010-02-03 13:45:12 +0100270 struct net *net;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200271 const char *table;
Jan Engelhardtf79fca52008-11-24 16:06:17 -0800272 const void *entryinfo;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200273 const struct xt_target *target;
274 void *targinfo;
275 unsigned int hook_mask;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200276 u_int8_t family;
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200277};
278
Jan Engelhardta2df1642008-10-08 11:35:19 +0200279/* Target destructor parameters */
280struct xt_tgdtor_param {
Patrick McHardyadd67462010-02-03 13:45:12 +0100281 struct net *net;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200282 const struct xt_target *target;
283 void *targinfo;
Jan Engelhardt916a9172008-10-08 11:35:20 +0200284 u_int8_t family;
Jan Engelhardta2df1642008-10-08 11:35:19 +0200285};
286
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800287struct xt_match {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800288 struct list_head list;
289
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +0200290 const char name[XT_EXTENSION_MAXNAMELEN];
Richard Kennedydaaf83d2009-01-12 00:06:11 +0000291 u_int8_t revision;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800292
Harald Welte2e4e6a12006-01-12 13:30:04 -0800293 /* Return true or false: return FALSE and set *hotdrop = 1 to
294 force immediate packet drop. */
295 /* Arguments changed since 2.6.9, as this must now handle
296 non-linear skb, using skb_header_pointer and
297 skb_ip_make_writable. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700298 bool (*match)(const struct sk_buff *skb,
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200299 struct xt_action_param *);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800300
301 /* Called when user tries to insert an entry of this type. */
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100302 int (*checkentry)(const struct xt_mtchk_param *);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800303
304 /* Called when entry of this type deleted. */
Jan Engelhardt6be3d852008-10-08 11:35:19 +0200305 void (*destroy)(const struct xt_mtdtor_param *);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100306#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -0800307 /* Called when userspace align differs from kernel space one */
Jan Engelhardt739674f2009-06-26 08:23:19 +0200308 void (*compat_from_user)(void *dst, const void *src);
309 int (*compat_to_user)(void __user *dst, const void *src);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100310#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800311 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
312 struct module *me;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800313
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800314 const char *table;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800315 unsigned int matchsize;
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100316#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700317 unsigned int compatsize;
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100318#endif
Patrick McHardy37f9f732006-03-20 17:59:06 -0800319 unsigned int hooks;
320 unsigned short proto;
Patrick McHardyc4b88512006-03-20 18:03:40 -0800321
322 unsigned short family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800323};
324
325/* Registration hooks for targets. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800326struct xt_target {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800327 struct list_head list;
328
Jan Engelhardt4b2cbd42010-04-27 15:34:34 +0200329 const char name[XT_EXTENSION_MAXNAMELEN];
Jan Engelhardtf5c511c2010-03-18 14:02:10 +0100330 u_int8_t revision;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800331
Harald Welte2e4e6a12006-01-12 13:30:04 -0800332 /* Returns verdict. Argument order changed since 2.6.9, as this
333 must now handle non-linear skbs, using skb_copy_bits and
334 skb_ip_make_writable. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700335 unsigned int (*target)(struct sk_buff *skb,
Jan Engelhardtde74c162009-07-05 18:26:37 +0200336 const struct xt_action_param *);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800337
338 /* Called when user tries to insert an entry of this type:
339 hook_mask is a bitmask of hooks from which it can be
340 called. */
Luciano Coelho7ea7b852010-05-20 15:59:16 +0200341 /* Should return 0 on success or an error code otherwise (-Exxxx). */
Jan Engelhardt135367b2010-03-19 17:16:42 +0100342 int (*checkentry)(const struct xt_tgchk_param *);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800343
344 /* Called when entry of this type deleted. */
Jan Engelhardta2df1642008-10-08 11:35:19 +0200345 void (*destroy)(const struct xt_tgdtor_param *);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100346#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -0800347 /* Called when userspace align differs from kernel space one */
Jan Engelhardt739674f2009-06-26 08:23:19 +0200348 void (*compat_from_user)(void *dst, const void *src);
349 int (*compat_to_user)(void __user *dst, const void *src);
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100350#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800351 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
352 struct module *me;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800353
Jan Engelhardtecb6f852008-01-31 03:54:47 -0800354 const char *table;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800355 unsigned int targetsize;
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100356#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700357 unsigned int compatsize;
Alexey Dobriyanc30f5402010-02-02 15:03:24 +0100358#endif
Patrick McHardy37f9f732006-03-20 17:59:06 -0800359 unsigned int hooks;
360 unsigned short proto;
Patrick McHardyc4b88512006-03-20 18:03:40 -0800361
362 unsigned short family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800363};
364
365/* Furniture shopping... */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800366struct xt_table {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800367 struct list_head list;
368
Harald Welte2e4e6a12006-01-12 13:30:04 -0800369 /* What hooks you will enter on */
370 unsigned int valid_hooks;
371
Harald Welte2e4e6a12006-01-12 13:30:04 -0800372 /* Man behind the curtain... */
Stephen Hemminger4a2f9652009-02-18 16:29:44 +0100373 struct xt_table_info *private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800374
375 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
376 struct module *me;
377
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200378 u_int8_t af; /* address/protocol family */
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200379 int priority; /* hook order */
Stephen Hemminger4a2f9652009-02-18 16:29:44 +0100380
381 /* A unique name... */
382 const char name[XT_TABLE_MAXNAMELEN];
Harald Welte2e4e6a12006-01-12 13:30:04 -0800383};
384
385#include <linux/netfilter_ipv4.h>
386
387/* The table itself */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800388struct xt_table_info {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800389 /* Size per table */
390 unsigned int size;
391 /* Number of entries: FIXME. --RR */
392 unsigned int number;
393 /* Initial number of entries. Needed for module usage count */
394 unsigned int initial_entries;
395
396 /* Entry points and underflows */
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800397 unsigned int hook_entry[NF_INET_NUMHOOKS];
398 unsigned int underflow[NF_INET_NUMHOOKS];
Harald Welte2e4e6a12006-01-12 13:30:04 -0800399
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200400 /*
401 * Number of user chains. Since tables cannot have loops, at most
402 * @stacksize jumps (number of user chains) can possibly be made.
403 */
404 unsigned int stacksize;
Eric Dumazet7489aec2010-05-31 16:41:35 +0200405 unsigned int __percpu *stackptr;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200406 void ***jumpstack;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800407 /* ipt_entry tables: one per CPU */
Eric Dumazet259d4e42007-12-04 23:24:56 -0800408 /* Note : this field MUST be the last one, see XT_TABLE_INFO_SZ */
Stephen Hemminger78454472009-02-20 10:35:32 +0100409 void *entries[1];
Harald Welte2e4e6a12006-01-12 13:30:04 -0800410};
411
Eric Dumazet259d4e42007-12-04 23:24:56 -0800412#define XT_TABLE_INFO_SZ (offsetof(struct xt_table_info, entries) \
413 + nr_cpu_ids * sizeof(char *))
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800414extern int xt_register_target(struct xt_target *target);
415extern void xt_unregister_target(struct xt_target *target);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700416extern int xt_register_targets(struct xt_target *target, unsigned int n);
417extern void xt_unregister_targets(struct xt_target *target, unsigned int n);
418
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800419extern int xt_register_match(struct xt_match *target);
420extern void xt_unregister_match(struct xt_match *target);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700421extern int xt_register_matches(struct xt_match *match, unsigned int n);
422extern void xt_unregister_matches(struct xt_match *match, unsigned int n);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800423
Jan Engelhardt916a9172008-10-08 11:35:20 +0200424extern int xt_check_match(struct xt_mtchk_param *,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200425 unsigned int size, u_int8_t proto, bool inv_proto);
Jan Engelhardt916a9172008-10-08 11:35:20 +0200426extern int xt_check_target(struct xt_tgchk_param *,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200427 unsigned int size, u_int8_t proto, bool inv_proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800428
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800429extern struct xt_table *xt_register_table(struct net *net,
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200430 const struct xt_table *table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800431 struct xt_table_info *bootstrap,
432 struct xt_table_info *newinfo);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800433extern void *xt_unregister_table(struct xt_table *table);
434
435extern struct xt_table_info *xt_replace_table(struct xt_table *table,
436 unsigned int num_counters,
437 struct xt_table_info *newinfo,
438 int *error);
439
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200440extern struct xt_match *xt_find_match(u8 af, const char *name, u8 revision);
441extern struct xt_target *xt_find_target(u8 af, const char *name, u8 revision);
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200442extern struct xt_match *xt_request_find_match(u8 af, const char *name,
443 u8 revision);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200444extern struct xt_target *xt_request_find_target(u8 af, const char *name,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800445 u8 revision);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200446extern int xt_find_revision(u8 af, const char *name, u8 revision,
447 int target, int *err);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800448
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200449extern struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800450 const char *name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800451extern void xt_table_unlock(struct xt_table *t);
452
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200453extern int xt_proto_init(struct net *net, u_int8_t af);
454extern void xt_proto_fini(struct net *net, u_int8_t af);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800455
456extern struct xt_table_info *xt_alloc_table_info(unsigned int size);
457extern void xt_free_table_info(struct xt_table_info *info);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700458
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200459/**
460 * xt_recseq - recursive seqcount for netfilter use
461 *
462 * Packet processing changes the seqcount only if no recursion happened
463 * get_counters() can use read_seqcount_begin()/read_seqcount_retry(),
464 * because we use the normal seqcount convention :
465 * Low order bit set to 1 if a writer is active.
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700466 */
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200467DECLARE_PER_CPU(seqcount_t, xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700468
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200469/**
470 * xt_write_recseq_begin - start of a write section
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700471 *
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200472 * Begin packet processing : all readers must wait the end
473 * 1) Must be called with preemption disabled
474 * 2) softirqs must be disabled too (or we should use irqsafe_cpu_add())
475 * Returns :
476 * 1 if no recursion on this cpu
477 * 0 if recursion detected
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700478 */
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200479static inline unsigned int xt_write_recseq_begin(void)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700480{
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200481 unsigned int addend;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700482
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200483 /*
484 * Low order bit of sequence is set if we already
485 * called xt_write_recseq_begin().
486 */
487 addend = (__this_cpu_read(xt_recseq.sequence) + 1) & 1;
488
489 /*
490 * This is kind of a write_seqcount_begin(), but addend is 0 or 1
491 * We dont check addend value to avoid a test and conditional jump,
492 * since addend is most likely 1
493 */
494 __this_cpu_add(xt_recseq.sequence, addend);
495 smp_wmb();
496
497 return addend;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700498}
499
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200500/**
501 * xt_write_recseq_end - end of a write section
502 * @addend: return value from previous xt_write_recseq_begin()
503 *
504 * End packet processing : all readers can proceed
505 * 1) Must be called with preemption disabled
506 * 2) softirqs must be disabled too (or we should use irqsafe_cpu_add())
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700507 */
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200508static inline void xt_write_recseq_end(unsigned int addend)
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700509{
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200510 /* this is kind of a write_seqcount_end(), but addend is 0 or 1 */
511 smp_wmb();
512 __this_cpu_add(xt_recseq.sequence, addend);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700513}
Harald Welte2e4e6a12006-01-12 13:30:04 -0800514
Eric Dumazetb8dfe492009-03-25 17:31:52 +0100515/*
516 * This helper is performance critical and must be inlined
517 */
518static inline unsigned long ifname_compare_aligned(const char *_a,
519 const char *_b,
520 const char *_mask)
521{
522 const unsigned long *a = (const unsigned long *)_a;
523 const unsigned long *b = (const unsigned long *)_b;
524 const unsigned long *mask = (const unsigned long *)_mask;
525 unsigned long ret;
526
527 ret = (a[0] ^ b[0]) & mask[0];
528 if (IFNAMSIZ > sizeof(unsigned long))
529 ret |= (a[1] ^ b[1]) & mask[1];
530 if (IFNAMSIZ > 2 * sizeof(unsigned long))
531 ret |= (a[2] ^ b[2]) & mask[2];
532 if (IFNAMSIZ > 3 * sizeof(unsigned long))
533 ret |= (a[3] ^ b[3]) & mask[3];
534 BUILD_BUG_ON(IFNAMSIZ > 4 * sizeof(unsigned long));
535 return ret;
536}
537
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200538extern struct nf_hook_ops *xt_hook_link(const struct xt_table *, nf_hookfn *);
539extern void xt_hook_unlink(const struct xt_table *, struct nf_hook_ops *);
540
Dmitry Mishin27229712006-04-01 02:25:19 -0800541#ifdef CONFIG_COMPAT
542#include <net/compat.h>
543
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800544struct compat_xt_entry_match {
Dmitry Mishin27229712006-04-01 02:25:19 -0800545 union {
546 struct {
547 u_int16_t match_size;
548 char name[XT_FUNCTION_MAXNAMELEN - 1];
549 u_int8_t revision;
550 } user;
Patrick McHardy46c5ea3c2006-05-02 05:12:22 +0200551 struct {
552 u_int16_t match_size;
553 compat_uptr_t match;
554 } kernel;
Dmitry Mishin27229712006-04-01 02:25:19 -0800555 u_int16_t match_size;
556 } u;
557 unsigned char data[0];
558};
559
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800560struct compat_xt_entry_target {
Dmitry Mishin27229712006-04-01 02:25:19 -0800561 union {
562 struct {
563 u_int16_t target_size;
564 char name[XT_FUNCTION_MAXNAMELEN - 1];
565 u_int8_t revision;
566 } user;
Patrick McHardy46c5ea3c2006-05-02 05:12:22 +0200567 struct {
568 u_int16_t target_size;
569 compat_uptr_t target;
570 } kernel;
Dmitry Mishin27229712006-04-01 02:25:19 -0800571 u_int16_t target_size;
572 } u;
573 unsigned char data[0];
574};
575
576/* FIXME: this works only on 32 bit tasks
577 * need to change whole approach in order to calculate align as function of
578 * current task alignment */
579
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800580struct compat_xt_counters {
Alexey Dobriyan0a026042010-02-10 15:00:32 +0100581 compat_u64 pcnt, bcnt; /* Packet and byte counters */
Dmitry Mishin27229712006-04-01 02:25:19 -0800582};
583
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800584struct compat_xt_counters_info {
Dmitry Mishin27229712006-04-01 02:25:19 -0800585 char name[XT_TABLE_MAXNAMELEN];
586 compat_uint_t num_counters;
587 struct compat_xt_counters counters[0];
588};
589
Alexey Dobriyan42107f52010-02-10 15:03:27 +0100590struct _compat_xt_align {
591 __u8 u8;
592 __u16 u16;
593 __u32 u32;
594 compat_u64 u64;
595};
596
Alexey Dobriyana79ff732010-04-13 11:21:46 +0200597#define COMPAT_XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _compat_xt_align))
Dmitry Mishin27229712006-04-01 02:25:19 -0800598
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200599extern void xt_compat_lock(u_int8_t af);
600extern void xt_compat_unlock(u_int8_t af);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700601
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100602extern int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200603extern void xt_compat_flush_offsets(u_int8_t af);
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100604extern void xt_compat_init_offsets(u_int8_t af, unsigned int number);
Florian Westphal3e5e5242010-02-15 18:17:10 +0100605extern int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800606
Jan Engelhardt5452e422008-04-14 11:15:35 +0200607extern int xt_compat_match_offset(const struct xt_match *match);
Patrick McHardy89566952007-12-17 21:46:40 -0800608extern int xt_compat_match_from_user(struct xt_entry_match *m,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800609 void **dstptr, unsigned int *size);
Jan Engelhardt739674f2009-06-26 08:23:19 +0200610extern int xt_compat_match_to_user(const struct xt_entry_match *m,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800611 void __user **dstptr, unsigned int *size);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700612
Jan Engelhardt5452e422008-04-14 11:15:35 +0200613extern int xt_compat_target_offset(const struct xt_target *target);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700614extern void xt_compat_target_from_user(struct xt_entry_target *t,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800615 void **dstptr, unsigned int *size);
Jan Engelhardt739674f2009-06-26 08:23:19 +0200616extern int xt_compat_target_to_user(const struct xt_entry_target *t,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800617 void __user **dstptr, unsigned int *size);
Dmitry Mishin27229712006-04-01 02:25:19 -0800618
619#endif /* CONFIG_COMPAT */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800620#endif /* __KERNEL__ */
621
622#endif /* _X_TABLES_H */