blob: 8723e66c7f614a1fc809651ab71bc228d526fec0 [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 Schuymer6622a012005-01-19 21:09:05 +000036/* dDfault command line options. Do not mess around with the already
37 * assigned numbers unless you know what you are doing */
Bart De Schuymer62423742002-07-14 19:06:20 +000038static struct option ebt_original_options[] =
39{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000040 { "append" , required_argument, 0, 'A' },
41 { "insert" , required_argument, 0, 'I' },
42 { "delete" , required_argument, 0, 'D' },
43 { "list" , optional_argument, 0, 'L' },
Bart De Schuymer9af14f92002-07-10 20:49:10 +000044 { "Lc" , no_argument , 0, 4 },
45 { "Ln" , no_argument , 0, 5 },
46 { "Lx" , no_argument , 0, 6 },
Bart De Schuymer22d03a22003-05-03 20:28:22 +000047 { "Lmac2" , no_argument , 0, 12 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000048 { "zero" , optional_argument, 0, 'Z' },
49 { "flush" , optional_argument, 0, 'F' },
50 { "policy" , required_argument, 0, 'P' },
51 { "in-interface" , required_argument, 0, 'i' },
52 { "in-if" , required_argument, 0, 'i' },
53 { "logical-in" , required_argument, 0, 2 },
54 { "logical-out" , required_argument, 0, 3 },
55 { "out-interface" , required_argument, 0, 'o' },
56 { "out-if" , required_argument, 0, 'o' },
57 { "version" , no_argument , 0, 'V' },
58 { "help" , no_argument , 0, 'h' },
59 { "jump" , required_argument, 0, 'j' },
Bart De Schuymerb5917d82005-01-25 21:15:59 +000060 { "set-counter" , required_argument, 0, 'c' },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000061 { "proto" , required_argument, 0, 'p' },
62 { "protocol" , required_argument, 0, 'p' },
63 { "db" , required_argument, 0, 'b' },
64 { "source" , required_argument, 0, 's' },
65 { "src" , required_argument, 0, 's' },
66 { "destination" , required_argument, 0, 'd' },
67 { "dst" , required_argument, 0, 'd' },
68 { "table" , required_argument, 0, 't' },
Bart De Schuymerc8531032002-06-14 21:55:29 +000069 { "modprobe" , required_argument, 0, 'M' },
Bart De Schuymer1ab41562002-06-23 17:09:54 +000070 { "new-chain" , required_argument, 0, 'N' },
71 { "rename-chain" , required_argument, 0, 'E' },
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +000072 { "delete-chain" , optional_argument, 0, 'X' },
Bart De Schuymer5885b362002-12-03 20:51:36 +000073 { "atomic-init" , no_argument , 0, 7 },
74 { "atomic-commit" , no_argument , 0, 8 },
75 { "atomic-file" , required_argument, 0, 9 },
76 { "atomic-save" , no_argument , 0, 10 },
Bart De Schuymer8d1d8942002-07-15 20:09:09 +000077 { "init-table" , no_argument , 0, 11 },
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000078 { 0 }
79};
80
81static struct option *ebt_options = ebt_original_options;
82
Bart De Schuymer6622a012005-01-19 21:09:05 +000083/* Holds all the data */
84static struct ebt_u_replace *replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000085
Bart De Schuymer6622a012005-01-19 21:09:05 +000086/* The chosen table */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000087static struct ebt_u_table *table = NULL;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000088
Bart De Schuymer6622a012005-01-19 21:09:05 +000089/* The pointers in here are special:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +000090 * The struct ebt_target * pointer is actually a struct ebt_u_target * pointer.
91 * instead of making yet a few other structs, we just do a cast.
92 * We need a struct ebt_u_target pointer because we know the address of the data
93 * they point to won't change. We want to allow that the struct ebt_u_target.t
94 * member can change.
Bart De Schuymer6622a012005-01-19 21:09:05 +000095 * Same holds for the struct ebt_match and struct ebt_watcher pointers */
96static struct ebt_u_entry *new_entry;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000097
Bart De Schuymer1abc55d2002-06-01 19:23:47 +000098
Bart De Schuymer6622a012005-01-19 21:09:05 +000099static int global_option_offset;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000100#define OPTION_OFFSET 256
101static struct option *
102merge_options(struct option *oldopts, const struct option *newopts,
103 unsigned int *options_offset)
104{
105 unsigned int num_old, num_new, i;
106 struct option *merge;
107
108 if (!newopts || !oldopts || !options_offset)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000109 ebt_print_bug("merge wrong");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000110 for (num_old = 0; oldopts[num_old].name; num_old++);
111 for (num_new = 0; newopts[num_new].name; num_new++);
112
113 global_option_offset += OPTION_OFFSET;
114 *options_offset = global_option_offset;
115
116 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
117 if (!merge)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000118 ebt_print_memory();
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000119 memcpy(merge, oldopts, num_old * sizeof(struct option));
120 for (i = 0; i < num_new; i++) {
121 merge[num_old + i] = newopts[i];
122 merge[num_old + i].val += *options_offset;
123 }
124 memset(merge + num_old + num_new, 0, sizeof(struct option));
Bart De Schuymer6622a012005-01-19 21:09:05 +0000125 /* Only free dynamically allocated stuff */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000126 if (oldopts != ebt_original_options)
127 free(oldopts);
128
129 return merge;
130}
131
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000132static void merge_match(struct ebt_u_match *m)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000133{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000134 ebt_options = merge_options
135 (ebt_options, m->extra_ops, &(m->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000136}
137
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000138static void merge_watcher(struct ebt_u_watcher *w)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000139{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000140 ebt_options = merge_options
141 (ebt_options, w->extra_ops, &(w->option_offset));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000142}
143
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000144static void merge_target(struct ebt_u_target *t)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000145{
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000146 ebt_options = merge_options
147 (ebt_options, t->extra_ops, &(t->option_offset));
Bart De Schuymer9a0fbf22003-01-11 16:16:54 +0000148}
149
Bart De Schuymer6622a012005-01-19 21:09:05 +0000150/* Be backwards compatible, so don't use '+' in kernel */
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000151#define IF_WILDCARD 1
152static void print_iface(const char *iface)
153{
154 char *c;
155
156 if ((c = strchr(iface, IF_WILDCARD)))
157 *c = '+';
158 printf("%s ", iface);
159 if (c)
160 *c = IF_WILDCARD;
161}
162
Bart De Schuymer6622a012005-01-19 21:09:05 +0000163/* We use replace->flags, so we can't use the following values:
164 * 0x01 == OPT_COMMAND, 0x02 == OPT_TABLE, 0x100 == OPT_ZERO */
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000165#define LIST_N 0x04
166#define LIST_C 0x08
167#define LIST_X 0x10
168#define LIST_MAC2 0x20
169
Bart De Schuymer6622a012005-01-19 21:09:05 +0000170/* Helper function for list_rules() */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000171static void list_em(struct ebt_u_entries *entries)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000172{
173 int i, j, space = 0, digits;
174 struct ebt_u_entry *hlp;
175 struct ebt_u_match_list *m_l;
176 struct ebt_u_watcher_list *w_l;
177 struct ebt_u_match *m;
178 struct ebt_u_watcher *w;
179 struct ebt_u_target *t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000180
Bart De Schuymer6622a012005-01-19 21:09:05 +0000181 if (replace->flags & LIST_MAC2)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000182 ebt_printstyle_mac = 2;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000183 hlp = entries->entries;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000184 if (replace->flags & LIST_X && entries->policy != EBT_ACCEPT) {
185 printf("ebtables -t %s -P %s %s\n", replace->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000186 entries->name, ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000187 } else if (!(replace->flags & LIST_X)) {
Bart De Schuymerc87c9642002-08-01 15:34:16 +0000188 printf("\nBridge chain: %s, entries: %d, policy: %s\n",
189 entries->name, entries->nentries,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000190 ebt_standard_targets[-entries->policy - 1]);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000191 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000192
Bart De Schuymer60332e02002-06-23 08:01:47 +0000193 i = entries->nentries;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000194 while (i > 9) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000195 space++;
196 i /= 10;
197 }
198
Bart De Schuymer60332e02002-06-23 08:01:47 +0000199 for (i = 0; i < entries->nentries; i++) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000200 if (replace->flags & LIST_N) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000201 digits = 0;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000202 /* A little work to get nice rule numbers. */
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000203 j = i + 1;
204 while (j > 9) {
205 digits++;
206 j /= 10;
207 }
208 for (j = 0; j < space - digits; j++)
209 printf(" ");
210 printf("%d. ", i + 1);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000211 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000212 if (replace->flags & LIST_X)
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000213 printf("ebtables -t %s -A %s ",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000214 replace->name, entries->name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000215
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000216 /* The standard target's print() uses this to find out
217 * the name of a udc */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000218 hlp->replace = replace;
Bart De Schuymercaa9a462004-12-05 15:59:17 +0000219
Bart De Schuymer6622a012005-01-19 21:09:05 +0000220 /* Don't print anything about the protocol if no protocol was
221 * specified, obviously this means any protocol will do. */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000222 if (!(hlp->bitmask & EBT_NOPROTO)) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000223 printf("-p ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000224 if (hlp->invflags & EBT_IPROTO)
225 printf("! ");
226 if (hlp->bitmask & EBT_802_3)
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000227 printf("Length ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000228 else {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000229 struct ethertypeent *ent;
230
Bart De Schuymer64182a32004-01-21 20:39:54 +0000231 ent = getethertypebynumber
232 (ntohs(hlp->ethproto));
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000233 if (!ent)
Bart De Schuymer60332e02002-06-23 08:01:47 +0000234 printf("0x%x ", ntohs(hlp->ethproto));
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000235 else
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000236 printf("%s ", ent->e_name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000237 }
238 }
239 if (hlp->bitmask & EBT_SOURCEMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000240 printf("-s ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000241 if (hlp->invflags & EBT_ISOURCE)
242 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000243 ebt_print_mac_and_mask(hlp->sourcemac, hlp->sourcemsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000244 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000245 }
246 if (hlp->bitmask & EBT_DESTMAC) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000247 printf("-d ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000248 if (hlp->invflags & EBT_IDEST)
249 printf("! ");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000250 ebt_print_mac_and_mask(hlp->destmac, hlp->destmsk);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000251 printf(" ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000252 }
253 if (hlp->in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000254 printf("-i ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000255 if (hlp->invflags & EBT_IIN)
256 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000257 print_iface(hlp->in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000258 }
259 if (hlp->logical_in[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000260 printf("--logical-in ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000261 if (hlp->invflags & EBT_ILOGICALIN)
262 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000263 print_iface(hlp->logical_in);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000264 }
265 if (hlp->logical_out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000266 printf("--logical-out ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000267 if (hlp->invflags & EBT_ILOGICALOUT)
268 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000269 print_iface(hlp->logical_out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000270 }
271 if (hlp->out[0] != '\0') {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000272 printf("-o ");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000273 if (hlp->invflags & EBT_IOUT)
274 printf("! ");
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000275 print_iface(hlp->out);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000276 }
277
278 m_l = hlp->m_list;
279 while (m_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000280 m = ebt_find_match(m_l->m->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000281 if (!m)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000282 ebt_print_bug("Match not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000283 m->print(hlp, m_l->m);
284 m_l = m_l->next;
285 }
286 w_l = hlp->w_list;
287 while (w_l) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000288 w = ebt_find_watcher(w_l->w->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000289 if (!w)
Bart De Schuymer64182a32004-01-21 20:39:54 +0000290 ebt_print_bug("Watcher not found");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000291 w->print(hlp, w_l->w);
292 w_l = w_l->next;
293 }
294
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000295 printf("-j ");
296 if (strcmp(hlp->t->u.name, EBT_STANDARD_TARGET))
297 printf("%s ", hlp->t->u.name);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000298 t = ebt_find_target(hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000299 if (!t)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000300 ebt_print_bug("Target '%s' not found", hlp->t->u.name);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000301 t->print(hlp, hlp->t);
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000302 if (replace->flags & LIST_C) {
303 if (replace->flags & LIST_X)
304 printf("-c %llu %llu", hlp->cnt.pcnt, hlp->cnt.bcnt);
305 else
306 printf(", pcnt = %llu -- bcnt = %llu", hlp->cnt.pcnt, hlp->cnt.bcnt);
307 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000308 printf("\n");
309 hlp = hlp->next;
310 }
311}
312
Bart De Schuymer62423742002-07-14 19:06:20 +0000313static void print_help()
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000314{
315 struct ebt_u_match_list *m_l;
316 struct ebt_u_watcher_list *w_l;
317
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000318 PRINT_VERSION;
319 printf(
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000320"Usage:\n"
321"ebtables -[ADI] chain rule-specification [options]\n"
322"ebtables -P chain target\n"
323"ebtables -[LFZ] [chain]\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000324"ebtables -[NX] [chain]\n"
325"ebtables -E old-chain-name new-chain-name\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000326"Commands:\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000327"--append -A chain : append to chain\n"
328"--delete -D chain : delete matching rule from chain\n"
329"--delete -D chain rulenum : delete rule at position rulenum from chain\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000330"--insert -I chain rulenum : insert rule at position rulenum in chain\n"
Bart De Schuymer23f6dcf2002-08-17 09:14:07 +0000331"--list -L [chain] : list the rules in a chain or in all chains\n"
332"--flush -F [chain] : delete all rules in chain or in all chains\n"
333"--init-table : replace the kernel table with the initial table\n"
334"--zero -Z [chain] : put counters on zero in chain or in all chains\n"
335"--policy -P chain target : change policy on chain to target\n"
336"--new-chain -N chain : create a user defined chain\n"
337"--rename-chain -E old new : rename a chain\n"
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000338"--delete-chain -X [chain] : delete a user defined chain\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000339"--atomic-commit : update the kernel w/t table contained in <FILE>\n"
340"--atomic-init : put the initial kernel table into <FILE>\n"
341"--atomic-save : put the current kernel table into <FILE>\n"
Bart De Schuymer97819962002-12-11 21:23:07 +0000342"--atomic-file file : set <FILE> to file\n\n"
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000343"Options:\n"
344"--proto -p [!] proto : protocol hexadecimal, by name or LENGTH\n"
345"--src -s [!] address[/mask]: source mac address\n"
346"--dst -d [!] address[/mask]: destination mac address\n"
Bart De Schuymer6622a012005-01-19 21:09:05 +0000347"--in-if -i [!] name[+] : network input interface name\n"
348"--out-if -o [!] name[+] : network output interface name\n"
349"--logical-in [!] name[+] : logical bridge input interface name\n"
350"--logical-out [!] name[+] : logical bridge output interface name\n"
Bart De Schuymer5cbc8e02002-07-14 21:15:28 +0000351"--modprobe -M program : try to insert modules using this program\n"
Bart De Schuymer5885b362002-12-03 20:51:36 +0000352"--version -V : print package version\n\n"
353"Environment variable:\n"
354ATOMIC_ENV_VARIABLE " : if set <FILE> (see above) will equal its value"
355"\n\n");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000356 m_l = new_entry->m_list;
357 while (m_l) {
358 ((struct ebt_u_match *)m_l->m)->help();
359 printf("\n");
360 m_l = m_l->next;
361 }
362 w_l = new_entry->w_list;
363 while (w_l) {
364 ((struct ebt_u_watcher *)w_l->w)->help();
365 printf("\n");
366 w_l = w_l->next;
367 }
368 ((struct ebt_u_target *)new_entry->t)->help();
369 printf("\n");
370 if (table->help)
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000371 table->help(ebt_hooknames);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000372}
373
Bart De Schuymer6622a012005-01-19 21:09:05 +0000374/* Execute command L */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000375static void list_rules()
376{
377 int i;
378
Bart De Schuymer6622a012005-01-19 21:09:05 +0000379 if (!(replace->flags & LIST_X))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000380 printf("Bridge table: %s\n", table->name);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000381 if (replace->selected_chain != -1) {
382 list_em(ebt_to_chain(replace));
Bart De Schuymer60332e02002-06-23 08:01:47 +0000383 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000384 struct ebt_u_chain_list *cl = replace->udc;
Bart De Schuymer60332e02002-06-23 08:01:47 +0000385
Bart De Schuymer6622a012005-01-19 21:09:05 +0000386 /* Create new chains and rename standard chains when necessary */
387 if (replace->flags & LIST_X) {
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000388 while (cl) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000389 printf("ebtables -t %s -N %s\n", replace->name,
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000390 cl->udc->name);
391 cl = cl->next;
392 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000393 cl = replace->udc;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000394 for (i = 0; i < NF_BR_NUMHOOKS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000395 if (replace->valid_hooks & (1 << i) &&
396 strcmp(replace->hook_entry[i]->name,
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000397 ebt_hooknames[i]))
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000398 printf("ebtables -t %s -E %s %s\n",
Bart De Schuymer6622a012005-01-19 21:09:05 +0000399 replace->name, ebt_hooknames[i],
400 replace->hook_entry[i]->name);
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000401 }
Bart De Schuymer60332e02002-06-23 08:01:47 +0000402 i = 0;
403 while (1) {
404 if (i < NF_BR_NUMHOOKS) {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000405 if (replace->valid_hooks & (1 << i))
406 list_em(replace->hook_entry[i]);
Bart De Schuymer60332e02002-06-23 08:01:47 +0000407 i++;
408 continue;
409 } else {
410 if (!cl)
411 break;
412 list_em(cl->udc);
413 cl = cl->next;
414 }
415 }
416 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000417}
418
Bart De Schuymercc440052002-11-06 21:10:33 +0000419static int parse_delete_rule(const char *argv, int *rule_nr, int *rule_nr_end)
420{
421 char *colon = strchr(argv, ':'), *buffer;
422
423 if (colon) {
424 *colon = '\0';
425 if (*(colon + 1) == '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000426 *rule_nr_end = -1; /* Until the last rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000427 else {
428 *rule_nr_end = strtol(colon + 1, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000429 if (*buffer != '\0' || *rule_nr_end == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000430 return -1;
431 }
432 }
433 if (colon == argv)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000434 *rule_nr = 1; /* Beginning with the first rule */
Bart De Schuymercc440052002-11-06 21:10:33 +0000435 else {
436 *rule_nr = strtol(argv, &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000437 if (*buffer != '\0' || *rule_nr == 0)
Bart De Schuymercc440052002-11-06 21:10:33 +0000438 return -1;
439 }
440 if (!colon)
441 *rule_nr_end = *rule_nr;
Bart De Schuymercc440052002-11-06 21:10:33 +0000442 return 0;
443}
444
Bart De Schuymer6622a012005-01-19 21:09:05 +0000445static int parse_iface(char *iface, char *option)
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000446{
447 char *c;
448
449 if ((c = strchr(iface, '+'))) {
450 if (*(c + 1) != '\0') {
451 ebt_print_error("Spurious characters after '+' "
452 "wildcard for %s", option);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000453 return -1;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000454 } else
455 *c = IF_WILDCARD;
456 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000457 return 0;
Bart De Schuymer37d520d2004-10-24 07:36:15 +0000458}
459
Bart De Schuymer6622a012005-01-19 21:09:05 +0000460void ebt_early_init_once()
461{
462 ebt_iterate_matches(merge_match);
463 ebt_iterate_watchers(merge_watcher);
464 ebt_iterate_targets(merge_target);
465}
466
467#define ebt_print_error2(format, args...) {__ebt_print_error(format, ##args); \
468 return -1;}
469#define ebt_check_option2(flags,mask) \
470({ebt_check_option(flags,mask); \
471 if (ebt_errormsg[0] != '\0') \
472 return -1;})
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000473#define ebt_check_inverse2(option) \
474({int __ret = ebt_check_inverse(option); \
475if (ebt_errormsg[0] != '\0') \
476 return -1; \
477if (!optarg) \
478 __ebt_print_error("Option without (mandatory) argument"); \
Bart De Schuymer6622a012005-01-19 21:09:05 +0000479__ret;})
480#define OPT_COMMANDS (replace->flags & OPT_COMMAND || replace->flags & OPT_ZERO)
481
Bart De Schuymer64182a32004-01-21 20:39:54 +0000482#define OPT_COMMAND 0x01
483#define OPT_TABLE 0x02
484#define OPT_IN 0x04
485#define OPT_OUT 0x08
486#define OPT_JUMP 0x10
487#define OPT_PROTOCOL 0x20
488#define OPT_SOURCE 0x40
489#define OPT_DEST 0x80
490#define OPT_ZERO 0x100
491#define OPT_LOGICALIN 0x200
492#define OPT_LOGICALOUT 0x400
Bart De Schuymer6622a012005-01-19 21:09:05 +0000493#define OPT_KERNELDATA 0x800 /* This value is also defined in ebtablesd.c */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000494#define OPT_COUNT 0x1000
Bart De Schuymer6622a012005-01-19 21:09:05 +0000495int do_command(int argc, char *argv[], int exec_style,
496 struct ebt_u_replace *replace_)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000497{
Bart De Schuymer923a5732002-08-11 12:01:33 +0000498 char *buffer;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000499 int c, i;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000500 /* Needed for the -Z option (we can have -Z <this> -L <that>) */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000501 int zerochain = -1;
Bart De Schuymerf8f8f292002-06-25 15:43:57 +0000502 int policy = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000503 int rule_nr = 0;
504 int rule_nr_end = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000505 struct ebt_u_target *t;
506 struct ebt_u_match *m;
507 struct ebt_u_watcher *w;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000508 struct ebt_u_match_list *m_l;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000509 struct ebt_u_watcher_list *w_l;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000510 struct ebt_u_entries *entries;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000511
Bart De Schuymera615b962002-11-03 14:54:09 +0000512 opterr = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000513 ebt_modprobe = NULL;
Bart De Schuymera615b962002-11-03 14:54:09 +0000514
Bart De Schuymer6622a012005-01-19 21:09:05 +0000515 replace = replace_;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000516
Bart De Schuymer6622a012005-01-19 21:09:05 +0000517 /* The daemon doesn't use the environment variable */
518 if (exec_style == EXEC_STYLE_PRG) {
519 buffer = getenv(ATOMIC_ENV_VARIABLE);
520 if (buffer) {
521 replace->filename = malloc(strlen(buffer) + 1);
522 if (!replace->filename)
523 ebt_print_memory();
524 strcpy(replace->filename, buffer);
525 buffer = NULL;
526 }
Bart De Schuymer64182a32004-01-21 20:39:54 +0000527 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000528
Bart De Schuymer6622a012005-01-19 21:09:05 +0000529 replace->flags &= OPT_KERNELDATA; /* ebtablesd needs OPT_KERNELDATA */
530 replace->selected_chain = -1;
531 replace->command = 'h';
532
533 if (!new_entry) {
534 new_entry = (struct ebt_u_entry *)malloc(sizeof(struct ebt_u_entry));
535 if (!new_entry)
536 ebt_print_memory();
537 }
538 /* Put some sane values in our new entry */
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000539 ebt_initialize_entry(new_entry);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000540 new_entry->replace = replace;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000541
Bart De Schuymer6622a012005-01-19 21:09:05 +0000542 /* The scenario induced by this loop makes that:
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000543 * '-t' ,'-M' and --atomic (if specified) have to come
Bart De Schuymer6622a012005-01-19 21:09:05 +0000544 * before '-A' and the like */
Bart De Schuymer60332e02002-06-23 08:01:47 +0000545
Bart De Schuymer6622a012005-01-19 21:09:05 +0000546 /* Getopt saves the day */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000547 while ((c = getopt_long(argc, argv,
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000548 "-A:D: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 +0000549 switch (c) {
550
Bart De Schuymer6622a012005-01-19 21:09:05 +0000551 case 'A': /* Add a rule */
552 case 'D': /* Delete a rule */
553 case 'P': /* Define policy */
554 case 'I': /* Insert a rule */
555 case 'N': /* Make a user defined chain */
556 case 'E': /* Rename chain */
557 case 'X': /* Delete chain */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000558 /* We allow -N chainname -P policy */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000559 if (replace->command == 'N' && c == 'P') {
560 replace->command = c;
561 optind--; /* No table specified */
Bart De Schuymer64182a32004-01-21 20:39:54 +0000562 goto handle_P;
563 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000564 if (OPT_COMMANDS)
565 ebt_print_error2("Multiple commands are not allowed");
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000566
Bart De Schuymer6622a012005-01-19 21:09:05 +0000567 replace->command = c;
568 replace->flags |= OPT_COMMAND;
569 if (!(replace->flags & OPT_KERNELDATA))
570 ebt_get_kernel_table(replace, 0);
571 if (optarg && (optarg[0] == '-' || !strcmp(optarg, "!")))
572 ebt_print_error2("No chain name specified");
573 if (c == 'N') {
574 ebt_new_chain(replace, optarg, EBT_ACCEPT);
575 /* This is needed to get -N x -P y working */
576 replace->selected_chain = ebt_get_chainnr(replace, optarg);
577 break;
578 } else if (c == 'X') {
579 if (optind >= argc) {
580 replace->selected_chain = -1;
581 ebt_delete_chain(replace);
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000582 break;
583 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000584
585 if (optind < argc - 1)
586 ebt_print_error2("No extra options allowed with -X");
587
588 if ((replace->selected_chain = ebt_get_chainnr(replace, argv[optind])) == -1)
589 ebt_print_error2("Chain %s doesn't exist", argv[optind]);
590 ebt_delete_chain(replace);
591 if (ebt_errormsg[0] != '\0')
592 return -1;
593 optind++;
Bart De Schuymer90f2c2e2003-07-13 18:48:02 +0000594 break;
595 }
596
Bart De Schuymer6622a012005-01-19 21:09:05 +0000597 if ((replace->selected_chain = ebt_get_chainnr(replace, optarg)) == -1)
598 ebt_print_error2("Chain %s doesn't exist", optarg);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000599 if (c == 'E') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000600 if (optind >= argc)
601 ebt_print_error2("No new chain name specified");
602 if (optind < argc - 1)
603 ebt_print_error2("No extra options allowed with -E");
Bart De Schuymer64182a32004-01-21 20:39:54 +0000604 if (strlen(argv[optind])>=EBT_CHAIN_MAXNAMELEN)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000605 ebt_print_error2("Chain name length can't exceed %d", EBT_CHAIN_MAXNAMELEN - 1);
606 if (ebt_get_chainnr(replace, argv[optind]) != -1)
607 ebt_print_error2("Chain %s already exists", argv[optind]);
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000608 if (ebt_find_target(argv[optind]))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000609 ebt_print_error2("Target with name %s exists", argv[optind]);
610 ebt_rename_chain(replace, argv[optind]);
Bart De Schuymer1ab41562002-06-23 17:09:54 +0000611 optind++;
612 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000613 } else if (c == 'D' && optind < argc && (argv[optind][0] != '-' || (argv[optind][1] >= '0' && argv[optind][1] <= '9'))) {
614 if (optind != argc - 1)
615 ebt_print_error2("No extra options allowed with -D start_nr[:end_nr]");
616 if (parse_delete_rule(argv[optind], &rule_nr, &rule_nr_end))
617 ebt_print_error2("Problem with the specified rule number(s)");
Bart De Schuymercc440052002-11-06 21:10:33 +0000618 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000619 } else if (c == 'I') {
620 if (optind >= argc || (argv[optind][0] == '-' && (argv[optind][1] < '0' || argv[optind][1] > '9')))
621 ebt_print_error2("No rulenr for -I specified");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000622 rule_nr = strtol(argv[optind], &buffer, 10);
Bart De Schuymerc3b97f52003-04-17 17:03:49 +0000623 if (*buffer != '\0')
Bart De Schuymer6622a012005-01-19 21:09:05 +0000624 ebt_print_error2("Problem with the specified rule number");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000625 optind++;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000626 } else if (c == 'P') {
Bart De Schuymer64182a32004-01-21 20:39:54 +0000627handle_P:
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000628 if (optind >= argc)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000629 ebt_print_error2("No policy specified");
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000630 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000631 if (!strcmp(argv[optind], ebt_standard_targets[i])) {
Bart De Schuymer60332e02002-06-23 08:01:47 +0000632 policy = -i -1;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000633 if (policy == EBT_CONTINUE)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000634 ebt_print_error2("Wrong policy: '%s'", argv[optind]);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000635 break;
636 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000637 optind++;
638 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000639 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000640 case 'L': /* List */
641 case 'F': /* Flush */
642 case 'Z': /* Zero counters */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000643 if (c == 'Z') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000644 if ((replace->flags & OPT_ZERO) || (replace->flags & OPT_COMMAND && replace->command != 'L'))
645print_zero:
646 ebt_print_error2("Command -Z only allowed together with command -L");
647 replace->flags |= OPT_ZERO;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000648 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000649 replace->command = c;
650 if (replace->flags & OPT_COMMAND)
651 ebt_print_error2("Multiple commands are not allowed");
652 replace->flags |= OPT_COMMAND;
653 if (replace->flags & OPT_ZERO)
654 goto print_zero;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000655 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000656
657#ifdef SILENT_DAEMON
658 if (c== 'L' && exec_style == EXEC_STYLE_DAEMON)
659 ebt_print_error2("-L not supported in daemon mode not supported in daemon mode");
660#endif
661
662 if (!(replace->flags & OPT_KERNELDATA))
663 ebt_get_kernel_table(replace, 0);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000664 i = -1;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000665 if (optind < argc && argv[optind][0] != '-') {
666 if ((i = ebt_get_chainnr(replace, argv[optind])) == -1)
Bart De Schuymerd2ced822005-01-23 19:19:00 +0000667 ebt_print_error2("Chain %s doesn't exist", argv[optind]);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000668 optind++;
669 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000670 if (i != -1) {
671 if (c == 'Z')
672 zerochain = i;
673 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000674 replace->selected_chain = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000675 }
676 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000677 case 'V': /* Version */
678 replace->command = 'V';
679 if (OPT_COMMANDS)
680 ebt_print_error2("Multiple commands are not allowed");
681 if (exec_style == EXEC_STYLE_DAEMON)
682 ebt_print_error2(PROGNAME" v"PROGVERSION" ("PROGDATE")\n");
Bart De Schuymer57a3f6a2003-04-01 16:59:33 +0000683 PRINT_VERSION;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000684 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000685 case 'M': /* Modprobe */
686 if (OPT_COMMANDS)
687 ebt_print_error2("Please put the -M option earlier");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000688 ebt_modprobe = optarg;
Bart De Schuymerc8531032002-06-14 21:55:29 +0000689 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000690 case 'h': /* Help */
Bart De Schuymer47962862005-01-20 22:09:36 +0000691#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000692 if (exec_style == EXEC_STYLE_DAEMON)
693 ebt_print_error2("-h not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000694#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000695 if (OPT_COMMANDS)
696 ebt_print_error2("Multiple commands are not allowed");
697 replace->command = 'h';
Bart De Schuymerc8531032002-06-14 21:55:29 +0000698
Bart De Schuymer6622a012005-01-19 21:09:05 +0000699 /* All other arguments should be extension names */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000700 while (optind < argc) {
701 struct ebt_u_match *m;
702 struct ebt_u_watcher *w;
703
Bart De Schuymer6622a012005-01-19 21:09:05 +0000704 if (!strcasecmp("list_extensions", argv[optind])) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000705 ebt_list_extensions();
Bart De Schuymer6622a012005-01-19 21:09:05 +0000706 exit(0);
707 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000708 if ((m = ebt_find_match(argv[optind])))
709 ebt_add_match(new_entry, m);
710 else if ((w = ebt_find_watcher(argv[optind])))
711 ebt_add_watcher(new_entry, w);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000712 else {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000713 if (!(t = ebt_find_target(argv[optind])))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000714 ebt_print_error2("Extension %s not found", argv[optind]);
715 if (replace->flags & OPT_JUMP)
716 ebt_print_error2("Sorry, you can only see help for one target extension at a time");
717 replace->flags |= OPT_JUMP;
718 new_entry->t = (struct ebt_entry_target *)t;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000719 }
720 optind++;
721 }
722 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000723 case 't': /* Table */
724 if (replace->command != 'h')
725 ebt_print_error2("Please put the -t option first");
726 ebt_check_option2(&(replace->flags), OPT_TABLE);
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000727 if (strlen(optarg) > EBT_TABLE_MAXNAMELEN - 1)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000728 ebt_print_error2("Table name too long");
729 strcpy(replace->name, optarg);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000730 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000731 case 'i': /* Input interface */
732 case 2 : /* Logical input interface */
733 case 'o': /* Output interface */
734 case 3 : /* Logical output interface */
735 case 'j': /* Target */
736 case 'p': /* Net family protocol */
737 case 's': /* Source mac */
738 case 'd': /* Destination mac */
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000739 case 'c': /* Set counters */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000740 if (!OPT_COMMANDS)
741 ebt_print_error2("No command specified");
742 if (replace->command != 'A' && replace->command != 'D' && replace->command != 'I')
743 ebt_print_error2("Command and option do not match");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000744 if (c == 'i') {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000745 ebt_check_option2(&(replace->flags), OPT_IN);
746 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
747 ebt_print_error2("Use -i only in INPUT, FORWARD, PREROUTING and BROUTING chains");
748 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000749 new_entry->invflags |= EBT_IIN;
750
Bart De Schuymer6622a012005-01-19 21:09:05 +0000751 if (strlen(optarg) >= IFNAMSIZ)
752 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
753 strcpy(new_entry->in, optarg);
754 if (parse_iface(new_entry->in, "-i"))
755 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000756 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000757 } else if (c == 2) {
758 ebt_check_option2(&(replace->flags), OPT_LOGICALIN);
759 if (replace->selected_chain > 2 && replace->selected_chain < NF_BR_BROUTING)
760 ebt_print_error2("Use --logical-in only in INPUT, FORWARD, PREROUTING and BROUTING chains");
761 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000762 new_entry->invflags |= EBT_ILOGICALIN;
763
Bart De Schuymer6622a012005-01-19 21:09:05 +0000764 if (strlen(optarg) >= IFNAMSIZ)
765 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
766 strcpy(new_entry->logical_in, optarg);
767 if (parse_iface(new_entry->logical_in, "--logical-in"))
768 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000769 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000770 } else if (c == 'o') {
771 ebt_check_option2(&(replace->flags), OPT_OUT);
772 if (replace->selected_chain < 2 || replace->selected_chain == NF_BR_BROUTING)
773 ebt_print_error2("Use -o only in OUTPUT, FORWARD and POSTROUTING chains");
774 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000775 new_entry->invflags |= EBT_IOUT;
776
Bart De Schuymer6622a012005-01-19 21:09:05 +0000777 if (strlen(optarg) >= IFNAMSIZ)
778 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
779 strcpy(new_entry->out, optarg);
780 if (parse_iface(new_entry->out, "-o"))
781 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000782 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000783 } else if (c == 3) {
784 ebt_check_option2(&(replace->flags), OPT_LOGICALOUT);
785 if (replace->selected_chain < 2)
786 ebt_print_error2("Use --logical-out only in OUTPUT, FORWARD and POSTROUTING chains");
787 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000788 new_entry->invflags |= EBT_ILOGICALOUT;
789
Bart De Schuymer6622a012005-01-19 21:09:05 +0000790 if (strlen(optarg) >= IFNAMSIZ)
791 ebt_print_error2("Interface name length must be less than %d", IFNAMSIZ);
792 strcpy(new_entry->logical_out, optarg);
793 if (parse_iface(new_entry->logical_out, "--logical-out"))
794 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000795 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000796 } else if (c == 'j') {
797 ebt_check_option2(&(replace->flags), OPT_JUMP);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000798 for (i = 0; i < NUM_STANDARD_TARGETS; i++)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000799 if (!strcmp(optarg, ebt_standard_targets[i])) {
800 t = ebt_find_target(EBT_STANDARD_TARGET);
801 ((struct ebt_standard_target *) t->t)->verdict = -i - 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000802 break;
803 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000804 if (-i - 1 == EBT_RETURN && replace->selected_chain < NF_BR_NUMHOOKS) {
805 ebt_print_error2("Return target only for user defined chains");
806 } else if (i != NUM_STANDARD_TARGETS)
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +0000807 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000808
809 if ((i = ebt_get_chainnr(replace, optarg)) != -1) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000810 if (i < NF_BR_NUMHOOKS)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000811 ebt_print_error2("Don't jump to a standard chain");
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000812 t = ebt_find_target(EBT_STANDARD_TARGET);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000813 ((struct ebt_standard_target *) t->t)->verdict = i - NF_BR_NUMHOOKS;
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000814 break;
815 } else {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000816 /* Must be an extension then */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000817 struct ebt_u_target *t;
Bart De Schuymerdd5594b2002-06-26 18:05:20 +0000818
Bart De Schuymer8339ff12004-01-14 20:05:27 +0000819 t = ebt_find_target(optarg);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000820 /* -j standard not allowed either */
821 if (!t || t == (struct ebt_u_target *)new_entry->t)
822 ebt_print_error2("Illegal target name");
823 new_entry->t = (struct ebt_entry_target *)t;
824 ebt_find_target(EBT_STANDARD_TARGET)->used = 0;
825 t->used = 1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000826 }
827 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000828 } else if (c == 's') {
829 ebt_check_option2(&(replace->flags), OPT_SOURCE);
830 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000831 new_entry->invflags |= EBT_ISOURCE;
832
Bart De Schuymer6622a012005-01-19 21:09:05 +0000833 if (ebt_get_mac_and_mask(optarg, new_entry->sourcemac, new_entry->sourcemsk))
834 ebt_print_error2("Problem with specified source mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000835 new_entry->bitmask |= EBT_SOURCEMAC;
836 break;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000837 } else if (c == 'd') {
838 ebt_check_option2(&(replace->flags), OPT_DEST);
839 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000840 new_entry->invflags |= EBT_IDEST;
841
Bart De Schuymer6622a012005-01-19 21:09:05 +0000842 if (ebt_get_mac_and_mask(optarg, new_entry->destmac, new_entry->destmsk))
843 ebt_print_error2("Problem with specified destination mac");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000844 new_entry->bitmask |= EBT_DESTMAC;
845 break;
Bart De Schuymerb5917d82005-01-25 21:15:59 +0000846 } else if (c == 'c') {
847 ebt_check_option2(&(replace->flags), OPT_COUNT);
848 if (ebt_check_inverse2(optarg))
849 ebt_print_error2("Unexpected '!' after -c");
850 if (optind >= argc)
851 ebt_print_error2("Option -c needs 2 arguments");
852
853 new_entry->cnt.pcnt = strtoull(optarg, &buffer, 10);
854 if (*buffer != '\0')
855 ebt_print_error2("Packet counter '%s' invalid", optarg)
856 new_entry->cnt.bcnt = strtoull(argv[optind], &buffer, 10);
857 if (*buffer != '\0')
858 ebt_print_error2("Packet counter '%s' invalid", argv[optind])
859 optind++;
860 break;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000861 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000862 ebt_check_option2(&(replace->flags), OPT_PROTOCOL);
863 if (ebt_check_inverse2(optarg))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000864 new_entry->invflags |= EBT_IPROTO;
865
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000866 new_entry->bitmask &= ~((unsigned int)EBT_NOPROTO);
Bart De Schuymer6622a012005-01-19 21:09:05 +0000867 i = strtol(optarg, &buffer, 16);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000868 if (*buffer == '\0' && (i < 0 || i > 0xFFFF))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000869 ebt_print_error2("Problem with the specified protocol");
Bart De Schuymer28bf6f62002-06-26 18:57:24 +0000870 if (*buffer != '\0') {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000871 struct ethertypeent *ent;
872
Bart De Schuymer6622a012005-01-19 21:09:05 +0000873 if (!strcasecmp(optarg, "LENGTH")) {
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000874 new_entry->bitmask |= EBT_802_3;
875 break;
876 }
Bart De Schuymer6622a012005-01-19 21:09:05 +0000877 ent = getethertypebyname(optarg);
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000878 if (!ent)
Bart De Schuymer6622a012005-01-19 21:09:05 +0000879 ebt_print_error2("Problem with the specified Ethernet protocol (%s), perhaps "_PATH_ETHERTYPES " is missing", optarg)
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000880 new_entry->ethproto = ent->e_ethertype;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000881 } else
882 new_entry->ethproto = i;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000883
Bart De Schuymer6622a012005-01-19 21:09:05 +0000884 if (new_entry->ethproto < 0x0600)
885 ebt_print_error2("Sorry, protocols have values above or equal to 0x0600");
886 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000887 case 4 : /* Lc */
Bart De Schuymer47962862005-01-20 22:09:36 +0000888#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000889 if (exec_style == EXEC_STYLE_DAEMON)
890 ebt_print_error2("--Lc is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000891#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000892 ebt_check_option2(&(replace->flags), LIST_C);
893 if (replace->command != 'L')
Bart De Schuymer64182a32004-01-21 20:39:54 +0000894 ebt_print_error("Use --Lc with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000895 replace->flags |= LIST_C;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000896 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000897 case 5 : /* Ln */
Bart De Schuymer47962862005-01-20 22:09:36 +0000898#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000899 if (exec_style == EXEC_STYLE_DAEMON)
900 ebt_print_error2("--Ln is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000901#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000902 ebt_check_option2(&(replace->flags), LIST_N);
903 if (replace->command != 'L')
904 ebt_print_error2("Use --Ln with -L");
905 if (replace->flags & LIST_X)
906 ebt_print_error2("--Lx is not compatible with --Ln");
907 replace->flags |= LIST_N;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000908 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000909 case 6 : /* Lx */
Bart De Schuymer47962862005-01-20 22:09:36 +0000910#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000911 if (exec_style == EXEC_STYLE_DAEMON)
912 ebt_print_error2("--Lx is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000913#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000914 ebt_check_option2(&(replace->flags), LIST_X);
915 if (replace->command != 'L')
916 ebt_print_error2("Use --Lx with -L");
Bart De Schuymer6622a012005-01-19 21:09:05 +0000917 if (replace->flags & LIST_N)
918 ebt_print_error2("--Lx is not compatible with --Ln");
919 replace->flags |= LIST_X;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000920 break;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000921 case 12 : /* Lmac2 */
Bart De Schuymer47962862005-01-20 22:09:36 +0000922#ifdef SILENT_DAEMON
Bart De Schuymer6622a012005-01-19 21:09:05 +0000923 if (exec_style == EXEC_STYLE_DAEMON)
924 ebt_print_error("--Lmac2 is not supported in daemon mode");
Bart De Schuymer47962862005-01-20 22:09:36 +0000925#endif
Bart De Schuymer6622a012005-01-19 21:09:05 +0000926 ebt_check_option2(&(replace->flags), LIST_MAC2);
927 if (replace->command != 'L')
928 ebt_print_error2("Use --Lmac2 with -L");
929 replace->flags |= LIST_MAC2;
Bart De Schuymer22d03a22003-05-03 20:28:22 +0000930 break;
Bart De Schuymer9895a8e2003-01-11 10:14:24 +0000931 case 8 : /* atomic-commit */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000932 if (exec_style == EXEC_STYLE_DAEMON)
933 ebt_print_error2("--atomic-commit is not supported in daemon mode");
934 replace->command = c;
935 if (OPT_COMMANDS)
936 ebt_print_error2("Multiple commands are not allowed");
937 replace->flags |= OPT_COMMAND;
938 if (!replace->filename)
939 ebt_print_error2("No atomic file specified");
940 /* Get the information from the file */
941 ebt_get_table(replace, 0);
942 /* We don't want the kernel giving us its counters,
Bart De Schuymer64182a32004-01-21 20:39:54 +0000943 * they would overwrite the counters extracted from
Bart De Schuymer6622a012005-01-19 21:09:05 +0000944 * the file */
945 replace->num_counters = 0;
946 /* Make sure the table will be written to the kernel */
947 free(replace->filename);
948 replace->filename = NULL;
Bart De Schuymer62423742002-07-14 19:06:20 +0000949 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000950 case 7 : /* atomic-init */
951 case 10: /* atomic-save */
952 case 11: /* init-table */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000953 if (exec_style == EXEC_STYLE_DAEMON) {
954 if (c == 7) {
955 ebt_print_error2("--atomic-init is not supported in daemon mode");
956 } else if (c == 10)
957 ebt_print_error2("--atomic-save is not supported in daemon mode");
958 ebt_print_error2("--init-table is not supported in daemon mode");
959 }
960 replace->command = c;
961 if (OPT_COMMANDS)
962 ebt_print_error2("Multiple commands are not allowed");
963 if (c != 11 && !replace->filename)
964 ebt_print_error2("No atomic file specified");
965 replace->flags |= OPT_COMMAND;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000966 {
Bart De Schuymer6622a012005-01-19 21:09:05 +0000967 char *tmp = replace->filename;
Bart De Schuymer64182a32004-01-21 20:39:54 +0000968 int init = 1;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000969
Bart De Schuymer64182a32004-01-21 20:39:54 +0000970 if (c == 10)
971 init = 0;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000972 /* Get the kernel table */
973 replace->filename = NULL;
974 ebt_get_kernel_table(replace, init);
975 replace->filename = tmp;
Bart De Schuymer5885b362002-12-03 20:51:36 +0000976 }
Bart De Schuymer5885b362002-12-03 20:51:36 +0000977 break;
Bart De Schuymerc7bfa272002-11-20 19:40:13 +0000978 case 9 : /* atomic */
Bart De Schuymer6622a012005-01-19 21:09:05 +0000979 if (exec_style == EXEC_STYLE_DAEMON)
980 ebt_print_error2("--atomic is not supported in daemon mode");
981 if (OPT_COMMANDS)
982 ebt_print_error2("--atomic has to come before the command");
983 /* A possible memory leak here, but this is not
984 * executed in daemon mode */
985 replace->filename = (char *)malloc(strlen(optarg) + 1);
986 strcpy(replace->filename, optarg);
Bart De Schuymer62423742002-07-14 19:06:20 +0000987 break;
Bart De Schuymera615b962002-11-03 14:54:09 +0000988 case 1 :
989 if (!strcmp(optarg, "!"))
Bart De Schuymer6622a012005-01-19 21:09:05 +0000990 ebt_check_inverse2(optarg);
Bart De Schuymera615b962002-11-03 14:54:09 +0000991 else
Bart De Schuymer6622a012005-01-19 21:09:05 +0000992 ebt_print_error2("Bad argument : %s", optarg);
993 /* ebt_check_inverse() did optind++ */
Bart De Schuymera615b962002-11-03 14:54:09 +0000994 optind--;
995 continue;
Bart De Schuymer9af14f92002-07-10 20:49:10 +0000996 default:
Bart De Schuymer6622a012005-01-19 21:09:05 +0000997 /* Is it a target option? */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +0000998 t = (struct ebt_u_target *)new_entry->t;
Bart De Schuymer6622a012005-01-19 21:09:05 +0000999 if ((t->parse(c - t->option_offset, argv, argc, new_entry, &t->flags, &t->t)))
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001000 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001001
Bart De Schuymer6622a012005-01-19 21:09:05 +00001002 /* Is it a match_option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001003 for (m = ebt_matches; m; m = m->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001004 if (m->parse(c - m->option_offset, argv, argc, new_entry, &m->flags, &m->m))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001005 break;
1006
1007 if (m != NULL) {
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001008 if (m->used == 0) {
1009 ebt_add_match(new_entry, m);
1010 m->used = 1;
1011 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001012 goto check_extension;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001013 }
1014
Bart De Schuymer6622a012005-01-19 21:09:05 +00001015 /* Is it a watcher option? */
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001016 for (w = ebt_watchers; w; w = w->next)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001017 if (w->parse(c-w->option_offset, argv, argc, new_entry, &w->flags, &w->w))
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001018 break;
1019
1020 if (w == NULL)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001021 ebt_print_error2("Unknown argument: '%s', %c, '%c'", argv[optind - 1], (char)optopt, (char)c);
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001022 if (w->used == 0) {
1023 ebt_add_watcher(new_entry, w);
1024 w->used = 1;
1025 }
Bart De Schuymerdd5594b2002-06-26 18:05:20 +00001026check_extension:
Bart De Schuymer6622a012005-01-19 21:09:05 +00001027 if (replace->command != 'A' && replace->command != 'I' && replace->command != 'D')
1028 ebt_print_error2("Extensions only for -A, -I and -D");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001029 }
Bart De Schuymer8339ff12004-01-14 20:05:27 +00001030 ebt_invert = 0;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001031 }
1032
Bart De Schuymer6622a012005-01-19 21:09:05 +00001033 /* Just in case we didn't catch an error */
1034 if (ebt_errormsg[0] != '\0')
1035 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001036
Bart De Schuymer6622a012005-01-19 21:09:05 +00001037 if (!(table = ebt_find_table(replace->name)))
1038 ebt_print_error2("Bad table name");
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001039
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001040 if (replace->command == 'h' && !(replace->flags & OPT_ZERO)) {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001041 print_help();
Bart De Schuymerd2ced822005-01-23 19:19:00 +00001042 if (exec_style == EXEC_STYLE_PRG)
1043 exit(0);
1044 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001045
Bart De Schuymer6622a012005-01-19 21:09:05 +00001046 /* Do the final checks */
1047 if (replace->command == 'A' || replace->command == 'I' ||
1048 replace->command == 'D') {
1049 /* This will put the hook_mask right for the chains */
1050 ebt_check_for_loops(replace);
1051 if (ebt_errormsg[0] != '\0')
1052 return -1;
1053 entries = ebt_to_chain(replace);
Bart De Schuymer60332e02002-06-23 08:01:47 +00001054 m_l = new_entry->m_list;
1055 w_l = new_entry->w_list;
1056 t = (struct ebt_u_target *)new_entry->t;
1057 while (m_l) {
1058 m = (struct ebt_u_match *)(m_l->m);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001059 m->final_check(new_entry, m->m, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001060 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001061 if (ebt_errormsg[0] != '\0')
1062 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001063 m_l = m_l->next;
1064 }
1065 while (w_l) {
1066 w = (struct ebt_u_watcher *)(w_l->w);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001067 w->final_check(new_entry, w->w, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001068 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001069 if (ebt_errormsg[0] != '\0')
1070 return -1;
Bart De Schuymer60332e02002-06-23 08:01:47 +00001071 w_l = w_l->next;
1072 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001073 t->final_check(new_entry, t->t, replace->name,
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001074 entries->hook_mask, 0);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001075 if (ebt_errormsg[0] != '\0')
1076 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001077 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001078 /* So, the extensions can work with the host endian.
1079 * The kernel does not have to do this of course */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001080 new_entry->ethproto = htons(new_entry->ethproto);
1081
Bart De Schuymer6622a012005-01-19 21:09:05 +00001082 if (replace->command == 'P') {
1083 if (replace->selected_chain < NF_BR_NUMHOOKS && policy == EBT_RETURN)
1084 ebt_print_error2("Policy RETURN only allowed for user defined chains");
1085 ebt_change_policy(replace, policy);
1086 if (ebt_errormsg[0] != '\0')
1087 return -1;
1088 } else if (replace->command == 'L') {
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001089 list_rules();
Bart De Schuymer6622a012005-01-19 21:09:05 +00001090 if (!(replace->flags & OPT_ZERO) && exec_style == EXEC_STYLE_PRG)
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001091 exit(0);
1092 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001093 if (replace->flags & OPT_ZERO) {
1094 replace->selected_chain = zerochain;
1095 ebt_zero_counters(replace);
1096 } else if (replace->command == 'F') {
1097 ebt_flush_chains(replace);
1098 } else if (replace->command == 'A' || replace->command == 'I') {
1099 ebt_add_rule(replace, new_entry, rule_nr);
1100 if (ebt_errormsg[0] != '\0')
1101 return -1;
1102 /* Makes undoing the add easier (jumps to delete_the_rule) */
1103 if (rule_nr <= 0)
1104 rule_nr--;
1105 rule_nr_end = rule_nr;
1106
1107 ebt_check_for_loops(replace);
1108 if (ebt_errormsg[0] != '\0')
1109 goto delete_the_rule;
1110
1111 /* Do the final_check(), for all entries.
1112 * This is needed when adding a rule that has a chain target */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001113 i = -1;
1114 while (1) {
1115 struct ebt_u_entry *e;
1116
1117 i++;
Bart De Schuymer6622a012005-01-19 21:09:05 +00001118 entries = ebt_nr_to_chain(replace, i);
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001119 if (!entries) {
1120 if (i < NF_BR_NUMHOOKS)
1121 continue;
1122 else
1123 break;
1124 }
1125 e = entries->entries;
1126 while (e) {
Bart De Schuymer6622a012005-01-19 21:09:05 +00001127 /* Userspace extensions use host endian */
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001128 e->ethproto = ntohs(e->ethproto);
Bart De Schuymer6622a012005-01-19 21:09:05 +00001129 ebt_do_final_checks(replace, e, entries);
1130 if (ebt_errormsg[0] != '\0')
1131 goto delete_the_rule;
Bart De Schuymer7b9aaeb2002-06-23 20:38:34 +00001132 e->ethproto = htons(e->ethproto);
1133 e = e->next;
1134 }
1135 }
Bart De Schuymer6622a012005-01-19 21:09:05 +00001136 /* Don't reuse the added rule */
1137 new_entry = NULL;
1138 } else if (replace->command == 'D')
1139delete_the_rule: /* This needs to be followed by a check on ebt_errormsg[0] */
1140 ebt_delete_rule(replace, new_entry, rule_nr, rule_nr_end);
1141 /* Commands -N, -E, -X, --atomic-commit, --atomic-commit, --atomic-save,
1142 * --init-table fall through */
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001143
Bart De Schuymer6622a012005-01-19 21:09:05 +00001144 if (ebt_errormsg[0] != '\0')
1145 return -1;
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001146 if (table->check)
Bart De Schuymer6622a012005-01-19 21:09:05 +00001147 table->check(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001148
Bart De Schuymer6622a012005-01-19 21:09:05 +00001149 if (exec_style == EXEC_STYLE_PRG) {/* Implies ebt_errormsg[0] == '\0' */
1150 ebt_deliver_table(replace);
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001151
Bart De Schuymer6622a012005-01-19 21:09:05 +00001152 if (replace->counterchanges)
1153 ebt_deliver_counters(replace, exec_style);
1154 }
Bart De Schuymer1abc55d2002-06-01 19:23:47 +00001155 return 0;
1156}