blob: 2b0807fbcf1dbafeb07689ee4a43099c3213a11d [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')))
715 ebt_print_error2("No rulenr for -I specified");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000716 rule_nr = strtol(argv[optind], &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000717 if (*buffer != '\0')
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000718 ebt_print_error2("Problem with the specified rule number '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000719 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000720 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000721handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000722 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000723 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000724 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000725 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000726 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000727 if (policy == EBT_CONTINUE)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000728 ebt_print_error2("Wrong policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000729 break;
730 }
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000731 if (i == NUM_STANDARD_TARGETS)
732 ebt_print_error2("Unknown policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000733 optind++;
734 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000735 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000736 case 'L': /* List */
737 case 'F': /* Flush */
738 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000739 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000740 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
741print_zero:
742 ebt_print_error2("Command -Z only allowed together with command -L");
743 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000744 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000745 if (replace->flags & OPT_COMMAND)
746 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000747 replace->command = c;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000748 replace->flags |= OPT_COMMAND;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000749 if (replace->flags & OPT_ZERO && c != 'L')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000750 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000751 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000752
753#ifdef SILENT_DAEMON
754 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000755 ebt_print_error2("-L not supported in daemon mode");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000756#endif
757
758 if (!(replace->flags & OPT_KERNELDATA))
759 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000760 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000761 if (optind < argc && argv[optind][0] != '-') {
762 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000763 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 optind++;
765 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000766 if (i != -1) {
767 if (c == 'Z')
768 zerochain = i;
769 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000770 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000771 }
772 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000773 case 'V': /* Version */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000774 if (OPT_COMMANDS)
775 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000776 replace->command = 'V';
Bart De Schuymer6622a012005-01-19 21:09:05 +0000777 if (exec_style == EXEC_STYLE_DAEMON)
778 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000779 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000780 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000781 case 'M': /* Modprobe */
782 if (OPT_COMMANDS)
783 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000784 free(ebt_modprobe);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000785 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000786 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000787 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000788#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000789 if (exec_style == EXEC_STYLE_DAEMON)
790 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000791#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000792 if (OPT_COMMANDS)
793 ebt_print_error2("Multiple commands are not allowed");
794 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000795
Bart De Schuymer6622a012005-01-19 21:09:05 +0000796 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000797 while (optind < argc) {
798 struct ebt_u_match *m;
799 struct ebt_u_watcher *w;
800
Bart De Schuymer6622a012005-01-19 21:09:05 +0000801 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000802 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000803 exit(0);
804 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000805 if ((m = ebt_find_match(argv[optind])))
806 ebt_add_match(new_entry, m);
807 else if ((w = ebt_find_watcher(argv[optind])))
808 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000809 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000810 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000811 ebt_print_error2("Extension '%s' not found", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000812 if (replace->flags & OPT_JUMP)
813 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
814 replace->flags |= OPT_JUMP;
815 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000816 }
817 optind++;
818 }
819 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 case 't': /* Table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000821 if (OPT_COMMANDS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000822 ebt_print_error2("Please put the -t option first");
823 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000824 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000825 ebt_print_error2("Table name length cannot exceed %d characters", EBT_TABLE_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000826 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000827 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000828 case 'i': /* Input interface */
829 case 2 : /* Logical input interface */
830 case 'o': /* Output interface */
831 case 3 : /* Logical output interface */
832 case 'j': /* Target */
833 case 'p': /* Net family protocol */
834 case 's': /* Source mac */
835 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000836 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000837 if (!OPT_COMMANDS)
838 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000839 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000840 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000841 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000842 ebt_check_option2(&(replace->flags), OPT_IN);
843 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
844 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
845 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000846 new_entry->invflags |= EBT_IIN;
847
Bart De Schuymer6622a012005-01-19 21:09:05 +0000848 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000849big_iface_length:
850 ebt_print_error2("Interface name length cannot exceed %d characters", IFNAMSIZ - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000851 strcpy(new_entry->in, optarg);
852 if (parse_iface(new_entry->in, "-i"))
853 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000854 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000855 } else if (c == 2) {
856 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
857 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
858 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
859 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000860 new_entry->invflags |= EBT_ILOGICALIN;
861
Bart De Schuymer6622a012005-01-19 21:09:05 +0000862 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000863 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000864 strcpy(new_entry->logical_in, optarg);
865 if (parse_iface(new_entry->logical_in, "--logical-in"))
866 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000867 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000868 } else if (c == 'o') {
869 ebt_check_option2(&(replace->flags), OPT_OUT);
870 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
871 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
872 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000873 new_entry->invflags |= EBT_IOUT;
874
Bart De Schuymer6622a012005-01-19 21:09:05 +0000875 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000876 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000877 strcpy(new_entry->out, optarg);
878 if (parse_iface(new_entry->out, "-o"))
879 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000880 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000881 } else if (c == 3) {
882 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000883 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000884 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
885 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000886 new_entry->invflags |= EBT_ILOGICALOUT;
887
Bart De Schuymer6622a012005-01-19 21:09:05 +0000888 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000889 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000890 strcpy(new_entry->logical_out, optarg);
891 if (parse_iface(new_entry->logical_out, "--logical-out"))
892 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000893 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000894 } else if (c == 'j') {
895 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000896 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000897 if (!strcmp(optarg, ebt_standard_targets[i])) {
898 t = ebt_find_target(EBT_STANDARD_TARGET);
899 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000900 break;
901 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000902 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
903 ebt_print_error2("Return target only for user defined chains");
904 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000905 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000906
907 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000908 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000909 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000910 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000911 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000912 break;
913 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000914 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000915 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000916
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000917 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000918 /* -j standard not allowed either */
919 if (!t || t == (struct ebt_u_target *)new_entry->t)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000920 ebt_print_error2("Illegal target name '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000921 new_entry->t = (struct ebt_entry_target *)t;
922 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
923 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000924 }
925 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000926 } else if (c == 's') {
927 ebt_check_option2(&(replace->flags), OPT_SOURCE);
928 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000929 new_entry->invflags |= EBT_ISOURCE;
930
Bart De Schuymer6622a012005-01-19 21:09:05 +0000931 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000932 ebt_print_error2("Problem with specified source mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000933 new_entry->bitmask |= EBT_SOURCEMAC;
934 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000935 } else if (c == 'd') {
936 ebt_check_option2(&(replace->flags), OPT_DEST);
937 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000938 new_entry->invflags |= EBT_IDEST;
939
Bart De Schuymer6622a012005-01-19 21:09:05 +0000940 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000941 ebt_print_error2("Problem with specified destination mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000942 new_entry->bitmask |= EBT_DESTMAC;
943 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000944 } else if (c == 'c') {
945 ebt_check_option2(&(replace->flags), OPT_COUNT);
946 if (ebt_check_inverse2(optarg))
947 ebt_print_error2("Unexpected '!' after -c");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000948 if (optind >= argc || optarg[0] == '-' || argv[optind][0] == '-')
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000949 ebt_print_error2("Option -c needs 2 arguments");
950
951 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
952 if (*buffer != '\0')
953 ebt_print_error2("Packet counter '%s' invalid", optarg)
954 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
955 if (*buffer != '\0')
956 ebt_print_error2("Packet counter '%s' invalid", argv[optind])
957 optind++;
958 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000959 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000960 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
961 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000962 new_entry->invflags |= EBT_IPROTO;
963
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000964 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000965 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000966 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000967 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000968 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000969 struct ethertypeent *ent;
970
Bart De Schuymer6622a012005-01-19 21:09:05 +0000971 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000972 new_entry->bitmask |= EBT_802_3;
973 break;
974 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000975 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000976 if (!ent)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000977 ebt_print_error2("Problem with the specified Ethernet protocol '%s', perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000978 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000979 } else
980 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000981
Bart De Schuymer6622a012005-01-19 21:09:05 +0000982 if (new_entry->ethproto < 0x0600)
983 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
984 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000985 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000986#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000987 if (exec_style == EXEC_STYLE_DAEMON)
988 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000989#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000990 ebt_check_option2(&(replace->flags), LIST_C);
991 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000992 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000993 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000994 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000995 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000996#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000997 if (exec_style == EXEC_STYLE_DAEMON)
998 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000999#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001000 ebt_check_option2(&(replace->flags), LIST_N);
1001 if (replace->command != 'L')
1002 ebt_print_error2("Use --Ln with -L");
1003 if (replace->flags & LIST_X)
1004 ebt_print_error2("--Lx is not compatible with --Ln");
1005 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001006 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00001007 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +00001008#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +00001009 if (exec_style == EXEC_STYLE_DAEMON)
1010 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001011#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001012 ebt_check_option2(&(replace->flags), LIST_X);
1013 if (replace->command != 'L')
1014 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +00001015 if (replace->flags & LIST_N)
1016 ebt_print_error2("--Lx is not compatible with --Ln");
1017 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001018 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001019 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +00001020#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +00001021 if (exec_style == EXEC_STYLE_DAEMON)
1022 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001023#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001024 ebt_check_option2(&(replace->flags), LIST_MAC2);
1025 if (replace->command != 'L')
1026 ebt_print_error2("Use --Lmac2 with -L");
1027 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001028 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00001029 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001030 if (exec_style == EXEC_STYLE_DAEMON)
1031 ebt_print_error2("--atomic-commit is not supported in daemon mode");
1032 replace->command = c;
1033 if (OPT_COMMANDS)
1034 ebt_print_error2("Multiple commands are not allowed");
1035 replace->flags |= OPT_COMMAND;
1036 if (!replace->filename)
1037 ebt_print_error2("No atomic file specified");
1038 /* Get the information from the file */
1039 ebt_get_table(replace, 0);
1040 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +00001041 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +00001042 * the file */
1043 replace->num_counters = 0;
1044 /* Make sure the table will be written to the kernel */
1045 free(replace->filename);
1046 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +00001047 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001048 case 7 : /* atomic-init */
1049 case 10: /* atomic-save */
1050 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001051 if (exec_style == EXEC_STYLE_DAEMON) {
1052 if (c == 7) {
1053 ebt_print_error2("--atomic-init is not supported in daemon mode");
1054 } else if (c == 10)
1055 ebt_print_error2("--atomic-save is not supported in daemon mode");
1056 ebt_print_error2("--init-table is not supported in daemon mode");
1057 }
1058 replace->command = c;
1059 if (OPT_COMMANDS)
1060 ebt_print_error2("Multiple commands are not allowed");
1061 if (c != 11 && !replace->filename)
1062 ebt_print_error2("No atomic file specified");
1063 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001064 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001065 char *tmp = replace->filename;
Bart De Schuymer64182a32004-01-21 20:39:54 +00001066 int init = 1;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001067
Bart De Schuymer64182a32004-01-21 20:39:54 +00001068 if (c == 10)
1069 init = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001070 /* Get the kernel table */
1071 replace->filename = NULL;
1072 ebt_get_kernel_table(replace, init);
1073 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001074 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001075 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001076 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001077 if (exec_style == EXEC_STYLE_DAEMON)
1078 ebt_print_error2("--atomic is not supported in daemon mode");
1079 if (OPT_COMMANDS)
1080 ebt_print_error2("--atomic has to come before the command");
1081 /* A possible memory leak here, but this is not
1082 * executed in daemon mode */
1083 replace->filename = (char *)malloc(strlen(optarg) + 1);
1084 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001085 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001086 case 1 :
1087 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001088 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001089 else
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001090 ebt_print_error2("Bad argument : '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001091 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001092 optind--;
1093 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001094 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001095 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001096 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001097 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1098 if (ebt_errormsg[0] != '\0')
1099 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001100 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001101 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001102
Bart De Schuymer6622a012005-01-19 21:09:05 +00001103 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001104 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001105 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001106 break;
1107
1108 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001109 if (ebt_errormsg[0] != '\0')
1110 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001111 if (m->used == 0) {
1112 ebt_add_match(new_entry, m);
1113 m->used = 1;
1114 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001115 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001116 }
1117
Bart De Schuymer6622a012005-01-19 21:09:05 +00001118 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001119 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001120 if (w->parse(c - w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001121 break;
1122
1123 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001124 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymerff587202005-02-08 20:02:28 +00001125 if (ebt_errormsg[0] != '\0')
1126 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001127 if (w->used == 0) {
1128 ebt_add_watcher(new_entry, w);
1129 w->used = 1;
1130 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001131check_extension:
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001132 if (replace->command != 'A' && replace->command != 'I' &&
1133 replace->command != 'D' && replace->command != 'C')
1134 ebt_print_error2("Extensions only for -A, -I, -D and -C");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001135 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001136 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001137 }
1138
Bart De Schuymer6622a012005-01-19 21:09:05 +00001139 /* Just in case we didn't catch an error */
1140 if (ebt_errormsg[0] != '\0')
1141 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001142
Bart De Schuymer6622a012005-01-19 21:09:05 +00001143 if (!(table = ebt_find_table(replace->name)))
1144 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001145
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001146 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001147 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001148 if (exec_style == EXEC_STYLE_PRG)
1149 exit(0);
1150 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001151
Bart De Schuymer6622a012005-01-19 21:09:05 +00001152 /* Do the final checks */
1153 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001154 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001155 /* This will put the hook_mask right for the chains */
1156 ebt_check_for_loops(replace);
1157 if (ebt_errormsg[0] != '\0')
1158 return -1;
1159 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001160 m_l = new_entry->m_list;
1161 w_l = new_entry->w_list;
1162 t = (struct ebt_u_target *)new_entry->t;
1163 while (m_l) {
1164 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001165 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001166 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001167 if (ebt_errormsg[0] != '\0')
1168 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001169 m_l = m_l->next;
1170 }
1171 while (w_l) {
1172 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001173 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001174 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001175 if (ebt_errormsg[0] != '\0')
1176 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001177 w_l = w_l->next;
1178 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001179 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001180 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001181 if (ebt_errormsg[0] != '\0')
1182 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001183 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001184 /* So, the extensions can work with the host endian.
1185 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001186 new_entry->ethproto = htons(new_entry->ethproto);
1187
Bart De Schuymer6622a012005-01-19 21:09:05 +00001188 if (replace->command == 'P') {
1189 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1190 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1191 ebt_change_policy(replace, policy);
1192 if (ebt_errormsg[0] != '\0')
1193 return -1;
1194 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001195 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001196 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001197 exit(0);
1198 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001199 if (replace->flags & OPT_ZERO) {
1200 replace->selected_chain = zerochain;
1201 ebt_zero_counters(replace);
1202 } else if (replace->command == 'F') {
1203 ebt_flush_chains(replace);
1204 } else if (replace->command == 'A' || replace->command == 'I') {
1205 ebt_add_rule(replace, new_entry, rule_nr);
1206 if (ebt_errormsg[0] != '\0')
1207 return -1;
1208 /* Makes undoing the add easier (jumps to delete_the_rule) */
1209 if (rule_nr <= 0)
1210 rule_nr--;
1211 rule_nr_end = rule_nr;
1212
1213 ebt_check_for_loops(replace);
1214 if (ebt_errormsg[0] != '\0')
1215 goto delete_the_rule;
1216
1217 /* Do the final_check(), for all entries.
1218 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001219 i = -1;
1220 while (1) {
1221 struct ebt_u_entry *e;
1222
1223 i++;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001224 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001225 if (!entries) {
1226 if (i < NF_BR_NUMHOOKS)
1227 continue;
1228 else
1229 break;
1230 }
1231 e = entries->entries;
1232 while (e) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001233 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001234 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001235 ebt_do_final_checks(replace, e, entries);
1236 if (ebt_errormsg[0] != '\0')
1237 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001238 e->ethproto = htons(e->ethproto);
1239 e = e->next;
1240 }
1241 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001242 /* Don't reuse the added rule */
1243 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001244 } else if (replace->command == 'D') {
1245delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001246 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001247 if (ebt_errormsg[0] != '\0')
1248 return -1;
1249 } else if (replace->command == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001250 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus), chcounter);
Bart De Schuymerff587202005-02-08 20:02:28 +00001251 if (ebt_errormsg[0] != '\0')
1252 return -1;
1253 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001254 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1255 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001256
Bart De Schuymer6622a012005-01-19 21:09:05 +00001257 if (ebt_errormsg[0] != '\0')
1258 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001259 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001260 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001261
Bart De Schuymer6622a012005-01-19 21:09:05 +00001262 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1263 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001264
Bart De Schuymer6622a012005-01-19 21:09:05 +00001265 if (replace->counterchanges)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001266 ebt_deliver_counters(replace, EXEC_STYLE_PRG);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001267 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001268 return 0;
1269}