blob: 91f40f272f0aaa809f45787db5e94a6d4c2afde3 [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') {
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000608 if (ebt_get_chainnr(replace, optarg) != -1)
609 ebt_print_error2("Chain %s already exists", optarg);
610 else if (ebt_find_target(optarg))
611 ebt_print_error2("Target with name %s exists", optarg);
612 else if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN)
613 ebt_print_error2("Chain name length can't exceed %d",
614 EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000615 ebt_new_chain(replace, optarg, EBT_ACCEPT);
616 /* This is needed to get -N x -P y working */
617 replace->selected_chain = ebt_get_chainnr(replace, optarg);
618 break;
619 } else if (c == 'X') {
620 if (optind >= argc) {
621 replace->selected_chain = -1;
622 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000623 break;
624 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000625
626 if (optind < argc - 1)
627 ebt_print_error2("No extra options allowed with -X");
628
629 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000630 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000631 ebt_delete_chain(replace);
632 if (ebt_errormsg[0] != '\0')
633 return -1;
634 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000635 break;
636 }
637
Bart De Schuymer6622a012005-01-19 21:09:05 +0000638 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000639 ebt_print_error2("Chain '%s' doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000640 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000641 if (optind >= argc)
642 ebt_print_error2("No new chain name specified");
643 if (optind < argc - 1)
644 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000645 if (strlen(argv[optind]) >= EBT_CHAIN_MAXNAMELEN)
646 ebt_print_error2("Chain name length can't exceed %d characters", EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000647 if (ebt_get_chainnr(replace, argv[optind]) != -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000648 ebt_print_error2("Chain '%s' already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000649 if (ebt_find_target(argv[optind]))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000650 ebt_print_error2("Target with name '%s' exists", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000651 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000652 optind++;
653 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000654 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
655 if (optind != argc - 1)
656 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000657 if (parse_rule_range(argv[optind], &rule_nr, &rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000658 ebt_print_error2("Problem with the specified rule number(s) '%s'", argv[optind]);
Bart De Schuymercc440052002-11-06 21:10:33 +0000659 optind++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000660 } else if (c == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000661 if ((chcounter = parse_change_counters_rule(argc, argv, &rule_nr, &rule_nr_end, exec_style)) == -1)
Bart De Schuymerff587202005-02-08 20:02:28 +0000662 return -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000663 } else if (c == 'I') {
664 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
Bart De Schuymer01581232005-07-24 09:46:09 +0000665 rule_nr = 1;
666 else {
667 rule_nr = strtol(argv[optind], &buffer, 10);
668 if (*buffer != '\0')
669 ebt_print_error2("Problem with the specified rule number '%s'", argv[optind]);
670 optind++;
671 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000672 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000673handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000674 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000675 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000676 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000677 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000678 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000679 if (policy == EBT_CONTINUE)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000680 ebt_print_error2("Wrong policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000681 break;
682 }
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000683 if (i == NUM_STANDARD_TARGETS)
684 ebt_print_error2("Unknown policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000685 optind++;
686 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000687 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000688 case 'L': /* List */
689 case 'F': /* Flush */
690 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000691 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000692 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
693print_zero:
694 ebt_print_error2("Command -Z only allowed together with command -L");
695 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000696 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697 if (replace->flags & OPT_COMMAND)
698 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000699 replace->command = c;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000700 replace->flags |= OPT_COMMAND;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000701 if (replace->flags & OPT_ZERO && c != 'L')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000702 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000703 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000704
705#ifdef SILENT_DAEMON
706 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000707 ebt_print_error2("-L not supported in daemon mode");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000708#endif
709
710 if (!(replace->flags & OPT_KERNELDATA))
711 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000712 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000713 if (optind < argc && argv[optind][0] != '-') {
714 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000715 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000716 optind++;
717 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000718 if (i != -1) {
719 if (c == 'Z')
720 zerochain = i;
721 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000722 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000723 }
724 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000725 case 'V': /* Version */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000726 if (OPT_COMMANDS)
727 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000728 replace->command = 'V';
Bart De Schuymer6622a012005-01-19 21:09:05 +0000729 if (exec_style == EXEC_STYLE_DAEMON)
730 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000731 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000732 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000733 case 'M': /* Modprobe */
734 if (OPT_COMMANDS)
735 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000736 free(ebt_modprobe);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000737 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000738 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000739 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000740#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000741 if (exec_style == EXEC_STYLE_DAEMON)
742 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000743#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000744 if (OPT_COMMANDS)
745 ebt_print_error2("Multiple commands are not allowed");
746 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000747
Bart De Schuymer6622a012005-01-19 21:09:05 +0000748 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000749 while (optind < argc) {
750 struct ebt_u_match *m;
751 struct ebt_u_watcher *w;
752
Bart De Schuymer6622a012005-01-19 21:09:05 +0000753 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000754 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000755 exit(0);
756 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000757 if ((m = ebt_find_match(argv[optind])))
758 ebt_add_match(new_entry, m);
759 else if ((w = ebt_find_watcher(argv[optind])))
760 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000761 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000762 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000763 ebt_print_error2("Extension '%s' not found", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 if (replace->flags & OPT_JUMP)
765 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
766 replace->flags |= OPT_JUMP;
767 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000768 }
769 optind++;
770 }
771 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000772 case 't': /* Table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000773 if (OPT_COMMANDS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000774 ebt_print_error2("Please put the -t option first");
775 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000776 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000777 ebt_print_error2("Table name length cannot exceed %d characters", EBT_TABLE_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000778 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000779 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000780 case 'i': /* Input interface */
781 case 2 : /* Logical input interface */
782 case 'o': /* Output interface */
783 case 3 : /* Logical output interface */
784 case 'j': /* Target */
785 case 'p': /* Net family protocol */
786 case 's': /* Source mac */
787 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000788 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000789 if (!OPT_COMMANDS)
790 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000791 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000792 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000793 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000794 ebt_check_option2(&(replace->flags), OPT_IN);
795 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
796 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
797 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000798 new_entry->invflags |= EBT_IIN;
799
Bart De Schuymer6622a012005-01-19 21:09:05 +0000800 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000801big_iface_length:
802 ebt_print_error2("Interface name length cannot exceed %d characters", IFNAMSIZ - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000803 strcpy(new_entry->in, optarg);
804 if (parse_iface(new_entry->in, "-i"))
805 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000806 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000807 } else if (c == 2) {
808 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
809 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
810 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
811 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000812 new_entry->invflags |= EBT_ILOGICALIN;
813
Bart De Schuymer6622a012005-01-19 21:09:05 +0000814 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000815 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000816 strcpy(new_entry->logical_in, optarg);
817 if (parse_iface(new_entry->logical_in, "--logical-in"))
818 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000819 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 } else if (c == 'o') {
821 ebt_check_option2(&(replace->flags), OPT_OUT);
822 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
823 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
824 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000825 new_entry->invflags |= EBT_IOUT;
826
Bart De Schuymer6622a012005-01-19 21:09:05 +0000827 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000828 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000829 strcpy(new_entry->out, optarg);
830 if (parse_iface(new_entry->out, "-o"))
831 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000832 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000833 } else if (c == 3) {
834 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000835 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000836 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
837 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000838 new_entry->invflags |= EBT_ILOGICALOUT;
839
Bart De Schuymer6622a012005-01-19 21:09:05 +0000840 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000841 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000842 strcpy(new_entry->logical_out, optarg);
843 if (parse_iface(new_entry->logical_out, "--logical-out"))
844 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000845 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000846 } else if (c == 'j') {
847 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000848 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000849 if (!strcmp(optarg, ebt_standard_targets[i])) {
850 t = ebt_find_target(EBT_STANDARD_TARGET);
851 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000852 break;
853 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000854 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
855 ebt_print_error2("Return target only for user defined chains");
856 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000857 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000858
859 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000860 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000861 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000862 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000863 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000864 break;
865 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000866 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000867 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000868
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000869 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000870 /* -j standard not allowed either */
871 if (!t || t == (struct ebt_u_target *)new_entry->t)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000872 ebt_print_error2("Illegal target name '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000873 new_entry->t = (struct ebt_entry_target *)t;
874 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
875 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000876 }
877 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000878 } else if (c == 's') {
879 ebt_check_option2(&(replace->flags), OPT_SOURCE);
880 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000881 new_entry->invflags |= EBT_ISOURCE;
882
Bart De Schuymer6622a012005-01-19 21:09:05 +0000883 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000884 ebt_print_error2("Problem with specified source mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000885 new_entry->bitmask |= EBT_SOURCEMAC;
886 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000887 } else if (c == 'd') {
888 ebt_check_option2(&(replace->flags), OPT_DEST);
889 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000890 new_entry->invflags |= EBT_IDEST;
891
Bart De Schuymer6622a012005-01-19 21:09:05 +0000892 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000893 ebt_print_error2("Problem with specified destination mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000894 new_entry->bitmask |= EBT_DESTMAC;
895 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000896 } else if (c == 'c') {
897 ebt_check_option2(&(replace->flags), OPT_COUNT);
898 if (ebt_check_inverse2(optarg))
899 ebt_print_error2("Unexpected '!' after -c");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000900 if (optind >= argc || optarg[0] == '-' || argv[optind][0] == '-')
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000901 ebt_print_error2("Option -c needs 2 arguments");
902
903 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
904 if (*buffer != '\0')
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000905 ebt_print_error2("Packet counter '%s' invalid", optarg);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000906 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
907 if (*buffer != '\0')
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000908 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000909 optind++;
910 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000911 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000912 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
913 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000914 new_entry->invflags |= EBT_IPROTO;
915
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000916 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000917 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000918 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000919 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000920 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000921 struct ethertypeent *ent;
922
Bart De Schuymer6622a012005-01-19 21:09:05 +0000923 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000924 new_entry->bitmask |= EBT_802_3;
925 break;
926 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000927 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000928 if (!ent)
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000929 ebt_print_error2("Problem with the specified Ethernet protocol '%s', perhaps "_PATH_ETHERTYPES " is missing", optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000930 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000931 } else
932 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000933
Bart De Schuymer6622a012005-01-19 21:09:05 +0000934 if (new_entry->ethproto < 0x0600)
935 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
936 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000937 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000938#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000939 if (exec_style == EXEC_STYLE_DAEMON)
940 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000941#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000942 ebt_check_option2(&(replace->flags), LIST_C);
943 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000944 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000945 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000946 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000947 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000948#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000949 if (exec_style == EXEC_STYLE_DAEMON)
950 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000951#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000952 ebt_check_option2(&(replace->flags), LIST_N);
953 if (replace->command != 'L')
954 ebt_print_error2("Use --Ln with -L");
955 if (replace->flags & LIST_X)
956 ebt_print_error2("--Lx is not compatible with --Ln");
957 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000958 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000959 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +0000960#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000961 if (exec_style == EXEC_STYLE_DAEMON)
962 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000963#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000964 ebt_check_option2(&(replace->flags), LIST_X);
965 if (replace->command != 'L')
966 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000967 if (replace->flags & LIST_N)
968 ebt_print_error2("--Lx is not compatible with --Ln");
969 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000970 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000971 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +0000972#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000973 if (exec_style == EXEC_STYLE_DAEMON)
974 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000975#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000976 ebt_check_option2(&(replace->flags), LIST_MAC2);
977 if (replace->command != 'L')
978 ebt_print_error2("Use --Lmac2 with -L");
979 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000980 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000981 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000982 if (exec_style == EXEC_STYLE_DAEMON)
983 ebt_print_error2("--atomic-commit is not supported in daemon mode");
984 replace->command = c;
985 if (OPT_COMMANDS)
986 ebt_print_error2("Multiple commands are not allowed");
987 replace->flags |= OPT_COMMAND;
988 if (!replace->filename)
989 ebt_print_error2("No atomic file specified");
990 /* Get the information from the file */
991 ebt_get_table(replace, 0);
992 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000993 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +0000994 * the file */
995 replace->num_counters = 0;
996 /* Make sure the table will be written to the kernel */
997 free(replace->filename);
998 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000999 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001000 case 7 : /* atomic-init */
1001 case 10: /* atomic-save */
1002 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001003 if (exec_style == EXEC_STYLE_DAEMON) {
1004 if (c == 7) {
1005 ebt_print_error2("--atomic-init is not supported in daemon mode");
1006 } else if (c == 10)
1007 ebt_print_error2("--atomic-save is not supported in daemon mode");
1008 ebt_print_error2("--init-table is not supported in daemon mode");
1009 }
1010 replace->command = c;
1011 if (OPT_COMMANDS)
1012 ebt_print_error2("Multiple commands are not allowed");
1013 if (c != 11 && !replace->filename)
1014 ebt_print_error2("No atomic file specified");
1015 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001016 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001017 char *tmp = replace->filename;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001018
Bart De Schuymer6622a012005-01-19 21:09:05 +00001019 /* Get the kernel table */
1020 replace->filename = NULL;
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001021 ebt_get_kernel_table(replace, c == 10 ? 0 : 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001022 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001023 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001024 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001025 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001026 if (exec_style == EXEC_STYLE_DAEMON)
1027 ebt_print_error2("--atomic is not supported in daemon mode");
1028 if (OPT_COMMANDS)
1029 ebt_print_error2("--atomic has to come before the command");
1030 /* A possible memory leak here, but this is not
1031 * executed in daemon mode */
1032 replace->filename = (char *)malloc(strlen(optarg) + 1);
1033 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001034 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001035 case 1 :
1036 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001037 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001038 else
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001039 ebt_print_error2("Bad argument : '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001040 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001041 optind--;
1042 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001043 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001044 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001045 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001046 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1047 if (ebt_errormsg[0] != '\0')
1048 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001049 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001050 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001051
Bart De Schuymer6622a012005-01-19 21:09:05 +00001052 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001053 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001054 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001055 break;
1056
1057 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001058 if (ebt_errormsg[0] != '\0')
1059 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001060 if (m->used == 0) {
1061 ebt_add_match(new_entry, m);
1062 m->used = 1;
1063 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001064 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001065 }
1066
Bart De Schuymer6622a012005-01-19 21:09:05 +00001067 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001068 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001069 if (w->parse(c - w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001070 break;
1071
1072 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001073 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymerff587202005-02-08 20:02:28 +00001074 if (ebt_errormsg[0] != '\0')
1075 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001076 if (w->used == 0) {
1077 ebt_add_watcher(new_entry, w);
1078 w->used = 1;
1079 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001080check_extension:
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001081 if (replace->command != 'A' && replace->command != 'I' &&
1082 replace->command != 'D' && replace->command != 'C')
1083 ebt_print_error2("Extensions only for -A, -I, -D and -C");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001084 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001085 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001086 }
1087
Bart De Schuymer6622a012005-01-19 21:09:05 +00001088 /* Just in case we didn't catch an error */
1089 if (ebt_errormsg[0] != '\0')
1090 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001091
Bart De Schuymer6622a012005-01-19 21:09:05 +00001092 if (!(table = ebt_find_table(replace->name)))
1093 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001094
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001095 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001096 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001097 if (exec_style == EXEC_STYLE_PRG)
1098 exit(0);
1099 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001100
Bart De Schuymer6622a012005-01-19 21:09:05 +00001101 /* Do the final checks */
1102 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001103 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001104 /* This will put the hook_mask right for the chains */
1105 ebt_check_for_loops(replace);
1106 if (ebt_errormsg[0] != '\0')
1107 return -1;
1108 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001109 m_l = new_entry->m_list;
1110 w_l = new_entry->w_list;
1111 t = (struct ebt_u_target *)new_entry->t;
1112 while (m_l) {
1113 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001114 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001115 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001116 if (ebt_errormsg[0] != '\0')
1117 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001118 m_l = m_l->next;
1119 }
1120 while (w_l) {
1121 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001122 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001123 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001124 if (ebt_errormsg[0] != '\0')
1125 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001126 w_l = w_l->next;
1127 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001128 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001129 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001130 if (ebt_errormsg[0] != '\0')
1131 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001132 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001133 /* So, the extensions can work with the host endian.
1134 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001135 new_entry->ethproto = htons(new_entry->ethproto);
1136
Bart De Schuymer6622a012005-01-19 21:09:05 +00001137 if (replace->command == 'P') {
1138 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1139 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1140 ebt_change_policy(replace, policy);
1141 if (ebt_errormsg[0] != '\0')
1142 return -1;
1143 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001144 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001145 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001146 exit(0);
1147 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001148 if (replace->flags & OPT_ZERO) {
1149 replace->selected_chain = zerochain;
1150 ebt_zero_counters(replace);
1151 } else if (replace->command == 'F') {
1152 ebt_flush_chains(replace);
1153 } else if (replace->command == 'A' || replace->command == 'I') {
1154 ebt_add_rule(replace, new_entry, rule_nr);
1155 if (ebt_errormsg[0] != '\0')
1156 return -1;
1157 /* Makes undoing the add easier (jumps to delete_the_rule) */
1158 if (rule_nr <= 0)
1159 rule_nr--;
1160 rule_nr_end = rule_nr;
1161
1162 ebt_check_for_loops(replace);
1163 if (ebt_errormsg[0] != '\0')
1164 goto delete_the_rule;
1165
1166 /* Do the final_check(), for all entries.
1167 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001168 i = -1;
1169 while (1) {
1170 struct ebt_u_entry *e;
1171
1172 i++;
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001173 entries = replace->chains[i];
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001174 if (!entries) {
1175 if (i < NF_BR_NUMHOOKS)
1176 continue;
1177 else
1178 break;
1179 }
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001180 e = entries->entries->next;
1181 while (e != entries->entries) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001182 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001183 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001184 ebt_do_final_checks(replace, e, entries);
1185 if (ebt_errormsg[0] != '\0')
1186 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001187 e->ethproto = htons(e->ethproto);
1188 e = e->next;
1189 }
1190 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001191 /* Don't reuse the added rule */
1192 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001193 } else if (replace->command == 'D') {
1194delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001195 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001196 if (ebt_errormsg[0] != '\0')
1197 return -1;
1198 } else if (replace->command == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001199 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus), chcounter);
Bart De Schuymerff587202005-02-08 20:02:28 +00001200 if (ebt_errormsg[0] != '\0')
1201 return -1;
1202 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001203 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1204 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001205
Bart De Schuymer6622a012005-01-19 21:09:05 +00001206 if (ebt_errormsg[0] != '\0')
1207 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001208 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001209 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001210
Bart De Schuymer6622a012005-01-19 21:09:05 +00001211 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1212 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001213
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001214 if (replace->nentries)
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001215 ebt_deliver_counters(replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001216 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001217 return 0;
1218}