blob: 568a690f6a628a9e9db14745a1b6befa6b19246c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebtables
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * ebtables.c,v 2.0, April, 2002
8 *
9 * This code is stongly inspired on the iptables code which is
10 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
11 */
12
13#ifndef __LINUX_BRIDGE_EFF_H
14#define __LINUX_BRIDGE_EFF_H
15#include <linux/if.h>
16#include <linux/netfilter_bridge.h>
17#include <linux/if_ether.h>
18
19#define EBT_TABLE_MAXNAMELEN 32
20#define EBT_CHAIN_MAXNAMELEN EBT_TABLE_MAXNAMELEN
21#define EBT_FUNCTION_MAXNAMELEN EBT_TABLE_MAXNAMELEN
22
23/* verdicts >0 are "branches" */
24#define EBT_ACCEPT -1
25#define EBT_DROP -2
26#define EBT_CONTINUE -3
27#define EBT_RETURN -4
28#define NUM_STANDARD_TARGETS 4
Bart De Schuymerd12cdc32006-11-29 02:35:40 +010029/* ebtables target modules store the verdict inside an int. We can
30 * reclaim a part of this int for backwards compatible extensions.
31 * The 4 lsb are more than enough to store the verdict. */
32#define EBT_VERDICT_BITS 0x0000000F
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020034struct xt_match;
35struct xt_target;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct ebt_counter
38{
39 uint64_t pcnt;
40 uint64_t bcnt;
41};
42
43struct ebt_replace
44{
45 char name[EBT_TABLE_MAXNAMELEN];
46 unsigned int valid_hooks;
47 /* nr of rules in the table */
48 unsigned int nentries;
49 /* total size of the entries */
50 unsigned int entries_size;
51 /* start of the chains */
Al Viro1e419cd2006-11-30 19:28:48 -080052 struct ebt_entries __user *hook_entry[NF_BR_NUMHOOKS];
53 /* nr of counters userspace expects back */
54 unsigned int num_counters;
55 /* where the kernel will put the old counters */
56 struct ebt_counter __user *counters;
57 char __user *entries;
58};
59
60struct ebt_replace_kernel
61{
62 char name[EBT_TABLE_MAXNAMELEN];
63 unsigned int valid_hooks;
64 /* nr of rules in the table */
65 unsigned int nentries;
66 /* total size of the entries */
67 unsigned int entries_size;
68 /* start of the chains */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
70 /* nr of counters userspace expects back */
71 unsigned int num_counters;
72 /* where the kernel will put the old counters */
73 struct ebt_counter *counters;
74 char *entries;
75};
76
77struct ebt_entries {
78 /* this field is always set to zero
79 * See EBT_ENTRY_OR_ENTRIES.
80 * Must be same size as ebt_entry.bitmask */
81 unsigned int distinguisher;
82 /* the chain name */
83 char name[EBT_CHAIN_MAXNAMELEN];
84 /* counter offset for this chain */
85 unsigned int counter_offset;
86 /* one standard (accept, drop, return) per hook */
87 int policy;
88 /* nr. of entries */
89 unsigned int nentries;
90 /* entry list */
91 char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
92};
93
94/* used for the bitmask of struct ebt_entry */
95
96/* This is a hack to make a difference between an ebt_entry struct and an
97 * ebt_entries struct when traversing the entries from start to end.
98 * Using this simplifies the code alot, while still being able to use
99 * ebt_entries.
100 * Contrary, iptables doesn't use something like ebt_entries and therefore uses
101 * different techniques for naming the policy and such. So, iptables doesn't
102 * need a hack like this.
103 */
104#define EBT_ENTRY_OR_ENTRIES 0x01
105/* these are the normal masks */
106#define EBT_NOPROTO 0x02
107#define EBT_802_3 0x04
108#define EBT_SOURCEMAC 0x08
109#define EBT_DESTMAC 0x10
110#define EBT_F_MASK (EBT_NOPROTO | EBT_802_3 | EBT_SOURCEMAC | EBT_DESTMAC \
111 | EBT_ENTRY_OR_ENTRIES)
112
113#define EBT_IPROTO 0x01
114#define EBT_IIN 0x02
115#define EBT_IOUT 0x04
116#define EBT_ISOURCE 0x8
117#define EBT_IDEST 0x10
118#define EBT_ILOGICALIN 0x20
119#define EBT_ILOGICALOUT 0x40
120#define EBT_INV_MASK (EBT_IPROTO | EBT_IIN | EBT_IOUT | EBT_ILOGICALIN \
121 | EBT_ILOGICALOUT | EBT_ISOURCE | EBT_IDEST)
122
123struct ebt_entry_match
124{
125 union {
126 char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt043ef462008-10-08 11:35:15 +0200127 struct xt_match *match;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } u;
129 /* size of data */
130 unsigned int match_size;
131 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
132};
133
134struct ebt_entry_watcher
135{
136 union {
137 char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt043ef462008-10-08 11:35:15 +0200138 struct xt_target *watcher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 } u;
140 /* size of data */
141 unsigned int watcher_size;
142 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
143};
144
145struct ebt_entry_target
146{
147 union {
148 char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt043ef462008-10-08 11:35:15 +0200149 struct xt_target *target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } u;
151 /* size of data */
152 unsigned int target_size;
153 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
154};
155
156#define EBT_STANDARD_TARGET "standard"
157struct ebt_standard_target
158{
159 struct ebt_entry_target target;
160 int verdict;
161};
162
163/* one entry */
164struct ebt_entry {
165 /* this needs to be the first field */
166 unsigned int bitmask;
167 unsigned int invflags;
Al Viro47c183f2006-11-14 21:11:51 -0800168 __be16 ethproto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* the physical in-dev */
170 char in[IFNAMSIZ];
171 /* the logical in-dev */
172 char logical_in[IFNAMSIZ];
173 /* the physical out-dev */
174 char out[IFNAMSIZ];
175 /* the logical out-dev */
176 char logical_out[IFNAMSIZ];
177 unsigned char sourcemac[ETH_ALEN];
178 unsigned char sourcemsk[ETH_ALEN];
179 unsigned char destmac[ETH_ALEN];
180 unsigned char destmsk[ETH_ALEN];
181 /* sizeof ebt_entry + matches */
182 unsigned int watchers_offset;
183 /* sizeof ebt_entry + matches + watchers */
184 unsigned int target_offset;
185 /* sizeof ebt_entry + matches + watchers + target */
186 unsigned int next_offset;
187 unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
188};
189
190/* {g,s}etsockopt numbers */
191#define EBT_BASE_CTL 128
192
193#define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
194#define EBT_SO_SET_COUNTERS (EBT_SO_SET_ENTRIES+1)
195#define EBT_SO_SET_MAX (EBT_SO_SET_COUNTERS+1)
196
197#define EBT_SO_GET_INFO (EBT_BASE_CTL)
198#define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO+1)
199#define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES+1)
200#define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO+1)
201#define EBT_SO_GET_MAX (EBT_SO_GET_INIT_ENTRIES+1)
202
203#ifdef __KERNEL__
204
205/* return values for match() functions */
206#define EBT_MATCH 0
207#define EBT_NOMATCH 1
208
209struct ebt_match
210{
211 struct list_head list;
212 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200213 bool (*match)(const struct sk_buff *skb, const struct net_device *in,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200214 const struct net_device *out, const struct xt_match *match,
215 const void *matchinfo, int offset, unsigned int protoff,
216 bool *hotdrop);
217 bool (*checkentry)(const char *table, const void *entry,
218 const struct xt_match *match, void *matchinfo,
219 unsigned int hook_mask);
220 void (*destroy)(const struct xt_match *match, void *matchinfo);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200221 unsigned int matchsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200222 u_int8_t revision;
223 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct module *me;
225};
226
227struct ebt_watcher
228{
229 struct list_head list;
230 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200231 unsigned int (*target)(struct sk_buff *skb,
232 const struct net_device *in, const struct net_device *out,
233 unsigned int hook_num, const struct xt_target *target,
234 const void *targinfo);
235 bool (*checkentry)(const char *table, const void *entry,
236 const struct xt_target *target, void *targinfo,
237 unsigned int hook_mask);
238 void (*destroy)(const struct xt_target *target, void *targinfo);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200239 unsigned int targetsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200240 u_int8_t revision;
241 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct module *me;
243};
244
245struct ebt_target
246{
247 struct list_head list;
248 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200249 /* returns one of the standard EBT_* verdicts */
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200250 unsigned int (*target)(struct sk_buff *skb,
251 const struct net_device *in, const struct net_device *out,
252 unsigned int hook_num, const struct xt_target *target,
253 const void *targinfo);
254 bool (*checkentry)(const char *table, const void *entry,
255 const struct xt_target *target, void *targinfo,
256 unsigned int hook_mask);
257 void (*destroy)(const struct xt_target *target, void *targinfo);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200258 unsigned int targetsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200259 u_int8_t revision;
260 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 struct module *me;
262};
263
264/* used for jumping from and into user defined chains (udc) */
265struct ebt_chainstack
266{
267 struct ebt_entries *chaininfo; /* pointer to chain data */
268 struct ebt_entry *e; /* pointer to entry data */
269 unsigned int n; /* n'th entry */
270};
271
272struct ebt_table_info
273{
274 /* total size of the entries */
275 unsigned int entries_size;
276 unsigned int nentries;
277 /* pointers to the start of the chains */
278 struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
279 /* room to maintain the stack used for jumping from and into udc */
280 struct ebt_chainstack **chainstack;
281 char *entries;
282 struct ebt_counter counters[0] ____cacheline_aligned;
283};
284
285struct ebt_table
286{
287 struct list_head list;
288 char name[EBT_TABLE_MAXNAMELEN];
Al Viro1e419cd2006-11-30 19:28:48 -0800289 struct ebt_replace_kernel *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned int valid_hooks;
291 rwlock_t lock;
292 /* e.g. could be the table explicitly only allows certain
293 * matches, targets, ... 0 == let it in */
294 int (*check)(const struct ebt_table_info *info,
295 unsigned int valid_hooks);
296 /* the data used by the kernel */
297 struct ebt_table_info *private;
298 struct module *me;
299};
300
301#define EBT_ALIGN(s) (((s) + (__alignof__(struct ebt_replace)-1)) & \
302 ~(__alignof__(struct ebt_replace)-1))
303extern int ebt_register_table(struct ebt_table *table);
304extern void ebt_unregister_table(struct ebt_table *table);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700305extern unsigned int ebt_do_table(unsigned int hook, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 const struct net_device *in, const struct net_device *out,
307 struct ebt_table *table);
308
309/* Used in the kernel match() functions */
310#define FWINV(bool,invflg) ((bool) ^ !!(info->invflags & invflg))
311/* True if the hook mask denotes that the rule is in a base chain,
312 * used in the check() functions */
313#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
314/* Clear the bit in the hook mask that tells if the rule is on a base chain */
315#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
316/* True if the target is not a standard target */
317#define INVALID_TARGET (info->target < -NUM_STANDARD_TARGETS || info->target >= 0)
318
319#endif /* __KERNEL__ */
320
321/* blatently stolen from ip_tables.h
322 * fn returns 0 to continue iteration */
323#define EBT_MATCH_ITERATE(e, fn, args...) \
324({ \
325 unsigned int __i; \
326 int __ret = 0; \
327 struct ebt_entry_match *__match; \
328 \
329 for (__i = sizeof(struct ebt_entry); \
330 __i < (e)->watchers_offset; \
331 __i += __match->match_size + \
332 sizeof(struct ebt_entry_match)) { \
333 __match = (void *)(e) + __i; \
334 \
335 __ret = fn(__match , ## args); \
336 if (__ret != 0) \
337 break; \
338 } \
339 if (__ret == 0) { \
340 if (__i != (e)->watchers_offset) \
341 __ret = -EINVAL; \
342 } \
343 __ret; \
344})
345
346#define EBT_WATCHER_ITERATE(e, fn, args...) \
347({ \
348 unsigned int __i; \
349 int __ret = 0; \
350 struct ebt_entry_watcher *__watcher; \
351 \
352 for (__i = e->watchers_offset; \
353 __i < (e)->target_offset; \
354 __i += __watcher->watcher_size + \
355 sizeof(struct ebt_entry_watcher)) { \
356 __watcher = (void *)(e) + __i; \
357 \
358 __ret = fn(__watcher , ## args); \
359 if (__ret != 0) \
360 break; \
361 } \
362 if (__ret == 0) { \
363 if (__i != (e)->target_offset) \
364 __ret = -EINVAL; \
365 } \
366 __ret; \
367})
368
369#define EBT_ENTRY_ITERATE(entries, size, fn, args...) \
370({ \
371 unsigned int __i; \
372 int __ret = 0; \
373 struct ebt_entry *__entry; \
374 \
375 for (__i = 0; __i < (size);) { \
376 __entry = (void *)(entries) + __i; \
377 __ret = fn(__entry , ## args); \
378 if (__ret != 0) \
379 break; \
380 if (__entry->bitmask != 0) \
381 __i += __entry->next_offset; \
382 else \
383 __i += sizeof(struct ebt_entries); \
384 } \
385 if (__ret == 0) { \
386 if (__i != (size)) \
387 __ret = -EINVAL; \
388 } \
389 __ret; \
390})
391
392#endif