blob: 08a9e577cfc5fa6e82ff761b3a855ea7582a90b2 [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 Schuymerab611e22005-02-14 20:20:03 +0000185static void list_em(struct ebt_u_entries *entries, struct ebt_cntchanges *cc)
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 Schuymer60332e02002-06-23 08:01:47 +0000199 hlp = entries->entries;
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 Schuymer229079b2005-06-18 14:42:21 +0000323#ifdef EBT_DEBUG
Bart De Schuymerab611e22005-02-14 20:20:03 +0000324 while (cc->type == CNT_DEL)
325 cc = cc->next;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000326 if (cc->change != 0) /* In daemon mode, only change==0 is allowed */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000327 ebt_print_bug("cc->change != 0");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000328 cc = cc->next;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000329#endif
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000330 if (replace->flags & LIST_X)
Bart De Schuymerab611e22005-02-14 20:20:03 +0000331 printf("-c %llu %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000332 else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000333 printf(", pcnt = %llu -- bcnt = %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000334 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000335 printf("\n");
336 hlp = hlp->next;
337 }
338}
339
Bart De Schuymer62423742002-07-14 19:06:20 +0000340static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000341{
342 struct ebt_u_match_list *m_l;
343 struct ebt_u_watcher_list *w_l;
344
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000345 PRINT_VERSION;
346 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000347"Usage:\n"
348"ebtables -[ADI] chain rule-specification [options]\n"
349"ebtables -P chain target\n"
350"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000351"ebtables -[NX] [chain]\n"
352"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000353"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000354"--append -A chain : append to chain\n"
355"--delete -D chain : delete matching rule from chain\n"
356"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000357"--change-counters -C chain\n"
358" [rulenum] pcnt bcnt : change counters of existing rule\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000359"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000360"--list -L [chain] : list the rules in a chain or in all chains\n"
361"--flush -F [chain] : delete all rules in chain or in all chains\n"
362"--init-table : replace the kernel table with the initial table\n"
363"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
364"--policy -P chain target : change policy on chain to target\n"
365"--new-chain -N chain : create a user defined chain\n"
366"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000367"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000368"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
369"--atomic-init : put the initial kernel table into <FILE>\n"
370"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000371"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000372"Options:\n"
373"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
374"--src -s [!] address[/mask]: source mac address\n"
375"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000376"--in-if -i [!] name[+] : network input interface name\n"
377"--out-if -o [!] name[+] : network output interface name\n"
378"--logical-in [!] name[+] : logical bridge input interface name\n"
379"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000380"--set-counters -c chain\n"
381" pcnt bcnt : set the counters of the to be added rule\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000382"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000383"--version -V : print package version\n\n"
384"Environment variable:\n"
385ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
386"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000387 m_l = new_entry->m_list;
388 while (m_l) {
389 ((struct ebt_u_match *)m_l->m)->help();
390 printf("\n");
391 m_l = m_l->next;
392 }
393 w_l = new_entry->w_list;
394 while (w_l) {
395 ((struct ebt_u_watcher *)w_l->w)->help();
396 printf("\n");
397 w_l = w_l->next;
398 }
399 ((struct ebt_u_target *)new_entry->t)->help();
400 printf("\n");
401 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000402 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000403}
404
Bart De Schuymer6622a012005-01-19 21:09:05 +0000405/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000406static void list_rules()
407{
Bart De Schuymer229079b2005-06-18 14:42:21 +0000408 int i;
409#ifdef EBT_DEBUG
410 int j;
411#endif
Bart De Schuymerab611e22005-02-14 20:20:03 +0000412 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000413
Bart De Schuymer6622a012005-01-19 21:09:05 +0000414 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000415 printf("Bridge table: %s\n", table->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000416 if (replace->selected_chain != -1) {
Bart De Schuymer229079b2005-06-18 14:42:21 +0000417#ifdef EBT_DEBUG
418
Bart De Schuymerab611e22005-02-14 20:20:03 +0000419 for (i = 0; i < replace->selected_chain; i++) {
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000420 if (i < NF_BR_NUMHOOKS && !(replace->hook_entry[i]))
Bart De Schuymerab611e22005-02-14 20:20:03 +0000421 continue;
422 j = ebt_nr_to_chain(replace, i)->nentries;
423 while (j) {
424 if (cc->type != CNT_DEL)
425 j--;
426 cc = cc->next;
427 }
428 }
Bart De Schuymer229079b2005-06-18 14:42:21 +0000429#endif
Bart De Schuymerab611e22005-02-14 20:20:03 +0000430 list_em(ebt_to_chain(replace), cc);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000431 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000432 struct ebt_u_chain_list *cl = replace->udc;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000433
Bart De Schuymer6622a012005-01-19 21:09:05 +0000434 /* Create new chains and rename standard chains when necessary */
435 if (replace->flags & LIST_X) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000436 while (cl) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000437 printf("ebtables -t %s -N %s\n", replace->name,
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000438 cl->udc->name);
439 cl = cl->next;
440 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000441 cl = replace->udc;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000442 for (i = 0; i < NF_BR_NUMHOOKS; i++)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000443 if (replace->hook_entry[i] &&
444 strcmp(replace->hook_entry[i]->name, ebt_hooknames[i]))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000445 printf("ebtables -t %s -E %s %s\n",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000446 replace->name, ebt_hooknames[i],
447 replace->hook_entry[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000448 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000449 i = 0;
450 while (1) {
451 if (i < NF_BR_NUMHOOKS) {
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000452 if (replace->hook_entry[i]) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000453 list_em(replace->hook_entry[i], cc);
Bart De Schuymer229079b2005-06-18 14:42:21 +0000454#ifdef EBT_DEBUG
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000455 j = replace->hook_entry[i]->nentries;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000456#endif
Bart De Schuymerab611e22005-02-14 20:20:03 +0000457 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000458 } else {
459 if (!cl)
460 break;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000461 list_em(cl->udc, cc);
Bart De Schuymer229079b2005-06-18 14:42:21 +0000462#ifdef EBT_DEBUG
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000463 j = cl->udc->nentries;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000464#endif
Bart De Schuymer60332e02002-06-23 08:01:47 +0000465 cl = cl->next;
466 }
Bart De Schuymer229079b2005-06-18 14:42:21 +0000467#ifdef EBT_DEBUG
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000468 while (j) {
469 if (cc->type != CNT_DEL)
470 j--;
471 cc = cc->next;
472 }
Bart De Schuymer229079b2005-06-18 14:42:21 +0000473#endif
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000474 i++;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000475 }
476 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000477}
478
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000479static int parse_rule_range(const char *argv, int *rule_nr, int *rule_nr_end)
Bart De Schuymercc440052002-11-06 21:10:33 +0000480{
481 char *colon = strchr(argv, ':'), *buffer;
482
483 if (colon) {
484 *colon = '\0';
485 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000486 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000487 else {
488 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000489 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000490 return -1;
491 }
492 }
493 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000494 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000495 else {
496 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000497 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000498 return -1;
499 }
500 if (!colon)
501 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000502 return 0;
503}
504
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000505/* Incrementing or decrementing rules in daemon mode is not supported as the
Bart De Schuymer229079b2005-06-18 14:42:21 +0000506 * involved code overload is not worth it (too annoying to take the increased
507 * counters in the kernel into account). */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000508static 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 +0000509{
510 char *buffer;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000511 int ret = 0;
Bart De Schuymerff587202005-02-08 20:02:28 +0000512
513 if (optind + 1 >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')) ||
514 (argv[optind + 1][0] == '-' && (argv[optind + 1][1] < '0' && argv[optind + 1][1] > '9')))
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000515 ebt_print_error2("The command -C needs at least 2 arguments");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000516 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 +0000517 if (optind + 3 != argc)
518 ebt_print_error2("No extra options allowed with -C start_nr[:end_nr] pcnt bcnt");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000519 if (parse_rule_range(argv[optind], rule_nr, rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000520 ebt_print_error2("Something is wrong with the rule number specification '%s'", argv[optind]);
521 optind++;
522 }
523
Bart De Schuymerab611e22005-02-14 20:20:03 +0000524 if (argv[optind][0] == '+') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000525 if (exec_style == EXEC_STYLE_DAEMON)
526daemon_incr:
527 ebt_print_error2("Incrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
528 ret += 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000529 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymerab611e22005-02-14 20:20:03 +0000530 } else if (argv[optind][0] == '-') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000531 if (exec_style == EXEC_STYLE_DAEMON)
532daemon_decr:
533 ebt_print_error2("Decrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
534 ret += 2;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000535 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000536 } else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000537 new_entry->cnt_surplus.pcnt = strtoull(argv[optind], &buffer, 10);
Bart De Schuymerff587202005-02-08 20:02:28 +0000538
Bart De Schuymerab611e22005-02-14 20:20:03 +0000539 if (*buffer != '\0')
540 goto invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000541 optind++;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000542 if (argv[optind][0] == '+') {
543 if (exec_style == EXEC_STYLE_DAEMON)
544 goto daemon_incr;
545 ret += 3;
546 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
547 } else if (argv[optind][0] == '-') {
548 if (exec_style == EXEC_STYLE_DAEMON)
549 goto daemon_decr;
550 ret += 6;
551 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
552 } else
553 new_entry->cnt_surplus.bcnt = strtoull(argv[optind], &buffer, 10);
554
555 if (*buffer != '\0')
556 goto invalid;
557 optind++;
558 return ret;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000559invalid:
560 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerff587202005-02-08 20:02:28 +0000561}
562
Bart De Schuymer6622a012005-01-19 21:09:05 +0000563static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000564{
565 char *c;
566
567 if ((c = strchr(iface, '+'))) {
568 if (*(c + 1) != '\0') {
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000569 ebt_print_error("Spurious characters after '+' wildcard for '%s'", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000570 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000571 } else
572 *c = IF_WILDCARD;
573 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000574 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000575}
576
Bart De Schuymer6622a012005-01-19 21:09:05 +0000577void ebt_early_init_once()
578{
579 ebt_iterate_matches(merge_match);
580 ebt_iterate_watchers(merge_watcher);
581 ebt_iterate_targets(merge_target);
582}
583
Bart De Schuymer229079b2005-06-18 14:42:21 +0000584/* We use exec_style instead of #ifdef's because ebtables.so is a shared object. */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000585int do_command(int argc, char *argv[], int exec_style,
586 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000587{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000588 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000589 int c, i;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000590 int zerochain = -1; /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000591 int chcounter; /* Needed for -C */
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000592 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000593 int rule_nr = 0;
594 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000595 struct ebt_u_target *t;
596 struct ebt_u_match *m;
597 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000598 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000599 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000600 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000601
Bart De Schuymera615b962002-11-03 14:54:09 +0000602 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000603 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000604
Bart De Schuymer6622a012005-01-19 21:09:05 +0000605 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000606
Bart De Schuymer6622a012005-01-19 21:09:05 +0000607 /* The daemon doesn't use the environment variable */
608 if (exec_style == EXEC_STYLE_PRG) {
609 buffer = getenv(ATOMIC_ENV_VARIABLE);
610 if (buffer) {
611 replace->filename = malloc(strlen(buffer) + 1);
612 if (!replace->filename)
613 ebt_print_memory();
614 strcpy(replace->filename, buffer);
615 buffer = NULL;
616 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000617 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000618
Bart De Schuymer6622a012005-01-19 21:09:05 +0000619 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
620 replace->selected_chain = -1;
621 replace->command = 'h';
622
623 if (!new_entry) {
624 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
625 if (!new_entry)
626 ebt_print_memory();
627 }
628 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000629 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000630 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000631
Bart De Schuymer6622a012005-01-19 21:09:05 +0000632 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000633 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000634 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000635
Bart De Schuymer6622a012005-01-19 21:09:05 +0000636 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000637 while ((c = getopt_long(argc, argv,
Bart De Schuymerff587202005-02-08 20:02:28 +0000638 "-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 +0000639 switch (c) {
640
Bart De Schuymer6622a012005-01-19 21:09:05 +0000641 case 'A': /* Add a rule */
642 case 'D': /* Delete a rule */
Bart De Schuymerff587202005-02-08 20:02:28 +0000643 case 'C': /* Change counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 case 'P': /* Define policy */
645 case 'I': /* Insert a rule */
646 case 'N': /* Make a user defined chain */
647 case 'E': /* Rename chain */
648 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000649 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000650 if (replace->command == 'N' && c == 'P') {
651 replace->command = c;
652 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000653 goto handle_P;
654 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000655 if (OPT_COMMANDS)
656 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000657
Bart De Schuymer6622a012005-01-19 21:09:05 +0000658 replace->command = c;
659 replace->flags |= OPT_COMMAND;
660 if (!(replace->flags & OPT_KERNELDATA))
661 ebt_get_kernel_table(replace, 0);
662 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
663 ebt_print_error2("No chain name specified");
664 if (c == 'N') {
665 ebt_new_chain(replace, optarg, EBT_ACCEPT);
666 /* This is needed to get -N x -P y working */
667 replace->selected_chain = ebt_get_chainnr(replace, optarg);
668 break;
669 } else if (c == 'X') {
670 if (optind >= argc) {
671 replace->selected_chain = -1;
672 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000673 break;
674 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000675
676 if (optind < argc - 1)
677 ebt_print_error2("No extra options allowed with -X");
678
679 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000680 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000681 ebt_delete_chain(replace);
682 if (ebt_errormsg[0] != '\0')
683 return -1;
684 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000685 break;
686 }
687
Bart De Schuymer6622a012005-01-19 21:09:05 +0000688 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000689 ebt_print_error2("Chain '%s' doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000690 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000691 if (optind >= argc)
692 ebt_print_error2("No new chain name specified");
693 if (optind < argc - 1)
694 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000695 if (strlen(argv[optind]) >= EBT_CHAIN_MAXNAMELEN)
696 ebt_print_error2("Chain name length can't exceed %d characters", EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697 if (ebt_get_chainnr(replace, argv[optind]) != -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000698 ebt_print_error2("Chain '%s' already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000699 if (ebt_find_target(argv[optind]))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000700 ebt_print_error2("Target with name '%s' exists", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000701 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000702 optind++;
703 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000704 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
705 if (optind != argc - 1)
706 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000707 if (parse_rule_range(argv[optind], &rule_nr, &rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000708 ebt_print_error2("Problem with the specified rule number(s) '%s'", argv[optind]);
Bart De Schuymercc440052002-11-06 21:10:33 +0000709 optind++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000710 } else if (c == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000711 if ((chcounter = parse_change_counters_rule(argc, argv, &rule_nr, &rule_nr_end, exec_style)) == -1)
Bart De Schuymerff587202005-02-08 20:02:28 +0000712 return -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000713 } else if (c == 'I') {
714 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
Bart De Schuymer01581232005-07-24 09:46:09 +0000715 rule_nr = 1;
716 else {
717 rule_nr = strtol(argv[optind], &buffer, 10);
718 if (*buffer != '\0')
719 ebt_print_error2("Problem with the specified rule number '%s'", argv[optind]);
720 optind++;
721 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000722 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000723handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000724 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000725 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000726 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000727 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000728 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000729 if (policy == EBT_CONTINUE)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000730 ebt_print_error2("Wrong policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000731 break;
732 }
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000733 if (i == NUM_STANDARD_TARGETS)
734 ebt_print_error2("Unknown policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000735 optind++;
736 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000737 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000738 case 'L': /* List */
739 case 'F': /* Flush */
740 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000741 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000742 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
743print_zero:
744 ebt_print_error2("Command -Z only allowed together with command -L");
745 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000746 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000747 if (replace->flags & OPT_COMMAND)
748 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000749 replace->command = c;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000750 replace->flags |= OPT_COMMAND;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000751 if (replace->flags & OPT_ZERO && c != 'L')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000752 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000753 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000754
755#ifdef SILENT_DAEMON
756 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000757 ebt_print_error2("-L not supported in daemon mode");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000758#endif
759
760 if (!(replace->flags & OPT_KERNELDATA))
761 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000762 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000763 if (optind < argc && argv[optind][0] != '-') {
764 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000765 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000766 optind++;
767 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000768 if (i != -1) {
769 if (c == 'Z')
770 zerochain = i;
771 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000772 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000773 }
774 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000775 case 'V': /* Version */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000776 if (OPT_COMMANDS)
777 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000778 replace->command = 'V';
Bart De Schuymer6622a012005-01-19 21:09:05 +0000779 if (exec_style == EXEC_STYLE_DAEMON)
780 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000781 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000782 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000783 case 'M': /* Modprobe */
784 if (OPT_COMMANDS)
785 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000786 free(ebt_modprobe);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000787 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000788 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000789 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000790#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000791 if (exec_style == EXEC_STYLE_DAEMON)
792 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000793#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000794 if (OPT_COMMANDS)
795 ebt_print_error2("Multiple commands are not allowed");
796 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000797
Bart De Schuymer6622a012005-01-19 21:09:05 +0000798 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000799 while (optind < argc) {
800 struct ebt_u_match *m;
801 struct ebt_u_watcher *w;
802
Bart De Schuymer6622a012005-01-19 21:09:05 +0000803 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000804 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000805 exit(0);
806 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000807 if ((m = ebt_find_match(argv[optind])))
808 ebt_add_match(new_entry, m);
809 else if ((w = ebt_find_watcher(argv[optind])))
810 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000811 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000812 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000813 ebt_print_error2("Extension '%s' not found", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000814 if (replace->flags & OPT_JUMP)
815 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
816 replace->flags |= OPT_JUMP;
817 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000818 }
819 optind++;
820 }
821 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000822 case 't': /* Table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000823 if (OPT_COMMANDS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000824 ebt_print_error2("Please put the -t option first");
825 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000826 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000827 ebt_print_error2("Table name length cannot exceed %d characters", EBT_TABLE_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000828 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000829 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000830 case 'i': /* Input interface */
831 case 2 : /* Logical input interface */
832 case 'o': /* Output interface */
833 case 3 : /* Logical output interface */
834 case 'j': /* Target */
835 case 'p': /* Net family protocol */
836 case 's': /* Source mac */
837 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000838 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000839 if (!OPT_COMMANDS)
840 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000841 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000842 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000843 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000844 ebt_check_option2(&(replace->flags), OPT_IN);
845 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
846 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
847 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000848 new_entry->invflags |= EBT_IIN;
849
Bart De Schuymer6622a012005-01-19 21:09:05 +0000850 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000851big_iface_length:
852 ebt_print_error2("Interface name length cannot exceed %d characters", IFNAMSIZ - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000853 strcpy(new_entry->in, optarg);
854 if (parse_iface(new_entry->in, "-i"))
855 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000856 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000857 } else if (c == 2) {
858 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
859 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
860 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
861 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000862 new_entry->invflags |= EBT_ILOGICALIN;
863
Bart De Schuymer6622a012005-01-19 21:09:05 +0000864 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000865 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000866 strcpy(new_entry->logical_in, optarg);
867 if (parse_iface(new_entry->logical_in, "--logical-in"))
868 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000869 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000870 } else if (c == 'o') {
871 ebt_check_option2(&(replace->flags), OPT_OUT);
872 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
873 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
874 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000875 new_entry->invflags |= EBT_IOUT;
876
Bart De Schuymer6622a012005-01-19 21:09:05 +0000877 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000878 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000879 strcpy(new_entry->out, optarg);
880 if (parse_iface(new_entry->out, "-o"))
881 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000882 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000883 } else if (c == 3) {
884 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000885 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000886 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
887 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000888 new_entry->invflags |= EBT_ILOGICALOUT;
889
Bart De Schuymer6622a012005-01-19 21:09:05 +0000890 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000891 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000892 strcpy(new_entry->logical_out, optarg);
893 if (parse_iface(new_entry->logical_out, "--logical-out"))
894 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000895 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000896 } else if (c == 'j') {
897 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000898 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000899 if (!strcmp(optarg, ebt_standard_targets[i])) {
900 t = ebt_find_target(EBT_STANDARD_TARGET);
901 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000902 break;
903 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000904 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
905 ebt_print_error2("Return target only for user defined chains");
906 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000907 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000908
909 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000910 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000911 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000912 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000913 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000914 break;
915 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000916 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000917 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000918
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000919 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000920 /* -j standard not allowed either */
921 if (!t || t == (struct ebt_u_target *)new_entry->t)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000922 ebt_print_error2("Illegal target name '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000923 new_entry->t = (struct ebt_entry_target *)t;
924 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
925 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000926 }
927 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000928 } else if (c == 's') {
929 ebt_check_option2(&(replace->flags), OPT_SOURCE);
930 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000931 new_entry->invflags |= EBT_ISOURCE;
932
Bart De Schuymer6622a012005-01-19 21:09:05 +0000933 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000934 ebt_print_error2("Problem with specified source mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000935 new_entry->bitmask |= EBT_SOURCEMAC;
936 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000937 } else if (c == 'd') {
938 ebt_check_option2(&(replace->flags), OPT_DEST);
939 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000940 new_entry->invflags |= EBT_IDEST;
941
Bart De Schuymer6622a012005-01-19 21:09:05 +0000942 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000943 ebt_print_error2("Problem with specified destination mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000944 new_entry->bitmask |= EBT_DESTMAC;
945 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000946 } else if (c == 'c') {
947 ebt_check_option2(&(replace->flags), OPT_COUNT);
948 if (ebt_check_inverse2(optarg))
949 ebt_print_error2("Unexpected '!' after -c");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000950 if (optind >= argc || optarg[0] == '-' || argv[optind][0] == '-')
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000951 ebt_print_error2("Option -c needs 2 arguments");
952
953 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
954 if (*buffer != '\0')
955 ebt_print_error2("Packet counter '%s' invalid", optarg)
956 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
957 if (*buffer != '\0')
958 ebt_print_error2("Packet counter '%s' invalid", argv[optind])
959 optind++;
960 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000961 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000962 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
963 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000964 new_entry->invflags |= EBT_IPROTO;
965
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000966 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000967 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000968 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000969 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000970 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000971 struct ethertypeent *ent;
972
Bart De Schuymer6622a012005-01-19 21:09:05 +0000973 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000974 new_entry->bitmask |= EBT_802_3;
975 break;
976 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000977 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000978 if (!ent)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000979 ebt_print_error2("Problem with the specified Ethernet protocol '%s', perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000980 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000981 } else
982 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000983
Bart De Schuymer6622a012005-01-19 21:09:05 +0000984 if (new_entry->ethproto < 0x0600)
985 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
986 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000987 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000988#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000989 if (exec_style == EXEC_STYLE_DAEMON)
990 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000991#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000992 ebt_check_option2(&(replace->flags), LIST_C);
993 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000994 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000995 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000996 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000997 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000998#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000999 if (exec_style == EXEC_STYLE_DAEMON)
1000 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001001#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001002 ebt_check_option2(&(replace->flags), LIST_N);
1003 if (replace->command != 'L')
1004 ebt_print_error2("Use --Ln with -L");
1005 if (replace->flags & LIST_X)
1006 ebt_print_error2("--Lx is not compatible with --Ln");
1007 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001008 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00001009 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +00001010#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +00001011 if (exec_style == EXEC_STYLE_DAEMON)
1012 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001013#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001014 ebt_check_option2(&(replace->flags), LIST_X);
1015 if (replace->command != 'L')
1016 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +00001017 if (replace->flags & LIST_N)
1018 ebt_print_error2("--Lx is not compatible with --Ln");
1019 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001020 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001021 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +00001022#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +00001023 if (exec_style == EXEC_STYLE_DAEMON)
1024 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001025#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001026 ebt_check_option2(&(replace->flags), LIST_MAC2);
1027 if (replace->command != 'L')
1028 ebt_print_error2("Use --Lmac2 with -L");
1029 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001030 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00001031 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001032 if (exec_style == EXEC_STYLE_DAEMON)
1033 ebt_print_error2("--atomic-commit is not supported in daemon mode");
1034 replace->command = c;
1035 if (OPT_COMMANDS)
1036 ebt_print_error2("Multiple commands are not allowed");
1037 replace->flags |= OPT_COMMAND;
1038 if (!replace->filename)
1039 ebt_print_error2("No atomic file specified");
1040 /* Get the information from the file */
1041 ebt_get_table(replace, 0);
1042 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +00001043 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +00001044 * the file */
1045 replace->num_counters = 0;
1046 /* Make sure the table will be written to the kernel */
1047 free(replace->filename);
1048 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +00001049 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001050 case 7 : /* atomic-init */
1051 case 10: /* atomic-save */
1052 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001053 if (exec_style == EXEC_STYLE_DAEMON) {
1054 if (c == 7) {
1055 ebt_print_error2("--atomic-init is not supported in daemon mode");
1056 } else if (c == 10)
1057 ebt_print_error2("--atomic-save is not supported in daemon mode");
1058 ebt_print_error2("--init-table is not supported in daemon mode");
1059 }
1060 replace->command = c;
1061 if (OPT_COMMANDS)
1062 ebt_print_error2("Multiple commands are not allowed");
1063 if (c != 11 && !replace->filename)
1064 ebt_print_error2("No atomic file specified");
1065 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001066 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001067 char *tmp = replace->filename;
Bart De Schuymer64182a32004-01-21 20:39:54 +00001068 int init = 1;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001069
Bart De Schuymer64182a32004-01-21 20:39:54 +00001070 if (c == 10)
1071 init = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001072 /* Get the kernel table */
1073 replace->filename = NULL;
1074 ebt_get_kernel_table(replace, init);
1075 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001076 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001077 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001078 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001079 if (exec_style == EXEC_STYLE_DAEMON)
1080 ebt_print_error2("--atomic is not supported in daemon mode");
1081 if (OPT_COMMANDS)
1082 ebt_print_error2("--atomic has to come before the command");
1083 /* A possible memory leak here, but this is not
1084 * executed in daemon mode */
1085 replace->filename = (char *)malloc(strlen(optarg) + 1);
1086 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001087 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001088 case 1 :
1089 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001090 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001091 else
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001092 ebt_print_error2("Bad argument : '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001093 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001094 optind--;
1095 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001096 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001097 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001098 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001099 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1100 if (ebt_errormsg[0] != '\0')
1101 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001102 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001103 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001104
Bart De Schuymer6622a012005-01-19 21:09:05 +00001105 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001106 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001107 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001108 break;
1109
1110 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001111 if (ebt_errormsg[0] != '\0')
1112 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001113 if (m->used == 0) {
1114 ebt_add_match(new_entry, m);
1115 m->used = 1;
1116 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001117 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001118 }
1119
Bart De Schuymer6622a012005-01-19 21:09:05 +00001120 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001121 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001122 if (w->parse(c - w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001123 break;
1124
1125 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001126 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymerff587202005-02-08 20:02:28 +00001127 if (ebt_errormsg[0] != '\0')
1128 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001129 if (w->used == 0) {
1130 ebt_add_watcher(new_entry, w);
1131 w->used = 1;
1132 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001133check_extension:
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001134 if (replace->command != 'A' && replace->command != 'I' &&
1135 replace->command != 'D' && replace->command != 'C')
1136 ebt_print_error2("Extensions only for -A, -I, -D and -C");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001137 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001138 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001139 }
1140
Bart De Schuymer6622a012005-01-19 21:09:05 +00001141 /* Just in case we didn't catch an error */
1142 if (ebt_errormsg[0] != '\0')
1143 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001144
Bart De Schuymer6622a012005-01-19 21:09:05 +00001145 if (!(table = ebt_find_table(replace->name)))
1146 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001147
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001148 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001149 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001150 if (exec_style == EXEC_STYLE_PRG)
1151 exit(0);
1152 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001153
Bart De Schuymer6622a012005-01-19 21:09:05 +00001154 /* Do the final checks */
1155 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001156 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001157 /* This will put the hook_mask right for the chains */
1158 ebt_check_for_loops(replace);
1159 if (ebt_errormsg[0] != '\0')
1160 return -1;
1161 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001162 m_l = new_entry->m_list;
1163 w_l = new_entry->w_list;
1164 t = (struct ebt_u_target *)new_entry->t;
1165 while (m_l) {
1166 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001167 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001168 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001169 if (ebt_errormsg[0] != '\0')
1170 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001171 m_l = m_l->next;
1172 }
1173 while (w_l) {
1174 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001175 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001176 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001177 if (ebt_errormsg[0] != '\0')
1178 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001179 w_l = w_l->next;
1180 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001181 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001182 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001183 if (ebt_errormsg[0] != '\0')
1184 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001185 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001186 /* So, the extensions can work with the host endian.
1187 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001188 new_entry->ethproto = htons(new_entry->ethproto);
1189
Bart De Schuymer6622a012005-01-19 21:09:05 +00001190 if (replace->command == 'P') {
1191 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1192 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1193 ebt_change_policy(replace, policy);
1194 if (ebt_errormsg[0] != '\0')
1195 return -1;
1196 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001197 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001198 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001199 exit(0);
1200 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001201 if (replace->flags & OPT_ZERO) {
1202 replace->selected_chain = zerochain;
1203 ebt_zero_counters(replace);
1204 } else if (replace->command == 'F') {
1205 ebt_flush_chains(replace);
1206 } else if (replace->command == 'A' || replace->command == 'I') {
1207 ebt_add_rule(replace, new_entry, rule_nr);
1208 if (ebt_errormsg[0] != '\0')
1209 return -1;
1210 /* Makes undoing the add easier (jumps to delete_the_rule) */
1211 if (rule_nr <= 0)
1212 rule_nr--;
1213 rule_nr_end = rule_nr;
1214
1215 ebt_check_for_loops(replace);
1216 if (ebt_errormsg[0] != '\0')
1217 goto delete_the_rule;
1218
1219 /* Do the final_check(), for all entries.
1220 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001221 i = -1;
1222 while (1) {
1223 struct ebt_u_entry *e;
1224
1225 i++;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001226 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001227 if (!entries) {
1228 if (i < NF_BR_NUMHOOKS)
1229 continue;
1230 else
1231 break;
1232 }
1233 e = entries->entries;
1234 while (e) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001235 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001236 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001237 ebt_do_final_checks(replace, e, entries);
1238 if (ebt_errormsg[0] != '\0')
1239 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001240 e->ethproto = htons(e->ethproto);
1241 e = e->next;
1242 }
1243 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001244 /* Don't reuse the added rule */
1245 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001246 } else if (replace->command == 'D') {
1247delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001248 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001249 if (ebt_errormsg[0] != '\0')
1250 return -1;
1251 } else if (replace->command == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001252 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus), chcounter);
Bart De Schuymerff587202005-02-08 20:02:28 +00001253 if (ebt_errormsg[0] != '\0')
1254 return -1;
1255 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001256 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1257 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001258
Bart De Schuymer6622a012005-01-19 21:09:05 +00001259 if (ebt_errormsg[0] != '\0')
1260 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001261 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001262 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001263
Bart De Schuymer6622a012005-01-19 21:09:05 +00001264 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1265 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001266
Bart De Schuymer6622a012005-01-19 21:09:05 +00001267 if (replace->counterchanges)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001268 ebt_deliver_counters(replace, EXEC_STYLE_PRG);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001269 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001270 return 0;
1271}