• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/in.h>
15 #include <linux/if_arp.h>
16 #include <linux/skbuff.h>
17 #endif
18 #include <linux/types.h>
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 
27 #define ARPT_DEV_ADDR_LEN_MAX 16
28 
29 struct arpt_devaddr_info {
30 	char addr[ARPT_DEV_ADDR_LEN_MAX];
31 	char mask[ARPT_DEV_ADDR_LEN_MAX];
32 };
33 
34 /* Yes, Virginia, you have to zero the padding. */
35 struct arpt_arp {
36 	/* Source and target IP addr */
37 	struct in_addr src, tgt;
38 	/* Mask for src and target IP addr */
39 	struct in_addr smsk, tmsk;
40 
41 	/* Device hw address length, src+target device addresses */
42 	u_int8_t arhln, arhln_mask;
43 	struct arpt_devaddr_info src_devaddr;
44 	struct arpt_devaddr_info tgt_devaddr;
45 
46 	/* ARP operation code. */
47 	__be16 arpop, arpop_mask;
48 
49 	/* ARP hardware address and protocol address format. */
50 	__be16 arhrd, arhrd_mask;
51 	__be16 arpro, arpro_mask;
52 
53 	/* The protocol address length is only accepted if it is 4
54 	 * so there is no use in offering a way to do filtering on it.
55 	 */
56 
57 	char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
58 	unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
59 
60 	/* Flags word */
61 	u_int8_t flags;
62 	/* Inverse flags */
63 	u_int16_t invflags;
64 };
65 
66 #define arpt_entry_target xt_entry_target
67 #define arpt_standard_target xt_standard_target
68 
69 /* Values for "flag" field in struct arpt_ip (general arp structure).
70  * No flags defined yet.
71  */
72 #define ARPT_F_MASK		0x00	/* All possible flag bits mask. */
73 
74 /* Values for "inv" field in struct arpt_arp. */
75 #define ARPT_INV_VIA_IN		0x0001	/* Invert the sense of IN IFACE. */
76 #define ARPT_INV_VIA_OUT	0x0002	/* Invert the sense of OUT IFACE */
77 #define ARPT_INV_SRCIP		0x0004	/* Invert the sense of SRC IP. */
78 #define ARPT_INV_TGTIP		0x0008	/* Invert the sense of TGT IP. */
79 #define ARPT_INV_SRCDEVADDR	0x0010	/* Invert the sense of SRC DEV ADDR. */
80 #define ARPT_INV_TGTDEVADDR	0x0020	/* Invert the sense of TGT DEV ADDR. */
81 #define ARPT_INV_ARPOP		0x0040	/* Invert the sense of ARP OP. */
82 #define ARPT_INV_ARPHRD		0x0080	/* Invert the sense of ARP HRD. */
83 #define ARPT_INV_ARPPRO		0x0100	/* Invert the sense of ARP PRO. */
84 #define ARPT_INV_ARPHLN		0x0200	/* Invert the sense of ARP HLN. */
85 #define ARPT_INV_MASK		0x03FF	/* All possible flag bits mask. */
86 
87 /* This structure defines each of the firewall rules.  Consists of 3
88    parts which are 1) general ARP header stuff 2) match specific
89    stuff 3) the target to perform if the rule matches */
90 struct arpt_entry
91 {
92 	struct arpt_arp arp;
93 
94 	/* Size of arpt_entry + matches */
95 	u_int16_t target_offset;
96 	/* Size of arpt_entry + matches + target */
97 	u_int16_t next_offset;
98 
99 	/* Back pointer */
100 	unsigned int comefrom;
101 
102 	/* Packet and byte counters. */
103 	struct xt_counters counters;
104 
105 	/* The matches (if any), then the target. */
106 	unsigned char elems[0];
107 };
108 
109 /*
110  * New IP firewall options for [gs]etsockopt at the RAW IP level.
111  * Unlike BSD Linux inherits IP options so you don't have to use a raw
112  * socket for this. Instead we check rights in the calls.
113  *
114  * ATTENTION: check linux/in.h before adding new number here.
115  */
116 #define ARPT_BASE_CTL		96
117 
118 #define ARPT_SO_SET_REPLACE		(ARPT_BASE_CTL)
119 #define ARPT_SO_SET_ADD_COUNTERS	(ARPT_BASE_CTL + 1)
120 #define ARPT_SO_SET_MAX			ARPT_SO_SET_ADD_COUNTERS
121 
122 #define ARPT_SO_GET_INFO		(ARPT_BASE_CTL)
123 #define ARPT_SO_GET_ENTRIES		(ARPT_BASE_CTL + 1)
124 /* #define ARPT_SO_GET_REVISION_MATCH	(APRT_BASE_CTL + 2) */
125 #define ARPT_SO_GET_REVISION_TARGET	(ARPT_BASE_CTL + 3)
126 #define ARPT_SO_GET_MAX			(ARPT_SO_GET_REVISION_TARGET)
127 
128 /* CONTINUE verdict for targets */
129 #define ARPT_CONTINUE XT_CONTINUE
130 
131 /* For standard target */
132 #define ARPT_RETURN XT_RETURN
133 
134 /* The argument to ARPT_SO_GET_INFO */
135 struct arpt_getinfo
136 {
137 	/* Which table: caller fills this in. */
138 	char name[ARPT_TABLE_MAXNAMELEN];
139 
140 	/* Kernel fills these in. */
141 	/* Which hook entry points are valid: bitmask */
142 	unsigned int valid_hooks;
143 
144 	/* Hook entry points: one per netfilter hook. */
145 	unsigned int hook_entry[NF_ARP_NUMHOOKS];
146 
147 	/* Underflow points. */
148 	unsigned int underflow[NF_ARP_NUMHOOKS];
149 
150 	/* Number of entries */
151 	unsigned int num_entries;
152 
153 	/* Size of entries. */
154 	unsigned int size;
155 };
156 
157 /* The argument to ARPT_SO_SET_REPLACE. */
158 struct arpt_replace
159 {
160 	/* Which table. */
161 	char name[ARPT_TABLE_MAXNAMELEN];
162 
163 	/* Which hook entry points are valid: bitmask.  You can't
164            change this. */
165 	unsigned int valid_hooks;
166 
167 	/* Number of entries */
168 	unsigned int num_entries;
169 
170 	/* Total size of new entries */
171 	unsigned int size;
172 
173 	/* Hook entry points. */
174 	unsigned int hook_entry[NF_ARP_NUMHOOKS];
175 
176 	/* Underflow points. */
177 	unsigned int underflow[NF_ARP_NUMHOOKS];
178 
179 	/* Information about old entries: */
180 	/* Number of counters (must be equal to current number of entries). */
181 	unsigned int num_counters;
182 	/* The old entries' counters. */
183 	struct xt_counters __user *counters;
184 
185 	/* The entries (hang off end: not really an array). */
186 	struct arpt_entry entries[0];
187 };
188 
189 /* The argument to ARPT_SO_ADD_COUNTERS. */
190 #define arpt_counters_info xt_counters_info
191 #define arpt_counters xt_counters
192 
193 /* The argument to ARPT_SO_GET_ENTRIES. */
194 struct arpt_get_entries
195 {
196 	/* Which table: user fills this in. */
197 	char name[ARPT_TABLE_MAXNAMELEN];
198 
199 	/* User fills this in: total entry size. */
200 	unsigned int size;
201 
202 	/* The entries. */
203 	struct arpt_entry entrytable[0];
204 };
205 
206 /* Standard return verdict, or do jump. */
207 #define ARPT_STANDARD_TARGET XT_STANDARD_TARGET
208 /* Error verdict. */
209 #define ARPT_ERROR_TARGET XT_ERROR_TARGET
210 
211 /* Helper functions */
arpt_get_target(struct arpt_entry * e)212 static __inline__ struct arpt_entry_target *arpt_get_target(struct arpt_entry *e)
213 {
214 	return (void *)e + e->target_offset;
215 }
216 
217 /* fn returns 0 to continue iteration */
218 #define ARPT_ENTRY_ITERATE(entries, size, fn, args...) \
219 	XT_ENTRY_ITERATE(struct arpt_entry, entries, size, fn, ## args)
220 
221 /*
222  *	Main firewall chains definitions and global var's definitions.
223  */
224 #ifdef __KERNEL__
225 
226 /* Standard entry. */
227 struct arpt_standard
228 {
229 	struct arpt_entry entry;
230 	struct arpt_standard_target target;
231 };
232 
233 struct arpt_error_target
234 {
235 	struct arpt_entry_target target;
236 	char errorname[ARPT_FUNCTION_MAXNAMELEN];
237 };
238 
239 struct arpt_error
240 {
241 	struct arpt_entry entry;
242 	struct arpt_error_target target;
243 };
244 
245 #define ARPT_ENTRY_INIT(__size)						       \
246 {									       \
247 	.target_offset	= sizeof(struct arpt_entry),			       \
248 	.next_offset	= (__size),					       \
249 }
250 
251 #define ARPT_STANDARD_INIT(__verdict)					       \
252 {									       \
253 	.entry		= ARPT_ENTRY_INIT(sizeof(struct arpt_standard)),       \
254 	.target		= XT_TARGET_INIT(ARPT_STANDARD_TARGET,		       \
255 					 sizeof(struct arpt_standard_target)), \
256 	.target.verdict	= -(__verdict) - 1,				       \
257 }
258 
259 #define ARPT_ERROR_INIT							       \
260 {									       \
261 	.entry		= ARPT_ENTRY_INIT(sizeof(struct arpt_error)),	       \
262 	.target		= XT_TARGET_INIT(ARPT_ERROR_TARGET,		       \
263 					 sizeof(struct arpt_error_target)),    \
264 	.target.errorname = "ERROR",					       \
265 }
266 
267 extern struct xt_table *arpt_register_table(struct net *net,
268 					    struct xt_table *table,
269 					    const struct arpt_replace *repl);
270 extern void arpt_unregister_table(struct xt_table *table);
271 extern unsigned int arpt_do_table(struct sk_buff *skb,
272 				  unsigned int hook,
273 				  const struct net_device *in,
274 				  const struct net_device *out,
275 				  struct xt_table *table);
276 
277 #define ARPT_ALIGN(s) XT_ALIGN(s)
278 
279 #ifdef CONFIG_COMPAT
280 #include <net/compat.h>
281 
282 struct compat_arpt_entry
283 {
284 	struct arpt_arp arp;
285 	u_int16_t target_offset;
286 	u_int16_t next_offset;
287 	compat_uint_t comefrom;
288 	struct compat_xt_counters counters;
289 	unsigned char elems[0];
290 };
291 
292 static inline struct arpt_entry_target *
compat_arpt_get_target(struct compat_arpt_entry * e)293 compat_arpt_get_target(struct compat_arpt_entry *e)
294 {
295 	return (void *)e + e->target_offset;
296 }
297 
298 #define COMPAT_ARPT_ALIGN(s)	COMPAT_XT_ALIGN(s)
299 
300 /* fn returns 0 to continue iteration */
301 #define COMPAT_ARPT_ENTRY_ITERATE(entries, size, fn, args...) \
302 	XT_ENTRY_ITERATE(struct compat_arpt_entry, entries, size, fn, ## args)
303 
304 #define COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entries, size, n, fn, args...) \
305 	XT_ENTRY_ITERATE_CONTINUE(struct compat_arpt_entry, entries, size, n, \
306 				  fn, ## args)
307 
308 #endif /* CONFIG_COMPAT */
309 #endif /*__KERNEL__*/
310 #endif /* _ARPTABLES_H */
311