blob: 2d0cd61d241e74a4f12f54e0a7528bd384c167bc [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001/*
Bart De Schuymerc56a31a2002-07-25 08:16:08 +00002 * ebtables.c, v2.0 July 2002
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00003 *
4 * Author: Bart De Schuymer
5 *
Bart De Schuymer6622a012005-01-19 21:09:05 +00006 * This code was stongly inspired on the iptables code which is
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00007 * 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#include <getopt.h>
25#include <string.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000026#include <stdio.h>
27#include <stdlib.h>
Bart De Schuymerd4586482002-08-11 16:15:55 +000028#include <stdarg.h>
Bart De Schuymer41830412002-06-05 19:41:28 +000029#include <netinet/ether.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000030#include "include/ebtables_u.h"
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000031#include "include/ethernetdb.h"
Bart De Schuymerc8531032002-06-14 21:55:29 +000032#include <unistd.h>
33#include <fcntl.h>
34#include <sys/wait.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000035
Bart De Schuymer6622a012005-01-19 21:09:05 +000036/* dDfault command line options. Do not mess around with the already
37 * assigned numbers unless you know what you are doing */
Bart De Schuymer62423742002-07-14 19:06:20 +000038static struct option ebt_original_options[] =
39{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000040 { "append" , required_argument, 0, 'A' },
41 { "insert" , required_argument, 0, 'I' },
42 { "delete" , required_argument, 0, 'D' },
43 { "list" , optional_argument, 0, 'L' },
Bart De Schuymer9af14f92002-07-10 20:49:10 +000044 { "Lc" , no_argument , 0, 4 },
45 { "Ln" , no_argument , 0, 5 },
46 { "Lx" , no_argument , 0, 6 },
Bart De Schuymer22d03a22003-05-03 20:28:22 +000047 { "Lmac2" , no_argument , 0, 12 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000048 { "zero" , optional_argument, 0, 'Z' },
49 { "flush" , optional_argument, 0, 'F' },
50 { "policy" , required_argument, 0, 'P' },
51 { "in-interface" , required_argument, 0, 'i' },
52 { "in-if" , required_argument, 0, 'i' },
53 { "logical-in" , required_argument, 0, 2 },
54 { "logical-out" , required_argument, 0, 3 },
55 { "out-interface" , required_argument, 0, 'o' },
56 { "out-if" , required_argument, 0, 'o' },
57 { "version" , no_argument , 0, 'V' },
58 { "help" , no_argument , 0, 'h' },
59 { "jump" , required_argument, 0, 'j' },
60 { "proto" , required_argument, 0, 'p' },
61 { "protocol" , required_argument, 0, 'p' },
62 { "db" , required_argument, 0, 'b' },
63 { "source" , required_argument, 0, 's' },
64 { "src" , required_argument, 0, 's' },
65 { "destination" , required_argument, 0, 'd' },
66 { "dst" , required_argument, 0, 'd' },
67 { "table" , required_argument, 0, 't' },
Bart De Schuymerc8531032002-06-14 21:55:29 +000068 { "modprobe" , required_argument, 0, 'M' },
Bart De Schuymer1ab41562002-06-23 17:09:54 +000069 { "new-chain" , required_argument, 0, 'N' },
70 { "rename-chain" , required_argument, 0, 'E' },
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +000071 { "delete-chain" , optional_argument, 0, 'X' },
Bart De Schuymer5885b362002-12-03 20:51:36 +000072 { "atomic-init" , no_argument , 0, 7 },
73 { "atomic-commit" , no_argument , 0, 8 },
74 { "atomic-file" , required_argument, 0, 9 },
75 { "atomic-save" , no_argument , 0, 10 },
Bart De Schuymer8d1d8942002-07-15 20:09:09 +000076 { "init-table" , no_argument , 0, 11 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000077 { 0 }
78};
79
80static struct option *ebt_options = ebt_original_options;
81
Bart De Schuymer6622a012005-01-19 21:09:05 +000082/* Holds all the data */
83static struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000084
Bart De Schuymer6622a012005-01-19 21:09:05 +000085/* The chosen table */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000086static struct ebt_u_table *table = NULL;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000087
Bart De Schuymer6622a012005-01-19 21:09:05 +000088/* The pointers in here are special:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000089 * The struct ebt_target * pointer is actually a struct ebt_u_target * pointer.
90 * instead of making yet a few other structs, we just do a cast.
91 * We need a struct ebt_u_target pointer because we know the address of the data
92 * they point to won't change. We want to allow that the struct ebt_u_target.t
93 * member can change.
Bart De Schuymer6622a012005-01-19 21:09:05 +000094 * Same holds for the struct ebt_match and struct ebt_watcher pointers */
95static struct ebt_u_entry *new_entry;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000096
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000097
Bart De Schuymer6622a012005-01-19 21:09:05 +000098static int global_option_offset;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000099#define OPTION_OFFSET 256
100static struct option *
101merge_options(struct option *oldopts, const struct option *newopts,
102 unsigned int *options_offset)
103{
104 unsigned int num_old, num_new, i;
105 struct option *merge;
106
107 if (!newopts || !oldopts || !options_offset)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000108 ebt_print_bug("merge wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000109 for (num_old = 0; oldopts[num_old].name; num_old++);
110 for (num_new = 0; newopts[num_new].name; num_new++);
111
112 global_option_offset += OPTION_OFFSET;
113 *options_offset = global_option_offset;
114
115 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
116 if (!merge)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000117 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000118 memcpy(merge, oldopts, num_old * sizeof(struct option));
119 for (i = 0; i < num_new; i++) {
120 merge[num_old + i] = newopts[i];
121 merge[num_old + i].val += *options_offset;
122 }
123 memset(merge + num_old + num_new, 0, sizeof(struct option));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000124 /* Only free dynamically allocated stuff */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000125 if (oldopts != ebt_original_options)
126 free(oldopts);
127
128 return merge;
129}
130
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000131static void merge_match(struct ebt_u_match *m)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000132{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000133 ebt_options = merge_options
134 (ebt_options, m->extra_ops, &(m->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000135}
136
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000137static void merge_watcher(struct ebt_u_watcher *w)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000138{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000139 ebt_options = merge_options
140 (ebt_options, w->extra_ops, &(w->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000141}
142
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000143static void merge_target(struct ebt_u_target *t)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000144{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000145 ebt_options = merge_options
146 (ebt_options, t->extra_ops, &(t->option_offset));
Bart De Schuymer9a0fbf22003-01-11 16:16:54 +0000147}
148
Bart De Schuymer6622a012005-01-19 21:09:05 +0000149/* Be backwards compatible, so don't use '+' in kernel */
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000150#define IF_WILDCARD 1
151static void print_iface(const char *iface)
152{
153 char *c;
154
155 if ((c = strchr(iface, IF_WILDCARD)))
156 *c = '+';
157 printf("%s ", iface);
158 if (c)
159 *c = IF_WILDCARD;
160}
161
Bart De Schuymer6622a012005-01-19 21:09:05 +0000162/* We use replace->flags, so we can't use the following values:
163 * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO */
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000164#define LIST_N 0x04
165#define LIST_C 0x08
166#define LIST_X 0x10
167#define LIST_MAC2 0x20
168
Bart De Schuymer6622a012005-01-19 21:09:05 +0000169/* Helper function for list_rules() */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000170static void list_em(struct ebt_u_entries *entries)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000171{
172 int i, j, space = 0, digits;
173 struct ebt_u_entry *hlp;
174 struct ebt_u_match_list *m_l;
175 struct ebt_u_watcher_list *w_l;
176 struct ebt_u_match *m;
177 struct ebt_u_watcher *w;
178 struct ebt_u_target *t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000179
Bart De Schuymer6622a012005-01-19 21:09:05 +0000180 if (replace->flags & LIST_MAC2)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000181 ebt_printstyle_mac = 2;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000182 hlp = entries->entries;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000183 if (replace->flags & LIST_X && entries->policy != EBT_ACCEPT) {
184 printf("ebtables -t %s -P %s %s\n", replace->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000185 entries->name, ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000186 } else if (!(replace->flags & LIST_X)) {
Bart De Schuymerc87c9642002-08-01 15:34:16 +0000187 printf("\nBridge chain: %s, entries: %d, policy: %s\n",
188 entries->name, entries->nentries,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000189 ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000190 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000191
Bart De Schuymer60332e02002-06-23 08:01:47 +0000192 i = entries->nentries;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000193 while (i > 9) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000194 space++;
195 i /= 10;
196 }
197
Bart De Schuymer60332e02002-06-23 08:01:47 +0000198 for (i = 0; i < entries->nentries; i++) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000199 if (replace->flags & LIST_N) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000200 digits = 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000201 /* A little work to get nice rule numbers. */
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000202 j = i + 1;
203 while (j > 9) {
204 digits++;
205 j /= 10;
206 }
207 for (j = 0; j < space - digits; j++)
208 printf(" ");
209 printf("%d. ", i + 1);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000210 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000211 if (replace->flags & LIST_X)
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000212 printf("ebtables -t %s -A %s ",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000213 replace->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000214
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000215 /* The standard target's print() uses this to find out
216 * the name of a udc */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000217 hlp->replace = replace;
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000218
Bart De Schuymer6622a012005-01-19 21:09:05 +0000219 /* Don't print anything about the protocol if no protocol was
220 * specified, obviously this means any protocol will do. */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000221 if (!(hlp->bitmask & EBT_NOPROTO)) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000222 printf("-p ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000223 if (hlp->invflags & EBT_IPROTO)
224 printf("! ");
225 if (hlp->bitmask & EBT_802_3)
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000226 printf("Length ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000227 else {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000228 struct ethertypeent *ent;
229
Bart De Schuymer64182a32004-01-21 20:39:54 +0000230 ent = getethertypebynumber
231 (ntohs(hlp->ethproto));
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000232 if (!ent)
Bart De Schuymer60332e02002-06-23 08:01:47 +0000233 printf("0x%x ", ntohs(hlp->ethproto));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000234 else
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000235 printf("%s ", ent->e_name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000236 }
237 }
238 if (hlp->bitmask & EBT_SOURCEMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000239 printf("-s ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000240 if (hlp->invflags & EBT_ISOURCE)
241 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000242 ebt_print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000243 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000244 }
245 if (hlp->bitmask & EBT_DESTMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000246 printf("-d ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000247 if (hlp->invflags & EBT_IDEST)
248 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000249 ebt_print_mac_and_mask(hlp->destmac, hlp->destmsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000250 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000251 }
252 if (hlp->in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000253 printf("-i ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000254 if (hlp->invflags & EBT_IIN)
255 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000256 print_iface(hlp->in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000257 }
258 if (hlp->logical_in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000259 printf("--logical-in ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000260 if (hlp->invflags & EBT_ILOGICALIN)
261 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000262 print_iface(hlp->logical_in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000263 }
264 if (hlp->logical_out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000265 printf("--logical-out ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000266 if (hlp->invflags & EBT_ILOGICALOUT)
267 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000268 print_iface(hlp->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000269 }
270 if (hlp->out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000271 printf("-o ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000272 if (hlp->invflags & EBT_IOUT)
273 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000274 print_iface(hlp->out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000275 }
276
277 m_l = hlp->m_list;
278 while (m_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000279 m = ebt_find_match(m_l->m->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000280 if (!m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000281 ebt_print_bug("Match not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000282 m->print(hlp, m_l->m);
283 m_l = m_l->next;
284 }
285 w_l = hlp->w_list;
286 while (w_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000287 w = ebt_find_watcher(w_l->w->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000288 if (!w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000289 ebt_print_bug("Watcher not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000290 w->print(hlp, w_l->w);
291 w_l = w_l->next;
292 }
293
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000294 printf("-j ");
295 if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET))
296 printf("%s ", hlp->t->u.name);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000297 t = ebt_find_target(hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000298 if (!t)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000299 ebt_print_bug("Target '%s' not found", hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000300 t->print(hlp, hlp->t);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000301 if (replace->flags & LIST_C)
Bart De Schuymer73fccca2002-10-17 22:00:23 +0000302 printf(", pcnt = %llu -- bcnt = %llu",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000303 replace->counters[entries->counter_offset + i].pcnt,
304 replace->counters[entries->counter_offset + i].bcnt);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000305 printf("\n");
306 hlp = hlp->next;
307 }
308}
309
Bart De Schuymer62423742002-07-14 19:06:20 +0000310static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000311{
312 struct ebt_u_match_list *m_l;
313 struct ebt_u_watcher_list *w_l;
314
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000315 PRINT_VERSION;
316 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000317"Usage:\n"
318"ebtables -[ADI] chain rule-specification [options]\n"
319"ebtables -P chain target\n"
320"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000321"ebtables -[NX] [chain]\n"
322"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000323"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000324"--append -A chain : append to chain\n"
325"--delete -D chain : delete matching rule from chain\n"
326"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000327"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000328"--list -L [chain] : list the rules in a chain or in all chains\n"
329"--flush -F [chain] : delete all rules in chain or in all chains\n"
330"--init-table : replace the kernel table with the initial table\n"
331"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
332"--policy -P chain target : change policy on chain to target\n"
333"--new-chain -N chain : create a user defined chain\n"
334"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000335"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000336"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
337"--atomic-init : put the initial kernel table into <FILE>\n"
338"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000339"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000340"Options:\n"
341"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
342"--src -s [!] address[/mask]: source mac address\n"
343"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000344"--in-if -i [!] name[+] : network input interface name\n"
345"--out-if -o [!] name[+] : network output interface name\n"
346"--logical-in [!] name[+] : logical bridge input interface name\n"
347"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000348"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000349"--version -V : print package version\n\n"
350"Environment variable:\n"
351ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
352"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000353 m_l = new_entry->m_list;
354 while (m_l) {
355 ((struct ebt_u_match *)m_l->m)->help();
356 printf("\n");
357 m_l = m_l->next;
358 }
359 w_l = new_entry->w_list;
360 while (w_l) {
361 ((struct ebt_u_watcher *)w_l->w)->help();
362 printf("\n");
363 w_l = w_l->next;
364 }
365 ((struct ebt_u_target *)new_entry->t)->help();
366 printf("\n");
367 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000368 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000369 exit(0);
370}
371
Bart De Schuymer6622a012005-01-19 21:09:05 +0000372/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000373static void list_rules()
374{
375 int i;
376
Bart De Schuymer6622a012005-01-19 21:09:05 +0000377 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000378 printf("Bridge table: %s\n", table->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000379 if (replace->selected_chain != -1) {
380 list_em(ebt_to_chain(replace));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000381 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000382 struct ebt_u_chain_list *cl = replace->udc;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000383
Bart De Schuymer6622a012005-01-19 21:09:05 +0000384 /* Create new chains and rename standard chains when necessary */
385 if (replace->flags & LIST_X) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000386 while (cl) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000387 printf("ebtables -t %s -N %s\n", replace->name,
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000388 cl->udc->name);
389 cl = cl->next;
390 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000391 cl = replace->udc;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000392 for (i = 0; i < NF_BR_NUMHOOKS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000393 if (replace->valid_hooks & (1 << i) &&
394 strcmp(replace->hook_entry[i]->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000395 ebt_hooknames[i]))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000396 printf("ebtables -t %s -E %s %s\n",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000397 replace->name, ebt_hooknames[i],
398 replace->hook_entry[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000399 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000400 i = 0;
401 while (1) {
402 if (i < NF_BR_NUMHOOKS) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000403 if (replace->valid_hooks & (1 << i))
404 list_em(replace->hook_entry[i]);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000405 i++;
406 continue;
407 } else {
408 if (!cl)
409 break;
410 list_em(cl->udc);
411 cl = cl->next;
412 }
413 }
414 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000415}
416
Bart De Schuymercc440052002-11-06 21:10:33 +0000417static int parse_delete_rule(const char *argv, int *rule_nr, int *rule_nr_end)
418{
419 char *colon = strchr(argv, ':'), *buffer;
420
421 if (colon) {
422 *colon = '\0';
423 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000424 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000425 else {
426 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000427 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000428 return -1;
429 }
430 }
431 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000432 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000433 else {
434 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000435 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000436 return -1;
437 }
438 if (!colon)
439 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000440 return 0;
441}
442
Bart De Schuymer6622a012005-01-19 21:09:05 +0000443static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000444{
445 char *c;
446
447 if ((c = strchr(iface, '+'))) {
448 if (*(c + 1) != '\0') {
449 ebt_print_error("Spurious characters after '+' "
450 "wildcard for %s", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000451 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000452 } else
453 *c = IF_WILDCARD;
454 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000455 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000456}
457
Bart De Schuymer6622a012005-01-19 21:09:05 +0000458void ebt_early_init_once()
459{
460 ebt_iterate_matches(merge_match);
461 ebt_iterate_watchers(merge_watcher);
462 ebt_iterate_targets(merge_target);
463}
464
465#define ebt_print_error2(format, args...) {__ebt_print_error(format, ##args); \
466 return -1;}
467#define ebt_check_option2(flags,mask) \
468({ebt_check_option(flags,mask); \
469 if (ebt_errormsg[0] != '\0') \
470 return -1;})
471#define ebt_check_inverse2(option) \
472({int __ret = ebt_check_inverse(option); \
473if (ebt_errormsg[0] != '\0') \
474 return -1; \
475__ret;})
476#define OPT_COMMANDS (replace->flags & OPT_COMMAND || replace->flags & OPT_ZERO)
477
Bart De Schuymer64182a32004-01-21 20:39:54 +0000478#define OPT_COMMAND 0x01
479#define OPT_TABLE 0x02
480#define OPT_IN 0x04
481#define OPT_OUT 0x08
482#define OPT_JUMP 0x10
483#define OPT_PROTOCOL 0x20
484#define OPT_SOURCE 0x40
485#define OPT_DEST 0x80
486#define OPT_ZERO 0x100
487#define OPT_LOGICALIN 0x200
488#define OPT_LOGICALOUT 0x400
Bart De Schuymer6622a012005-01-19 21:09:05 +0000489#define OPT_KERNELDATA 0x800 /* This value is also defined in ebtablesd.c */
490int do_command(int argc, char *argv[], int exec_style,
491 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000492{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000493 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000494 int c, i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000495 /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000496 int zerochain = -1;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000497 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000498 int rule_nr = 0;
499 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000500 struct ebt_u_target *t;
501 struct ebt_u_match *m;
502 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000503 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000504 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000505 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000506
Bart De Schuymera615b962002-11-03 14:54:09 +0000507 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000508 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000509
Bart De Schuymer6622a012005-01-19 21:09:05 +0000510 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000511
Bart De Schuymer6622a012005-01-19 21:09:05 +0000512 /* The daemon doesn't use the environment variable */
513 if (exec_style == EXEC_STYLE_PRG) {
514 buffer = getenv(ATOMIC_ENV_VARIABLE);
515 if (buffer) {
516 replace->filename = malloc(strlen(buffer) + 1);
517 if (!replace->filename)
518 ebt_print_memory();
519 strcpy(replace->filename, buffer);
520 buffer = NULL;
521 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000522 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000523
Bart De Schuymer6622a012005-01-19 21:09:05 +0000524 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
525 replace->selected_chain = -1;
526 replace->command = 'h';
527
528 if (!new_entry) {
529 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
530 if (!new_entry)
531 ebt_print_memory();
532 }
533 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000534 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000535 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000536
Bart De Schuymer6622a012005-01-19 21:09:05 +0000537 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000538 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000539 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000540
Bart De Schuymer6622a012005-01-19 21:09:05 +0000541 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000542 while ((c = getopt_long(argc, argv,
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000543 "-A:D:I:N:E:X::L::Z::F::P:Vhi:o:j:p:s:d:t:M:", ebt_options, NULL)) != -1) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000544 switch (c) {
545
Bart De Schuymer6622a012005-01-19 21:09:05 +0000546 case 'A': /* Add a rule */
547 case 'D': /* Delete a rule */
548 case 'P': /* Define policy */
549 case 'I': /* Insert a rule */
550 case 'N': /* Make a user defined chain */
551 case 'E': /* Rename chain */
552 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000553 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000554 if (replace->command == 'N' && c == 'P') {
555 replace->command = c;
556 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000557 goto handle_P;
558 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000559 if (OPT_COMMANDS)
560 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000561
Bart De Schuymer6622a012005-01-19 21:09:05 +0000562 replace->command = c;
563 replace->flags |= OPT_COMMAND;
564 if (!(replace->flags & OPT_KERNELDATA))
565 ebt_get_kernel_table(replace, 0);
566 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
567 ebt_print_error2("No chain name specified");
568 if (c == 'N') {
569 ebt_new_chain(replace, optarg, EBT_ACCEPT);
570 /* This is needed to get -N x -P y working */
571 replace->selected_chain = ebt_get_chainnr(replace, optarg);
572 break;
573 } else if (c == 'X') {
574 if (optind >= argc) {
575 replace->selected_chain = -1;
576 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000577 break;
578 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000579
580 if (optind < argc - 1)
581 ebt_print_error2("No extra options allowed with -X");
582
583 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
584 ebt_print_error2("Chain %s doesn't exist", argv[optind]);
585 ebt_delete_chain(replace);
586 if (ebt_errormsg[0] != '\0')
587 return -1;
588 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000589 break;
590 }
591
Bart De Schuymer6622a012005-01-19 21:09:05 +0000592 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
593 ebt_print_error2("Chain %s doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000594 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000595 if (optind >= argc)
596 ebt_print_error2("No new chain name specified");
597 if (optind < argc - 1)
598 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer64182a32004-01-21 20:39:54 +0000599 if (strlen(argv[optind])>=EBT_CHAIN_MAXNAMELEN)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000600 ebt_print_error2("Chain name length can't exceed %d", EBT_CHAIN_MAXNAMELEN - 1);
601 if (ebt_get_chainnr(replace, argv[optind]) != -1)
602 ebt_print_error2("Chain %s already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000603 if (ebt_find_target(argv[optind]))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000604 ebt_print_error2("Target with name %s exists", argv[optind]);
605 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000606 optind++;
607 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000608 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
609 if (optind != argc - 1)
610 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
611 if (parse_delete_rule(argv[optind], &rule_nr, &rule_nr_end))
612 ebt_print_error2("Problem with the specified rule number(s)");
Bart De Schuymercc440052002-11-06 21:10:33 +0000613 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000614 } else if (c == 'I') {
615 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
616 ebt_print_error2("No rulenr for -I specified");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000617 rule_nr = strtol(argv[optind], &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000618 if (*buffer != '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000619 ebt_print_error2("Problem with the specified rule number");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000620 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000621 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000622handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000623 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000624 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000625 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000626 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000627 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000628 if (policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000629 ebt_print_error2("Wrong policy: '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000630 break;
631 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000632 optind++;
633 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000634 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000635 case 'L': /* List */
636 case 'F': /* Flush */
637 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000638 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000639 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
640print_zero:
641 ebt_print_error2("Command -Z only allowed together with command -L");
642 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000643 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 replace->command = c;
645 if (replace->flags & OPT_COMMAND)
646 ebt_print_error2("Multiple commands are not allowed");
647 replace->flags |= OPT_COMMAND;
648 if (replace->flags & OPT_ZERO)
649 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000650 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000651
652#ifdef SILENT_DAEMON
653 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
654 ebt_print_error2("-L not supported in daemon mode not supported in daemon mode");
655#endif
656
657 if (!(replace->flags & OPT_KERNELDATA))
658 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000659 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000660 if (optind < argc && argv[optind][0] != '-') {
661 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
662 ebt_print_error2("Bad chain");
663 optind++;
664 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000665 if (i != -1) {
666 if (c == 'Z')
667 zerochain = i;
668 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000669 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000670 }
671 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000672 case 'V': /* Version */
673 replace->command = 'V';
674 if (OPT_COMMANDS)
675 ebt_print_error2("Multiple commands are not allowed");
676 if (exec_style == EXEC_STYLE_DAEMON)
677 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000678 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000679 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000680 case 'M': /* Modprobe */
681 if (OPT_COMMANDS)
682 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000683 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000684 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000685 case 'h': /* Help */
686 if (exec_style == EXEC_STYLE_DAEMON)
687 ebt_print_error2("-h not supported in daemon mode");
688 if (OPT_COMMANDS)
689 ebt_print_error2("Multiple commands are not allowed");
690 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000691
Bart De Schuymer6622a012005-01-19 21:09:05 +0000692 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000693 while (optind < argc) {
694 struct ebt_u_match *m;
695 struct ebt_u_watcher *w;
696
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000698 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000699 exit(0);
700 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000701 if ((m = ebt_find_match(argv[optind])))
702 ebt_add_match(new_entry, m);
703 else if ((w = ebt_find_watcher(argv[optind])))
704 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000705 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000706 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000707 ebt_print_error2("Extension %s not found", argv[optind]);
708 if (replace->flags & OPT_JUMP)
709 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
710 replace->flags |= OPT_JUMP;
711 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000712 }
713 optind++;
714 }
715 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000716 case 't': /* Table */
717 if (replace->command != 'h')
718 ebt_print_error2("Please put the -t option first");
719 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000720 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000721 ebt_print_error2("Table name too long");
722 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000723 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000724 case 'i': /* Input interface */
725 case 2 : /* Logical input interface */
726 case 'o': /* Output interface */
727 case 3 : /* Logical output interface */
728 case 'j': /* Target */
729 case 'p': /* Net family protocol */
730 case 's': /* Source mac */
731 case 'd': /* Destination mac */
732 if (!OPT_COMMANDS)
733 ebt_print_error2("No command specified");
734 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I')
735 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000736 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000737 ebt_check_option2(&(replace->flags), OPT_IN);
738 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
739 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
740 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000741 new_entry->invflags |= EBT_IIN;
742
Bart De Schuymer6622a012005-01-19 21:09:05 +0000743 if (strlen(optarg) >= IFNAMSIZ)
744 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
745 strcpy(new_entry->in, optarg);
746 if (parse_iface(new_entry->in, "-i"))
747 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000748 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000749 } else if (c == 2) {
750 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
751 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
752 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
753 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000754 new_entry->invflags |= EBT_ILOGICALIN;
755
Bart De Schuymer6622a012005-01-19 21:09:05 +0000756 if (strlen(optarg) >= IFNAMSIZ)
757 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
758 strcpy(new_entry->logical_in, optarg);
759 if (parse_iface(new_entry->logical_in, "--logical-in"))
760 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000761 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000762 } else if (c == 'o') {
763 ebt_check_option2(&(replace->flags), OPT_OUT);
764 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
765 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
766 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000767 new_entry->invflags |= EBT_IOUT;
768
Bart De Schuymer6622a012005-01-19 21:09:05 +0000769 if (strlen(optarg) >= IFNAMSIZ)
770 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
771 strcpy(new_entry->out, optarg);
772 if (parse_iface(new_entry->out, "-o"))
773 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000774 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000775 } else if (c == 3) {
776 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
777 if (replace->selected_chain < 2)
778 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
779 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000780 new_entry->invflags |= EBT_ILOGICALOUT;
781
Bart De Schuymer6622a012005-01-19 21:09:05 +0000782 if (strlen(optarg) >= IFNAMSIZ)
783 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
784 strcpy(new_entry->logical_out, optarg);
785 if (parse_iface(new_entry->logical_out, "--logical-out"))
786 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000787 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000788 } else if (c == 'j') {
789 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000790 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000791 if (!strcmp(optarg, ebt_standard_targets[i])) {
792 t = ebt_find_target(EBT_STANDARD_TARGET);
793 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000794 break;
795 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000796 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
797 ebt_print_error2("Return target only for user defined chains");
798 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000799 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000800
801 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000802 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000803 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000804 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000805 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000806 break;
807 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000808 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000809 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000810
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000811 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000812 /* -j standard not allowed either */
813 if (!t || t == (struct ebt_u_target *)new_entry->t)
814 ebt_print_error2("Illegal target name");
815 new_entry->t = (struct ebt_entry_target *)t;
816 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
817 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000818 }
819 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 } else if (c == 's') {
821 ebt_check_option2(&(replace->flags), OPT_SOURCE);
822 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000823 new_entry->invflags |= EBT_ISOURCE;
824
Bart De Schuymer6622a012005-01-19 21:09:05 +0000825 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
826 ebt_print_error2("Problem with specified source mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000827 new_entry->bitmask |= EBT_SOURCEMAC;
828 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000829 } else if (c == 'd') {
830 ebt_check_option2(&(replace->flags), OPT_DEST);
831 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000832 new_entry->invflags |= EBT_IDEST;
833
Bart De Schuymer6622a012005-01-19 21:09:05 +0000834 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
835 ebt_print_error2("Problem with specified destination mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000836 new_entry->bitmask |= EBT_DESTMAC;
837 break;
838 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000839 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
840 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000841 new_entry->invflags |= EBT_IPROTO;
842
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000843 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000844 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000845 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000846 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000847 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000848 struct ethertypeent *ent;
849
Bart De Schuymer6622a012005-01-19 21:09:05 +0000850 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000851 new_entry->bitmask |= EBT_802_3;
852 break;
853 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000854 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000855 if (!ent)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000856 ebt_print_error2("Problem with the specified Ethernet protocol (%s), perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000857 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000858 } else
859 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000860
Bart De Schuymer6622a012005-01-19 21:09:05 +0000861 if (new_entry->ethproto < 0x0600)
862 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
863 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000864 case 4 : /* Lc */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000865 if (exec_style == EXEC_STYLE_DAEMON)
866 ebt_print_error2("--Lc is not supported in daemon mode");
867 ebt_check_option2(&(replace->flags), LIST_C);
868 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000869 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000870 if (replace->flags & LIST_X)
871 ebt_print_error2("--Lx is not compatible with --Lc");
872 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000873 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000874 case 5 : /* Ln */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000875 if (exec_style == EXEC_STYLE_DAEMON)
876 ebt_print_error2("--Ln is not supported in daemon mode");
877 ebt_check_option2(&(replace->flags), LIST_N);
878 if (replace->command != 'L')
879 ebt_print_error2("Use --Ln with -L");
880 if (replace->flags & LIST_X)
881 ebt_print_error2("--Lx is not compatible with --Ln");
882 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000883 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000884 case 6 : /* Lx */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000885 if (exec_style == EXEC_STYLE_DAEMON)
886 ebt_print_error2("--Lx is not supported in daemon mode");
887 ebt_check_option2(&(replace->flags), LIST_X);
888 if (replace->command != 'L')
889 ebt_print_error2("Use --Lx with -L");
890 if (replace->flags & LIST_C)
891 ebt_print_error2("--Lx is not compatible with --Lc");
892 if (replace->flags & LIST_N)
893 ebt_print_error2("--Lx is not compatible with --Ln");
894 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000895 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000896 case 12 : /* Lmac2 */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000897 if (exec_style == EXEC_STYLE_DAEMON)
898 ebt_print_error("--Lmac2 is not supported in daemon mode");
899 ebt_check_option2(&(replace->flags), LIST_MAC2);
900 if (replace->command != 'L')
901 ebt_print_error2("Use --Lmac2 with -L");
902 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000903 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000904 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000905 if (exec_style == EXEC_STYLE_DAEMON)
906 ebt_print_error2("--atomic-commit is not supported in daemon mode");
907 replace->command = c;
908 if (OPT_COMMANDS)
909 ebt_print_error2("Multiple commands are not allowed");
910 replace->flags |= OPT_COMMAND;
911 if (!replace->filename)
912 ebt_print_error2("No atomic file specified");
913 /* Get the information from the file */
914 ebt_get_table(replace, 0);
915 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000916 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +0000917 * the file */
918 replace->num_counters = 0;
919 /* Make sure the table will be written to the kernel */
920 free(replace->filename);
921 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000922 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000923 case 7 : /* atomic-init */
924 case 10: /* atomic-save */
925 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000926 if (exec_style == EXEC_STYLE_DAEMON) {
927 if (c == 7) {
928 ebt_print_error2("--atomic-init is not supported in daemon mode");
929 } else if (c == 10)
930 ebt_print_error2("--atomic-save is not supported in daemon mode");
931 ebt_print_error2("--init-table is not supported in daemon mode");
932 }
933 replace->command = c;
934 if (OPT_COMMANDS)
935 ebt_print_error2("Multiple commands are not allowed");
936 if (c != 11 && !replace->filename)
937 ebt_print_error2("No atomic file specified");
938 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000939 {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000940 char *tmp = replace->filename;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000941 int init = 1;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000942
Bart De Schuymer64182a32004-01-21 20:39:54 +0000943 if (c == 10)
944 init = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000945 /* Get the kernel table */
946 replace->filename = NULL;
947 ebt_get_kernel_table(replace, init);
948 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000949 }
Bart De Schuymer5885b362002-12-03 20:51:36 +0000950 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000951 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000952 if (exec_style == EXEC_STYLE_DAEMON)
953 ebt_print_error2("--atomic is not supported in daemon mode");
954 if (OPT_COMMANDS)
955 ebt_print_error2("--atomic has to come before the command");
956 /* A possible memory leak here, but this is not
957 * executed in daemon mode */
958 replace->filename = (char *)malloc(strlen(optarg) + 1);
959 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +0000960 break;
Bart De Schuymera615b962002-11-03 14:54:09 +0000961 case 1 :
962 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000963 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +0000964 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000965 ebt_print_error2("Bad argument : %s", optarg);
966 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +0000967 optind--;
968 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000969 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +0000970 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000971 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000972 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t)))
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000973 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000974
Bart De Schuymer6622a012005-01-19 21:09:05 +0000975 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000976 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000977 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000978 break;
979
980 if (m != NULL) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000981 if (m->used == 0) {
982 ebt_add_match(new_entry, m);
983 m->used = 1;
984 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000985 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000986 }
987
Bart De Schuymer6622a012005-01-19 21:09:05 +0000988 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000989 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000990 if (w->parse(c-w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000991 break;
992
993 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000994 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000995 if (w->used == 0) {
996 ebt_add_watcher(new_entry, w);
997 w->used = 1;
998 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000999check_extension:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001000 if (replace->command != 'A' && replace->command != 'I' && replace->command != 'D')
1001 ebt_print_error2("Extensions only for -A, -I and -D");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001002 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001003 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001004 }
1005
Bart De Schuymer6622a012005-01-19 21:09:05 +00001006 /* Just in case we didn't catch an error */
1007 if (ebt_errormsg[0] != '\0')
1008 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001009
Bart De Schuymer6622a012005-01-19 21:09:05 +00001010 if (!(table = ebt_find_table(replace->name)))
1011 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001012
Bart De Schuymer6622a012005-01-19 21:09:05 +00001013 if (replace->command == 'h' && !(replace->flags & OPT_ZERO))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001014 print_help();
1015
Bart De Schuymer6622a012005-01-19 21:09:05 +00001016 /* Do the final checks */
1017 if (replace->command == 'A' || replace->command == 'I' ||
1018 replace->command == 'D') {
1019 /* This will put the hook_mask right for the chains */
1020 ebt_check_for_loops(replace);
1021 if (ebt_errormsg[0] != '\0')
1022 return -1;
1023 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001024 m_l = new_entry->m_list;
1025 w_l = new_entry->w_list;
1026 t = (struct ebt_u_target *)new_entry->t;
1027 while (m_l) {
1028 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001029 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001030 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001031 if (ebt_errormsg[0] != '\0')
1032 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001033 m_l = m_l->next;
1034 }
1035 while (w_l) {
1036 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001037 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001038 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001039 if (ebt_errormsg[0] != '\0')
1040 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001041 w_l = w_l->next;
1042 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001043 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001044 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001045 if (ebt_errormsg[0] != '\0')
1046 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001047 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001048 /* So, the extensions can work with the host endian.
1049 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001050 new_entry->ethproto = htons(new_entry->ethproto);
1051
Bart De Schuymer6622a012005-01-19 21:09:05 +00001052 if (replace->command == 'P') {
1053 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1054 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1055 ebt_change_policy(replace, policy);
1056 if (ebt_errormsg[0] != '\0')
1057 return -1;
1058 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001059 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001060 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001061 exit(0);
1062 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001063 if (replace->flags & OPT_ZERO) {
1064 replace->selected_chain = zerochain;
1065 ebt_zero_counters(replace);
1066 } else if (replace->command == 'F') {
1067 ebt_flush_chains(replace);
1068 } else if (replace->command == 'A' || replace->command == 'I') {
1069 ebt_add_rule(replace, new_entry, rule_nr);
1070 if (ebt_errormsg[0] != '\0')
1071 return -1;
1072 /* Makes undoing the add easier (jumps to delete_the_rule) */
1073 if (rule_nr <= 0)
1074 rule_nr--;
1075 rule_nr_end = rule_nr;
1076
1077 ebt_check_for_loops(replace);
1078 if (ebt_errormsg[0] != '\0')
1079 goto delete_the_rule;
1080
1081 /* Do the final_check(), for all entries.
1082 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001083 i = -1;
1084 while (1) {
1085 struct ebt_u_entry *e;
1086
1087 i++;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001088 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001089 if (!entries) {
1090 if (i < NF_BR_NUMHOOKS)
1091 continue;
1092 else
1093 break;
1094 }
1095 e = entries->entries;
1096 while (e) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001097 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001098 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001099 ebt_do_final_checks(replace, e, entries);
1100 if (ebt_errormsg[0] != '\0')
1101 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001102 e->ethproto = htons(e->ethproto);
1103 e = e->next;
1104 }
1105 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001106 /* Don't reuse the added rule */
1107 new_entry = NULL;
1108 } else if (replace->command == 'D')
1109delete_the_rule: /* This needs to be followed by a check on ebt_errormsg[0] */
1110 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
1111 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1112 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001113
Bart De Schuymer6622a012005-01-19 21:09:05 +00001114 if (ebt_errormsg[0] != '\0')
1115 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001116 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001117 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001118
Bart De Schuymer6622a012005-01-19 21:09:05 +00001119 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1120 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001121
Bart De Schuymer6622a012005-01-19 21:09:05 +00001122 if (replace->counterchanges)
1123 ebt_deliver_counters(replace, exec_style);
1124 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001125 return 0;
1126}