blob: c74118b4cff67f099f4f8af03a1e1dba8f2060b7 [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 Schuymer6622a012005-01-19 21:09:05 +000029#define EXEC_STYLE_PRG 0
30#define EXEC_STYLE_DAEMON 1
31
Bart De Schuymerf81c2702003-07-23 21:07:04 +000032#ifndef EBT_MIN_ALIGN
33#define EBT_MIN_ALIGN (__alignof__(struct ebt_entry_target))
34#endif
35#define EBT_ALIGN(s) (((s) + (EBT_MIN_ALIGN-1)) & ~(EBT_MIN_ALIGN-1))
Bart De Schuymer8339ff12004-01-14 20:05:27 +000036#define ERRORMSG_MAXLEN 128
Bart De Schuymerf81c2702003-07-23 21:07:04 +000037
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000038struct ebt_u_entries
39{
Bart De Schuymer09765902002-06-23 08:15:51 +000040 int policy;
Bart De Schuymer376ccfb2002-07-23 20:52:18 +000041 unsigned int nentries;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000042 /* counter offset for this chain */
Bart De Schuymer09765902002-06-23 08:15:51 +000043 unsigned int counter_offset;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000044 /* used for udc */
Bart De Schuymer09765902002-06-23 08:15:51 +000045 unsigned int hook_mask;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000046 char *kernel_start;
Bart De Schuymer09765902002-06-23 08:15:51 +000047 char name[EBT_CHAIN_MAXNAMELEN];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000048 struct ebt_u_entry *entries;
49};
50
Bart De Schuymer8339ff12004-01-14 20:05:27 +000051struct ebt_cntchanges
52{
53 unsigned short type;
Bart De Schuymer9d14e842005-05-24 20:49:27 +000054 unsigned short change; /* determines incremental/decremental/change */
Bart De Schuymer8339ff12004-01-14 20:05:27 +000055 struct ebt_cntchanges *next;
56};
57
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000058#define EBT_ORI_MAX_CHAINS 10
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000059struct ebt_u_replace
60{
61 char name[EBT_TABLE_MAXNAMELEN];
62 unsigned int valid_hooks;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000063 /* nr of rules in the table */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000064 unsigned int nentries;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +000065 unsigned int num_chains;
66 unsigned int max_chains;
67 struct ebt_u_entries **chains;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000068 /* nr of counters userspace expects back */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000069 unsigned int num_counters;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000070 /* where the kernel will put the old counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000071 struct ebt_counter *counters;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000072 /*
73 * can be used e.g. to know if a standard option
74 * has been specified twice
75 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000076 unsigned int flags;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000077 /* we stick the specified command (e.g. -A) in here */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000078 char command;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000079 /*
Bart De Schuymer8339ff12004-01-14 20:05:27 +000080 * here we stick the chain to do our thing on (can be -1 if unspecified)
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000081 */
Bart De Schuymer8339ff12004-01-14 20:05:27 +000082 int selected_chain;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000083 /* used for the atomic option */
Bart De Schuymer8330e892002-07-14 19:06:47 +000084 char *filename;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +000085 /* tells what happened to the old rules */
Bart De Schuymer8339ff12004-01-14 20:05:27 +000086 struct ebt_cntchanges *counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000087};
88
89struct ebt_u_table
90{
91 char name[EBT_TABLE_MAXNAMELEN];
Bart De Schuymerf57f50f2002-08-29 16:52:36 +000092 void (*check)(struct ebt_u_replace *repl);
Bart De Schuymer8339ff12004-01-14 20:05:27 +000093 void (*help)(const char **);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000094 struct ebt_u_table *next;
95};
96
97struct ebt_u_match_list
98{
99 struct ebt_u_match_list *next;
100 struct ebt_entry_match *m;
101};
102
103struct ebt_u_watcher_list
104{
105 struct ebt_u_watcher_list *next;
106 struct ebt_entry_watcher *w;
107};
108
109struct ebt_u_entry
110{
Bart De Schuymer376ccfb2002-07-23 20:52:18 +0000111 unsigned int bitmask;
112 unsigned int invflags;
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000113 uint16_t ethproto;
Bart De Schuymer376ccfb2002-07-23 20:52:18 +0000114 char in[IFNAMSIZ];
115 char logical_in[IFNAMSIZ];
116 char out[IFNAMSIZ];
117 char logical_out[IFNAMSIZ];
Bart De Schuymere3cceb72002-07-26 12:47:33 +0000118 unsigned char sourcemac[ETH_ALEN];
119 unsigned char sourcemsk[ETH_ALEN];
120 unsigned char destmac[ETH_ALEN];
121 unsigned char destmsk[ETH_ALEN];
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000122 struct ebt_u_match_list *m_list;
123 struct ebt_u_watcher_list *w_list;
124 struct ebt_entry_target *t;
125 struct ebt_u_entry *next;
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000126 struct ebt_counter cnt;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000127 struct ebt_counter cnt_surplus; /* for increasing/decreasing a counter and for option 'C' */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000128 /* the standard target needs this to know the name of a udc when
129 * printing out rules. */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000130 struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000131};
132
133struct ebt_u_match
134{
135 char name[EBT_FUNCTION_MAXNAMELEN];
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000136 /* size of the real match data */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000137 unsigned int size;
138 void (*help)(void);
139 void (*init)(struct ebt_entry_match *m);
140 int (*parse)(int c, char **argv, int argc,
141 const struct ebt_u_entry *entry, unsigned int *flags,
142 struct ebt_entry_match **match);
143 void (*final_check)(const struct ebt_u_entry *entry,
144 const struct ebt_entry_match *match,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000145 const char *name, unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000146 void (*print)(const struct ebt_u_entry *entry,
147 const struct ebt_entry_match *match);
148 int (*compare)(const struct ebt_entry_match *m1,
149 const struct ebt_entry_match *m2);
150 const struct option *extra_ops;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000151 /*
152 * can be used e.g. to check for multiple occurance of the same option
153 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000154 unsigned int flags;
155 unsigned int option_offset;
156 struct ebt_entry_match *m;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000157 /*
158 * if used == 1 we no longer have to add it to
159 * the match chain of the new entry
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000160 * be sure to put it back on 0 when finished
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000161 */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000162 unsigned int used;
163 struct ebt_u_match *next;
164};
165
166struct ebt_u_watcher
167{
168 char name[EBT_FUNCTION_MAXNAMELEN];
169 unsigned int size;
170 void (*help)(void);
171 void (*init)(struct ebt_entry_watcher *w);
172 int (*parse)(int c, char **argv, int argc,
173 const struct ebt_u_entry *entry, unsigned int *flags,
174 struct ebt_entry_watcher **watcher);
175 void (*final_check)(const struct ebt_u_entry *entry,
176 const struct ebt_entry_watcher *watch, const char *name,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000177 unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000178 void (*print)(const struct ebt_u_entry *entry,
179 const struct ebt_entry_watcher *watcher);
180 int (*compare)(const struct ebt_entry_watcher *w1,
181 const struct ebt_entry_watcher *w2);
182 const struct option *extra_ops;
183 unsigned int flags;
184 unsigned int option_offset;
185 struct ebt_entry_watcher *w;
186 unsigned int used;
187 struct ebt_u_watcher *next;
188};
189
190struct ebt_u_target
191{
192 char name[EBT_FUNCTION_MAXNAMELEN];
193 unsigned int size;
194 void (*help)(void);
195 void (*init)(struct ebt_entry_target *t);
196 int (*parse)(int c, char **argv, int argc,
197 const struct ebt_u_entry *entry, unsigned int *flags,
198 struct ebt_entry_target **target);
199 void (*final_check)(const struct ebt_u_entry *entry,
200 const struct ebt_entry_target *target, const char *name,
Bart De Schuymer76712df2002-08-25 21:54:06 +0000201 unsigned int hookmask, unsigned int time);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000202 void (*print)(const struct ebt_u_entry *entry,
203 const struct ebt_entry_target *target);
204 int (*compare)(const struct ebt_entry_target *t1,
205 const struct ebt_entry_target *t2);
206 const struct option *extra_ops;
207 unsigned int option_offset;
208 unsigned int flags;
209 struct ebt_entry_target *t;
210 unsigned int used;
211 struct ebt_u_target *next;
212};
213
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000214/* libebtc.c */
215
216extern struct ebt_u_table *ebt_tables;
217extern struct ebt_u_match *ebt_matches;
218extern struct ebt_u_watcher *ebt_watchers;
219extern struct ebt_u_target *ebt_targets;
220
221void ebt_register_table(struct ebt_u_table *);
222void ebt_register_match(struct ebt_u_match *);
223void ebt_register_watcher(struct ebt_u_watcher *);
224void ebt_register_target(struct ebt_u_target *t);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000225int ebt_get_kernel_table(struct ebt_u_replace *replace, int init);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000226struct ebt_u_target *ebt_find_target(const char *name);
227struct ebt_u_match *ebt_find_match(const char *name);
228struct ebt_u_watcher *ebt_find_watcher(const char *name);
229struct ebt_u_table *ebt_find_table(const char *name);
Bart De Schuymer0cb01792003-05-04 16:52:04 +0000230int ebtables_insmod(const char *modname);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000231void ebt_list_extensions();
232void ebt_initialize_entry(struct ebt_u_entry *e);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000233void ebt_cleanup_replace(struct ebt_u_replace *replace);
234void ebt_reinit_extensions();
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000235void ebt_double_chains(struct ebt_u_replace *replace);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000236void ebt_free_u_entry(struct ebt_u_entry *e);
237struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
238 const char* arg);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000239struct ebt_u_entries *ebt_to_chain(const struct ebt_u_replace *replace);
240struct ebt_u_entries *ebt_name_to_chain(const struct ebt_u_replace *replace,
241 const char* arg);
242int ebt_get_chainnr(const struct ebt_u_replace *replace, const char* arg);
243/**/
244void ebt_change_policy(struct ebt_u_replace *replace, int policy);
245void ebt_flush_chains(struct ebt_u_replace *replace);
246int ebt_check_rule_exists(struct ebt_u_replace *replace,
247 struct ebt_u_entry *new_entry);
248void ebt_add_rule(struct ebt_u_replace *replace, struct ebt_u_entry *new_entry,
249 int rule_nr);
250void ebt_delete_rule(struct ebt_u_replace *replace,
251 struct ebt_u_entry *new_entry, int begin, int end);
252void ebt_zero_counters(struct ebt_u_replace *replace);
Bart De Schuymerff587202005-02-08 20:02:28 +0000253void ebt_change_counters(struct ebt_u_replace *replace,
254 struct ebt_u_entry *new_entry, int begin, int end,
Bart De Schuymer9d14e842005-05-24 20:49:27 +0000255 struct ebt_counter *cnt, int mask);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000256void ebt_new_chain(struct ebt_u_replace *replace, const char *name, int policy);
257void ebt_delete_chain(struct ebt_u_replace *replace);
258void ebt_rename_chain(struct ebt_u_replace *replace, const char *name);
259/**/
260void ebt_do_final_checks(struct ebt_u_replace *replace, struct ebt_u_entry *e,
261 struct ebt_u_entries *entries);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000262int ebt_check_for_references(struct ebt_u_replace *replace, int print_err);
263int ebt_check_for_references2(struct ebt_u_replace *replace, int chain_nr,
264 int print_err);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000265void ebt_check_for_loops(struct ebt_u_replace *replace);
266void ebt_add_match(struct ebt_u_entry *new_entry, struct ebt_u_match *m);
267void ebt_add_watcher(struct ebt_u_entry *new_entry, struct ebt_u_watcher *w);
268void ebt_iterate_matches(void (*f)(struct ebt_u_match *));
269void ebt_iterate_watchers(void (*f)(struct ebt_u_watcher *));
270void ebt_iterate_targets(void (*f)(struct ebt_u_target *));
Bart De Schuymer64182a32004-01-21 20:39:54 +0000271void __ebt_print_bug(char *file, int line, char *format, ...);
272void __ebt_print_error(char *format, ...);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000273
274/* communication.c */
275
Bart De Schuymer64182a32004-01-21 20:39:54 +0000276int ebt_get_table(struct ebt_u_replace *repl, int init);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000277void ebt_deliver_counters(struct ebt_u_replace *repl, int exec_style);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000278void ebt_deliver_table(struct ebt_u_replace *repl);
279
280/* useful_functions.c */
281
282extern int ebt_invert;
283void ebt_check_option(unsigned int *flags, unsigned int mask);
Bart De Schuymerff587202005-02-08 20:02:28 +0000284#define ebt_check_inverse(arg) _ebt_check_inverse(arg, argc, argv)
285int _ebt_check_inverse(const char option[], int argc, char **argv);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000286void ebt_print_mac(const char *mac);
287void ebt_print_mac_and_mask(const char *mac, const char *mask);
288int ebt_get_mac_and_mask(char *from, char *to, char *mask);
289void ebt_parse_ip_address(char *address, uint32_t *addr, uint32_t *msk);
290char *ebt_mask_to_dotted(uint32_t mask);
291
Bart De Schuymer6622a012005-01-19 21:09:05 +0000292int do_command(int argc, char *argv[], int exec_style,
293 struct ebt_u_replace *replace_);
294
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000295struct ethertypeent *parseethertypebynumber(int type);
296
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000297#define ebt_to_chain(repl) repl->chains[repl->selected_chain]
Bart De Schuymer64182a32004-01-21 20:39:54 +0000298#define ebt_print_bug(format, args...) \
299 __ebt_print_bug(__FILE__, __LINE__, format, ##args)
300#define ebt_print_error(format,args...) __ebt_print_error(format, ##args);
Bart De Schuymerff587202005-02-08 20:02:28 +0000301#define ebt_print_error2(format, args...) {__ebt_print_error(format, ##args); \
302 return -1;}
303#define ebt_check_option2(flags,mask) \
304({ebt_check_option(flags,mask); \
305 if (ebt_errormsg[0] != '\0') \
306 return -1;})
307#define ebt_check_inverse2(option) \
308({int __ret = ebt_check_inverse(option); \
309if (ebt_errormsg[0] != '\0') \
310 return -1; \
311if (!optarg) { \
312 __ebt_print_error("Option without (mandatory) argument"); \
313 return -1; \
314} \
315__ret;})
Bart De Schuymer64182a32004-01-21 20:39:54 +0000316#define ebt_print_memory() {printf("Ebtables: " __FILE__ \
Bart De Schuymer1f7f0022003-01-19 10:36:29 +0000317 " %s %d :Out of memory.\n", __FUNCTION__, __LINE__); exit(-1);}
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000318
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000319/* used for keeping the rule counters right during rule adds or deletes */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000320#define CNT_NORM 0
321#define CNT_DEL 1
322#define CNT_ADD 2
323#define CNT_OWRITE 3
324#define CNT_ZERO 4
Bart De Schuymerff587202005-02-08 20:02:28 +0000325#define CNT_CHANGE 5
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000326
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000327extern const char *ebt_hooknames[NF_BR_NUMHOOKS];
328extern const char *ebt_standard_targets[NUM_STANDARD_TARGETS];
329extern char ebt_errormsg[ERRORMSG_MAXLEN];
330extern char *ebt_modprobe;
331extern int ebt_silent;
332extern int ebt_printstyle_mac;
333
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000334/*
335 * Transforms a target string into the right integer,
336 * returns 0 on success.
337 */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000338#define FILL_TARGET(_str, _pos) ({ \
339 int _i, _ret = 0; \
340 for (_i = 0; _i < NUM_STANDARD_TARGETS; _i++) \
341 if (!strcmp(_str, ebt_standard_targets[_i])) {\
342 _pos = -_i - 1; \
343 break; \
344 } \
345 if (_i == NUM_STANDARD_TARGETS) \
346 _ret = 1; \
347 _ret; \
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000348})
349
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000350/* Transforms the target value to an index into standard_targets[] */
Bart De Schuymer9cfd6542002-08-13 16:08:08 +0000351#define TARGET_INDEX(_value) (-_value - 1)
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000352/* Returns a target string corresponding to the value */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000353#define TARGET_NAME(_value) (ebt_standard_targets[TARGET_INDEX(_value)])
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000354/* True if the hook mask denotes that the rule is in a base chain */
Bart De Schuymer76712df2002-08-25 21:54:06 +0000355#define BASE_CHAIN (hookmask & (1 << NF_BR_NUMHOOKS))
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000356/* 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 +0000357#define CLEAR_BASE_CHAIN_BIT (hookmask &= ~(1 << NF_BR_NUMHOOKS))
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000358#define PRINT_VERSION printf(PROGNAME" v"PROGVERSION" ("PROGDATE")\n")
359#ifndef PROC_SYS_MODPROBE
360#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
361#endif
362#define ATOMIC_ENV_VARIABLE "EBTABLES_ATOMIC_FILE"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000363#endif /* EBTABLES_U_H */