linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / include / linux / netfilter_arp / arp_tables.h
1 /*
2  *      Format of an ARP firewall descriptor
3  *
4  *      src, tgt, src_mask, tgt_mask, arpop, arpop_mask are always stored in
5  *      network byte order.
6  *      flags are stored in host byte order (of course).
7  */
8
9 #ifndef _ARPTABLES_H
10 #define _ARPTABLES_H
11
12 #ifdef __KERNEL__
13 #include <linux/if.h>
14 #include <linux/types.h>
15 #include <linux/in.h>
16 #include <linux/if_arp.h>
17 #include <linux/skbuff.h>
18 #endif
19 #include <linux/compiler.h>
20 #include <linux/netfilter_arp.h>
21
22 #include <linux/netfilter/x_tables.h>
23
24 #define ARPT_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
25 #define ARPT_TABLE_MAXNAMELEN XT_TABLE_MAXNAMELEN
26 #define arpt_target xt_target
27 #define arpt_table xt_table
28
29 #define ARPT_DEV_ADDR_LEN_MAX 16
30
31 struct arpt_devaddr_info {
32         char addr[ARPT_DEV_ADDR_LEN_MAX];
33         char mask[ARPT_DEV_ADDR_LEN_MAX];
34 };
35
36 /* Yes, Virginia, you have to zero the padding. */
37 struct arpt_arp {
38         /* Source and target IP addr */
39         struct in_addr src, tgt;
40         /* Mask for src and target IP addr */
41         struct in_addr smsk, tmsk;
42
43         /* Device hw address length, src+target device addresses */
44         u_int8_t arhln, arhln_mask;
45         struct arpt_devaddr_info src_devaddr;
46         struct arpt_devaddr_info tgt_devaddr;
47
48         /* ARP operation code. */
49         u_int16_t arpop, arpop_mask;
50
51         /* ARP hardware address and protocol address format. */
52         u_int16_t arhrd, arhrd_mask;
53         u_int16_t arpro, arpro_mask;
54
55         /* The protocol address length is only accepted if it is 4
56          * so there is no use in offering a way to do filtering on it.
57          */
58
59         char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
60         unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
61
62         /* Flags word */
63         u_int8_t flags;
64         /* Inverse flags */
65         u_int16_t invflags;
66 };
67
68 struct arpt_entry_target
69 {
70         union {
71                 struct {
72                         u_int16_t target_size;
73
74                         /* Used by userspace */
75                         char name[ARPT_FUNCTION_MAXNAMELEN-1];
76                         u_int8_t revision;
77                 } user;
78                 struct {
79                         u_int16_t target_size;
80
81                         /* Used inside the kernel */
82                         struct arpt_target *target;
83                 } kernel;
84
85                 /* Total length */
86                 u_int16_t target_size;
87         } u;
88
89         unsigned char data[0];
90 };
91
92 struct arpt_standard_target
93 {
94         struct arpt_entry_target target;
95         int verdict;
96 };
97
98 /* Values for "flag" field in struct arpt_ip (general arp structure).
99  * No flags defined yet.
100  */
101 #define ARPT_F_MASK             0x00    /* All possible flag bits mask. */
102
103 /* Values for "inv" field in struct arpt_arp. */
104 #define ARPT_INV_VIA_IN         0x0001  /* Invert the sense of IN IFACE. */
105 #define ARPT_INV_VIA_OUT        0x0002  /* Invert the sense of OUT IFACE */
106 #define ARPT_INV_SRCIP          0x0004  /* Invert the sense of SRC IP. */
107 #define ARPT_INV_TGTIP          0x0008  /* Invert the sense of TGT IP. */
108 #define ARPT_INV_SRCDEVADDR     0x0010  /* Invert the sense of SRC DEV ADDR. */
109 #define ARPT_INV_TGTDEVADDR     0x0020  /* Invert the sense of TGT DEV ADDR. */
110 #define ARPT_INV_ARPOP          0x0040  /* Invert the sense of ARP OP. */
111 #define ARPT_INV_ARPHRD         0x0080  /* Invert the sense of ARP HRD. */
112 #define ARPT_INV_ARPPRO         0x0100  /* Invert the sense of ARP PRO. */
113 #define ARPT_INV_ARPHLN         0x0200  /* Invert the sense of ARP HLN. */
114 #define ARPT_INV_MASK           0x03FF  /* All possible flag bits mask. */
115
116 /* This structure defines each of the firewall rules.  Consists of 3
117    parts which are 1) general ARP header stuff 2) match specific
118    stuff 3) the target to perform if the rule matches */
119 struct arpt_entry
120 {
121         struct arpt_arp arp;
122
123         /* Size of arpt_entry + matches */
124         u_int16_t target_offset;
125         /* Size of arpt_entry + matches + target */
126         u_int16_t next_offset;
127
128         /* Back pointer */
129         unsigned int comefrom;
130
131         /* Packet and byte counters. */
132         struct xt_counters counters;
133
134         /* The matches (if any), then the target. */
135         unsigned char elems[0];
136 };
137
138 /*
139  * New IP firewall options for [gs]etsockopt at the RAW IP level.
140  * Unlike BSD Linux inherits IP options so you don't have to use a raw
141  * socket for this. Instead we check rights in the calls.
142  */
143 #define ARPT_CTL_OFFSET         32
144 #define ARPT_BASE_CTL           (XT_BASE_CTL+ARPT_CTL_OFFSET)
145
146 #define ARPT_SO_SET_REPLACE             (XT_SO_SET_REPLACE+ARPT_CTL_OFFSET)
147 #define ARPT_SO_SET_ADD_COUNTERS        (XT_SO_SET_ADD_COUNTERS+ARPT_CTL_OFFSET)
148 #define ARPT_SO_SET_MAX                 (XT_SO_SET_MAX+ARPT_CTL_OFFSET)
149
150 #define ARPT_SO_GET_INFO                (XT_SO_GET_INFO+ARPT_CTL_OFFSET)
151 #define ARPT_SO_GET_ENTRIES             (XT_SO_GET_ENTRIES+ARPT_CTL_OFFSET)
152 /* #define ARPT_SO_GET_REVISION_MATCH   XT_SO_GET_REVISION_MATCH  */
153 #define ARPT_SO_GET_REVISION_TARGET     (XT_SO_GET_REVISION_TARGET+ARPT_CTL_OFFSET)
154 #define ARPT_SO_GET_MAX                 (XT_SO_GET_REVISION_TARGET+ARPT_CTL_OFFSET)
155
156 /* CONTINUE verdict for targets */
157 #define ARPT_CONTINUE XT_CONTINUE
158
159 /* For standard target */
160 #define ARPT_RETURN XT_RETURN
161
162 /* The argument to ARPT_SO_GET_INFO */
163 struct arpt_getinfo
164 {
165         /* Which table: caller fills this in. */
166         char name[ARPT_TABLE_MAXNAMELEN];
167
168         /* Kernel fills these in. */
169         /* Which hook entry points are valid: bitmask */
170         unsigned int valid_hooks;
171
172         /* Hook entry points: one per netfilter hook. */
173         unsigned int hook_entry[NF_ARP_NUMHOOKS];
174
175         /* Underflow points. */
176         unsigned int underflow[NF_ARP_NUMHOOKS];
177
178         /* Number of entries */
179         unsigned int num_entries;
180
181         /* Size of entries. */
182         unsigned int size;
183 };
184
185 /* The argument to ARPT_SO_SET_REPLACE. */
186 struct arpt_replace
187 {
188         /* Which table. */
189         char name[ARPT_TABLE_MAXNAMELEN];
190
191         /* Which hook entry points are valid: bitmask.  You can't
192            change this. */
193         unsigned int valid_hooks;
194
195         /* Number of entries */
196         unsigned int num_entries;
197
198         /* Total size of new entries */
199         unsigned int size;
200
201         /* Hook entry points. */
202         unsigned int hook_entry[NF_ARP_NUMHOOKS];
203
204         /* Underflow points. */
205         unsigned int underflow[NF_ARP_NUMHOOKS];
206
207         /* Information about old entries: */
208         /* Number of counters (must be equal to current number of entries). */
209         unsigned int num_counters;
210         /* The old entries' counters. */
211         struct xt_counters __user *counters;
212
213         /* The entries (hang off end: not really an array). */
214         struct arpt_entry entries[0];
215 };
216
217 /* The argument to ARPT_SO_ADD_COUNTERS. */
218 #define arpt_counters_info xt_counters_info
219
220 /* The argument to ARPT_SO_GET_ENTRIES. */
221 struct arpt_get_entries
222 {
223         /* Which table: user fills this in. */
224         char name[ARPT_TABLE_MAXNAMELEN];
225
226         /* User fills this in: total entry size. */
227         unsigned int size;
228
229         /* The entries. */
230         struct arpt_entry entrytable[0];
231 };
232
233 /* Standard return verdict, or do jump. */
234 #define ARPT_STANDARD_TARGET XT_STANDARD_TARGET
235 /* Error verdict. */
236 #define ARPT_ERROR_TARGET XT_ERROR_TARGET
237
238 /* Helper functions */
239 static __inline__ struct arpt_entry_target *arpt_get_target(struct arpt_entry *e)
240 {
241         return (void *)e + e->target_offset;
242 }
243
244 /* fn returns 0 to continue iteration */
245 #define ARPT_ENTRY_ITERATE(entries, size, fn, args...)          \
246 ({                                                              \
247         unsigned int __i;                                       \
248         int __ret = 0;                                          \
249         struct arpt_entry *__entry;                             \
250                                                                 \
251         for (__i = 0; __i < (size); __i += __entry->next_offset) { \
252                 __entry = (void *)(entries) + __i;              \
253                                                                 \
254                 __ret = fn(__entry , ## args);                  \
255                 if (__ret != 0)                                 \
256                         break;                                  \
257         }                                                       \
258         __ret;                                                  \
259 })
260
261 /*
262  *      Main firewall chains definitions and global var's definitions.
263  */
264 #ifdef __KERNEL__
265
266 #define arpt_register_target(tgt) xt_register_target(NF_ARP, tgt)
267 #define arpt_unregister_target(tgt) xt_unregister_target(NF_ARP, tgt)
268
269 extern int arpt_register_table(struct arpt_table *table,
270                                const struct arpt_replace *repl);
271 extern void arpt_unregister_table(struct arpt_table *table);
272 extern unsigned int arpt_do_table(struct sk_buff **pskb,
273                                   unsigned int hook,
274                                   const struct net_device *in,
275                                   const struct net_device *out,
276                                   struct arpt_table *table,
277                                   void *userdata);
278
279 #define ARPT_ALIGN(s) (((s) + (__alignof__(struct arpt_entry)-1)) & ~(__alignof__(struct arpt_entry)-1))
280 #endif /*__KERNEL__*/
281 #endif /* _ARPTABLES_H */