blob: 855b4fcd12ab6a370e1a5b054310983adb7df80a [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 Schuymer1abc55d2002-06-01 19:23:47 +000028#include "include/ebtables_u.h"
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000029#include "include/ethernetdb.h"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000030
Bart De Schuymer229079b2005-06-18 14:42:21 +000031/* Checks whether a command has already been specified */
Bart De Schuymerab611e22005-02-14 20:20:03 +000032#define OPT_COMMANDS (replace->flags & OPT_COMMAND || replace->flags & OPT_ZERO)
33
34#define OPT_COMMAND 0x01
35#define OPT_TABLE 0x02
36#define OPT_IN 0x04
37#define OPT_OUT 0x08
38#define OPT_JUMP 0x10
39#define OPT_PROTOCOL 0x20
40#define OPT_SOURCE 0x40
41#define OPT_DEST 0x80
42#define OPT_ZERO 0x100
43#define OPT_LOGICALIN 0x200
44#define OPT_LOGICALOUT 0x400
45#define OPT_KERNELDATA 0x800 /* This value is also defined in ebtablesd.c */
46#define OPT_COUNT 0x1000 /* This value is also defined in libebtc.c */
47#define OPT_CNT_INCR 0x2000 /* This value is also defined in libebtc.c */
48#define OPT_CNT_DECR 0x4000 /* This value is also defined in libebtc.c */
49
Bart De Schuymer58ae0402005-02-19 18:21:07 +000050/* Default command line options. Do not mess around with the already
Bart De Schuymer6622a012005-01-19 21:09:05 +000051 * assigned numbers unless you know what you are doing */
Bart De Schuymer62423742002-07-14 19:06:20 +000052static struct option ebt_original_options[] =
53{
Bart De Schuymerab611e22005-02-14 20:20:03 +000054 { "append" , required_argument, 0, 'A' },
55 { "insert" , required_argument, 0, 'I' },
56 { "delete" , required_argument, 0, 'D' },
57 { "list" , optional_argument, 0, 'L' },
58 { "Lc" , no_argument , 0, 4 },
59 { "Ln" , no_argument , 0, 5 },
60 { "Lx" , no_argument , 0, 6 },
61 { "Lmac2" , no_argument , 0, 12 },
62 { "zero" , optional_argument, 0, 'Z' },
63 { "flush" , optional_argument, 0, 'F' },
64 { "policy" , required_argument, 0, 'P' },
65 { "in-interface" , required_argument, 0, 'i' },
66 { "in-if" , required_argument, 0, 'i' },
67 { "logical-in" , required_argument, 0, 2 },
68 { "logical-out" , required_argument, 0, 3 },
69 { "out-interface" , required_argument, 0, 'o' },
70 { "out-if" , required_argument, 0, 'o' },
71 { "version" , no_argument , 0, 'V' },
72 { "help" , no_argument , 0, 'h' },
73 { "jump" , required_argument, 0, 'j' },
74 { "set-counters" , required_argument, 0, 'c' },
75 { "change-counters", required_argument, 0, 'C' },
76 { "proto" , required_argument, 0, 'p' },
77 { "protocol" , required_argument, 0, 'p' },
78 { "db" , required_argument, 0, 'b' },
79 { "source" , required_argument, 0, 's' },
80 { "src" , required_argument, 0, 's' },
81 { "destination" , required_argument, 0, 'd' },
82 { "dst" , required_argument, 0, 'd' },
83 { "table" , required_argument, 0, 't' },
84 { "modprobe" , required_argument, 0, 'M' },
85 { "new-chain" , required_argument, 0, 'N' },
86 { "rename-chain" , required_argument, 0, 'E' },
87 { "delete-chain" , optional_argument, 0, 'X' },
88 { "atomic-init" , no_argument , 0, 7 },
89 { "atomic-commit" , no_argument , 0, 8 },
90 { "atomic-file" , required_argument, 0, 9 },
91 { "atomic-save" , no_argument , 0, 10 },
92 { "init-table" , no_argument , 0, 11 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000093 { 0 }
94};
95
96static struct option *ebt_options = ebt_original_options;
97
Bart De Schuymer6622a012005-01-19 21:09:05 +000098/* Holds all the data */
99static struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000100
Bart De Schuymer6622a012005-01-19 21:09:05 +0000101/* The chosen table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000102static struct ebt_u_table *table;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000103
Bart De Schuymer6622a012005-01-19 21:09:05 +0000104/* The pointers in here are special:
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000105 * The struct ebt_target pointer is actually a struct ebt_u_target pointer.
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000106 * I do not feel like using a union.
Bart De Schuymer229079b2005-06-18 14:42:21 +0000107 * We need a struct ebt_u_target pointer because we know the address of the data
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000108 * they point to won't change. We want to allow that the struct ebt_u_target.t
109 * member can change.
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000110 * The same holds for the struct ebt_match and struct ebt_watcher pointers */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000111static struct ebt_u_entry *new_entry;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000112
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000113
Bart De Schuymer6622a012005-01-19 21:09:05 +0000114static int global_option_offset;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000115#define OPTION_OFFSET 256
Bart De Schuymer229079b2005-06-18 14:42:21 +0000116static struct option *merge_options(struct option *oldopts,
117 const struct option *newopts, unsigned int *options_offset)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000118{
119 unsigned int num_old, num_new, i;
120 struct option *merge;
121
122 if (!newopts || !oldopts || !options_offset)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000123 ebt_print_bug("merge wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000124 for (num_old = 0; oldopts[num_old].name; num_old++);
125 for (num_new = 0; newopts[num_new].name; num_new++);
126
127 global_option_offset += OPTION_OFFSET;
128 *options_offset = global_option_offset;
129
130 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
131 if (!merge)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000132 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000133 memcpy(merge, oldopts, num_old * sizeof(struct option));
134 for (i = 0; i < num_new; i++) {
135 merge[num_old + i] = newopts[i];
136 merge[num_old + i].val += *options_offset;
137 }
138 memset(merge + num_old + num_new, 0, sizeof(struct option));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000139 /* Only free dynamically allocated stuff */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000140 if (oldopts != ebt_original_options)
141 free(oldopts);
142
143 return merge;
144}
145
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000146static void merge_match(struct ebt_u_match *m)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000147{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000148 ebt_options = merge_options
149 (ebt_options, m->extra_ops, &(m->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000150}
151
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000152static void merge_watcher(struct ebt_u_watcher *w)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000153{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000154 ebt_options = merge_options
155 (ebt_options, w->extra_ops, &(w->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000156}
157
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000158static void merge_target(struct ebt_u_target *t)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000159{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000160 ebt_options = merge_options
161 (ebt_options, t->extra_ops, &(t->option_offset));
Bart De Schuymer9a0fbf22003-01-11 16:16:54 +0000162}
163
Bart De Schuymer6622a012005-01-19 21:09:05 +0000164/* Be backwards compatible, so don't use '+' in kernel */
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000165#define IF_WILDCARD 1
166static void print_iface(const char *iface)
167{
168 char *c;
169
170 if ((c = strchr(iface, IF_WILDCARD)))
171 *c = '+';
172 printf("%s ", iface);
173 if (c)
174 *c = IF_WILDCARD;
175}
176
Bart De Schuymer6622a012005-01-19 21:09:05 +0000177/* We use replace->flags, so we can't use the following values:
178 * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO */
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000179#define LIST_N 0x04
180#define LIST_C 0x08
181#define LIST_X 0x10
182#define LIST_MAC2 0x20
183
Bart De Schuymer6622a012005-01-19 21:09:05 +0000184/* Helper function for list_rules() */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000185static void list_em(struct ebt_u_entries *entries)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000186{
187 int i, j, space = 0, digits;
188 struct ebt_u_entry *hlp;
189 struct ebt_u_match_list *m_l;
190 struct ebt_u_watcher_list *w_l;
191 struct ebt_u_match *m;
192 struct ebt_u_watcher *w;
193 struct ebt_u_target *t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000194
Bart De Schuymer6622a012005-01-19 21:09:05 +0000195 if (replace->flags & LIST_MAC2)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000196 ebt_printstyle_mac = 2;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000197 else
198 ebt_printstyle_mac = 0;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000199 hlp = entries->entries->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000200 if (replace->flags & LIST_X && entries->policy != EBT_ACCEPT) {
201 printf("ebtables -t %s -P %s %s\n", replace->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000202 entries->name, ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000203 } else if (!(replace->flags & LIST_X)) {
Bart De Schuymerc87c9642002-08-01 15:34:16 +0000204 printf("\nBridge chain: %s, entries: %d, policy: %s\n",
205 entries->name, entries->nentries,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000206 ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000207 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000208
Bart De Schuymer229079b2005-06-18 14:42:21 +0000209 if (replace->flags & LIST_N) {
210 i = entries->nentries;
211 while (i > 9) {
212 space++;
213 i /= 10;
214 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000215 }
216
Bart De Schuymer60332e02002-06-23 08:01:47 +0000217 for (i = 0; i < entries->nentries; i++) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000218 if (replace->flags & LIST_N) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000219 digits = 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000220 /* A little work to get nice rule numbers. */
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000221 j = i + 1;
222 while (j > 9) {
223 digits++;
224 j /= 10;
225 }
226 for (j = 0; j < space - digits; j++)
227 printf(" ");
228 printf("%d. ", i + 1);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000229 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000230 if (replace->flags & LIST_X)
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000231 printf("ebtables -t %s -A %s ",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000232 replace->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000233
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000234 /* The standard target's print() uses this to find out
235 * the name of a udc */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000236 hlp->replace = replace;
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000237
Bart De Schuymer6622a012005-01-19 21:09:05 +0000238 /* Don't print anything about the protocol if no protocol was
239 * specified, obviously this means any protocol will do. */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000240 if (!(hlp->bitmask & EBT_NOPROTO)) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000241 printf("-p ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000242 if (hlp->invflags & EBT_IPROTO)
243 printf("! ");
244 if (hlp->bitmask & EBT_802_3)
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000245 printf("Length ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000246 else {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000247 struct ethertypeent *ent;
248
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000249 ent = getethertypebynumber(ntohs(hlp->ethproto));
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000250 if (!ent)
Bart De Schuymer60332e02002-06-23 08:01:47 +0000251 printf("0x%x ", ntohs(hlp->ethproto));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000252 else
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000253 printf("%s ", ent->e_name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000254 }
255 }
256 if (hlp->bitmask & EBT_SOURCEMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000257 printf("-s ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000258 if (hlp->invflags & EBT_ISOURCE)
259 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000260 ebt_print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000261 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000262 }
263 if (hlp->bitmask & EBT_DESTMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000264 printf("-d ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000265 if (hlp->invflags & EBT_IDEST)
266 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000267 ebt_print_mac_and_mask(hlp->destmac, hlp->destmsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000268 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000269 }
270 if (hlp->in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000271 printf("-i ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000272 if (hlp->invflags & EBT_IIN)
273 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000274 print_iface(hlp->in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000275 }
276 if (hlp->logical_in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000277 printf("--logical-in ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000278 if (hlp->invflags & EBT_ILOGICALIN)
279 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000280 print_iface(hlp->logical_in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000281 }
282 if (hlp->logical_out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000283 printf("--logical-out ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000284 if (hlp->invflags & EBT_ILOGICALOUT)
285 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000286 print_iface(hlp->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000287 }
288 if (hlp->out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000289 printf("-o ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000290 if (hlp->invflags & EBT_IOUT)
291 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000292 print_iface(hlp->out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000293 }
294
295 m_l = hlp->m_list;
296 while (m_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000297 m = ebt_find_match(m_l->m->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000298 if (!m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000299 ebt_print_bug("Match not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000300 m->print(hlp, m_l->m);
301 m_l = m_l->next;
302 }
303 w_l = hlp->w_list;
304 while (w_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000305 w = ebt_find_watcher(w_l->w->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000306 if (!w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000307 ebt_print_bug("Watcher not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000308 w->print(hlp, w_l->w);
309 w_l = w_l->next;
310 }
311
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000312 printf("-j ");
313 if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET))
314 printf("%s ", hlp->t->u.name);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000315 t = ebt_find_target(hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000316 if (!t)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000317 ebt_print_bug("Target '%s' not found", hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000318 t->print(hlp, hlp->t);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000319 if (replace->flags & LIST_C) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000320 uint64_t pcnt = hlp->cnt.pcnt;
321 uint64_t bcnt = hlp->cnt.bcnt;
322
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000323 if (replace->flags & LIST_X)
Bart De Schuymerab611e22005-02-14 20:20:03 +0000324 printf("-c %llu %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000325 else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000326 printf(", pcnt = %llu -- bcnt = %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000327 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000328 printf("\n");
329 hlp = hlp->next;
330 }
331}
332
Bart De Schuymer62423742002-07-14 19:06:20 +0000333static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000334{
335 struct ebt_u_match_list *m_l;
336 struct ebt_u_watcher_list *w_l;
337
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000338 PRINT_VERSION;
339 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000340"Usage:\n"
341"ebtables -[ADI] chain rule-specification [options]\n"
342"ebtables -P chain target\n"
343"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000344"ebtables -[NX] [chain]\n"
345"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000346"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000347"--append -A chain : append to chain\n"
348"--delete -D chain : delete matching rule from chain\n"
349"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000350"--change-counters -C chain\n"
351" [rulenum] pcnt bcnt : change counters of existing rule\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000352"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000353"--list -L [chain] : list the rules in a chain or in all chains\n"
354"--flush -F [chain] : delete all rules in chain or in all chains\n"
355"--init-table : replace the kernel table with the initial table\n"
356"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
357"--policy -P chain target : change policy on chain to target\n"
358"--new-chain -N chain : create a user defined chain\n"
359"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000360"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000361"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
362"--atomic-init : put the initial kernel table into <FILE>\n"
363"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000364"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000365"Options:\n"
366"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
367"--src -s [!] address[/mask]: source mac address\n"
368"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000369"--in-if -i [!] name[+] : network input interface name\n"
370"--out-if -o [!] name[+] : network output interface name\n"
371"--logical-in [!] name[+] : logical bridge input interface name\n"
372"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000373"--set-counters -c chain\n"
374" pcnt bcnt : set the counters of the to be added rule\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000375"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000376"--version -V : print package version\n\n"
377"Environment variable:\n"
378ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
379"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000380 m_l = new_entry->m_list;
381 while (m_l) {
382 ((struct ebt_u_match *)m_l->m)->help();
383 printf("\n");
384 m_l = m_l->next;
385 }
386 w_l = new_entry->w_list;
387 while (w_l) {
388 ((struct ebt_u_watcher *)w_l->w)->help();
389 printf("\n");
390 w_l = w_l->next;
391 }
392 ((struct ebt_u_target *)new_entry->t)->help();
393 printf("\n");
394 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000395 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000396}
397
Bart De Schuymer6622a012005-01-19 21:09:05 +0000398/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000399static void list_rules()
400{
Bart De Schuymer229079b2005-06-18 14:42:21 +0000401 int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000402
Bart De Schuymer6622a012005-01-19 21:09:05 +0000403 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000404 printf("Bridge table: %s\n", table->name);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000405 if (replace->selected_chain != -1)
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000406 list_em(ebt_to_chain(replace));
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000407 else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000408 /* Create new chains and rename standard chains when necessary */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000409 if (replace->flags & LIST_X && replace->num_chains > NF_BR_NUMHOOKS) {
410 for (i = NF_BR_NUMHOOKS; i < replace->num_chains; i++)
411 printf("ebtables -t %s -N %s\n", replace->name, replace->chains[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000412 for (i = 0; i < NF_BR_NUMHOOKS; i++)
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000413 if (replace->chains[i] && strcmp(replace->chains[i]->name, ebt_hooknames[i]))
414 printf("ebtables -t %s -E %s %s\n", replace->name, ebt_hooknames[i], replace->chains[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000415 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000416 for (i = 0; i < replace->num_chains; i++)
417 if (replace->chains[i])
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000418 list_em(replace->chains[i]);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000419 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000420}
421
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000422static int parse_rule_range(const char *argv, int *rule_nr, int *rule_nr_end)
Bart De Schuymercc440052002-11-06 21:10:33 +0000423{
424 char *colon = strchr(argv, ':'), *buffer;
425
426 if (colon) {
427 *colon = '\0';
428 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000429 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000430 else {
431 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000432 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000433 return -1;
434 }
435 }
436 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000437 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000438 else {
439 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000440 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000441 return -1;
442 }
443 if (!colon)
444 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000445 return 0;
446}
447
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000448/* Incrementing or decrementing rules in daemon mode is not supported as the
Bart De Schuymer229079b2005-06-18 14:42:21 +0000449 * involved code overload is not worth it (too annoying to take the increased
450 * counters in the kernel into account). */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000451static int parse_change_counters_rule(int argc, char **argv, int *rule_nr, int *rule_nr_end, int exec_style)
Bart De Schuymerff587202005-02-08 20:02:28 +0000452{
453 char *buffer;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000454 int ret = 0;
Bart De Schuymerff587202005-02-08 20:02:28 +0000455
456 if (optind + 1 >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')) ||
457 (argv[optind + 1][0] == '-' && (argv[optind + 1][1] < '0' && argv[optind + 1][1] > '9')))
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000458 ebt_print_error2("The command -C needs at least 2 arguments");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000459 if (optind + 2 < argc && (argv[optind + 2][0] != '-' || (argv[optind + 2][1] >= '0' && argv[optind + 2][1] <= '9'))) {
Bart De Schuymerff587202005-02-08 20:02:28 +0000460 if (optind + 3 != argc)
461 ebt_print_error2("No extra options allowed with -C start_nr[:end_nr] pcnt bcnt");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000462 if (parse_rule_range(argv[optind], rule_nr, rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000463 ebt_print_error2("Something is wrong with the rule number specification '%s'", argv[optind]);
464 optind++;
465 }
466
Bart De Schuymerab611e22005-02-14 20:20:03 +0000467 if (argv[optind][0] == '+') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000468 if (exec_style == EXEC_STYLE_DAEMON)
469daemon_incr:
470 ebt_print_error2("Incrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
471 ret += 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000472 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymerab611e22005-02-14 20:20:03 +0000473 } else if (argv[optind][0] == '-') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000474 if (exec_style == EXEC_STYLE_DAEMON)
475daemon_decr:
476 ebt_print_error2("Decrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
477 ret += 2;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000478 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000479 } else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000480 new_entry->cnt_surplus.pcnt = strtoull(argv[optind], &buffer, 10);
Bart De Schuymerff587202005-02-08 20:02:28 +0000481
Bart De Schuymerab611e22005-02-14 20:20:03 +0000482 if (*buffer != '\0')
483 goto invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000484 optind++;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000485 if (argv[optind][0] == '+') {
486 if (exec_style == EXEC_STYLE_DAEMON)
487 goto daemon_incr;
488 ret += 3;
489 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
490 } else if (argv[optind][0] == '-') {
491 if (exec_style == EXEC_STYLE_DAEMON)
492 goto daemon_decr;
493 ret += 6;
494 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
495 } else
496 new_entry->cnt_surplus.bcnt = strtoull(argv[optind], &buffer, 10);
497
498 if (*buffer != '\0')
499 goto invalid;
500 optind++;
501 return ret;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000502invalid:
503 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerff587202005-02-08 20:02:28 +0000504}
505
Bart De Schuymer6622a012005-01-19 21:09:05 +0000506static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000507{
508 char *c;
509
510 if ((c = strchr(iface, '+'))) {
511 if (*(c + 1) != '\0') {
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000512 ebt_print_error("Spurious characters after '+' wildcard for '%s'", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000513 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000514 } else
515 *c = IF_WILDCARD;
516 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000517 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000518}
519
Bart De Schuymer6622a012005-01-19 21:09:05 +0000520void ebt_early_init_once()
521{
522 ebt_iterate_matches(merge_match);
523 ebt_iterate_watchers(merge_watcher);
524 ebt_iterate_targets(merge_target);
525}
526
Bart De Schuymer229079b2005-06-18 14:42:21 +0000527/* We use exec_style instead of #ifdef's because ebtables.so is a shared object. */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000528int do_command(int argc, char *argv[], int exec_style,
529 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000530{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000531 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000532 int c, i;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000533 int zerochain = -1; /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000534 int chcounter; /* Needed for -C */
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000535 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000536 int rule_nr = 0;
537 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000538 struct ebt_u_target *t;
539 struct ebt_u_match *m;
540 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000541 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000542 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000543 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000544
Bart De Schuymera615b962002-11-03 14:54:09 +0000545 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000546 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000547
Bart De Schuymer6622a012005-01-19 21:09:05 +0000548 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000549
Bart De Schuymer6622a012005-01-19 21:09:05 +0000550 /* The daemon doesn't use the environment variable */
551 if (exec_style == EXEC_STYLE_PRG) {
552 buffer = getenv(ATOMIC_ENV_VARIABLE);
553 if (buffer) {
554 replace->filename = malloc(strlen(buffer) + 1);
555 if (!replace->filename)
556 ebt_print_memory();
557 strcpy(replace->filename, buffer);
558 buffer = NULL;
559 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000560 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000561
Bart De Schuymer6622a012005-01-19 21:09:05 +0000562 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
563 replace->selected_chain = -1;
564 replace->command = 'h';
565
566 if (!new_entry) {
567 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
568 if (!new_entry)
569 ebt_print_memory();
570 }
571 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000572 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000573 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000574
Bart De Schuymer6622a012005-01-19 21:09:05 +0000575 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000576 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000577 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000578
Bart De Schuymer6622a012005-01-19 21:09:05 +0000579 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000580 while ((c = getopt_long(argc, argv,
Bart De Schuymerff587202005-02-08 20:02:28 +0000581 "-A:D:C:I:N:E:X::L::Z::F::P:Vhi:o:j:c:p:s:d:t:M:", ebt_options, NULL)) != -1) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000582 switch (c) {
583
Bart De Schuymer6622a012005-01-19 21:09:05 +0000584 case 'A': /* Add a rule */
585 case 'D': /* Delete a rule */
Bart De Schuymerff587202005-02-08 20:02:28 +0000586 case 'C': /* Change counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000587 case 'P': /* Define policy */
588 case 'I': /* Insert a rule */
589 case 'N': /* Make a user defined chain */
590 case 'E': /* Rename chain */
591 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000592 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000593 if (replace->command == 'N' && c == 'P') {
594 replace->command = c;
595 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000596 goto handle_P;
597 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000598 if (OPT_COMMANDS)
599 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000600
Bart De Schuymer6622a012005-01-19 21:09:05 +0000601 replace->command = c;
602 replace->flags |= OPT_COMMAND;
603 if (!(replace->flags & OPT_KERNELDATA))
604 ebt_get_kernel_table(replace, 0);
605 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
606 ebt_print_error2("No chain name specified");
607 if (c == 'N') {
608 ebt_new_chain(replace, optarg, EBT_ACCEPT);
609 /* This is needed to get -N x -P y working */
610 replace->selected_chain = ebt_get_chainnr(replace, optarg);
611 break;
612 } else if (c == 'X') {
613 if (optind >= argc) {
614 replace->selected_chain = -1;
615 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000616 break;
617 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000618
619 if (optind < argc - 1)
620 ebt_print_error2("No extra options allowed with -X");
621
622 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000623 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000624 ebt_delete_chain(replace);
625 if (ebt_errormsg[0] != '\0')
626 return -1;
627 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000628 break;
629 }
630
Bart De Schuymer6622a012005-01-19 21:09:05 +0000631 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000632 ebt_print_error2("Chain '%s' doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000633 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000634 if (optind >= argc)
635 ebt_print_error2("No new chain name specified");
636 if (optind < argc - 1)
637 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000638 if (strlen(argv[optind]) >= EBT_CHAIN_MAXNAMELEN)
639 ebt_print_error2("Chain name length can't exceed %d characters", EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000640 if (ebt_get_chainnr(replace, argv[optind]) != -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000641 ebt_print_error2("Chain '%s' already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000642 if (ebt_find_target(argv[optind]))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000643 ebt_print_error2("Target with name '%s' exists", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000645 optind++;
646 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000647 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
648 if (optind != argc - 1)
649 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000650 if (parse_rule_range(argv[optind], &rule_nr, &rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000651 ebt_print_error2("Problem with the specified rule number(s) '%s'", argv[optind]);
Bart De Schuymercc440052002-11-06 21:10:33 +0000652 optind++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000653 } else if (c == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000654 if ((chcounter = parse_change_counters_rule(argc, argv, &rule_nr, &rule_nr_end, exec_style)) == -1)
Bart De Schuymerff587202005-02-08 20:02:28 +0000655 return -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000656 } else if (c == 'I') {
657 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
Bart De Schuymer01581232005-07-24 09:46:09 +0000658 rule_nr = 1;
659 else {
660 rule_nr = strtol(argv[optind], &buffer, 10);
661 if (*buffer != '\0')
662 ebt_print_error2("Problem with the specified rule number '%s'", argv[optind]);
663 optind++;
664 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000665 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000666handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000667 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000668 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000669 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000670 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000671 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000672 if (policy == EBT_CONTINUE)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000673 ebt_print_error2("Wrong policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000674 break;
675 }
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000676 if (i == NUM_STANDARD_TARGETS)
677 ebt_print_error2("Unknown policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000678 optind++;
679 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000680 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000681 case 'L': /* List */
682 case 'F': /* Flush */
683 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000684 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000685 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
686print_zero:
687 ebt_print_error2("Command -Z only allowed together with command -L");
688 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000689 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000690 if (replace->flags & OPT_COMMAND)
691 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000692 replace->command = c;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 replace->flags |= OPT_COMMAND;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000694 if (replace->flags & OPT_ZERO && c != 'L')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000695 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000696 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697
698#ifdef SILENT_DAEMON
699 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000700 ebt_print_error2("-L not supported in daemon mode");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000701#endif
702
703 if (!(replace->flags & OPT_KERNELDATA))
704 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000705 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000706 if (optind < argc && argv[optind][0] != '-') {
707 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000708 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000709 optind++;
710 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000711 if (i != -1) {
712 if (c == 'Z')
713 zerochain = i;
714 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000715 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000716 }
717 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000718 case 'V': /* Version */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000719 if (OPT_COMMANDS)
720 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000721 replace->command = 'V';
Bart De Schuymer6622a012005-01-19 21:09:05 +0000722 if (exec_style == EXEC_STYLE_DAEMON)
723 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000724 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000725 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000726 case 'M': /* Modprobe */
727 if (OPT_COMMANDS)
728 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000729 free(ebt_modprobe);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000730 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000731 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000732 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000733#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000734 if (exec_style == EXEC_STYLE_DAEMON)
735 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000736#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000737 if (OPT_COMMANDS)
738 ebt_print_error2("Multiple commands are not allowed");
739 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000740
Bart De Schuymer6622a012005-01-19 21:09:05 +0000741 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000742 while (optind < argc) {
743 struct ebt_u_match *m;
744 struct ebt_u_watcher *w;
745
Bart De Schuymer6622a012005-01-19 21:09:05 +0000746 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000747 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000748 exit(0);
749 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000750 if ((m = ebt_find_match(argv[optind])))
751 ebt_add_match(new_entry, m);
752 else if ((w = ebt_find_watcher(argv[optind])))
753 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000754 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000755 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000756 ebt_print_error2("Extension '%s' not found", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000757 if (replace->flags & OPT_JUMP)
758 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
759 replace->flags |= OPT_JUMP;
760 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000761 }
762 optind++;
763 }
764 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000765 case 't': /* Table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000766 if (OPT_COMMANDS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000767 ebt_print_error2("Please put the -t option first");
768 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000769 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000770 ebt_print_error2("Table name length cannot exceed %d characters", EBT_TABLE_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000771 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000772 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000773 case 'i': /* Input interface */
774 case 2 : /* Logical input interface */
775 case 'o': /* Output interface */
776 case 3 : /* Logical output interface */
777 case 'j': /* Target */
778 case 'p': /* Net family protocol */
779 case 's': /* Source mac */
780 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000781 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000782 if (!OPT_COMMANDS)
783 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000784 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000785 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000786 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000787 ebt_check_option2(&(replace->flags), OPT_IN);
788 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
789 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
790 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000791 new_entry->invflags |= EBT_IIN;
792
Bart De Schuymer6622a012005-01-19 21:09:05 +0000793 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000794big_iface_length:
795 ebt_print_error2("Interface name length cannot exceed %d characters", IFNAMSIZ - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000796 strcpy(new_entry->in, optarg);
797 if (parse_iface(new_entry->in, "-i"))
798 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000799 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000800 } else if (c == 2) {
801 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
802 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
803 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
804 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000805 new_entry->invflags |= EBT_ILOGICALIN;
806
Bart De Schuymer6622a012005-01-19 21:09:05 +0000807 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000808 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000809 strcpy(new_entry->logical_in, optarg);
810 if (parse_iface(new_entry->logical_in, "--logical-in"))
811 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000812 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000813 } else if (c == 'o') {
814 ebt_check_option2(&(replace->flags), OPT_OUT);
815 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
816 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
817 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000818 new_entry->invflags |= EBT_IOUT;
819
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000821 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000822 strcpy(new_entry->out, optarg);
823 if (parse_iface(new_entry->out, "-o"))
824 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000825 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000826 } else if (c == 3) {
827 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000828 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000829 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
830 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000831 new_entry->invflags |= EBT_ILOGICALOUT;
832
Bart De Schuymer6622a012005-01-19 21:09:05 +0000833 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000834 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000835 strcpy(new_entry->logical_out, optarg);
836 if (parse_iface(new_entry->logical_out, "--logical-out"))
837 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000838 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000839 } else if (c == 'j') {
840 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000841 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000842 if (!strcmp(optarg, ebt_standard_targets[i])) {
843 t = ebt_find_target(EBT_STANDARD_TARGET);
844 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000845 break;
846 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000847 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
848 ebt_print_error2("Return target only for user defined chains");
849 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000850 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000851
852 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000853 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000854 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000855 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000856 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000857 break;
858 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000859 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000860 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000861
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000862 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000863 /* -j standard not allowed either */
864 if (!t || t == (struct ebt_u_target *)new_entry->t)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000865 ebt_print_error2("Illegal target name '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000866 new_entry->t = (struct ebt_entry_target *)t;
867 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
868 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000869 }
870 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000871 } else if (c == 's') {
872 ebt_check_option2(&(replace->flags), OPT_SOURCE);
873 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000874 new_entry->invflags |= EBT_ISOURCE;
875
Bart De Schuymer6622a012005-01-19 21:09:05 +0000876 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000877 ebt_print_error2("Problem with specified source mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000878 new_entry->bitmask |= EBT_SOURCEMAC;
879 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000880 } else if (c == 'd') {
881 ebt_check_option2(&(replace->flags), OPT_DEST);
882 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000883 new_entry->invflags |= EBT_IDEST;
884
Bart De Schuymer6622a012005-01-19 21:09:05 +0000885 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000886 ebt_print_error2("Problem with specified destination mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000887 new_entry->bitmask |= EBT_DESTMAC;
888 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000889 } else if (c == 'c') {
890 ebt_check_option2(&(replace->flags), OPT_COUNT);
891 if (ebt_check_inverse2(optarg))
892 ebt_print_error2("Unexpected '!' after -c");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000893 if (optind >= argc || optarg[0] == '-' || argv[optind][0] == '-')
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000894 ebt_print_error2("Option -c needs 2 arguments");
895
896 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
897 if (*buffer != '\0')
898 ebt_print_error2("Packet counter '%s' invalid", optarg)
899 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
900 if (*buffer != '\0')
901 ebt_print_error2("Packet counter '%s' invalid", argv[optind])
902 optind++;
903 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000904 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000905 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
906 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000907 new_entry->invflags |= EBT_IPROTO;
908
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000909 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000910 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000911 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000912 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000913 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000914 struct ethertypeent *ent;
915
Bart De Schuymer6622a012005-01-19 21:09:05 +0000916 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000917 new_entry->bitmask |= EBT_802_3;
918 break;
919 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000920 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000921 if (!ent)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000922 ebt_print_error2("Problem with the specified Ethernet protocol '%s', perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000923 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000924 } else
925 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000926
Bart De Schuymer6622a012005-01-19 21:09:05 +0000927 if (new_entry->ethproto < 0x0600)
928 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
929 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000930 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000931#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000932 if (exec_style == EXEC_STYLE_DAEMON)
933 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000934#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000935 ebt_check_option2(&(replace->flags), LIST_C);
936 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000937 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000938 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000939 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000940 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000941#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000942 if (exec_style == EXEC_STYLE_DAEMON)
943 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000944#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000945 ebt_check_option2(&(replace->flags), LIST_N);
946 if (replace->command != 'L')
947 ebt_print_error2("Use --Ln with -L");
948 if (replace->flags & LIST_X)
949 ebt_print_error2("--Lx is not compatible with --Ln");
950 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000951 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000952 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +0000953#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000954 if (exec_style == EXEC_STYLE_DAEMON)
955 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000956#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000957 ebt_check_option2(&(replace->flags), LIST_X);
958 if (replace->command != 'L')
959 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000960 if (replace->flags & LIST_N)
961 ebt_print_error2("--Lx is not compatible with --Ln");
962 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000963 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000964 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +0000965#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000966 if (exec_style == EXEC_STYLE_DAEMON)
967 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000968#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000969 ebt_check_option2(&(replace->flags), LIST_MAC2);
970 if (replace->command != 'L')
971 ebt_print_error2("Use --Lmac2 with -L");
972 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000973 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000974 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000975 if (exec_style == EXEC_STYLE_DAEMON)
976 ebt_print_error2("--atomic-commit is not supported in daemon mode");
977 replace->command = c;
978 if (OPT_COMMANDS)
979 ebt_print_error2("Multiple commands are not allowed");
980 replace->flags |= OPT_COMMAND;
981 if (!replace->filename)
982 ebt_print_error2("No atomic file specified");
983 /* Get the information from the file */
984 ebt_get_table(replace, 0);
985 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000986 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +0000987 * the file */
988 replace->num_counters = 0;
989 /* Make sure the table will be written to the kernel */
990 free(replace->filename);
991 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000992 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000993 case 7 : /* atomic-init */
994 case 10: /* atomic-save */
995 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000996 if (exec_style == EXEC_STYLE_DAEMON) {
997 if (c == 7) {
998 ebt_print_error2("--atomic-init is not supported in daemon mode");
999 } else if (c == 10)
1000 ebt_print_error2("--atomic-save is not supported in daemon mode");
1001 ebt_print_error2("--init-table is not supported in daemon mode");
1002 }
1003 replace->command = c;
1004 if (OPT_COMMANDS)
1005 ebt_print_error2("Multiple commands are not allowed");
1006 if (c != 11 && !replace->filename)
1007 ebt_print_error2("No atomic file specified");
1008 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001009 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001010 char *tmp = replace->filename;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001011
Bart De Schuymer6622a012005-01-19 21:09:05 +00001012 /* Get the kernel table */
1013 replace->filename = NULL;
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001014 ebt_get_kernel_table(replace, c == 10 ? 0 : 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001015 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001016 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001017 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001018 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001019 if (exec_style == EXEC_STYLE_DAEMON)
1020 ebt_print_error2("--atomic is not supported in daemon mode");
1021 if (OPT_COMMANDS)
1022 ebt_print_error2("--atomic has to come before the command");
1023 /* A possible memory leak here, but this is not
1024 * executed in daemon mode */
1025 replace->filename = (char *)malloc(strlen(optarg) + 1);
1026 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001027 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001028 case 1 :
1029 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001030 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001031 else
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001032 ebt_print_error2("Bad argument : '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001033 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001034 optind--;
1035 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001036 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001037 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001038 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001039 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1040 if (ebt_errormsg[0] != '\0')
1041 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001042 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001043 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001044
Bart De Schuymer6622a012005-01-19 21:09:05 +00001045 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001046 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001047 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001048 break;
1049
1050 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001051 if (ebt_errormsg[0] != '\0')
1052 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001053 if (m->used == 0) {
1054 ebt_add_match(new_entry, m);
1055 m->used = 1;
1056 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001057 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001058 }
1059
Bart De Schuymer6622a012005-01-19 21:09:05 +00001060 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001061 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001062 if (w->parse(c - w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001063 break;
1064
1065 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001066 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymerff587202005-02-08 20:02:28 +00001067 if (ebt_errormsg[0] != '\0')
1068 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001069 if (w->used == 0) {
1070 ebt_add_watcher(new_entry, w);
1071 w->used = 1;
1072 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001073check_extension:
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001074 if (replace->command != 'A' && replace->command != 'I' &&
1075 replace->command != 'D' && replace->command != 'C')
1076 ebt_print_error2("Extensions only for -A, -I, -D and -C");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001077 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001078 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001079 }
1080
Bart De Schuymer6622a012005-01-19 21:09:05 +00001081 /* Just in case we didn't catch an error */
1082 if (ebt_errormsg[0] != '\0')
1083 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001084
Bart De Schuymer6622a012005-01-19 21:09:05 +00001085 if (!(table = ebt_find_table(replace->name)))
1086 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001087
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001088 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001089 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001090 if (exec_style == EXEC_STYLE_PRG)
1091 exit(0);
1092 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001093
Bart De Schuymer6622a012005-01-19 21:09:05 +00001094 /* Do the final checks */
1095 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001096 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001097 /* This will put the hook_mask right for the chains */
1098 ebt_check_for_loops(replace);
1099 if (ebt_errormsg[0] != '\0')
1100 return -1;
1101 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001102 m_l = new_entry->m_list;
1103 w_l = new_entry->w_list;
1104 t = (struct ebt_u_target *)new_entry->t;
1105 while (m_l) {
1106 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001107 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001108 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001109 if (ebt_errormsg[0] != '\0')
1110 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001111 m_l = m_l->next;
1112 }
1113 while (w_l) {
1114 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001115 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001116 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001117 if (ebt_errormsg[0] != '\0')
1118 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001119 w_l = w_l->next;
1120 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001121 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001122 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001123 if (ebt_errormsg[0] != '\0')
1124 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001125 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001126 /* So, the extensions can work with the host endian.
1127 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001128 new_entry->ethproto = htons(new_entry->ethproto);
1129
Bart De Schuymer6622a012005-01-19 21:09:05 +00001130 if (replace->command == 'P') {
1131 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1132 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1133 ebt_change_policy(replace, policy);
1134 if (ebt_errormsg[0] != '\0')
1135 return -1;
1136 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001137 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001138 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001139 exit(0);
1140 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001141 if (replace->flags & OPT_ZERO) {
1142 replace->selected_chain = zerochain;
1143 ebt_zero_counters(replace);
1144 } else if (replace->command == 'F') {
1145 ebt_flush_chains(replace);
1146 } else if (replace->command == 'A' || replace->command == 'I') {
1147 ebt_add_rule(replace, new_entry, rule_nr);
1148 if (ebt_errormsg[0] != '\0')
1149 return -1;
1150 /* Makes undoing the add easier (jumps to delete_the_rule) */
1151 if (rule_nr <= 0)
1152 rule_nr--;
1153 rule_nr_end = rule_nr;
1154
1155 ebt_check_for_loops(replace);
1156 if (ebt_errormsg[0] != '\0')
1157 goto delete_the_rule;
1158
1159 /* Do the final_check(), for all entries.
1160 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001161 i = -1;
1162 while (1) {
1163 struct ebt_u_entry *e;
1164
1165 i++;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001166 entries = replace->chains[i];
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001167 if (!entries) {
1168 if (i < NF_BR_NUMHOOKS)
1169 continue;
1170 else
1171 break;
1172 }
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001173 e = entries->entries->next;
1174 while (e != entries->entries) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001175 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001176 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001177 ebt_do_final_checks(replace, e, entries);
1178 if (ebt_errormsg[0] != '\0')
1179 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001180 e->ethproto = htons(e->ethproto);
1181 e = e->next;
1182 }
1183 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001184 /* Don't reuse the added rule */
1185 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001186 } else if (replace->command == 'D') {
1187delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001188 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001189 if (ebt_errormsg[0] != '\0')
1190 return -1;
1191 } else if (replace->command == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001192 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus), chcounter);
Bart De Schuymerff587202005-02-08 20:02:28 +00001193 if (ebt_errormsg[0] != '\0')
1194 return -1;
1195 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001196 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1197 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001198
Bart De Schuymer6622a012005-01-19 21:09:05 +00001199 if (ebt_errormsg[0] != '\0')
1200 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001201 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001202 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001203
Bart De Schuymer6622a012005-01-19 21:09:05 +00001204 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1205 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001206
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001207 if (replace->nentries)
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001208 ebt_deliver_counters(replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001209 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001210 return 0;
1211}