blob: 6de714fcce39cc086cbac032cc3c71cf6c64b5bb [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 Schuymer6c36d702006-12-22 18:33:18 +000028#include <inttypes.h>
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000029#include "include/ebtables_u.h"
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000030#include "include/ethernetdb.h"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000031
Bart De Schuymer229079b2005-06-18 14:42:21 +000032/* Checks whether a command has already been specified */
Bart De Schuymerab611e22005-02-14 20:20:03 +000033#define OPT_COMMANDS (replace->flags & OPT_COMMAND || replace->flags & OPT_ZERO)
34
35#define OPT_COMMAND 0x01
36#define OPT_TABLE 0x02
37#define OPT_IN 0x04
38#define OPT_OUT 0x08
39#define OPT_JUMP 0x10
40#define OPT_PROTOCOL 0x20
41#define OPT_SOURCE 0x40
42#define OPT_DEST 0x80
43#define OPT_ZERO 0x100
44#define OPT_LOGICALIN 0x200
45#define OPT_LOGICALOUT 0x400
46#define OPT_KERNELDATA 0x800 /* This value is also defined in ebtablesd.c */
47#define OPT_COUNT 0x1000 /* This value is also defined in libebtc.c */
48#define OPT_CNT_INCR 0x2000 /* This value is also defined in libebtc.c */
49#define OPT_CNT_DECR 0x4000 /* This value is also defined in libebtc.c */
50
Bart De Schuymer58ae0402005-02-19 18:21:07 +000051/* Default command line options. Do not mess around with the already
Bart De Schuymer6622a012005-01-19 21:09:05 +000052 * assigned numbers unless you know what you are doing */
Bart De Schuymer62423742002-07-14 19:06:20 +000053static struct option ebt_original_options[] =
54{
Bart De Schuymerab611e22005-02-14 20:20:03 +000055 { "append" , required_argument, 0, 'A' },
56 { "insert" , required_argument, 0, 'I' },
57 { "delete" , required_argument, 0, 'D' },
58 { "list" , optional_argument, 0, 'L' },
59 { "Lc" , no_argument , 0, 4 },
60 { "Ln" , no_argument , 0, 5 },
61 { "Lx" , no_argument , 0, 6 },
62 { "Lmac2" , no_argument , 0, 12 },
63 { "zero" , optional_argument, 0, 'Z' },
64 { "flush" , optional_argument, 0, 'F' },
65 { "policy" , required_argument, 0, 'P' },
66 { "in-interface" , required_argument, 0, 'i' },
67 { "in-if" , required_argument, 0, 'i' },
68 { "logical-in" , required_argument, 0, 2 },
69 { "logical-out" , required_argument, 0, 3 },
70 { "out-interface" , required_argument, 0, 'o' },
71 { "out-if" , required_argument, 0, 'o' },
72 { "version" , no_argument , 0, 'V' },
73 { "help" , no_argument , 0, 'h' },
74 { "jump" , required_argument, 0, 'j' },
75 { "set-counters" , required_argument, 0, 'c' },
76 { "change-counters", required_argument, 0, 'C' },
77 { "proto" , required_argument, 0, 'p' },
78 { "protocol" , required_argument, 0, 'p' },
79 { "db" , required_argument, 0, 'b' },
80 { "source" , required_argument, 0, 's' },
81 { "src" , required_argument, 0, 's' },
82 { "destination" , required_argument, 0, 'd' },
83 { "dst" , required_argument, 0, 'd' },
84 { "table" , required_argument, 0, 't' },
85 { "modprobe" , required_argument, 0, 'M' },
86 { "new-chain" , required_argument, 0, 'N' },
87 { "rename-chain" , required_argument, 0, 'E' },
88 { "delete-chain" , optional_argument, 0, 'X' },
89 { "atomic-init" , no_argument , 0, 7 },
90 { "atomic-commit" , no_argument , 0, 8 },
91 { "atomic-file" , required_argument, 0, 9 },
92 { "atomic-save" , no_argument , 0, 10 },
93 { "init-table" , no_argument , 0, 11 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000094 { 0 }
95};
96
97static struct option *ebt_options = ebt_original_options;
98
Bart De Schuymer6622a012005-01-19 21:09:05 +000099/* Holds all the data */
100static struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000101
Bart De Schuymer6622a012005-01-19 21:09:05 +0000102/* The chosen table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000103static struct ebt_u_table *table;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000104
Bart De Schuymer6622a012005-01-19 21:09:05 +0000105/* The pointers in here are special:
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000106 * The struct ebt_target pointer is actually a struct ebt_u_target pointer.
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000107 * I do not feel like using a union.
Bart De Schuymer229079b2005-06-18 14:42:21 +0000108 * We need a struct ebt_u_target pointer because we know the address of the data
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000109 * they point to won't change. We want to allow that the struct ebt_u_target.t
110 * member can change.
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000111 * The same holds for the struct ebt_match and struct ebt_watcher pointers */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000112static struct ebt_u_entry *new_entry;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000113
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000114
Bart De Schuymer6622a012005-01-19 21:09:05 +0000115static int global_option_offset;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000116#define OPTION_OFFSET 256
Bart De Schuymer229079b2005-06-18 14:42:21 +0000117static struct option *merge_options(struct option *oldopts,
118 const struct option *newopts, unsigned int *options_offset)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000119{
120 unsigned int num_old, num_new, i;
121 struct option *merge;
122
123 if (!newopts || !oldopts || !options_offset)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000124 ebt_print_bug("merge wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000125 for (num_old = 0; oldopts[num_old].name; num_old++);
126 for (num_new = 0; newopts[num_new].name; num_new++);
127
128 global_option_offset += OPTION_OFFSET;
129 *options_offset = global_option_offset;
130
131 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
132 if (!merge)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000133 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000134 memcpy(merge, oldopts, num_old * sizeof(struct option));
135 for (i = 0; i < num_new; i++) {
136 merge[num_old + i] = newopts[i];
137 merge[num_old + i].val += *options_offset;
138 }
139 memset(merge + num_old + num_new, 0, sizeof(struct option));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000140 /* Only free dynamically allocated stuff */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000141 if (oldopts != ebt_original_options)
142 free(oldopts);
143
144 return merge;
145}
146
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000147static void merge_match(struct ebt_u_match *m)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000148{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000149 ebt_options = merge_options
150 (ebt_options, m->extra_ops, &(m->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000151}
152
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000153static void merge_watcher(struct ebt_u_watcher *w)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000154{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000155 ebt_options = merge_options
156 (ebt_options, w->extra_ops, &(w->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000157}
158
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000159static void merge_target(struct ebt_u_target *t)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000160{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000161 ebt_options = merge_options
162 (ebt_options, t->extra_ops, &(t->option_offset));
Bart De Schuymer9a0fbf22003-01-11 16:16:54 +0000163}
164
Bart De Schuymer6622a012005-01-19 21:09:05 +0000165/* Be backwards compatible, so don't use '+' in kernel */
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000166#define IF_WILDCARD 1
167static void print_iface(const char *iface)
168{
169 char *c;
170
171 if ((c = strchr(iface, IF_WILDCARD)))
172 *c = '+';
173 printf("%s ", iface);
174 if (c)
175 *c = IF_WILDCARD;
176}
177
Bart De Schuymer6622a012005-01-19 21:09:05 +0000178/* We use replace->flags, so we can't use the following values:
179 * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO */
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000180#define LIST_N 0x04
181#define LIST_C 0x08
182#define LIST_X 0x10
183#define LIST_MAC2 0x20
184
Bart De Schuymer6622a012005-01-19 21:09:05 +0000185/* Helper function for list_rules() */
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000186static void list_em(struct ebt_u_entries *entries)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000187{
188 int i, j, space = 0, digits;
189 struct ebt_u_entry *hlp;
190 struct ebt_u_match_list *m_l;
191 struct ebt_u_watcher_list *w_l;
192 struct ebt_u_match *m;
193 struct ebt_u_watcher *w;
194 struct ebt_u_target *t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000195
Bart De Schuymer6622a012005-01-19 21:09:05 +0000196 if (replace->flags & LIST_MAC2)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000197 ebt_printstyle_mac = 2;
Bart De Schuymer229079b2005-06-18 14:42:21 +0000198 else
199 ebt_printstyle_mac = 0;
Bart De Schuymere94eaf72005-08-28 16:06:22 +0000200 hlp = entries->entries->next;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000201 if (replace->flags & LIST_X && entries->policy != EBT_ACCEPT) {
202 printf("ebtables -t %s -P %s %s\n", replace->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000203 entries->name, ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000204 } else if (!(replace->flags & LIST_X)) {
Bart De Schuymerc87c9642002-08-01 15:34:16 +0000205 printf("\nBridge chain: %s, entries: %d, policy: %s\n",
206 entries->name, entries->nentries,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000207 ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000208 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000209
Bart De Schuymer229079b2005-06-18 14:42:21 +0000210 if (replace->flags & LIST_N) {
211 i = entries->nentries;
212 while (i > 9) {
213 space++;
214 i /= 10;
215 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000216 }
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 Schuymer58ae0402005-02-19 18:21:07 +0000250 ent = getethertypebynumber(ntohs(hlp->ethproto));
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000251 if (!ent)
Bart De Schuymer60332e02002-06-23 08:01:47 +0000252 printf("0x%x ", ntohs(hlp->ethproto));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000253 else
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000254 printf("%s ", ent->e_name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000255 }
256 }
257 if (hlp->bitmask & EBT_SOURCEMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000258 printf("-s ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000259 if (hlp->invflags & EBT_ISOURCE)
260 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000261 ebt_print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000262 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000263 }
264 if (hlp->bitmask & EBT_DESTMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000265 printf("-d ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000266 if (hlp->invflags & EBT_IDEST)
267 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000268 ebt_print_mac_and_mask(hlp->destmac, hlp->destmsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000269 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000270 }
271 if (hlp->in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000272 printf("-i ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000273 if (hlp->invflags & EBT_IIN)
274 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000275 print_iface(hlp->in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000276 }
277 if (hlp->logical_in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000278 printf("--logical-in ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000279 if (hlp->invflags & EBT_ILOGICALIN)
280 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000281 print_iface(hlp->logical_in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000282 }
283 if (hlp->logical_out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000284 printf("--logical-out ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000285 if (hlp->invflags & EBT_ILOGICALOUT)
286 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000287 print_iface(hlp->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000288 }
289 if (hlp->out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000290 printf("-o ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000291 if (hlp->invflags & EBT_IOUT)
292 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000293 print_iface(hlp->out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000294 }
295
296 m_l = hlp->m_list;
297 while (m_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000298 m = ebt_find_match(m_l->m->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000299 if (!m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000300 ebt_print_bug("Match not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000301 m->print(hlp, m_l->m);
302 m_l = m_l->next;
303 }
304 w_l = hlp->w_list;
305 while (w_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000306 w = ebt_find_watcher(w_l->w->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000307 if (!w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000308 ebt_print_bug("Watcher not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000309 w->print(hlp, w_l->w);
310 w_l = w_l->next;
311 }
312
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000313 printf("-j ");
314 if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET))
315 printf("%s ", hlp->t->u.name);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000316 t = ebt_find_target(hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000317 if (!t)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000318 ebt_print_bug("Target '%s' not found", hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000319 t->print(hlp, hlp->t);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000320 if (replace->flags & LIST_C) {
Bart De Schuymerab611e22005-02-14 20:20:03 +0000321 uint64_t pcnt = hlp->cnt.pcnt;
322 uint64_t bcnt = hlp->cnt.bcnt;
323
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000324 if (replace->flags & LIST_X)
Bart De Schuymerab611e22005-02-14 20:20:03 +0000325 printf("-c %llu %llu", pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000326 else
Bart De Schuymer6c36d702006-12-22 18:33:18 +0000327 printf(", pcnt = %"PRIu64" -- bcnt = %"PRIu64, pcnt, bcnt);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000328 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000329 printf("\n");
330 hlp = hlp->next;
331 }
332}
333
Bart De Schuymer62423742002-07-14 19:06:20 +0000334static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000335{
336 struct ebt_u_match_list *m_l;
337 struct ebt_u_watcher_list *w_l;
338
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000339 PRINT_VERSION;
340 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000341"Usage:\n"
342"ebtables -[ADI] chain rule-specification [options]\n"
343"ebtables -P chain target\n"
344"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000345"ebtables -[NX] [chain]\n"
346"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000347"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000348"--append -A chain : append to chain\n"
349"--delete -D chain : delete matching rule from chain\n"
350"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000351"--change-counters -C chain\n"
352" [rulenum] pcnt bcnt : change counters of existing rule\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000353"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000354"--list -L [chain] : list the rules in a chain or in all chains\n"
355"--flush -F [chain] : delete all rules in chain or in all chains\n"
356"--init-table : replace the kernel table with the initial table\n"
357"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
358"--policy -P chain target : change policy on chain to target\n"
359"--new-chain -N chain : create a user defined chain\n"
360"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000361"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000362"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
363"--atomic-init : put the initial kernel table into <FILE>\n"
364"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000365"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000366"Options:\n"
367"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
368"--src -s [!] address[/mask]: source mac address\n"
369"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000370"--in-if -i [!] name[+] : network input interface name\n"
371"--out-if -o [!] name[+] : network output interface name\n"
372"--logical-in [!] name[+] : logical bridge input interface name\n"
373"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymerab611e22005-02-14 20:20:03 +0000374"--set-counters -c chain\n"
375" pcnt bcnt : set the counters of the to be added rule\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000376"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000377"--version -V : print package version\n\n"
378"Environment variable:\n"
379ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
380"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000381 m_l = new_entry->m_list;
382 while (m_l) {
383 ((struct ebt_u_match *)m_l->m)->help();
384 printf("\n");
385 m_l = m_l->next;
386 }
387 w_l = new_entry->w_list;
388 while (w_l) {
389 ((struct ebt_u_watcher *)w_l->w)->help();
390 printf("\n");
391 w_l = w_l->next;
392 }
393 ((struct ebt_u_target *)new_entry->t)->help();
394 printf("\n");
395 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000396 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000397}
398
Bart De Schuymer6622a012005-01-19 21:09:05 +0000399/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000400static void list_rules()
401{
Bart De Schuymer229079b2005-06-18 14:42:21 +0000402 int i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000403
Bart De Schuymer6622a012005-01-19 21:09:05 +0000404 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000405 printf("Bridge table: %s\n", table->name);
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000406 if (replace->selected_chain != -1)
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000407 list_em(ebt_to_chain(replace));
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000408 else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000409 /* Create new chains and rename standard chains when necessary */
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000410 if (replace->flags & LIST_X && replace->num_chains > NF_BR_NUMHOOKS) {
411 for (i = NF_BR_NUMHOOKS; i < replace->num_chains; i++)
412 printf("ebtables -t %s -N %s\n", replace->name, replace->chains[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000413 for (i = 0; i < NF_BR_NUMHOOKS; i++)
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000414 if (replace->chains[i] && strcmp(replace->chains[i]->name, ebt_hooknames[i]))
415 printf("ebtables -t %s -E %s %s\n", replace->name, ebt_hooknames[i], replace->chains[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000416 }
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +0000417 for (i = 0; i < replace->num_chains; i++)
418 if (replace->chains[i])
Bart De Schuymer482fbaf2005-08-28 13:16:25 +0000419 list_em(replace->chains[i]);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000420 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000421}
422
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000423static int parse_rule_range(const char *argv, int *rule_nr, int *rule_nr_end)
Bart De Schuymercc440052002-11-06 21:10:33 +0000424{
425 char *colon = strchr(argv, ':'), *buffer;
426
427 if (colon) {
428 *colon = '\0';
429 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000430 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000431 else {
432 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000433 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000434 return -1;
435 }
436 }
437 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000438 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000439 else {
440 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000441 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000442 return -1;
443 }
444 if (!colon)
445 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000446 return 0;
447}
448
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000449/* Incrementing or decrementing rules in daemon mode is not supported as the
Bart De Schuymer229079b2005-06-18 14:42:21 +0000450 * involved code overload is not worth it (too annoying to take the increased
451 * counters in the kernel into account). */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000452static 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 +0000453{
454 char *buffer;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000455 int ret = 0;
Bart De Schuymerff587202005-02-08 20:02:28 +0000456
457 if (optind + 1 >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')) ||
458 (argv[optind + 1][0] == '-' && (argv[optind + 1][1] < '0' && argv[optind + 1][1] > '9')))
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000459 ebt_print_error2("The command -C needs at least 2 arguments");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000460 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 +0000461 if (optind + 3 != argc)
462 ebt_print_error2("No extra options allowed with -C start_nr[:end_nr] pcnt bcnt");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000463 if (parse_rule_range(argv[optind], rule_nr, rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000464 ebt_print_error2("Something is wrong with the rule number specification '%s'", argv[optind]);
465 optind++;
466 }
467
Bart De Schuymerab611e22005-02-14 20:20:03 +0000468 if (argv[optind][0] == '+') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000469 if (exec_style == EXEC_STYLE_DAEMON)
470daemon_incr:
471 ebt_print_error2("Incrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
472 ret += 1;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000473 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymerab611e22005-02-14 20:20:03 +0000474 } else if (argv[optind][0] == '-') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000475 if (exec_style == EXEC_STYLE_DAEMON)
476daemon_decr:
477 ebt_print_error2("Decrementing rule counters (%s) not allowed in daemon mode", argv[optind]);
478 ret += 2;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000479 new_entry->cnt_surplus.pcnt = strtoull(argv[optind] + 1, &buffer, 10);
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000480 } else
Bart De Schuymerab611e22005-02-14 20:20:03 +0000481 new_entry->cnt_surplus.pcnt = strtoull(argv[optind], &buffer, 10);
Bart De Schuymerff587202005-02-08 20:02:28 +0000482
Bart De Schuymerab611e22005-02-14 20:20:03 +0000483 if (*buffer != '\0')
484 goto invalid;
Bart De Schuymerff587202005-02-08 20:02:28 +0000485 optind++;
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000486 if (argv[optind][0] == '+') {
487 if (exec_style == EXEC_STYLE_DAEMON)
488 goto daemon_incr;
489 ret += 3;
490 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
491 } else if (argv[optind][0] == '-') {
492 if (exec_style == EXEC_STYLE_DAEMON)
493 goto daemon_decr;
494 ret += 6;
495 new_entry->cnt_surplus.bcnt = strtoull(argv[optind] + 1, &buffer, 10);
496 } else
497 new_entry->cnt_surplus.bcnt = strtoull(argv[optind], &buffer, 10);
498
499 if (*buffer != '\0')
500 goto invalid;
501 optind++;
502 return ret;
Bart De Schuymerab611e22005-02-14 20:20:03 +0000503invalid:
504 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerff587202005-02-08 20:02:28 +0000505}
506
Bart De Schuymer6622a012005-01-19 21:09:05 +0000507static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000508{
509 char *c;
510
511 if ((c = strchr(iface, '+'))) {
512 if (*(c + 1) != '\0') {
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000513 ebt_print_error("Spurious characters after '+' wildcard for '%s'", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000514 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000515 } else
516 *c = IF_WILDCARD;
517 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000518 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000519}
520
Bart De Schuymer6622a012005-01-19 21:09:05 +0000521void ebt_early_init_once()
522{
523 ebt_iterate_matches(merge_match);
524 ebt_iterate_watchers(merge_watcher);
525 ebt_iterate_targets(merge_target);
526}
527
Bart De Schuymer229079b2005-06-18 14:42:21 +0000528/* We use exec_style instead of #ifdef's because ebtables.so is a shared object. */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000529int do_command(int argc, char *argv[], int exec_style,
530 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000531{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000532 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000533 int c, i;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000534 int zerochain = -1; /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000535 int chcounter; /* Needed for -C */
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000536 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000537 int rule_nr = 0;
538 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000539 struct ebt_u_target *t;
540 struct ebt_u_match *m;
541 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000542 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000543 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000544 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000545
Bart De Schuymera615b962002-11-03 14:54:09 +0000546 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000547 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000548
Bart De Schuymer6622a012005-01-19 21:09:05 +0000549 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000550
Bart De Schuymer6622a012005-01-19 21:09:05 +0000551 /* The daemon doesn't use the environment variable */
552 if (exec_style == EXEC_STYLE_PRG) {
553 buffer = getenv(ATOMIC_ENV_VARIABLE);
554 if (buffer) {
555 replace->filename = malloc(strlen(buffer) + 1);
556 if (!replace->filename)
557 ebt_print_memory();
558 strcpy(replace->filename, buffer);
559 buffer = NULL;
560 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000561 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000562
Bart De Schuymer6622a012005-01-19 21:09:05 +0000563 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
564 replace->selected_chain = -1;
565 replace->command = 'h';
566
567 if (!new_entry) {
568 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
569 if (!new_entry)
570 ebt_print_memory();
571 }
572 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000573 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000574 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000575
Bart De Schuymer6622a012005-01-19 21:09:05 +0000576 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000577 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000578 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000579
Bart De Schuymer6622a012005-01-19 21:09:05 +0000580 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000581 while ((c = getopt_long(argc, argv,
Bart De Schuymerff587202005-02-08 20:02:28 +0000582 "-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 +0000583 switch (c) {
584
Bart De Schuymer6622a012005-01-19 21:09:05 +0000585 case 'A': /* Add a rule */
586 case 'D': /* Delete a rule */
Bart De Schuymerff587202005-02-08 20:02:28 +0000587 case 'C': /* Change counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000588 case 'P': /* Define policy */
589 case 'I': /* Insert a rule */
590 case 'N': /* Make a user defined chain */
591 case 'E': /* Rename chain */
592 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000593 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000594 if (replace->command == 'N' && c == 'P') {
595 replace->command = c;
596 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000597 goto handle_P;
598 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000599 if (OPT_COMMANDS)
600 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000601
Bart De Schuymer6622a012005-01-19 21:09:05 +0000602 replace->command = c;
603 replace->flags |= OPT_COMMAND;
604 if (!(replace->flags & OPT_KERNELDATA))
605 ebt_get_kernel_table(replace, 0);
606 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
607 ebt_print_error2("No chain name specified");
608 if (c == 'N') {
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000609 if (ebt_get_chainnr(replace, optarg) != -1)
610 ebt_print_error2("Chain %s already exists", optarg);
611 else if (ebt_find_target(optarg))
612 ebt_print_error2("Target with name %s exists", optarg);
613 else if (strlen(optarg) >= EBT_CHAIN_MAXNAMELEN)
614 ebt_print_error2("Chain name length can't exceed %d",
615 EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000616 else if (strchr(optarg, ' ') != NULL)
617 ebt_print_error2("Use of ' ' not allowed in chain names");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000618 ebt_new_chain(replace, optarg, EBT_ACCEPT);
619 /* This is needed to get -N x -P y working */
620 replace->selected_chain = ebt_get_chainnr(replace, optarg);
621 break;
622 } else if (c == 'X') {
623 if (optind >= argc) {
624 replace->selected_chain = -1;
625 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000626 break;
627 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000628
629 if (optind < argc - 1)
630 ebt_print_error2("No extra options allowed with -X");
631
632 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000633 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000634 ebt_delete_chain(replace);
635 if (ebt_errormsg[0] != '\0')
636 return -1;
637 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000638 break;
639 }
640
Bart De Schuymer6622a012005-01-19 21:09:05 +0000641 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000642 ebt_print_error2("Chain '%s' doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000643 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 if (optind >= argc)
645 ebt_print_error2("No new chain name specified");
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000646 else if (optind < argc - 1)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000647 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000648 else if (strlen(argv[optind]) >= EBT_CHAIN_MAXNAMELEN)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000649 ebt_print_error2("Chain name length can't exceed %d characters", EBT_CHAIN_MAXNAMELEN - 1);
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000650 else if (ebt_get_chainnr(replace, argv[optind]) != -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000651 ebt_print_error2("Chain '%s' already exists", argv[optind]);
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000652 else if (ebt_find_target(argv[optind]))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000653 ebt_print_error2("Target with name '%s' exists", argv[optind]);
Bart De Schuymer472e3f82005-11-11 21:05:57 +0000654 else if (strchr(argv[optind], ' ') != NULL)
655 ebt_print_error2("Use of ' ' not allowed in chain names");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000656 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000657 optind++;
658 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000659 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
660 if (optind != argc - 1)
661 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000662 if (parse_rule_range(argv[optind], &rule_nr, &rule_nr_end))
Bart De Schuymerff587202005-02-08 20:02:28 +0000663 ebt_print_error2("Problem with the specified rule number(s) '%s'", argv[optind]);
Bart De Schuymercc440052002-11-06 21:10:33 +0000664 optind++;
Bart De Schuymerff587202005-02-08 20:02:28 +0000665 } else if (c == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +0000666 if ((chcounter = parse_change_counters_rule(argc, argv, &rule_nr, &rule_nr_end, exec_style)) == -1)
Bart De Schuymerff587202005-02-08 20:02:28 +0000667 return -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000668 } else if (c == 'I') {
669 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
Bart De Schuymer01581232005-07-24 09:46:09 +0000670 rule_nr = 1;
671 else {
672 rule_nr = strtol(argv[optind], &buffer, 10);
673 if (*buffer != '\0')
674 ebt_print_error2("Problem with the specified rule number '%s'", argv[optind]);
675 optind++;
676 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000677 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000678handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000679 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000680 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000681 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000682 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000683 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000684 if (policy == EBT_CONTINUE)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000685 ebt_print_error2("Wrong policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000686 break;
687 }
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000688 if (i == NUM_STANDARD_TARGETS)
689 ebt_print_error2("Unknown policy '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000690 optind++;
691 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000692 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000693 case 'L': /* List */
694 case 'F': /* Flush */
695 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000696 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000697 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
698print_zero:
699 ebt_print_error2("Command -Z only allowed together with command -L");
700 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000701 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000702 if (replace->flags & OPT_COMMAND)
703 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000704 replace->command = c;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000705 replace->flags |= OPT_COMMAND;
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000706 if (replace->flags & OPT_ZERO && c != 'L')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000707 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000708 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000709
710#ifdef SILENT_DAEMON
711 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000712 ebt_print_error2("-L not supported in daemon mode");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000713#endif
714
715 if (!(replace->flags & OPT_KERNELDATA))
716 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000717 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000718 if (optind < argc && argv[optind][0] != '-') {
719 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000720 ebt_print_error2("Chain '%s' doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000721 optind++;
722 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000723 if (i != -1) {
724 if (c == 'Z')
725 zerochain = i;
726 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000727 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000728 }
729 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000730 case 'V': /* Version */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000731 if (OPT_COMMANDS)
732 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000733 replace->command = 'V';
Bart De Schuymer6622a012005-01-19 21:09:05 +0000734 if (exec_style == EXEC_STYLE_DAEMON)
735 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000736 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000737 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000738 case 'M': /* Modprobe */
739 if (OPT_COMMANDS)
740 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymerd5dc87d2005-07-16 22:35:32 +0000741 free(ebt_modprobe);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000742 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000743 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000744 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000745#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000746 if (exec_style == EXEC_STYLE_DAEMON)
747 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000748#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000749 if (OPT_COMMANDS)
750 ebt_print_error2("Multiple commands are not allowed");
751 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000752
Bart De Schuymer6622a012005-01-19 21:09:05 +0000753 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000754 while (optind < argc) {
755 struct ebt_u_match *m;
756 struct ebt_u_watcher *w;
757
Bart De Schuymer6622a012005-01-19 21:09:05 +0000758 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000759 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000760 exit(0);
761 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000762 if ((m = ebt_find_match(argv[optind])))
763 ebt_add_match(new_entry, m);
764 else if ((w = ebt_find_watcher(argv[optind])))
765 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000766 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000767 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000768 ebt_print_error2("Extension '%s' not found", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000769 if (replace->flags & OPT_JUMP)
770 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
771 replace->flags |= OPT_JUMP;
772 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000773 }
774 optind++;
775 }
776 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000777 case 't': /* Table */
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000778 if (OPT_COMMANDS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000779 ebt_print_error2("Please put the -t option first");
780 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000781 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000782 ebt_print_error2("Table name length cannot exceed %d characters", EBT_TABLE_MAXNAMELEN - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000783 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000784 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000785 case 'i': /* Input interface */
786 case 2 : /* Logical input interface */
787 case 'o': /* Output interface */
788 case 3 : /* Logical output interface */
789 case 'j': /* Target */
790 case 'p': /* Net family protocol */
791 case 's': /* Source mac */
792 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000793 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000794 if (!OPT_COMMANDS)
795 ebt_print_error2("No command specified");
Bart De Schuymerab611e22005-02-14 20:20:03 +0000796 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I' && replace->command != 'C')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000797 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000798 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000799 ebt_check_option2(&(replace->flags), OPT_IN);
800 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
801 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
802 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000803 new_entry->invflags |= EBT_IIN;
804
Bart De Schuymer6622a012005-01-19 21:09:05 +0000805 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000806big_iface_length:
807 ebt_print_error2("Interface name length cannot exceed %d characters", IFNAMSIZ - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000808 strcpy(new_entry->in, optarg);
809 if (parse_iface(new_entry->in, "-i"))
810 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000811 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000812 } else if (c == 2) {
813 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
814 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
815 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
816 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000817 new_entry->invflags |= EBT_ILOGICALIN;
818
Bart De Schuymer6622a012005-01-19 21:09:05 +0000819 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000820 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000821 strcpy(new_entry->logical_in, optarg);
822 if (parse_iface(new_entry->logical_in, "--logical-in"))
823 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000824 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000825 } else if (c == 'o') {
826 ebt_check_option2(&(replace->flags), OPT_OUT);
827 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
828 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
829 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000830 new_entry->invflags |= EBT_IOUT;
831
Bart De Schuymer6622a012005-01-19 21:09:05 +0000832 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000833 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000834 strcpy(new_entry->out, optarg);
835 if (parse_iface(new_entry->out, "-o"))
836 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000837 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000838 } else if (c == 3) {
839 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000840 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000841 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
842 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000843 new_entry->invflags |= EBT_ILOGICALOUT;
844
Bart De Schuymer6622a012005-01-19 21:09:05 +0000845 if (strlen(optarg) >= IFNAMSIZ)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000846 goto big_iface_length;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000847 strcpy(new_entry->logical_out, optarg);
848 if (parse_iface(new_entry->logical_out, "--logical-out"))
849 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000850 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000851 } else if (c == 'j') {
852 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000853 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000854 if (!strcmp(optarg, ebt_standard_targets[i])) {
855 t = ebt_find_target(EBT_STANDARD_TARGET);
856 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000857 break;
858 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000859 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
860 ebt_print_error2("Return target only for user defined chains");
861 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000862 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000863
864 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000865 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000866 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000867 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000868 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000869 break;
870 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000871 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000872 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000873
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000874 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000875 /* -j standard not allowed either */
876 if (!t || t == (struct ebt_u_target *)new_entry->t)
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000877 ebt_print_error2("Illegal target name '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000878 new_entry->t = (struct ebt_entry_target *)t;
879 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
880 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000881 }
882 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000883 } else if (c == 's') {
884 ebt_check_option2(&(replace->flags), OPT_SOURCE);
885 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000886 new_entry->invflags |= EBT_ISOURCE;
887
Bart De Schuymer6622a012005-01-19 21:09:05 +0000888 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000889 ebt_print_error2("Problem with specified source mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000890 new_entry->bitmask |= EBT_SOURCEMAC;
891 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000892 } else if (c == 'd') {
893 ebt_check_option2(&(replace->flags), OPT_DEST);
894 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000895 new_entry->invflags |= EBT_IDEST;
896
Bart De Schuymer6622a012005-01-19 21:09:05 +0000897 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000898 ebt_print_error2("Problem with specified destination mac '%s'", optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000899 new_entry->bitmask |= EBT_DESTMAC;
900 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000901 } else if (c == 'c') {
902 ebt_check_option2(&(replace->flags), OPT_COUNT);
903 if (ebt_check_inverse2(optarg))
904 ebt_print_error2("Unexpected '!' after -c");
Bart De Schuymer58ae0402005-02-19 18:21:07 +0000905 if (optind >= argc || optarg[0] == '-' || argv[optind][0] == '-')
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000906 ebt_print_error2("Option -c needs 2 arguments");
907
908 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
909 if (*buffer != '\0')
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000910 ebt_print_error2("Packet counter '%s' invalid", optarg);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000911 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
912 if (*buffer != '\0')
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000913 ebt_print_error2("Packet counter '%s' invalid", argv[optind]);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000914 optind++;
915 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000916 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000917 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
918 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000919 new_entry->invflags |= EBT_IPROTO;
920
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000921 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000922 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000923 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000924 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000925 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000926 struct ethertypeent *ent;
927
Bart De Schuymer6622a012005-01-19 21:09:05 +0000928 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000929 new_entry->bitmask |= EBT_802_3;
930 break;
931 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000932 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000933 if (!ent)
Bart De Schuymercdc2cd02005-10-01 20:12:50 +0000934 ebt_print_error2("Problem with the specified Ethernet protocol '%s', perhaps "_PATH_ETHERTYPES " is missing", optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000935 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000936 } else
937 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000938
Bart De Schuymer6622a012005-01-19 21:09:05 +0000939 if (new_entry->ethproto < 0x0600)
940 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
941 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000942 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000943#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000944 if (exec_style == EXEC_STYLE_DAEMON)
945 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000946#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000947 ebt_check_option2(&(replace->flags), LIST_C);
948 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000949 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000950 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000951 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000952 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000953#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000954 if (exec_style == EXEC_STYLE_DAEMON)
955 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000956#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000957 ebt_check_option2(&(replace->flags), LIST_N);
958 if (replace->command != 'L')
959 ebt_print_error2("Use --Ln with -L");
960 if (replace->flags & LIST_X)
961 ebt_print_error2("--Lx is not compatible with --Ln");
962 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000963 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000964 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +0000965#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000966 if (exec_style == EXEC_STYLE_DAEMON)
967 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000968#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000969 ebt_check_option2(&(replace->flags), LIST_X);
970 if (replace->command != 'L')
971 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000972 if (replace->flags & LIST_N)
973 ebt_print_error2("--Lx is not compatible with --Ln");
974 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000975 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000976 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +0000977#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000978 if (exec_style == EXEC_STYLE_DAEMON)
979 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000980#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000981 ebt_check_option2(&(replace->flags), LIST_MAC2);
982 if (replace->command != 'L')
983 ebt_print_error2("Use --Lmac2 with -L");
984 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000985 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000986 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000987 if (exec_style == EXEC_STYLE_DAEMON)
988 ebt_print_error2("--atomic-commit is not supported in daemon mode");
989 replace->command = c;
990 if (OPT_COMMANDS)
991 ebt_print_error2("Multiple commands are not allowed");
992 replace->flags |= OPT_COMMAND;
993 if (!replace->filename)
994 ebt_print_error2("No atomic file specified");
995 /* Get the information from the file */
996 ebt_get_table(replace, 0);
997 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000998 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +0000999 * the file */
1000 replace->num_counters = 0;
1001 /* Make sure the table will be written to the kernel */
1002 free(replace->filename);
1003 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +00001004 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001005 case 7 : /* atomic-init */
1006 case 10: /* atomic-save */
1007 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001008 if (exec_style == EXEC_STYLE_DAEMON) {
1009 if (c == 7) {
1010 ebt_print_error2("--atomic-init is not supported in daemon mode");
1011 } else if (c == 10)
1012 ebt_print_error2("--atomic-save is not supported in daemon mode");
1013 ebt_print_error2("--init-table is not supported in daemon mode");
1014 }
1015 replace->command = c;
1016 if (OPT_COMMANDS)
1017 ebt_print_error2("Multiple commands are not allowed");
1018 if (c != 11 && !replace->filename)
1019 ebt_print_error2("No atomic file specified");
1020 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001021 {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001022 char *tmp = replace->filename;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001023
Bart De Schuymer6622a012005-01-19 21:09:05 +00001024 /* Get the kernel table */
1025 replace->filename = NULL;
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001026 ebt_get_kernel_table(replace, c == 10 ? 0 : 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001027 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +00001028 }
Bart De Schuymer5885b362002-12-03 20:51:36 +00001029 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +00001030 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +00001031 if (exec_style == EXEC_STYLE_DAEMON)
1032 ebt_print_error2("--atomic is not supported in daemon mode");
1033 if (OPT_COMMANDS)
1034 ebt_print_error2("--atomic has to come before the command");
1035 /* A possible memory leak here, but this is not
1036 * executed in daemon mode */
1037 replace->filename = (char *)malloc(strlen(optarg) + 1);
1038 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +00001039 break;
Bart De Schuymera615b962002-11-03 14:54:09 +00001040 case 1 :
1041 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +00001042 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +00001043 else
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001044 ebt_print_error2("Bad argument : '%s'", optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001045 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +00001046 optind--;
1047 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +00001048 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001049 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001050 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymerff587202005-02-08 20:02:28 +00001051 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t))) {
1052 if (ebt_errormsg[0] != '\0')
1053 return -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001054 goto check_extension;
Bart De Schuymerff587202005-02-08 20:02:28 +00001055 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001056
Bart De Schuymer6622a012005-01-19 21:09:05 +00001057 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001058 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001059 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001060 break;
1061
1062 if (m != NULL) {
Bart De Schuymerff587202005-02-08 20:02:28 +00001063 if (ebt_errormsg[0] != '\0')
1064 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001065 if (m->used == 0) {
1066 ebt_add_match(new_entry, m);
1067 m->used = 1;
1068 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001069 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001070 }
1071
Bart De Schuymer6622a012005-01-19 21:09:05 +00001072 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001073 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001074 if (w->parse(c - w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001075 break;
1076
Bart De Schuymer1c162af2006-04-11 18:22:37 +00001077 if (w == NULL && c == '?')
1078 ebt_print_error2("Unknown argument: '%s'", argv[optind - 1], (char)optopt, (char)c);
1079 else if (w == NULL) {
1080 if (!strcmp(t->name, "standard"))
1081 ebt_print_error2("Unknown argument: don't forget the -t option");
1082 else
1083 ebt_print_error2("Target-specific option does not correspond with specified target");
1084 }
Bart De Schuymerff587202005-02-08 20:02:28 +00001085 if (ebt_errormsg[0] != '\0')
1086 return -1;
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001087 if (w->used == 0) {
1088 ebt_add_watcher(new_entry, w);
1089 w->used = 1;
1090 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001091check_extension:
Bart De Schuymer58ae0402005-02-19 18:21:07 +00001092 if (replace->command != 'A' && replace->command != 'I' &&
1093 replace->command != 'D' && replace->command != 'C')
1094 ebt_print_error2("Extensions only for -A, -I, -D and -C");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001095 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001096 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001097 }
1098
Bart De Schuymer6622a012005-01-19 21:09:05 +00001099 /* Just in case we didn't catch an error */
1100 if (ebt_errormsg[0] != '\0')
1101 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001102
Bart De Schuymer6622a012005-01-19 21:09:05 +00001103 if (!(table = ebt_find_table(replace->name)))
1104 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001105
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001106 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001107 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001108 if (exec_style == EXEC_STYLE_PRG)
1109 exit(0);
1110 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001111
Bart De Schuymer6622a012005-01-19 21:09:05 +00001112 /* Do the final checks */
1113 if (replace->command == 'A' || replace->command == 'I' ||
Bart De Schuymerff587202005-02-08 20:02:28 +00001114 replace->command == 'D' || replace->command == 'C') {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001115 /* This will put the hook_mask right for the chains */
1116 ebt_check_for_loops(replace);
1117 if (ebt_errormsg[0] != '\0')
1118 return -1;
1119 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001120 m_l = new_entry->m_list;
1121 w_l = new_entry->w_list;
1122 t = (struct ebt_u_target *)new_entry->t;
1123 while (m_l) {
1124 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001125 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001126 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001127 if (ebt_errormsg[0] != '\0')
1128 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001129 m_l = m_l->next;
1130 }
1131 while (w_l) {
1132 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001133 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001134 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001135 if (ebt_errormsg[0] != '\0')
1136 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001137 w_l = w_l->next;
1138 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001139 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001140 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001141 if (ebt_errormsg[0] != '\0')
1142 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001143 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001144 /* So, the extensions can work with the host endian.
1145 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001146 new_entry->ethproto = htons(new_entry->ethproto);
1147
Bart De Schuymer6622a012005-01-19 21:09:05 +00001148 if (replace->command == 'P') {
1149 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1150 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1151 ebt_change_policy(replace, policy);
1152 if (ebt_errormsg[0] != '\0')
1153 return -1;
1154 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001155 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001156 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001157 exit(0);
1158 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001159 if (replace->flags & OPT_ZERO) {
1160 replace->selected_chain = zerochain;
1161 ebt_zero_counters(replace);
1162 } else if (replace->command == 'F') {
1163 ebt_flush_chains(replace);
1164 } else if (replace->command == 'A' || replace->command == 'I') {
1165 ebt_add_rule(replace, new_entry, rule_nr);
1166 if (ebt_errormsg[0] != '\0')
1167 return -1;
1168 /* Makes undoing the add easier (jumps to delete_the_rule) */
1169 if (rule_nr <= 0)
1170 rule_nr--;
1171 rule_nr_end = rule_nr;
1172
Bart De Schuymer24816d32007-02-11 12:47:58 +00001173 /* a jump to a udc requires checking for loops */
1174 if (!strcmp(new_entry->t->u.name, EBT_STANDARD_TARGET) &&
1175 ((struct ebt_standard_target *)(new_entry->t))->verdict >= 0) {
1176 /* FIXME: this can be done faster */
1177 ebt_check_for_loops(replace);
1178 if (ebt_errormsg[0] != '\0')
1179 goto delete_the_rule;
1180 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001181
1182 /* Do the final_check(), for all entries.
1183 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001184 i = -1;
Bart De Schuymer92306862006-11-10 20:33:25 +00001185 while (++i != replace->num_chains) {
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001186 struct ebt_u_entry *e;
1187
Bart De Schuymer9bfcfd82005-08-27 16:52:19 +00001188 entries = replace->chains[i];
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001189 if (!entries) {
1190 if (i < NF_BR_NUMHOOKS)
1191 continue;
1192 else
Bart De Schuymer92306862006-11-10 20:33:25 +00001193 ebt_print_bug("whoops\n");
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001194 }
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001195 e = entries->entries->next;
1196 while (e != entries->entries) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001197 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001198 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001199 ebt_do_final_checks(replace, e, entries);
1200 if (ebt_errormsg[0] != '\0')
1201 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001202 e->ethproto = htons(e->ethproto);
1203 e = e->next;
1204 }
1205 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001206 /* Don't reuse the added rule */
1207 new_entry = NULL;
Bart De Schuymerff587202005-02-08 20:02:28 +00001208 } else if (replace->command == 'D') {
1209delete_the_rule:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001210 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
Bart De Schuymerff587202005-02-08 20:02:28 +00001211 if (ebt_errormsg[0] != '\0')
1212 return -1;
1213 } else if (replace->command == 'C') {
Bart De Schuymer0436eda2005-03-28 20:29:37 +00001214 ebt_change_counters(replace, new_entry, rule_nr, rule_nr_end, &(new_entry->cnt_surplus), chcounter);
Bart De Schuymerff587202005-02-08 20:02:28 +00001215 if (ebt_errormsg[0] != '\0')
1216 return -1;
1217 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001218 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1219 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001220
Bart De Schuymer6622a012005-01-19 21:09:05 +00001221 if (ebt_errormsg[0] != '\0')
1222 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001223 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001224 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001225
Bart De Schuymer6622a012005-01-19 21:09:05 +00001226 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1227 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001228
Bart De Schuymere94eaf72005-08-28 16:06:22 +00001229 if (replace->nentries)
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +00001230 ebt_deliver_counters(replace);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001231 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001232 return 0;
1233}