blob: 82f854bf37e75239479e7bc8c11d2652ab2f921a [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
34struct ebt_counter
35{
36 uint64_t pcnt;
37 uint64_t bcnt;
38};
39
40struct ebt_replace
41{
42 char name[EBT_TABLE_MAXNAMELEN];
43 unsigned int valid_hooks;
44 /* nr of rules in the table */
45 unsigned int nentries;
46 /* total size of the entries */
47 unsigned int entries_size;
48 /* start of the chains */
Al Viro1e419cd2006-11-30 19:28:48 -080049 struct ebt_entries __user *hook_entry[NF_BR_NUMHOOKS];
50 /* nr of counters userspace expects back */
51 unsigned int num_counters;
52 /* where the kernel will put the old counters */
53 struct ebt_counter __user *counters;
54 char __user *entries;
55};
56
57struct ebt_replace_kernel
58{
59 char name[EBT_TABLE_MAXNAMELEN];
60 unsigned int valid_hooks;
61 /* nr of rules in the table */
62 unsigned int nentries;
63 /* total size of the entries */
64 unsigned int entries_size;
65 /* start of the chains */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
67 /* nr of counters userspace expects back */
68 unsigned int num_counters;
69 /* where the kernel will put the old counters */
70 struct ebt_counter *counters;
71 char *entries;
72};
73
74struct ebt_entries {
75 /* this field is always set to zero
76 * See EBT_ENTRY_OR_ENTRIES.
77 * Must be same size as ebt_entry.bitmask */
78 unsigned int distinguisher;
79 /* the chain name */
80 char name[EBT_CHAIN_MAXNAMELEN];
81 /* counter offset for this chain */
82 unsigned int counter_offset;
83 /* one standard (accept, drop, return) per hook */
84 int policy;
85 /* nr. of entries */
86 unsigned int nentries;
87 /* entry list */
88 char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
89};
90
91/* used for the bitmask of struct ebt_entry */
92
93/* This is a hack to make a difference between an ebt_entry struct and an
94 * ebt_entries struct when traversing the entries from start to end.
95 * Using this simplifies the code alot, while still being able to use
96 * ebt_entries.
97 * Contrary, iptables doesn't use something like ebt_entries and therefore uses
98 * different techniques for naming the policy and such. So, iptables doesn't
99 * need a hack like this.
100 */
101#define EBT_ENTRY_OR_ENTRIES 0x01
102/* these are the normal masks */
103#define EBT_NOPROTO 0x02
104#define EBT_802_3 0x04
105#define EBT_SOURCEMAC 0x08
106#define EBT_DESTMAC 0x10
107#define EBT_F_MASK (EBT_NOPROTO | EBT_802_3 | EBT_SOURCEMAC | EBT_DESTMAC \
108 | EBT_ENTRY_OR_ENTRIES)
109
110#define EBT_IPROTO 0x01
111#define EBT_IIN 0x02
112#define EBT_IOUT 0x04
113#define EBT_ISOURCE 0x8
114#define EBT_IDEST 0x10
115#define EBT_ILOGICALIN 0x20
116#define EBT_ILOGICALOUT 0x40
117#define EBT_INV_MASK (EBT_IPROTO | EBT_IIN | EBT_IOUT | EBT_ILOGICALIN \
118 | EBT_ILOGICALOUT | EBT_ISOURCE | EBT_IDEST)
119
120struct ebt_entry_match
121{
122 union {
123 char name[EBT_FUNCTION_MAXNAMELEN];
124 struct ebt_match *match;
125 } u;
126 /* size of data */
127 unsigned int match_size;
128 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
129};
130
131struct ebt_entry_watcher
132{
133 union {
134 char name[EBT_FUNCTION_MAXNAMELEN];
135 struct ebt_watcher *watcher;
136 } u;
137 /* size of data */
138 unsigned int watcher_size;
139 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
140};
141
142struct ebt_entry_target
143{
144 union {
145 char name[EBT_FUNCTION_MAXNAMELEN];
146 struct ebt_target *target;
147 } u;
148 /* size of data */
149 unsigned int target_size;
150 unsigned char data[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
151};
152
153#define EBT_STANDARD_TARGET "standard"
154struct ebt_standard_target
155{
156 struct ebt_entry_target target;
157 int verdict;
158};
159
160/* one entry */
161struct ebt_entry {
162 /* this needs to be the first field */
163 unsigned int bitmask;
164 unsigned int invflags;
Al Viro47c183f2006-11-14 21:11:51 -0800165 __be16 ethproto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* the physical in-dev */
167 char in[IFNAMSIZ];
168 /* the logical in-dev */
169 char logical_in[IFNAMSIZ];
170 /* the physical out-dev */
171 char out[IFNAMSIZ];
172 /* the logical out-dev */
173 char logical_out[IFNAMSIZ];
174 unsigned char sourcemac[ETH_ALEN];
175 unsigned char sourcemsk[ETH_ALEN];
176 unsigned char destmac[ETH_ALEN];
177 unsigned char destmsk[ETH_ALEN];
178 /* sizeof ebt_entry + matches */
179 unsigned int watchers_offset;
180 /* sizeof ebt_entry + matches + watchers */
181 unsigned int target_offset;
182 /* sizeof ebt_entry + matches + watchers + target */
183 unsigned int next_offset;
184 unsigned char elems[0] __attribute__ ((aligned (__alignof__(struct ebt_replace))));
185};
186
187/* {g,s}etsockopt numbers */
188#define EBT_BASE_CTL 128
189
190#define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
191#define EBT_SO_SET_COUNTERS (EBT_SO_SET_ENTRIES+1)
192#define EBT_SO_SET_MAX (EBT_SO_SET_COUNTERS+1)
193
194#define EBT_SO_GET_INFO (EBT_BASE_CTL)
195#define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO+1)
196#define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES+1)
197#define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO+1)
198#define EBT_SO_GET_MAX (EBT_SO_GET_INIT_ENTRIES+1)
199
200#ifdef __KERNEL__
201
202/* return values for match() functions */
203#define EBT_MATCH 0
204#define EBT_NOMATCH 1
205
206struct ebt_match
207{
208 struct list_head list;
209 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200210 bool (*match)(const struct sk_buff *skb, const struct net_device *in,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 const struct net_device *out, const void *matchdata,
212 unsigned int datalen);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200213 bool (*check)(const char *tablename, unsigned int hookmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 const struct ebt_entry *e, void *matchdata, unsigned int datalen);
215 void (*destroy)(void *matchdata, unsigned int datalen);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200216 unsigned int matchsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200217 u_int8_t revision;
218 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct module *me;
220};
221
222struct ebt_watcher
223{
224 struct list_head list;
225 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200226 unsigned int (*watcher)(const struct sk_buff *skb, unsigned int hooknr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 const struct net_device *in, const struct net_device *out,
228 const void *watcherdata, unsigned int datalen);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200229 bool (*check)(const char *tablename, unsigned int hookmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 const struct ebt_entry *e, void *watcherdata, unsigned int datalen);
231 void (*destroy)(void *watcherdata, unsigned int datalen);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200232 unsigned int targetsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200233 u_int8_t revision;
234 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct module *me;
236};
237
238struct ebt_target
239{
240 struct list_head list;
241 const char name[EBT_FUNCTION_MAXNAMELEN];
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200242 /* returns one of the standard EBT_* verdicts */
243 unsigned int (*target)(struct sk_buff *skb, unsigned int hooknr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 const struct net_device *in, const struct net_device *out,
245 const void *targetdata, unsigned int datalen);
Jan Engelhardt19eda872008-10-08 11:35:13 +0200246 bool (*check)(const char *tablename, unsigned int hookmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 const struct ebt_entry *e, void *targetdata, unsigned int datalen);
248 void (*destroy)(void *targetdata, unsigned int datalen);
Jan Engelhardt18219d32008-10-08 11:35:13 +0200249 unsigned int targetsize;
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200250 u_int8_t revision;
251 u_int8_t family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct module *me;
253};
254
255/* used for jumping from and into user defined chains (udc) */
256struct ebt_chainstack
257{
258 struct ebt_entries *chaininfo; /* pointer to chain data */
259 struct ebt_entry *e; /* pointer to entry data */
260 unsigned int n; /* n'th entry */
261};
262
263struct ebt_table_info
264{
265 /* total size of the entries */
266 unsigned int entries_size;
267 unsigned int nentries;
268 /* pointers to the start of the chains */
269 struct ebt_entries *hook_entry[NF_BR_NUMHOOKS];
270 /* room to maintain the stack used for jumping from and into udc */
271 struct ebt_chainstack **chainstack;
272 char *entries;
273 struct ebt_counter counters[0] ____cacheline_aligned;
274};
275
276struct ebt_table
277{
278 struct list_head list;
279 char name[EBT_TABLE_MAXNAMELEN];
Al Viro1e419cd2006-11-30 19:28:48 -0800280 struct ebt_replace_kernel *table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unsigned int valid_hooks;
282 rwlock_t lock;
283 /* e.g. could be the table explicitly only allows certain
284 * matches, targets, ... 0 == let it in */
285 int (*check)(const struct ebt_table_info *info,
286 unsigned int valid_hooks);
287 /* the data used by the kernel */
288 struct ebt_table_info *private;
289 struct module *me;
290};
291
292#define EBT_ALIGN(s) (((s) + (__alignof__(struct ebt_replace)-1)) & \
293 ~(__alignof__(struct ebt_replace)-1))
294extern int ebt_register_table(struct ebt_table *table);
295extern void ebt_unregister_table(struct ebt_table *table);
296extern int ebt_register_match(struct ebt_match *match);
297extern void ebt_unregister_match(struct ebt_match *match);
298extern int ebt_register_watcher(struct ebt_watcher *watcher);
299extern void ebt_unregister_watcher(struct ebt_watcher *watcher);
300extern int ebt_register_target(struct ebt_target *target);
301extern void ebt_unregister_target(struct ebt_target *target);
Herbert Xu3db05fe2007-10-15 00:53:15 -0700302extern unsigned int ebt_do_table(unsigned int hook, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 const struct net_device *in, const struct net_device *out,
304 struct ebt_table *table);
305
306/* Used in the kernel match() functions */
307#define FWINV(bool,invflg) ((bool) ^ !!(info->invflags & invflg))
308/* True if the hook mask denotes that the rule is in a base chain,
309 * used in the check() functions */
310#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
311/* Clear the bit in the hook mask that tells if the rule is on a base chain */
312#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
313/* True if the target is not a standard target */
314#define INVALID_TARGET (info->target < -NUM_STANDARD_TARGETS || info->target >= 0)
315
316#endif /* __KERNEL__ */
317
318/* blatently stolen from ip_tables.h
319 * fn returns 0 to continue iteration */
320#define EBT_MATCH_ITERATE(e, fn, args...) \
321({ \
322 unsigned int __i; \
323 int __ret = 0; \
324 struct ebt_entry_match *__match; \
325 \
326 for (__i = sizeof(struct ebt_entry); \
327 __i < (e)->watchers_offset; \
328 __i += __match->match_size + \
329 sizeof(struct ebt_entry_match)) { \
330 __match = (void *)(e) + __i; \
331 \
332 __ret = fn(__match , ## args); \
333 if (__ret != 0) \
334 break; \
335 } \
336 if (__ret == 0) { \
337 if (__i != (e)->watchers_offset) \
338 __ret = -EINVAL; \
339 } \
340 __ret; \
341})
342
343#define EBT_WATCHER_ITERATE(e, fn, args...) \
344({ \
345 unsigned int __i; \
346 int __ret = 0; \
347 struct ebt_entry_watcher *__watcher; \
348 \
349 for (__i = e->watchers_offset; \
350 __i < (e)->target_offset; \
351 __i += __watcher->watcher_size + \
352 sizeof(struct ebt_entry_watcher)) { \
353 __watcher = (void *)(e) + __i; \
354 \
355 __ret = fn(__watcher , ## args); \
356 if (__ret != 0) \
357 break; \
358 } \
359 if (__ret == 0) { \
360 if (__i != (e)->target_offset) \
361 __ret = -EINVAL; \
362 } \
363 __ret; \
364})
365
366#define EBT_ENTRY_ITERATE(entries, size, fn, args...) \
367({ \
368 unsigned int __i; \
369 int __ret = 0; \
370 struct ebt_entry *__entry; \
371 \
372 for (__i = 0; __i < (size);) { \
373 __entry = (void *)(entries) + __i; \
374 __ret = fn(__entry , ## args); \
375 if (__ret != 0) \
376 break; \
377 if (__entry->bitmask != 0) \
378 __i += __entry->next_offset; \
379 else \
380 __i += sizeof(struct ebt_entries); \
381 } \
382 if (__ret == 0) { \
383 if (__i != (size)) \
384 __ret = -EINVAL; \
385 } \
386 __ret; \
387})
388
389#endif