blob: 29a18013e81605284bec24a1473c9fe8036da7e7 [file] [log] [blame]
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001/*
Bart De Schuymerc56a31a2002-07-25 08:16:08 +00002 * ebtables.c, v2.0 July 2002
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00003 *
4 * Author: Bart De Schuymer
5 *
Bart De Schuymer6622a012005-01-19 21:09:05 +00006 * This code was stongly inspired on the iptables code which is
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00007 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <getopt.h>
25#include <string.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000026#include <stdio.h>
27#include <stdlib.h>
Bart De Schuymerd4586482002-08-11 16:15:55 +000028#include <stdarg.h>
Bart De Schuymer41830412002-06-05 19:41:28 +000029#include <netinet/ether.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000030#include "include/ebtables_u.h"
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000031#include "include/ethernetdb.h"
Bart De Schuymerc8531032002-06-14 21:55:29 +000032#include <unistd.h>
33#include <fcntl.h>
34#include <sys/wait.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000035
Bart De Schuymerab611e22005-02-14 20:20:03 +000036#define OPT_COMMANDS (replace->flags & OPT_COMMAND || replace->flags & OPT_ZERO)
37
38#define OPT_COMMAND 0x01
39#define OPT_TABLE 0x02
40#define OPT_IN 0x04
41#define OPT_OUT 0x08
42#define OPT_JUMP 0x10
43#define OPT_PROTOCOL 0x20
44#define OPT_SOURCE 0x40
45#define OPT_DEST 0x80
46#define OPT_ZERO 0x100
47#define OPT_LOGICALIN 0x200
48#define OPT_LOGICALOUT 0x400
49#define OPT_KERNELDATA 0x800 /* This value is also defined in ebtablesd.c */
50#define OPT_COUNT 0x1000 /* This value is also defined in libebtc.c */
51#define OPT_CNT_INCR 0x2000 /* This value is also defined in libebtc.c */
52#define OPT_CNT_DECR 0x4000 /* This value is also defined in libebtc.c */
53
Bart De Schuymer6622a012005-01-19 21:09:05 +000054/* dDfault command line options. Do not mess around with the already
55 * assigned numbers unless you know what you are doing */
Bart De Schuymer62423742002-07-14 19:06:20 +000056static struct option ebt_original_options[] =
57{
Bart De Schuymerab611e22005-02-14 20:20:03 +000058 { "append" , required_argument, 0, 'A' },
59 { "insert" , required_argument, 0, 'I' },
60 { "delete" , required_argument, 0, 'D' },
61 { "list" , optional_argument, 0, 'L' },
62 { "Lc" , no_argument , 0, 4 },
63 { "Ln" , no_argument , 0, 5 },
64 { "Lx" , no_argument , 0, 6 },
65 { "Lmac2" , no_argument , 0, 12 },
66 { "zero" , optional_argument, 0, 'Z' },
67 { "flush" , optional_argument, 0, 'F' },
68 { "policy" , required_argument, 0, 'P' },
69 { "in-interface" , required_argument, 0, 'i' },
70 { "in-if" , required_argument, 0, 'i' },
71 { "logical-in" , required_argument, 0, 2 },
72 { "logical-out" , required_argument, 0, 3 },
73 { "out-interface" , required_argument, 0, 'o' },
74 { "out-if" , required_argument, 0, 'o' },
75 { "version" , no_argument , 0, 'V' },
76 { "help" , no_argument , 0, 'h' },
77 { "jump" , required_argument, 0, 'j' },
78 { "set-counters" , required_argument, 0, 'c' },
79 { "change-counters", required_argument, 0, 'C' },
80 { "proto" , required_argument, 0, 'p' },
81 { "protocol" , required_argument, 0, 'p' },
82 { "db" , required_argument, 0, 'b' },
83 { "source" , required_argument, 0, 's' },
84 { "src" , required_argument, 0, 's' },
85 { "destination" , required_argument, 0, 'd' },
86 { "dst" , required_argument, 0, 'd' },
87 { "table" , required_argument, 0, 't' },
88 { "modprobe" , required_argument, 0, 'M' },
89 { "new-chain" , required_argument, 0, 'N' },
90 { "rename-chain" , required_argument, 0, 'E' },
91 { "delete-chain" , optional_argument, 0, 'X' },
92 { "atomic-init" , no_argument , 0, 7 },
93 { "atomic-commit" , no_argument , 0, 8 },
94 { "atomic-file" , required_argument, 0, 9 },
95 { "atomic-save" , no_argument , 0, 10 },
96 { "init-table" , no_argument , 0, 11 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000097 { 0 }
98};
99
100static struct option *ebt_options = ebt_original_options;
101
Bart De Schuymer6622a012005-01-19 21:09:05 +0000102/* Holds all the data */
103static struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000104
Bart De Schuymer6622a012005-01-19 21:09:05 +0000105/* The chosen table */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000106static struct ebt_u_table *table = NULL;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000107
Bart De Schuymer6622a012005-01-19 21:09:05 +0000108/* The pointers in here are special:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000109 * The struct ebt_target * pointer is actually a struct ebt_u_target * pointer.
110 * instead of making yet a few other structs, we just do a cast.
111 * We need a struct ebt_u_target pointer because we know the address of the data
112 * they point to won't change. We want to allow that the struct ebt_u_target.t
113 * member can change.
Bart De Schuymer6622a012005-01-19 21:09:05 +0000114 * Same holds for the struct ebt_match and struct ebt_watcher pointers */
115static struct ebt_u_entry *new_entry;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000116
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000117
Bart De Schuymer6622a012005-01-19 21:09:05 +0000118static int global_option_offset;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000119#define OPTION_OFFSET 256
120static struct option *
121merge_options(struct option *oldopts, const struct option *newopts,
122 unsigned int *options_offset)
123{
124 unsigned int num_old, num_new, i;
125 struct option *merge;
126
127 if (!newopts || !oldopts || !options_offset)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000128 ebt_print_bug("merge wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000129 for (num_old = 0; oldopts[num_old].name; num_old++);
130 for (num_new = 0; newopts[num_new].name; num_new++);
131
132 global_option_offset += OPTION_OFFSET;
133 *options_offset = global_option_offset;
134
135 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
136 if (!merge)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000137 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000138 memcpy(merge, oldopts, num_old * sizeof(struct option));
139 for (i = 0; i < num_new; i++) {
140 merge[num_old + i] = newopts[i];
141 merge[num_old + i].val += *options_offset;
142 }
143 memset(merge + num_old + num_new, 0, sizeof(struct option));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000144 /* Only free dynamically allocated stuff */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000145 if (oldopts != ebt_original_options)
146 free(oldopts);
147
148 return merge;
149}
150
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000151static void merge_match(struct ebt_u_match *m)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000152{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000153 ebt_options = merge_options
154 (ebt_options, m->extra_ops, &(m->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000155}
156
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000157static void merge_watcher(struct ebt_u_watcher *w)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000158{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000159 ebt_options = merge_options
160 (ebt_options, w->extra_ops, &(w->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000161}
162
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000163static void merge_target(struct ebt_u_target *t)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000164{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000165 ebt_options = merge_options
166 (ebt_options, t->extra_ops, &(t->option_offset));
Bart De Schuymer9a0fbf22003-01-11 16:16:54 +0000167}
168
Bart De Schuymer6622a012005-01-19 21:09:05 +0000169/* Be backwards compatible, so don't use '+' in kernel */
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000170#define IF_WILDCARD 1
171static void print_iface(const char *iface)
172{
173 char *c;
174
175 if ((c = strchr(iface, IF_WILDCARD)))
176 *c = '+';
177 printf("%s ", iface);
178 if (c)
179 *c = IF_WILDCARD;
180}
181
Bart De Schuymer6622a012005-01-19 21:09:05 +0000182/* We use replace->flags, so we can't use the following values:
183 * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO */
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000184#define LIST_N 0x04
185#define LIST_C 0x08
186#define LIST_X 0x10
187#define LIST_MAC2 0x20
188
Bart De Schuymer6622a012005-01-19 21:09:05 +0000189/* Helper function for list_rules() */
Bart De Schuymerab611e22005-02-14 20:20:03 +0000190static void list_em(struct ebt_u_entries *entries, struct ebt_cntchanges *cc)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000191{
192 int i, j, space = 0, digits;
193 struct ebt_u_entry *hlp;
194 struct ebt_u_match_list *m_l;
195 struct ebt_u_watcher_list *w_l;
196 struct ebt_u_match *m;
197 struct ebt_u_watcher *w;
198 struct ebt_u_target *t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000199
Bart De Schuymer6622a012005-01-19 21:09:05 +0000200 if (replace->flags & LIST_MAC2)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000201 ebt_printstyle_mac = 2;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000202 hlp = entries->entries;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000203 if (replace->flags & LIST_X && entries->policy != EBT_ACCEPT) {
204 printf("ebtables -t %s -P %s %s\n", replace->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000205 entries->name, ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000206 } else if (!(replace->flags & LIST_X)) {
Bart De Schuymerc87c9642002-08-01 15:34:16 +0000207 printf("\nBridge chain: %s, entries: %d, policy: %s\n",
208 entries->name, entries->nentries,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000209 ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000210 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000211
Bart De Schuymer60332e02002-06-23 08:01:47 +0000212 i = entries->nentries;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000213 while (i > 9) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000214 space++;
215 i /= 10;
216 }
217
Bart De Schuymer60332e02002-06-23 08:01:47 +0000218 for (i = 0; i < entries->nentries; i++) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000219 if (replace->flags & LIST_N) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000220 digits = 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000221 /* A little work to get nice rule numbers. */
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000222 j = i + 1;
223 while (j > 9) {
224 digits++;
225 j /= 10;
226 }
227 for (j = 0; j < space - digits; j++)
228 printf(" ");
229 printf("%d. ", i + 1);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000230 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000231 if (replace->flags & LIST_X)
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000232 printf("ebtables -t %s -A %s ",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000233 replace->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000234
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000235 /* The standard target's print() uses this to find out
236 * the name of a udc */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000237 hlp->replace = replace;
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000238
Bart De Schuymer6622a012005-01-19 21:09:05 +0000239 /* Don't print anything about the protocol if no protocol was
240 * specified, obviously this means any protocol will do. */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000241 if (!(hlp->bitmask & EBT_NOPROTO)) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000242 printf("-p ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000243 if (hlp->invflags & EBT_IPROTO)
244 printf("! ");
245 if (hlp->bitmask & EBT_802_3)
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000246 printf("Length ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000247 else {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000248 struct ethertypeent *ent;
249
Bart De Schuymer64182a32004-01-21 20:39:54 +0000250 ent = getethertypebynumber
251 (ntohs(hlp->ethproto));
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000252 if (!ent)
Bart De Schuymer60332e02002-06-23 08:01:47 +0000253 printf("0x%x ", ntohs(hlp->ethproto));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000254 else
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000255 printf("%s ", ent->e_name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000256 }
257 }
258 if (hlp->bitmask & EBT_SOURCEMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000259 printf("-s ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000260 if (hlp->invflags & EBT_ISOURCE)
261 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000262 ebt_print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000263 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000264 }
265 if (hlp->bitmask & EBT_DESTMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000266 printf("-d ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000267 if (hlp->invflags & EBT_IDEST)
268 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000269 ebt_print_mac_and_mask(hlp->destmac, hlp->destmsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000270 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000271 }
272 if (hlp->in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000273 printf("-i ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000274 if (hlp->invflags & EBT_IIN)
275 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000276 print_iface(hlp->in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000277 }
278 if (hlp->logical_in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000279 printf("--logical-in ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000280 if (hlp->invflags & EBT_ILOGICALIN)
281 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000282 print_iface(hlp->logical_in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000283 }
284 if (hlp->logical_out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000285 printf("--logical-out ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000286 if (hlp->invflags & EBT_ILOGICALOUT)
287 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000288 print_iface(hlp->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000289 }
290 if (hlp->out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000291 printf("-o ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000292 if (hlp->invflags & EBT_IOUT)
293 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000294 print_iface(hlp->out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000295 }
296
297 m_l = hlp->m_list;
298 while (m_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000299 m = ebt_find_match(m_l->m->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000300 if (!m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000301 ebt_print_bug("Match not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000302 m->print(hlp, m_l->m);
303 m_l = m_l->next;
304 }
305 w_l = hlp->w_list;
306 while (w_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000307 w = ebt_find_watcher(w_l->w->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000308 if (!w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000309 ebt_print_bug("Watcher not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000310 w->print(hlp, w_l->w);
311 w_l = w_l->next;
312 }
313
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000314 printf("-j ");
315 if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET))
316 printf("%s ", hlp->t->u.name);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000317 t = ebt_find_target(hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000318 if (!t)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000319 ebt_print_bug("Target '%s' not found", hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000320 t->print(hlp, hlp->t);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000321 if (replace->flags & LIST_C) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000322 uint64_t pcnt = hlp->cnt.pcnt;
323 uint64_t bcnt = hlp->cnt.bcnt;
324
325 while (cc->type == CNT_DEL)
326 cc = cc->next;
327 /* This can only happen in daemon mode */
328 if (cc->type == CNT_INCR) {
329 pcnt += hlp->cnt_surplus.pcnt;
330 bcnt += hlp->cnt_surplus.bcnt;
331 } else if (cc->type == CNT_DECR) {
332 pcnt -= hlp->cnt_surplus.pcnt;
333 bcnt -= hlp->cnt_surplus.bcnt;
334 }
335 cc = cc->next;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000336 if (replace->flags & LIST_X)
Bart De Schuymerab611e22005-02-14 20:20:03 +0000337 printf("-c %llu %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000338 else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000339 printf(", pcnt = %llu -- bcnt = %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000340 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000341 printf("\n");
342 hlp = hlp->next;
343 }
344}
345
Bart De Schuymer62423742002-07-14 19:06:20 +0000346static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000347{
348 struct ebt_u_match_list *m_l;
349 struct ebt_u_watcher_list *w_l;
350
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000351 PRINT_VERSION;
352 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000353"Usage:\n"
354"ebtables -[ADI] chain rule-specification [options]\n"
355"ebtables -P chain target\n"
356"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000357"ebtables -[NX] [chain]\n"
358"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000359"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000360"--append -A chain : append to chain\n"
361"--delete -D chain : delete matching rule from chain\n"
362"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000363"--change-counters -C chain\n"
364" [rulenum] pcnt bcnt : change counters of existing rule\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000365"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000366"--list -L [chain] : list the rules in a chain or in all chains\n"
367"--flush -F [chain] : delete all rules in chain or in all chains\n"
368"--init-table : replace the kernel table with the initial table\n"
369"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
370"--policy -P chain target : change policy on chain to target\n"
371"--new-chain -N chain : create a user defined chain\n"
372"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000373"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000374"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
375"--atomic-init : put the initial kernel table into <FILE>\n"
376"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000377"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000378"Options:\n"
379"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
380"--src -s [!] address[/mask]: source mac address\n"
381"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000382"--in-if -i [!] name[+] : network input interface name\n"
383"--out-if -o [!] name[+] : network output interface name\n"
384"--logical-in [!] name[+] : logical bridge input interface name\n"
385"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000386"--set-counters -c chain\n"
387" pcnt bcnt : set the counters of the to be added rule\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000388"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000389"--version -V : print package version\n\n"
390"Environment variable:\n"
391ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
392"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000393 m_l = new_entry->m_list;
394 while (m_l) {
395 ((struct ebt_u_match *)m_l->m)->help();
396 printf("\n");
397 m_l = m_l->next;
398 }
399 w_l = new_entry->w_list;
400 while (w_l) {
401 ((struct ebt_u_watcher *)w_l->w)->help();
402 printf("\n");
403 w_l = w_l->next;
404 }
405 ((struct ebt_u_target *)new_entry->t)->help();
406 printf("\n");
407 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000408 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000409}
410
Bart De Schuymer6622a012005-01-19 21:09:05 +0000411/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000412static void list_rules()
413{
Bart De Schuymerab611e22005-02-14 20:20:03 +0000414 int i, j;
415 struct ebt_cntchanges *cc = replace->counterchanges;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000416
Bart De Schuymer6622a012005-01-19 21:09:05 +0000417 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000418 printf("Bridge table: %s\n", table->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000419 if (replace->selected_chain != -1) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000420 for (i = 0; i < replace->selected_chain; i++) {
421 if (i < NF_BR_NUMHOOKS && !(replace->valid_hooks & (1 << i)))
422 continue;
423 j = ebt_nr_to_chain(replace, i)->nentries;
424 while (j) {
425 if (cc->type != CNT_DEL)
426 j--;
427 cc = cc->next;
428 }
429 }
430 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 Schuymer6622a012005-01-19 21:09:05 +0000443 if (replace->valid_hooks & (1 << i) &&
444 strcmp(replace->hook_entry[i]->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000445 ebt_hooknames[i]))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000446 printf("ebtables -t %s -E %s %s\n",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000447 replace->name, ebt_hooknames[i],
448 replace->hook_entry[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000449 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000450 i = 0;
451 while (1) {
452 if (i < NF_BR_NUMHOOKS) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000453 if (replace->valid_hooks & (1 << i)) {
454 list_em(replace->hook_entry[i], cc);
455 j = ebt_nr_to_chain(replace, i)->nentries;
456 while (j) {
457 if (cc->type != CNT_DEL)
458 j--;
459 cc = cc->next;
460 }
461 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000462 i++;
463 continue;
464 } else {
465 if (!cl)
466 break;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000467 list_em(cl->udc, cc);
468 j = ebt_nr_to_chain(replace, i)->nentries;
469 while (j) {
470 if (cc->type != CNT_DEL)
471 j--;
472 cc = cc->next;
473 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000474 cl = cl->next;
475 }
476 }
477 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000478}
479
Bart De Schuymercc440052002-11-06 21:10:33 +0000480static int parse_delete_rule(const char *argv, int *rule_nr, int *rule_nr_end)
481{
482 char *colon = strchr(argv, ':'), *buffer;
483
484 if (colon) {
485 *colon = '\0';
486 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000487 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000488 else {
489 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000490 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000491 return -1;
492 }
493 }
494 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000495 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000496 else {
497 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000498 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000499 return -1;
500 }
501 if (!colon)
502 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000503 return 0;
504}
505
Bart De Schuymerff587202005-02-08 20:02:28 +0000506static int parse_change_counters_rule(int argc, char **argv, int *rule_nr, int *rule_nr_end)
507{
508 char *buffer;
509
510 if (optind + 1 >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')) ||
511 (argv[optind + 1][0] == '-' && (argv[optind + 1][1] < '0' && argv[optind + 1][1] > '9')))
512 ebt_print_error2("The command -C needs at least 3 arguments");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000513 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 +0000514 if (optind + 3 != argc)
515 ebt_print_error2("No extra options allowed with -C start_nr[:end_nr] pcnt bcnt");
516 if (parse_delete_rule(argv[optind], rule_nr, rule_nr_end))
517 ebt_print_error2("Something is wrong with the rule number specification '%s'", argv[optind]);
518 optind++;
519 }
520
Bart De Schuymerab611e22005-02-14 20:20:03 +0000521 if (argv[optind][0] == '+') {
522 if (argv[optind + 1][0] != '+')
523 ebt_print_error2("If one counter is increased, the other should be increased too (perhaps put a '+' before '%s')", argv[optind + 1]);
524 replace->flags |= OPT_CNT_INCR;
525 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
526 if (*buffer != '\0')
527 goto invalid;
528 optind++;
529 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
530 } else if (argv[optind][0] == '-') {
531 if (argv[optind + 1][0] != '-')
532 ebt_print_error2("If one counter is decreased, the other should be decreased too (perhaps put a '-' before '%s')", argv[optind + 1]);
533 replace->flags |= OPT_CNT_DECR;
534 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
535 if (*buffer != '\0')
536 goto invalid;
537 optind++;
538 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
539 } else {
540 if (argv[optind + 1][0] == '-' || argv[optind + 1][0] == '+')
541 ebt_print_error2("If one counter is %screased, the other should be %screased too (perhaps put a '%s' before '%s')",
542 argv[optind + 1][0] == '-' ? "de" : "in", argv[optind + 1][0] == '-' ? "de" : "in", argv[optind]);
543 new_entry->cnt_surplus.pcnt = strtoull(argv[optind], &buffer, 10);
544 optind++;
545 new_entry->cnt_surplus.bcnt = strtoull(argv[optind], &buffer, 10);
546 }
Bart De Schuymerff587202005-02-08 20:02:28 +0000547
Bart De Schuymerab611e22005-02-14 20:20:03 +0000548 if (*buffer != '\0')
549 goto invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000550 optind++;
551 return 0;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000552invalid:
553 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerff587202005-02-08 20:02:28 +0000554}
555
Bart De Schuymer6622a012005-01-19 21:09:05 +0000556static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000557{
558 char *c;
559
560 if ((c = strchr(iface, '+'))) {
561 if (*(c + 1) != '\0') {
562 ebt_print_error("Spurious characters after '+' "
563 "wildcard for %s", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000564 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000565 } else
566 *c = IF_WILDCARD;
567 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000568 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000569}
570
Bart De Schuymer6622a012005-01-19 21:09:05 +0000571void ebt_early_init_once()
572{
573 ebt_iterate_matches(merge_match);
574 ebt_iterate_watchers(merge_watcher);
575 ebt_iterate_targets(merge_target);
576}
577
Bart De Schuymer6622a012005-01-19 21:09:05 +0000578int do_command(int argc, char *argv[], int exec_style,
579 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000580{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000581 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000582 int c, i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000583 /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000584 int zerochain = -1;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000585 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000586 int rule_nr = 0;
587 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000588 struct ebt_u_target *t;
589 struct ebt_u_match *m;
590 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000591 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000592 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000593 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000594
Bart De Schuymera615b962002-11-03 14:54:09 +0000595 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000596 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000597
Bart De Schuymer6622a012005-01-19 21:09:05 +0000598 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000599
Bart De Schuymer6622a012005-01-19 21:09:05 +0000600 /* The daemon doesn't use the environment variable */
601 if (exec_style == EXEC_STYLE_PRG) {
602 buffer = getenv(ATOMIC_ENV_VARIABLE);
603 if (buffer) {
604 replace->filename = malloc(strlen(buffer) + 1);
605 if (!replace->filename)
606 ebt_print_memory();
607 strcpy(replace->filename, buffer);
608 buffer = NULL;
609 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000610 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000611
Bart De Schuymer6622a012005-01-19 21:09:05 +0000612 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
613 replace->selected_chain = -1;
614 replace->command = 'h';
615
616 if (!new_entry) {
617 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
618 if (!new_entry)
619 ebt_print_memory();
620 }
621 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000622 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000623 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000624
Bart De Schuymer6622a012005-01-19 21:09:05 +0000625 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000626 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000627 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000628
Bart De Schuymer6622a012005-01-19 21:09:05 +0000629 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000630 while ((c = getopt_long(argc, argv,
Bart De Schuymerff587202005-02-08 20:02:28 +0000631 "-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 +0000632 switch (c) {
633
Bart De Schuymer6622a012005-01-19 21:09:05 +0000634 case 'A': /* Add a rule */
635 case 'D': /* Delete a rule */
Bart De Schuymerff587202005-02-08 20:02:28 +0000636 case 'C': /* Change counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000637 case 'P': /* Define policy */
638 case 'I': /* Insert a rule */
639 case 'N': /* Make a user defined chain */
640 case 'E': /* Rename chain */
641 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000642 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000643 if (replace->command == 'N' && c == 'P') {
644 replace->command = c;
645 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000646 goto handle_P;
647 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000648 if (OPT_COMMANDS)
649 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000650
Bart De Schuymer6622a012005-01-19 21:09:05 +0000651 replace->command = c;
652 replace->flags |= OPT_COMMAND;
653 if (!(replace->flags & OPT_KERNELDATA))
654 ebt_get_kernel_table(replace, 0);
655 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
656 ebt_print_error2("No chain name specified");
657 if (c == 'N') {
658 ebt_new_chain(replace, optarg, EBT_ACCEPT);
659 /* This is needed to get -N x -P y working */
660 replace->selected_chain = ebt_get_chainnr(replace, optarg);
661 break;
662 } else if (c == 'X') {
663 if (optind >= argc) {
664 replace->selected_chain = -1;
665 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000666 break;
667 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000668
669 if (optind < argc - 1)
670 ebt_print_error2("No extra options allowed with -X");
671
672 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
673 ebt_print_error2("Chain %s doesn't exist", argv[optind]);
674 ebt_delete_chain(replace);
675 if (ebt_errormsg[0] != '\0')
676 return -1;
677 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000678 break;
679 }
680
Bart De Schuymer6622a012005-01-19 21:09:05 +0000681 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
682 ebt_print_error2("Chain %s doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000683 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000684 if (optind >= argc)
685 ebt_print_error2("No new chain name specified");
686 if (optind < argc - 1)
687 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer64182a32004-01-21 20:39:54 +0000688 if (strlen(argv[optind])>=EBT_CHAIN_MAXNAMELEN)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000689 ebt_print_error2("Chain name length can't exceed %d", EBT_CHAIN_MAXNAMELEN - 1);
690 if (ebt_get_chainnr(replace, argv[optind]) != -1)
691 ebt_print_error2("Chain %s already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000692 if (ebt_find_target(argv[optind]))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 ebt_print_error2("Target with name %s exists", argv[optind]);
694 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000695 optind++;
696 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
698 if (optind != argc - 1)
699 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
700 if (parse_delete_rule(argv[optind], &rule_nr, &rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000701 ebt_print_error2("Problem with the specified rule number(s) '%s'", argv[optind]);
Bart De Schuymercc440052002-11-06 21:10:33 +0000702 optind++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000703 } else if (c == 'C') {
704 if (parse_change_counters_rule(argc, argv, &rule_nr, &rule_nr_end))
705 return -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000706 } else if (c == 'I') {
707 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
708 ebt_print_error2("No rulenr for -I specified");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000709 rule_nr = strtol(argv[optind], &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000710 if (*buffer != '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000711 ebt_print_error2("Problem with the specified rule number");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000712 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000713 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000714handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000715 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000716 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000717 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000718 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000719 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000720 if (policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000721 ebt_print_error2("Wrong policy: '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000722 break;
723 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000724 optind++;
725 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000726 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000727 case 'L': /* List */
728 case 'F': /* Flush */
729 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000730 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000731 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
732print_zero:
733 ebt_print_error2("Command -Z only allowed together with command -L");
734 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000735 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000736 replace->command = c;
737 if (replace->flags & OPT_COMMAND)
738 ebt_print_error2("Multiple commands are not allowed");
739 replace->flags |= OPT_COMMAND;
740 if (replace->flags & OPT_ZERO)
741 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000742 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000743
744#ifdef SILENT_DAEMON
745 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
746 ebt_print_error2("-L not supported in daemon mode not supported in daemon mode");
747#endif
748
749 if (!(replace->flags & OPT_KERNELDATA))
750 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000751 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000752 if (optind < argc && argv[optind][0] != '-') {
753 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000754 ebt_print_error2("Chain %s doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000755 optind++;
756 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000757 if (i != -1) {
758 if (c == 'Z')
759 zerochain = i;
760 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000761 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000762 }
763 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 case 'V': /* Version */
765 replace->command = 'V';
766 if (OPT_COMMANDS)
767 ebt_print_error2("Multiple commands are not allowed");
768 if (exec_style == EXEC_STYLE_DAEMON)
769 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000770 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000771 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000772 case 'M': /* Modprobe */
773 if (OPT_COMMANDS)
774 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000775 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000776 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000777 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000778#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000779 if (exec_style == EXEC_STYLE_DAEMON)
780 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000781#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000782 if (OPT_COMMANDS)
783 ebt_print_error2("Multiple commands are not allowed");
784 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000785
Bart De Schuymer6622a012005-01-19 21:09:05 +0000786 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000787 while (optind < argc) {
788 struct ebt_u_match *m;
789 struct ebt_u_watcher *w;
790
Bart De Schuymer6622a012005-01-19 21:09:05 +0000791 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000792 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000793 exit(0);
794 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000795 if ((m = ebt_find_match(argv[optind])))
796 ebt_add_match(new_entry, m);
797 else if ((w = ebt_find_watcher(argv[optind])))
798 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000799 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000800 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000801 ebt_print_error2("Extension %s not found", argv[optind]);
802 if (replace->flags & OPT_JUMP)
803 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
804 replace->flags |= OPT_JUMP;
805 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000806 }
807 optind++;
808 }
809 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000810 case 't': /* Table */
811 if (replace->command != 'h')
812 ebt_print_error2("Please put the -t option first");
813 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000814 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000815 ebt_print_error2("Table name too long");
816 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000817 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000818 case 'i': /* Input interface */
819 case 2 : /* Logical input interface */
820 case 'o': /* Output interface */
821 case 3 : /* Logical output interface */
822 case 'j': /* Target */
823 case 'p': /* Net family protocol */
824 case 's': /* Source mac */
825 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000826 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000827 if (!OPT_COMMANDS)
828 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000829 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000830 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000831 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000832 ebt_check_option2(&(replace->flags), OPT_IN);
833 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
834 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
835 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000836 new_entry->invflags |= EBT_IIN;
837
Bart De Schuymer6622a012005-01-19 21:09:05 +0000838 if (strlen(optarg) >= IFNAMSIZ)
839 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
840 strcpy(new_entry->in, optarg);
841 if (parse_iface(new_entry->in, "-i"))
842 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000843 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000844 } else if (c == 2) {
845 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
846 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
847 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
848 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000849 new_entry->invflags |= EBT_ILOGICALIN;
850
Bart De Schuymer6622a012005-01-19 21:09:05 +0000851 if (strlen(optarg) >= IFNAMSIZ)
852 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
853 strcpy(new_entry->logical_in, optarg);
854 if (parse_iface(new_entry->logical_in, "--logical-in"))
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 == 'o') {
858 ebt_check_option2(&(replace->flags), OPT_OUT);
859 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
860 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
861 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000862 new_entry->invflags |= EBT_IOUT;
863
Bart De Schuymer6622a012005-01-19 21:09:05 +0000864 if (strlen(optarg) >= IFNAMSIZ)
865 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
866 strcpy(new_entry->out, optarg);
867 if (parse_iface(new_entry->out, "-o"))
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 == 3) {
871 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
872 if (replace->selected_chain < 2)
873 ebt_print_error2("Use --logical-out 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_ILOGICALOUT;
876
Bart De Schuymer6622a012005-01-19 21:09:05 +0000877 if (strlen(optarg) >= IFNAMSIZ)
878 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
879 strcpy(new_entry->logical_out, optarg);
880 if (parse_iface(new_entry->logical_out, "--logical-out"))
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 == 'j') {
884 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000885 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000886 if (!strcmp(optarg, ebt_standard_targets[i])) {
887 t = ebt_find_target(EBT_STANDARD_TARGET);
888 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000889 break;
890 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000891 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
892 ebt_print_error2("Return target only for user defined chains");
893 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000894 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000895
896 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000897 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000898 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000899 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000900 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000901 break;
902 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000903 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000904 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000905
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000906 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000907 /* -j standard not allowed either */
908 if (!t || t == (struct ebt_u_target *)new_entry->t)
909 ebt_print_error2("Illegal target name");
910 new_entry->t = (struct ebt_entry_target *)t;
911 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
912 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000913 }
914 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000915 } else if (c == 's') {
916 ebt_check_option2(&(replace->flags), OPT_SOURCE);
917 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000918 new_entry->invflags |= EBT_ISOURCE;
919
Bart De Schuymer6622a012005-01-19 21:09:05 +0000920 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
921 ebt_print_error2("Problem with specified source mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000922 new_entry->bitmask |= EBT_SOURCEMAC;
923 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000924 } else if (c == 'd') {
925 ebt_check_option2(&(replace->flags), OPT_DEST);
926 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000927 new_entry->invflags |= EBT_IDEST;
928
Bart De Schuymer6622a012005-01-19 21:09:05 +0000929 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
930 ebt_print_error2("Problem with specified destination mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000931 new_entry->bitmask |= EBT_DESTMAC;
932 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000933 } else if (c == 'c') {
934 ebt_check_option2(&(replace->flags), OPT_COUNT);
935 if (ebt_check_inverse2(optarg))
936 ebt_print_error2("Unexpected '!' after -c");
937 if (optind >= argc)
938 ebt_print_error2("Option -c needs 2 arguments");
939
940 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
941 if (*buffer != '\0')
942 ebt_print_error2("Packet counter '%s' invalid", optarg)
943 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
944 if (*buffer != '\0')
945 ebt_print_error2("Packet counter '%s' invalid", argv[optind])
946 optind++;
947 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000948 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000949 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
950 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000951 new_entry->invflags |= EBT_IPROTO;
952
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000953 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000954 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000955 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000956 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000957 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000958 struct ethertypeent *ent;
959
Bart De Schuymer6622a012005-01-19 21:09:05 +0000960 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000961 new_entry->bitmask |= EBT_802_3;
962 break;
963 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000964 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000965 if (!ent)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000966 ebt_print_error2("Problem with the specified Ethernet protocol (%s), perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000967 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000968 } else
969 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000970
Bart De Schuymer6622a012005-01-19 21:09:05 +0000971 if (new_entry->ethproto < 0x0600)
972 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
973 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000974 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000975#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000976 if (exec_style == EXEC_STYLE_DAEMON)
977 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000978#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000979 ebt_check_option2(&(replace->flags), LIST_C);
980 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000981 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000982 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000983 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000984 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000985#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000986 if (exec_style == EXEC_STYLE_DAEMON)
987 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000988#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000989 ebt_check_option2(&(replace->flags), LIST_N);
990 if (replace->command != 'L')
991 ebt_print_error2("Use --Ln with -L");
992 if (replace->flags & LIST_X)
993 ebt_print_error2("--Lx is not compatible with --Ln");
994 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000995 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000996 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +0000997#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000998 if (exec_style == EXEC_STYLE_DAEMON)
999 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001000#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001001 ebt_check_option2(&(replace->flags), LIST_X);
1002 if (replace->command != 'L')
1003 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +00001004 if (replace->flags & LIST_N)
1005 ebt_print_error2("--Lx is not compatible with --Ln");
1006 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001007 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001008 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +00001009#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +00001010 if (exec_style == EXEC_STYLE_DAEMON)
1011 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +00001012#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +00001013 ebt_check_option2(&(replace->flags), LIST_MAC2);
1014 if (replace->command != 'L')
1015 ebt_print_error2("Use --Lmac2 with -L");
1016 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +00001017 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +00001018 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001019 if (exec_style == EXEC_STYLE_DAEMON)
1020 ebt_print_error2("--atomic-commit is not supported in daemon mode");
1021 replace->command = c;
1022 if (OPT_COMMANDS)
1023 ebt_print_error2("Multiple commands are not allowed");
1024 replace->flags |= OPT_COMMAND;
1025 if (!replace->filename)
1026 ebt_print_error2("No atomic file specified");
1027 /* Get the information from the file */
1028 ebt_get_table(replace, 0);
1029 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +00001030 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +00001031 * the file */
1032 replace->num_counters = 0;
1033 /* Make sure the table will be written to the kernel */
1034 free(replace->filename);
1035 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +00001036 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001037 case 7 : /* atomic-init */
1038 case 10: /* atomic-save */
1039 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001040 if (exec_style == EXEC_STYLE_DAEMON) {
1041 if (c == 7) {
1042 ebt_print_error2("--atomic-init is not supported in daemon mode");
1043 } else if (c == 10)
1044 ebt_print_error2("--atomic-save is not supported in daemon mode");
1045 ebt_print_error2("--init-table is not supported in daemon mode");
1046 }
1047 replace->command = c;
1048 if (OPT_COMMANDS)
1049 ebt_print_error2("Multiple commands are not allowed");
1050 if (c != 11 && !replace->filename)
1051 ebt_print_error2("No atomic file specified");
1052 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001053 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001054 char *tmp = replace->filename;
Bart De Schuymer64182a32004-01-21 20:39:54 +00001055 int init = 1;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001056
Bart De Schuymer64182a32004-01-21 20:39:54 +00001057 if (c == 10)
1058 init = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001059 /* Get the kernel table */
1060 replace->filename = NULL;
1061 ebt_get_kernel_table(replace, init);
1062 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001063 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001064 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001065 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001066 if (exec_style == EXEC_STYLE_DAEMON)
1067 ebt_print_error2("--atomic is not supported in daemon mode");
1068 if (OPT_COMMANDS)
1069 ebt_print_error2("--atomic has to come before the command");
1070 /* A possible memory leak here, but this is not
1071 * executed in daemon mode */
1072 replace->filename = (char *)malloc(strlen(optarg) + 1);
1073 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001074 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001075 case 1 :
1076 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001077 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001078 else
Bart De Schuymer6622a012005-01-19 21:09:05 +00001079 ebt_print_error2("Bad argument : %s", optarg);
1080 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001081 optind--;
1082 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001083 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001084 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001085 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001086 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1087 if (ebt_errormsg[0] != '\0')
1088 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001089 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001090 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001091
Bart De Schuymer6622a012005-01-19 21:09:05 +00001092 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001093 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001094 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001095 break;
1096
1097 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001098 if (ebt_errormsg[0] != '\0')
1099 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001100 if (m->used == 0) {
1101 ebt_add_match(new_entry, m);
1102 m->used = 1;
1103 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001104 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001105 }
1106
Bart De Schuymer6622a012005-01-19 21:09:05 +00001107 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001108 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001109 if (w->parse(c-w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001110 break;
1111
1112 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001113 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymerff587202005-02-08 20:02:28 +00001114 if (ebt_errormsg[0] != '\0')
1115 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001116 if (w->used == 0) {
1117 ebt_add_watcher(new_entry, w);
1118 w->used = 1;
1119 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001120check_extension:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001121 if (replace->command != 'A' && replace->command != 'I' && replace->command != 'D')
1122 ebt_print_error2("Extensions only for -A, -I and -D");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001123 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001124 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001125 }
1126
Bart De Schuymer6622a012005-01-19 21:09:05 +00001127 /* Just in case we didn't catch an error */
1128 if (ebt_errormsg[0] != '\0')
1129 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001130
Bart De Schuymer6622a012005-01-19 21:09:05 +00001131 if (!(table = ebt_find_table(replace->name)))
1132 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001133
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001134 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001135 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001136 if (exec_style == EXEC_STYLE_PRG)
1137 exit(0);
1138 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001139
Bart De Schuymer6622a012005-01-19 21:09:05 +00001140 /* Do the final checks */
1141 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001142 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001143 /* This will put the hook_mask right for the chains */
1144 ebt_check_for_loops(replace);
1145 if (ebt_errormsg[0] != '\0')
1146 return -1;
1147 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001148 m_l = new_entry->m_list;
1149 w_l = new_entry->w_list;
1150 t = (struct ebt_u_target *)new_entry->t;
1151 while (m_l) {
1152 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001153 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001154 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001155 if (ebt_errormsg[0] != '\0')
1156 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001157 m_l = m_l->next;
1158 }
1159 while (w_l) {
1160 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001161 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001162 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001163 if (ebt_errormsg[0] != '\0')
1164 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001165 w_l = w_l->next;
1166 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001167 t->final_check(new_entry, t->t, 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 Schuymer1abc55d2002-06-01 19:23:47 +00001171 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001172 /* So, the extensions can work with the host endian.
1173 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001174 new_entry->ethproto = htons(new_entry->ethproto);
1175
Bart De Schuymer6622a012005-01-19 21:09:05 +00001176 if (replace->command == 'P') {
1177 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1178 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1179 ebt_change_policy(replace, policy);
1180 if (ebt_errormsg[0] != '\0')
1181 return -1;
1182 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001183 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001184 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001185 exit(0);
1186 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001187 if (replace->flags & OPT_ZERO) {
1188 replace->selected_chain = zerochain;
1189 ebt_zero_counters(replace);
1190 } else if (replace->command == 'F') {
1191 ebt_flush_chains(replace);
1192 } else if (replace->command == 'A' || replace->command == 'I') {
1193 ebt_add_rule(replace, new_entry, rule_nr);
1194 if (ebt_errormsg[0] != '\0')
1195 return -1;
1196 /* Makes undoing the add easier (jumps to delete_the_rule) */
1197 if (rule_nr <= 0)
1198 rule_nr--;
1199 rule_nr_end = rule_nr;
1200
1201 ebt_check_for_loops(replace);
1202 if (ebt_errormsg[0] != '\0')
1203 goto delete_the_rule;
1204
1205 /* Do the final_check(), for all entries.
1206 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001207 i = -1;
1208 while (1) {
1209 struct ebt_u_entry *e;
1210
1211 i++;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001212 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001213 if (!entries) {
1214 if (i < NF_BR_NUMHOOKS)
1215 continue;
1216 else
1217 break;
1218 }
1219 e = entries->entries;
1220 while (e) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001221 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001222 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001223 ebt_do_final_checks(replace, e, entries);
1224 if (ebt_errormsg[0] != '\0')
1225 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001226 e->ethproto = htons(e->ethproto);
1227 e = e->next;
1228 }
1229 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001230 /* Don't reuse the added rule */
1231 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001232 } else if (replace->command == 'D') {
1233delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001234 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001235 if (ebt_errormsg[0] != '\0')
1236 return -1;
1237 } else if (replace->command == 'C') {
Bart De Schuymerab611e22005-02-14 20:20:03 +00001238 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus));
Bart De Schuymerff587202005-02-08 20:02:28 +00001239 if (ebt_errormsg[0] != '\0')
1240 return -1;
1241 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001242 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1243 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001244
Bart De Schuymer6622a012005-01-19 21:09:05 +00001245 if (ebt_errormsg[0] != '\0')
1246 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001247 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001248 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001249
Bart De Schuymer6622a012005-01-19 21:09:05 +00001250 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1251 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001252
Bart De Schuymer6622a012005-01-19 21:09:05 +00001253 if (replace->counterchanges)
1254 ebt_deliver_counters(replace, exec_style);
1255 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001256 return 0;
1257}