blob: 7e193c9241b35f3c7c1d1a8c91005af66e77f014 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/in.h>
15#include <linux/if_arp.h>
16#include <linux/skbuff.h>
17#endif
Patrick McHardyc8942f12008-05-21 14:08:38 -070018#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/compiler.h>
20#include <linux/netfilter_arp.h>
21
Harald Welte2e4e6a12006-01-12 13:30:04 -080022#include <linux/netfilter/x_tables.h>
23
Jan Engelhardt12b00c22010-10-13 15:56:56 +020024#ifndef __KERNEL__
Harald Welte2e4e6a12006-01-12 13:30:04 -080025#define ARPT_FUNCTION_MAXNAMELEN XT_FUNCTION_MAXNAMELEN
26#define ARPT_TABLE_MAXNAMELEN XT_TABLE_MAXNAMELEN
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +020027#define arpt_entry_target xt_entry_target
28#define arpt_standard_target xt_standard_target
Jan Engelhardt12b00c22010-10-13 15:56:56 +020029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define ARPT_DEV_ADDR_LEN_MAX 16
32
33struct arpt_devaddr_info {
34 char addr[ARPT_DEV_ADDR_LEN_MAX];
35 char mask[ARPT_DEV_ADDR_LEN_MAX];
36};
37
38/* Yes, Virginia, you have to zero the padding. */
39struct arpt_arp {
40 /* Source and target IP addr */
41 struct in_addr src, tgt;
42 /* Mask for src and target IP addr */
43 struct in_addr smsk, tmsk;
44
45 /* Device hw address length, src+target device addresses */
46 u_int8_t arhln, arhln_mask;
47 struct arpt_devaddr_info src_devaddr;
48 struct arpt_devaddr_info tgt_devaddr;
49
50 /* ARP operation code. */
Al Viro59b8bfd2006-09-28 14:21:07 -070051 __be16 arpop, arpop_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* ARP hardware address and protocol address format. */
Al Viro59b8bfd2006-09-28 14:21:07 -070054 __be16 arhrd, arhrd_mask;
55 __be16 arpro, arpro_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 /* The protocol address length is only accepted if it is 4
58 * so there is no use in offering a way to do filtering on it.
59 */
60
61 char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
62 unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];
63
64 /* Flags word */
65 u_int8_t flags;
66 /* Inverse flags */
67 u_int16_t invflags;
68};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Values for "flag" field in struct arpt_ip (general arp structure).
71 * No flags defined yet.
72 */
73#define ARPT_F_MASK 0x00 /* All possible flag bits mask. */
74
75/* Values for "inv" field in struct arpt_arp. */
76#define ARPT_INV_VIA_IN 0x0001 /* Invert the sense of IN IFACE. */
77#define ARPT_INV_VIA_OUT 0x0002 /* Invert the sense of OUT IFACE */
78#define ARPT_INV_SRCIP 0x0004 /* Invert the sense of SRC IP. */
79#define ARPT_INV_TGTIP 0x0008 /* Invert the sense of TGT IP. */
80#define ARPT_INV_SRCDEVADDR 0x0010 /* Invert the sense of SRC DEV ADDR. */
81#define ARPT_INV_TGTDEVADDR 0x0020 /* Invert the sense of TGT DEV ADDR. */
82#define ARPT_INV_ARPOP 0x0040 /* Invert the sense of ARP OP. */
83#define ARPT_INV_ARPHRD 0x0080 /* Invert the sense of ARP HRD. */
84#define ARPT_INV_ARPPRO 0x0100 /* Invert the sense of ARP PRO. */
85#define ARPT_INV_ARPHLN 0x0200 /* Invert the sense of ARP HLN. */
86#define ARPT_INV_MASK 0x03FF /* All possible flag bits mask. */
87
88/* This structure defines each of the firewall rules. Consists of 3
89 parts which are 1) general ARP header stuff 2) match specific
90 stuff 3) the target to perform if the rule matches */
91struct arpt_entry
92{
93 struct arpt_arp arp;
94
95 /* Size of arpt_entry + matches */
96 u_int16_t target_offset;
97 /* Size of arpt_entry + matches + target */
98 u_int16_t next_offset;
99
100 /* Back pointer */
101 unsigned int comefrom;
102
103 /* Packet and byte counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104 struct xt_counters counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* The matches (if any), then the target. */
107 unsigned char elems[0];
108};
109
110/*
111 * New IP firewall options for [gs]etsockopt at the RAW IP level.
112 * Unlike BSD Linux inherits IP options so you don't have to use a raw
113 * socket for this. Instead we check rights in the calls.
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800114 *
115 * ATTENTION: check linux/in.h before adding new number here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800117#define ARPT_BASE_CTL 96
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800119#define ARPT_SO_SET_REPLACE (ARPT_BASE_CTL)
120#define ARPT_SO_SET_ADD_COUNTERS (ARPT_BASE_CTL + 1)
121#define ARPT_SO_SET_MAX ARPT_SO_SET_ADD_COUNTERS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Yasuyuki Kozakaib96e7ec2006-11-14 19:48:48 -0800123#define ARPT_SO_GET_INFO (ARPT_BASE_CTL)
124#define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1)
125/* #define ARPT_SO_GET_REVISION_MATCH (APRT_BASE_CTL + 2) */
126#define ARPT_SO_GET_REVISION_TARGET (ARPT_BASE_CTL + 3)
127#define ARPT_SO_GET_MAX (ARPT_SO_GET_REVISION_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* CONTINUE verdict for targets */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130#define ARPT_CONTINUE XT_CONTINUE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/* For standard target */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800133#define ARPT_RETURN XT_RETURN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* The argument to ARPT_SO_GET_INFO */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800136struct arpt_getinfo {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* Which table: caller fills this in. */
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200138 char name[XT_TABLE_MAXNAMELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
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. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800158struct arpt_replace {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Which table. */
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200160 char name[XT_TABLE_MAXNAMELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Which hook entry points are valid: bitmask. You can't
163 change this. */
164 unsigned int valid_hooks;
165
166 /* Number of entries */
167 unsigned int num_entries;
168
169 /* Total size of new entries */
170 unsigned int size;
171
172 /* Hook entry points. */
173 unsigned int hook_entry[NF_ARP_NUMHOOKS];
174
175 /* Underflow points. */
176 unsigned int underflow[NF_ARP_NUMHOOKS];
177
178 /* Information about old entries: */
179 /* Number of counters (must be equal to current number of entries). */
180 unsigned int num_counters;
181 /* The old entries' counters. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800182 struct xt_counters __user *counters;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* The entries (hang off end: not really an array). */
185 struct arpt_entry entries[0];
186};
187
188/* The argument to ARPT_SO_ADD_COUNTERS. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800189#define arpt_counters_info xt_counters_info
Bart De Schuymer8c82d8d2007-01-09 14:33:11 -0800190#define arpt_counters xt_counters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/* The argument to ARPT_SO_GET_ENTRIES. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800193struct arpt_get_entries {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Which table: user fills this in. */
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200195 char name[XT_TABLE_MAXNAMELEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* User fills this in: total entry size. */
198 unsigned int size;
199
200 /* The entries. */
201 struct arpt_entry entrytable[0];
202};
203
204/* Standard return verdict, or do jump. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800205#define ARPT_STANDARD_TARGET XT_STANDARD_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Error verdict. */
Harald Welte2e4e6a12006-01-12 13:30:04 -0800207#define ARPT_ERROR_TARGET XT_ERROR_TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/* Helper functions */
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200210static __inline__ struct xt_entry_target *arpt_get_target(struct arpt_entry *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 return (void *)e + e->target_offset;
213}
214
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100215#ifndef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* fn returns 0 to continue iteration */
Patrick McHardy89c002d2007-12-17 21:46:59 -0800217#define ARPT_ENTRY_ITERATE(entries, size, fn, args...) \
218 XT_ENTRY_ITERATE(struct arpt_entry, entries, size, fn, ## args)
Jan Engelhardt72b2b1d2010-02-24 18:32:59 +0100219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
222 * Main firewall chains definitions and global var's definitions.
223 */
224#ifdef __KERNEL__
225
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700226/* Standard entry. */
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800227struct arpt_standard {
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700228 struct arpt_entry entry;
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200229 struct xt_standard_target target;
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700230};
231
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800232struct arpt_error_target {
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200233 struct xt_entry_target target;
Jan Engelhardt12b00c22010-10-13 15:56:56 +0200234 char errorname[XT_FUNCTION_MAXNAMELEN];
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700235};
236
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800237struct arpt_error {
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700238 struct arpt_entry entry;
239 struct arpt_error_target target;
240};
241
242#define ARPT_ENTRY_INIT(__size) \
243{ \
244 .target_offset = sizeof(struct arpt_entry), \
245 .next_offset = (__size), \
246}
247
248#define ARPT_STANDARD_INIT(__verdict) \
249{ \
250 .entry = ARPT_ENTRY_INIT(sizeof(struct arpt_standard)), \
251 .target = XT_TARGET_INIT(ARPT_STANDARD_TARGET, \
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200252 sizeof(struct xt_standard_target)), \
Patrick McHardy3c2ad462007-05-10 14:14:16 -0700253 .target.verdict = -(__verdict) - 1, \
254}
255
256#define ARPT_ERROR_INIT \
257{ \
258 .entry = ARPT_ENTRY_INIT(sizeof(struct arpt_error)), \
259 .target = XT_TARGET_INIT(ARPT_ERROR_TARGET, \
260 sizeof(struct arpt_error_target)), \
261 .target.errorname = "ERROR", \
262}
263
Jan Engelhardte3eaa992009-06-17 22:14:54 +0200264extern void *arpt_alloc_initial_table(const struct xt_table *);
Jan Engelhardt4abff072008-04-14 11:15:43 +0200265extern struct xt_table *arpt_register_table(struct net *net,
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200266 const struct xt_table *table,
Jan Engelhardt4abff072008-04-14 11:15:43 +0200267 const struct arpt_replace *repl);
268extern void arpt_unregister_table(struct xt_table *table);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700269extern unsigned int arpt_do_table(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned int hook,
271 const struct net_device *in,
272 const struct net_device *out,
Jan Engelhardt4abff072008-04-14 11:15:43 +0200273 struct xt_table *table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Patrick McHardy0495cf92007-12-17 21:55:34 -0800275#define ARPT_ALIGN(s) XT_ALIGN(s)
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800276
277#ifdef CONFIG_COMPAT
278#include <net/compat.h>
279
Eric Dumazetd94d9fe2009-11-04 09:50:58 -0800280struct compat_arpt_entry {
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800281 struct arpt_arp arp;
282 u_int16_t target_offset;
283 u_int16_t next_offset;
284 compat_uint_t comefrom;
285 struct compat_xt_counters counters;
286 unsigned char elems[0];
287};
288
Jan Engelhardt87a2e70d2010-10-13 16:11:22 +0200289static inline struct xt_entry_target *
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800290compat_arpt_get_target(struct compat_arpt_entry *e)
291{
292 return (void *)e + e->target_offset;
293}
294
295#define COMPAT_ARPT_ALIGN(s) COMPAT_XT_ALIGN(s)
296
Patrick McHardyd6a2ba02007-12-17 22:26:54 -0800297#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif /*__KERNEL__*/
299#endif /* _ARPTABLES_H */