blob: 5b040d209bacc6c12f59223ac5a0d8c41806e632 [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001/*
2 * $Id: ebtables.c,v 1.03 2002/01/19
3 *
4 * Copyright (C) 2001-2002 Bart De Schuymer
5 *
6 * This code is stongly inspired on the iptables code which is
7 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#ifndef EBTABLES_U_H
25#define EBTABLES_U_H
Bart De Schuymer9cfd6542002-08-13 16:08:08 +000026#include <netinet/in.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000027#include <linux/netfilter_bridge/ebtables.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000028
Bart De Schuymerf81c2702003-07-23 21:07:04 +000029#ifndef EBT_MIN_ALIGN
30#define EBT_MIN_ALIGN (__alignof__(struct ebt_entry_target))
31#endif
32#define EBT_ALIGN(s) (((s) + (EBT_MIN_ALIGN-1)) & ~(EBT_MIN_ALIGN-1))
Bart De Schuymer8339ff12004-01-14 20:05:27 +000033#define ERRORMSG_MAXLEN 128
Bart De Schuymerf81c2702003-07-23 21:07:04 +000034
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000035struct ebt_u_entries
36{
Bart De Schuymer09765902002-06-23 08:15:51 +000037 int policy;
Bart De Schuymer376ccfb2002-07-23 20:52:18 +000038 unsigned int nentries;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000039 /* counter offset for this chain */
Bart De Schuymer09765902002-06-23 08:15:51 +000040 unsigned int counter_offset;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000041 /* used for udc */
Bart De Schuymer09765902002-06-23 08:15:51 +000042 unsigned int hook_mask;
43 char name[EBT_CHAIN_MAXNAMELEN];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000044 struct ebt_u_entry *entries;
45};
46
Bart De Schuymer09765902002-06-23 08:15:51 +000047struct ebt_u_chain_list
48{
49 struct ebt_u_entries *udc;
50 struct ebt_u_chain_list *next;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000051 /* this is only used internally, in communication.c */
Bart De Schuymer09765902002-06-23 08:15:51 +000052 char *kernel_start;
53};
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000054
Bart De Schuymer8339ff12004-01-14 20:05:27 +000055struct ebt_cntchanges;
56struct ebt_cntchanges
57{
58 unsigned short type;
59 struct ebt_cntchanges *next;
60};
61
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000062struct ebt_u_replace
63{
64 char name[EBT_TABLE_MAXNAMELEN];
65 unsigned int valid_hooks;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000066 /* nr of rules in the table */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000067 unsigned int nentries;
68 struct ebt_u_entries *hook_entry[NF_BR_NUMHOOKS];
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000069 /* user defined chains (udc) list */
Bart De Schuymer09765902002-06-23 08:15:51 +000070 struct ebt_u_chain_list *udc;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000071 /* nr of counters userspace expects back */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000072 unsigned int num_counters;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000073 /* where the kernel will put the old counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000074 struct ebt_counter *counters;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000075 /*
76 * can be used e.g. to know if a standard option
77 * has been specified twice
78 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000079 unsigned int flags;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000080 /* we stick the specified command (e.g. -A) in here */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000081 char command;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000082 /*
Bart De Schuymer8339ff12004-01-14 20:05:27 +000083 * here we stick the chain to do our thing on (can be -1 if unspecified)
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000084 */
Bart De Schuymer8339ff12004-01-14 20:05:27 +000085 int selected_chain;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000086 /* used for the atomic option */
Bart De Schuymer8330e892002-07-14 19:06:47 +000087 char *filename;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000088 /* tells what happened to the old rules */
Bart De Schuymer8339ff12004-01-14 20:05:27 +000089 struct ebt_cntchanges *counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000090};
91
92struct ebt_u_table
93{
94 char name[EBT_TABLE_MAXNAMELEN];
Bart De Schuymerf57f50f2002-08-29 16:52:36 +000095 void (*check)(struct ebt_u_replace *repl);
Bart De Schuymer8339ff12004-01-14 20:05:27 +000096 void (*help)(const char **);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000097 struct ebt_u_table *next;
98};
99
100struct ebt_u_match_list
101{
102 struct ebt_u_match_list *next;
103 struct ebt_entry_match *m;
104};
105
106struct ebt_u_watcher_list
107{
108 struct ebt_u_watcher_list *next;
109 struct ebt_entry_watcher *w;
110};
111
112struct ebt_u_entry
113{
Bart De Schuymer376ccfb2002-07-23 20:52:18 +0000114 unsigned int bitmask;
115 unsigned int invflags;
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000116 uint16_t ethproto;
Bart De Schuymer376ccfb2002-07-23 20:52:18 +0000117 char in[IFNAMSIZ];
118 char logical_in[IFNAMSIZ];
119 char out[IFNAMSIZ];
120 char logical_out[IFNAMSIZ];
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000121 unsigned char sourcemac[ETH_ALEN];
122 unsigned char sourcemsk[ETH_ALEN];
123 unsigned char destmac[ETH_ALEN];
124 unsigned char destmsk[ETH_ALEN];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000125 struct ebt_u_match_list *m_list;
126 struct ebt_u_watcher_list *w_list;
127 struct ebt_entry_target *t;
128 struct ebt_u_entry *next;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000129 /* the standard target needs this to know the name of a udc when
130 * printing out rules. */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000131 struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000132};
133
134struct ebt_u_match
135{
136 char name[EBT_FUNCTION_MAXNAMELEN];
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000137 /* size of the real match data */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000138 unsigned int size;
139 void (*help)(void);
140 void (*init)(struct ebt_entry_match *m);
141 int (*parse)(int c, char **argv, int argc,
142 const struct ebt_u_entry *entry, unsigned int *flags,
143 struct ebt_entry_match **match);
144 void (*final_check)(const struct ebt_u_entry *entry,
145 const struct ebt_entry_match *match,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000146 const char *name, unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000147 void (*print)(const struct ebt_u_entry *entry,
148 const struct ebt_entry_match *match);
149 int (*compare)(const struct ebt_entry_match *m1,
150 const struct ebt_entry_match *m2);
151 const struct option *extra_ops;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000152 /*
153 * can be used e.g. to check for multiple occurance of the same option
154 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000155 unsigned int flags;
156 unsigned int option_offset;
157 struct ebt_entry_match *m;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000158 /*
159 * if used == 1 we no longer have to add it to
160 * the match chain of the new entry
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000161 * be sure to put it back on 0 when finished
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000162 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000163 unsigned int used;
164 struct ebt_u_match *next;
165};
166
167struct ebt_u_watcher
168{
169 char name[EBT_FUNCTION_MAXNAMELEN];
170 unsigned int size;
171 void (*help)(void);
172 void (*init)(struct ebt_entry_watcher *w);
173 int (*parse)(int c, char **argv, int argc,
174 const struct ebt_u_entry *entry, unsigned int *flags,
175 struct ebt_entry_watcher **watcher);
176 void (*final_check)(const struct ebt_u_entry *entry,
177 const struct ebt_entry_watcher *watch, const char *name,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000178 unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000179 void (*print)(const struct ebt_u_entry *entry,
180 const struct ebt_entry_watcher *watcher);
181 int (*compare)(const struct ebt_entry_watcher *w1,
182 const struct ebt_entry_watcher *w2);
183 const struct option *extra_ops;
184 unsigned int flags;
185 unsigned int option_offset;
186 struct ebt_entry_watcher *w;
187 unsigned int used;
188 struct ebt_u_watcher *next;
189};
190
191struct ebt_u_target
192{
193 char name[EBT_FUNCTION_MAXNAMELEN];
194 unsigned int size;
195 void (*help)(void);
196 void (*init)(struct ebt_entry_target *t);
197 int (*parse)(int c, char **argv, int argc,
198 const struct ebt_u_entry *entry, unsigned int *flags,
199 struct ebt_entry_target **target);
200 void (*final_check)(const struct ebt_u_entry *entry,
201 const struct ebt_entry_target *target, const char *name,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000202 unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000203 void (*print)(const struct ebt_u_entry *entry,
204 const struct ebt_entry_target *target);
205 int (*compare)(const struct ebt_entry_target *t1,
206 const struct ebt_entry_target *t2);
207 const struct option *extra_ops;
208 unsigned int option_offset;
209 unsigned int flags;
210 struct ebt_entry_target *t;
211 unsigned int used;
212 struct ebt_u_target *next;
213};
214
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000215/* libebtc.c */
216
217extern struct ebt_u_table *ebt_tables;
218extern struct ebt_u_match *ebt_matches;
219extern struct ebt_u_watcher *ebt_watchers;
220extern struct ebt_u_target *ebt_targets;
221
222void ebt_register_table(struct ebt_u_table *);
223void ebt_register_match(struct ebt_u_match *);
224void ebt_register_watcher(struct ebt_u_watcher *);
225void ebt_register_target(struct ebt_u_target *t);
226void ebt_get_kernel_table(struct ebt_u_replace *replace,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000227 struct ebt_u_table *table, int init);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000228struct ebt_u_target *ebt_find_target(const char *name);
229struct ebt_u_match *ebt_find_match(const char *name);
230struct ebt_u_watcher *ebt_find_watcher(const char *name);
231struct ebt_u_table *ebt_find_table(const char *name);
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000232int ebtables_insmod(const char *modname);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000233void ebt_list_extensions();
234void ebt_initialize_entry(struct ebt_u_entry *e);
235void ebt_free_u_entry(struct ebt_u_entry *e);
236struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
237 const char* arg);
238struct ebt_u_entries *ebt_nr_to_chain(const struct ebt_u_replace *replace,
239 int nr);
240struct ebt_u_entries *ebt_to_chain(const struct ebt_u_replace *replace);
241struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
242 const char* arg);
243int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg);
244/**/
245void ebt_change_policy(struct ebt_u_replace *replace, int policy);
246void ebt_flush_chains(struct ebt_u_replace *replace);
247int ebt_check_rule_exists(struct ebt_u_replace *replace,
248 struct ebt_u_entry *new_entry);
249void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry,
250 int rule_nr);
251void ebt_delete_rule(struct ebt_u_replace *replace,
252 struct ebt_u_entry *new_entry, int begin, int end);
253void ebt_zero_counters(struct ebt_u_replace *replace);
254void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy);
255void ebt_delete_chain(struct ebt_u_replace *replace);
256void ebt_rename_chain(struct ebt_u_replace *replace, const char *name);
257/**/
258void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e,
259 struct ebt_u_entries *entries);
260int ebt_check_for_references(struct ebt_u_replace *replace);
261int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr);
262void ebt_check_for_loops(struct ebt_u_replace *replace);
263void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m);
264void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w);
265void ebt_iterate_matches(void (*f)(struct ebt_u_match *));
266void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *));
267void ebt_iterate_targets(void (*f)(struct ebt_u_target *));
Bart De Schuymer64182a32004-01-21 20:39:54 +0000268void __ebt_print_bug(char *file, int line, char *format, ...);
269void __ebt_print_error(char *format, ...);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000270
271/* communication.c */
272
Bart De Schuymer64182a32004-01-21 20:39:54 +0000273int ebt_get_table(struct ebt_u_replace *repl, int init);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000274void ebt_deliver_counters(struct ebt_u_replace *repl);
275void ebt_deliver_table(struct ebt_u_replace *repl);
276
277/* useful_functions.c */
278
279extern int ebt_invert;
280void ebt_check_option(unsigned int *flags, unsigned int mask);
281int ebt_check_inverse(const char option[]);
282void ebt_print_mac(const char *mac);
283void ebt_print_mac_and_mask(const char *mac, const char *mask);
284int ebt_get_mac_and_mask(char *from, char *to, char *mask);
285void ebt_parse_ip_address(char *address, uint32_t *addr, uint32_t *msk);
286char *ebt_mask_to_dotted(uint32_t mask);
287
288struct ethertypeent *parseethertypebynumber(int type);
289
Bart De Schuymer64182a32004-01-21 20:39:54 +0000290#define ebt_print_bug(format, args...) \
291 __ebt_print_bug(__FILE__, __LINE__, format, ##args)
292#define ebt_print_error(format,args...) __ebt_print_error(format, ##args);
293#define ebt_print_memory() {printf("Ebtables: " __FILE__ \
Bart De Schuymer1f7f0022003-01-19 10:36:29 +0000294 " %s %d :Out of memory.\n", __FUNCTION__, __LINE__); exit(-1);}
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000295
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000296/* used for keeping the rule counters right during rule adds or deletes */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000297#define CNT_NORM 0
298#define CNT_DEL 1
299#define CNT_ADD 2
300#define CNT_OWRITE 3
301#define CNT_ZERO 4
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000302
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000303extern const char *ebt_hooknames[NF_BR_NUMHOOKS];
304extern const char *ebt_standard_targets[NUM_STANDARD_TARGETS];
305extern char ebt_errormsg[ERRORMSG_MAXLEN];
306extern char *ebt_modprobe;
307extern int ebt_silent;
308extern int ebt_printstyle_mac;
309
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000310/*
311 * Transforms a target string into the right integer,
312 * returns 0 on success.
313 */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000314#define FILL_TARGET(_str, _pos) ({ \
315 int _i, _ret = 0; \
316 for (_i = 0; _i < NUM_STANDARD_TARGETS; _i++) \
317 if (!strcmp(_str, ebt_standard_targets[_i])) {\
318 _pos = -_i - 1; \
319 break; \
320 } \
321 if (_i == NUM_STANDARD_TARGETS) \
322 _ret = 1; \
323 _ret; \
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000324})
325
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000326/* Transforms the target value to an index into standard_targets[] */
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000327#define TARGET_INDEX(_value) (-_value - 1)
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000328/* Returns a target string corresponding to the value */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000329#define TARGET_NAME(_value) (ebt_standard_targets[TARGET_INDEX(_value)])
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000330/* True if the hook mask denotes that the rule is in a base chain */
Bart De Schuymer76712df2002-08-25 21:54:06 +0000331#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000332/* Clear the bit in the hook_mask that tells if the rule is on a base chain */
Bart De Schuymer76712df2002-08-25 21:54:06 +0000333#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000334#define PRINT_VERSION printf(PROGNAME" v"PROGVERSION" ("PROGDATE")\n")
335#ifndef PROC_SYS_MODPROBE
336#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
337#endif
338#define ATOMIC_ENV_VARIABLE "EBTABLES_ATOMIC_FILE"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000339#endif /* EBTABLES_U_H */