blob: a73df3ea93c91cbac3124c3da0365c1ebd0cef62 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010032#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000033#include <stdio.h>
34#include <stdlib.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000040#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000041#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Jan Engelhardtf89c1712009-06-12 20:48:52 +020043#include "xshared.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Marc Bouchere6869a82000-03-20 06:03:29 +000052#define FMT_NUMERIC 0x0001
53#define FMT_NOCOUNTS 0x0002
54#define FMT_KILOMEGAGIGA 0x0004
55#define FMT_OPTIONS 0x0008
56#define FMT_NOTABLE 0x0010
57#define FMT_NOTARGET 0x0020
58#define FMT_VIA 0x0040
59#define FMT_NONEWLINE 0x0080
60#define FMT_LINENUMBERS 0x0100
61
62#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
63 | FMT_NUMERIC | FMT_NOTABLE)
64#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
65
66
67#define CMD_NONE 0x0000U
68#define CMD_INSERT 0x0001U
69#define CMD_DELETE 0x0002U
70#define CMD_DELETE_NUM 0x0004U
71#define CMD_REPLACE 0x0008U
72#define CMD_APPEND 0x0010U
73#define CMD_LIST 0x0020U
74#define CMD_FLUSH 0x0040U
75#define CMD_ZERO 0x0080U
76#define CMD_NEW_CHAIN 0x0100U
77#define CMD_DELETE_CHAIN 0x0200U
78#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000079#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020080#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070081#define CMD_ZERO_NUM 0x2000U
82#define NUMBER_OF_CMD 15
Marc Bouchere6869a82000-03-20 06:03:29 +000083static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070084 'Z', 'N', 'X', 'P', 'E', 'S' };
Marc Bouchere6869a82000-03-20 06:03:29 +000085
Jan Engelhardtf1e71012011-02-07 03:13:43 +010086#define OPT_FRAGMENT 0x00800U
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010087#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
88static const char optflags[]
Jan Engelhardtf1e71012011-02-07 03:13:43 +010089= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c', 'f'};
Marc Bouchere6869a82000-03-20 06:03:29 +000090
91static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010092 {.name = "append", .has_arg = 1, .val = 'A'},
93 {.name = "delete", .has_arg = 1, .val = 'D'},
94 {.name = "insert", .has_arg = 1, .val = 'I'},
95 {.name = "replace", .has_arg = 1, .val = 'R'},
96 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020097 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010098 {.name = "flush", .has_arg = 2, .val = 'F'},
99 {.name = "zero", .has_arg = 2, .val = 'Z'},
100 {.name = "new-chain", .has_arg = 1, .val = 'N'},
101 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
102 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
103 {.name = "policy", .has_arg = 1, .val = 'P'},
104 {.name = "source", .has_arg = 1, .val = 's'},
105 {.name = "destination", .has_arg = 1, .val = 'd'},
106 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
107 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
108 {.name = "protocol", .has_arg = 1, .val = 'p'},
109 {.name = "in-interface", .has_arg = 1, .val = 'i'},
110 {.name = "jump", .has_arg = 1, .val = 'j'},
111 {.name = "table", .has_arg = 1, .val = 't'},
112 {.name = "match", .has_arg = 1, .val = 'm'},
113 {.name = "numeric", .has_arg = 0, .val = 'n'},
114 {.name = "out-interface", .has_arg = 1, .val = 'o'},
115 {.name = "verbose", .has_arg = 0, .val = 'v'},
116 {.name = "exact", .has_arg = 0, .val = 'x'},
117 {.name = "fragments", .has_arg = 0, .val = 'f'},
118 {.name = "version", .has_arg = 0, .val = 'V'},
119 {.name = "help", .has_arg = 2, .val = 'h'},
120 {.name = "line-numbers", .has_arg = 0, .val = '0'},
121 {.name = "modprobe", .has_arg = 1, .val = 'M'},
122 {.name = "set-counters", .has_arg = 1, .val = 'c'},
123 {.name = "goto", .has_arg = 1, .val = 'g'},
124 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +0000125};
126
Illes Marci63e90632003-03-03 08:08:37 +0000127/* we need this for iptables-restore. iptables-restore.c sets line to the
128 * current line of the input file, in order to give a more precise error
129 * message. iptables itself doesn't need this, so it is initialized to the
130 * magic number of -1 */
131int line = -1;
132
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100133void iptables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
134
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100135struct xtables_globals iptables_globals = {
136 .option_offset = 0,
137 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500138 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100139 .exit_err = iptables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100140};
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142/* Table of legal combinations of commands and options. If any of the
143 * given commands make an option legal, that option is legal (applies to
144 * CMD_LIST and CMD_ZERO only).
145 * Key:
146 * + compulsory
147 * x illegal
148 * optional
149 */
150
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100151static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Marc Bouchere6869a82000-03-20 06:03:29 +0000152/* Well, it's better than "Re: Linux vs FreeBSD" */
153{
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100154 /* -n -s -d -p -j -v -x -i -o --line -c -f */
155/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
156/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100158/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
159/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
160/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
162/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700163/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000164/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
165/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100166/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' ','x'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200167/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
168/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000169};
170
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100171static const int inverse_for_options[NUMBER_OF_OPT] =
Marc Bouchere6869a82000-03-20 06:03:29 +0000172{
173/* -n */ 0,
174/* -s */ IPT_INV_SRCIP,
175/* -d */ IPT_INV_DSTIP,
176/* -p */ IPT_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IPT_INV_VIA_IN,
181/* -o */ IPT_INV_VIA_OUT,
182/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000183/*--line*/ 0,
184/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000185};
186
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100187#define opts iptables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500188#define prog_name iptables_globals.program_name
189#define prog_vers iptables_globals.program_version
Marc Bouchere6869a82000-03-20 06:03:29 +0000190
Phil Oester8cf65912005-09-19 15:00:33 +0000191int kernel_version;
192
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000193/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000194/* defined in netinet/in.h */
195#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000196#ifndef IPPROTO_ESP
197#define IPPROTO_ESP 50
198#endif
199#ifndef IPPROTO_AH
200#define IPPROTO_AH 51
201#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000202#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000203
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000204enum {
205 IPT_DOTTED_ADDR = 0,
206 IPT_DOTTED_MASK
207};
208
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200209static void __attribute__((noreturn))
Marc Bouchere6869a82000-03-20 06:03:29 +0000210exit_tryhelp(int status)
211{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000212 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000213 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000214 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500215 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500216 xtables_free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 exit(status);
218}
219
Patrick McHardy0b639362007-09-08 16:00:01 +0000220static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100221exit_printhelp(const struct xtables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000222{
223 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000224"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100225" %s -I chain [rulenum] rule-specification [options]\n"
226" %s -R chain rulenum rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000227" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200228" %s -[LS] [chain [rulenum]] [options]\n"
229" %s -[FZ] [chain] [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000230" %s -[NX] chain\n"
231" %s -E old-chain-name new-chain-name\n"
232" %s -P chain target [options]\n"
233" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500234 prog_name, prog_vers, prog_name, prog_name,
235 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100236 prog_name, prog_name, prog_name, prog_name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000237
238 printf(
239"Commands:\n"
240"Either long or short options are allowed.\n"
241" --append -A chain Append to chain\n"
242" --delete -D chain Delete matching rule from chain\n"
243" --delete -D chain rulenum\n"
244" Delete rule rulenum (1 = first) from chain\n"
245" --insert -I chain [rulenum]\n"
246" Insert in chain as rulenum (default 1=first)\n"
247" --replace -R chain rulenum\n"
248" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200249" --list -L [chain [rulenum]]\n"
250" List the rules in a chain or all chains\n"
251" --list-rules -S [chain [rulenum]]\n"
252" Print the rules in a chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000253" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700254" --zero -Z [chain [rulenum]]\n"
255" Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000256" --new -N chain Create a new user-defined chain\n"
257" --delete-chain\n"
258" -X [chain] Delete a user-defined chain\n"
259" --policy -P chain target\n"
260" Change policy on chain to target\n"
261" --rename-chain\n"
262" -E old-chain new-chain\n"
263" Change chain name, (moving any references)\n"
264
265"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200266"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100267"[!] --source -s address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000268" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100269"[!] --destination -d address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000270" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200271"[!] --in-interface -i input name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000272" network interface name ([+] for wildcard)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200273" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000274" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000275#ifdef IPT_F_GOTO
276" --goto -g chain\n"
277" jump to chain with no return\n"
278#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000279" --match -m match\n"
280" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000281" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200282"[!] --out-interface -o output name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000283" network interface name ([+] for wildcard)\n"
284" --table -t table table to manipulate (default: `filter')\n"
285" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000286" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000287" --exact -x expand numbers (display exact values)\n"
288"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000289" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000290" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000291"[!] --version -V print package version.\n");
292
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200293 print_extension_helps(xtables_targets, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000294 exit(0);
295}
296
Patrick McHardy0b639362007-09-08 16:00:01 +0000297void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100298iptables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000299{
300 va_list args;
301
302 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500303 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000304 vfprintf(stderr, msg, args);
305 va_end(args);
306 fprintf(stderr, "\n");
307 if (status == PARAMETER_PROBLEM)
308 exit_tryhelp(status);
309 if (status == VERSION_PROBLEM)
310 fprintf(stderr,
311 "Perhaps iptables or your kernel needs to be upgraded.\n");
312 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500313 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000314 exit(status);
315}
316
Marc Bouchere6869a82000-03-20 06:03:29 +0000317static void
318generic_opt_check(int command, int options)
319{
320 int i, j, legal = 0;
321
322 /* Check that commands are valid with options. Complicated by the
323 * fact that if an option is legal with *any* command given, it is
324 * legal overall (ie. -z and -l).
325 */
326 for (i = 0; i < NUMBER_OF_OPT; i++) {
327 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
328
329 for (j = 0; j < NUMBER_OF_CMD; j++) {
330 if (!(command & (1<<j)))
331 continue;
332
333 if (!(options & (1<<i))) {
334 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100335 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000336 "You need to supply the `-%c' "
337 "option for this command\n",
338 optflags[i]);
339 } else {
340 if (commands_v_options[j][i] != 'x')
341 legal = 1;
342 else if (legal == 0)
343 legal = -1;
344 }
345 }
346 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100347 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000348 "Illegal option `-%c' with this command\n",
349 optflags[i]);
350 }
351}
352
353static char
354opt2char(int option)
355{
356 const char *ptr;
357 for (ptr = optflags; option > 1; option >>= 1, ptr++);
358
359 return *ptr;
360}
361
362static char
363cmd2char(int option)
364{
365 const char *ptr;
366 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
367
368 return *ptr;
369}
370
371static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000372add_command(unsigned int *cmd, const int newcmd, const int othercmds,
373 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000374{
375 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100376 xtables_error(PARAMETER_PROBLEM, "unexpected ! flag");
Marc Bouchere6869a82000-03-20 06:03:29 +0000377 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100378 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Marc Bouchere6869a82000-03-20 06:03:29 +0000379 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
380 *cmd |= newcmd;
381}
382
Marc Bouchere6869a82000-03-20 06:03:29 +0000383/*
384 * All functions starting with "parse" should succeed, otherwise
385 * the program fails.
386 * Most routines return pointers to static data that may change
387 * between calls to the same or other routines with a few exceptions:
388 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
389 * return global static data.
390*/
391
Rusty Russell28381a42000-05-10 00:19:50 +0000392/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000393/* Can't be zero. */
394static int
395parse_rulenumber(const char *rule)
396{
Harald Welteed498492001-07-23 01:24:22 +0000397 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000398
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100399 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100400 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000401 "Invalid rule number `%s'", rule);
402
403 return rulenum;
404}
405
406static const char *
407parse_target(const char *targetname)
408{
409 const char *ptr;
410
411 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100412 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000413 "Invalid target name (too short)");
414
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200415 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100416 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000417 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200418 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000419
420 for (ptr = targetname; *ptr; ptr++)
421 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100422 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000423 "Invalid target name `%s'", targetname);
424 return targetname;
425}
426
Marc Bouchere6869a82000-03-20 06:03:29 +0000427static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100428set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Marc Bouchere6869a82000-03-20 06:03:29 +0000429 int invert)
430{
431 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100432 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 opt2char(option));
434 *options |= option;
435
436 if (invert) {
437 unsigned int i;
438 for (i = 0; 1 << i != option; i++);
439
440 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100441 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000442 "cannot have ! before -%c",
443 opt2char(option));
444 *invflg |= inverse_for_options[i];
445 }
446}
447
Marc Bouchere6869a82000-03-20 06:03:29 +0000448static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100449print_num(uint64_t number, unsigned int format)
Harald Weltea0b4f792001-03-25 19:55:04 +0000450{
451 if (format & FMT_KILOMEGAGIGA) {
452 if (number > 99999) {
453 number = (number + 500) / 1000;
454 if (number > 9999) {
455 number = (number + 500) / 1000;
456 if (number > 9999) {
457 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000458 if (number > 9999) {
459 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000460 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000461 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000462 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000463 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000464 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000465 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000466 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000467 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000468 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000469 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000470 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000471}
472
473
474static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100475print_header(unsigned int format, const char *chain, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000476{
477 struct ipt_counters counters;
478 const char *pol = iptc_get_policy(chain, &counters, handle);
479 printf("Chain %s", chain);
480 if (pol) {
481 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000482 if (!(format & FMT_NOCOUNTS)) {
483 fputc(' ', stdout);
484 print_num(counters.pcnt, (format|FMT_NOTABLE));
485 fputs("packets, ", stdout);
486 print_num(counters.bcnt, (format|FMT_NOTABLE));
487 fputs("bytes", stdout);
488 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000489 printf(")\n");
490 } else {
491 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000492 if (!iptc_get_references(&refs, chain, handle))
493 printf(" (ERROR obtaining refs)\n");
494 else
495 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000496 }
497
498 if (format & FMT_LINENUMBERS)
499 printf(FMT("%-4s ", "%s "), "num");
500 if (!(format & FMT_NOCOUNTS)) {
501 if (format & FMT_KILOMEGAGIGA) {
502 printf(FMT("%5s ","%s "), "pkts");
503 printf(FMT("%5s ","%s "), "bytes");
504 } else {
505 printf(FMT("%8s ","%s "), "pkts");
506 printf(FMT("%10s ","%s "), "bytes");
507 }
508 }
509 if (!(format & FMT_NOTARGET))
510 printf(FMT("%-9s ","%s "), "target");
511 fputs(" prot ", stdout);
512 if (format & FMT_OPTIONS)
513 fputs("opt", stdout);
514 if (format & FMT_VIA) {
515 printf(FMT(" %-6s ","%s "), "in");
516 printf(FMT("%-6s ","%s "), "out");
517 }
518 printf(FMT(" %-19s ","%s "), "source");
519 printf(FMT(" %-19s "," %s "), "destination");
520 printf("\n");
521}
522
Marc Bouchere6869a82000-03-20 06:03:29 +0000523
524static int
525print_match(const struct ipt_entry_match *m,
526 const struct ipt_ip *ip,
527 int numeric)
528{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100529 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100530 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000531
532 if (match) {
533 if (match->print)
534 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000535 else
Rusty Russellb039b022000-09-01 06:04:05 +0000536 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000537 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000538 if (m->u.user.name[0])
539 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000540 }
541 /* Don't stop iterating. */
542 return 0;
543}
544
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100545/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000546static void
547print_firewall(const struct ipt_entry *fw,
548 const char *targname,
549 unsigned int num,
550 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100551 struct iptc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000552{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100553 const struct xtables_target *target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000554 const struct ipt_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100555 uint8_t flags;
Marc Bouchere6869a82000-03-20 06:03:29 +0000556 char buf[BUFSIZ];
557
Marc Bouchere6869a82000-03-20 06:03:29 +0000558 if (!iptc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100559 target = xtables_find_target(targname, XTF_TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000560 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100561 target = xtables_find_target(IPT_STANDARD_TARGET,
562 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000563
564 t = ipt_get_target((struct ipt_entry *)fw);
565 flags = fw->ip.flags;
566
567 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200568 printf(FMT("%-4u ", "%u "), num);
Marc Bouchere6869a82000-03-20 06:03:29 +0000569
570 if (!(format & FMT_NOCOUNTS)) {
571 print_num(fw->counters.pcnt, format);
572 print_num(fw->counters.bcnt, format);
573 }
574
575 if (!(format & FMT_NOTARGET))
576 printf(FMT("%-9s ", "%s "), targname);
577
578 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
579 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100580 const char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000581 if (pname)
582 printf(FMT("%-5s", "%s "), pname);
583 else
584 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
585 }
586
587 if (format & FMT_OPTIONS) {
588 if (format & FMT_NOTABLE)
589 fputs("opt ", stdout);
590 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
591 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
592 fputc(' ', stdout);
593 }
594
595 if (format & FMT_VIA) {
596 char iface[IFNAMSIZ+2];
597
598 if (fw->ip.invflags & IPT_INV_VIA_IN) {
599 iface[0] = '!';
600 iface[1] = '\0';
601 }
602 else iface[0] = '\0';
603
604 if (fw->ip.iniface[0] != '\0') {
605 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000606 }
607 else if (format & FMT_NUMERIC) strcat(iface, "*");
608 else strcat(iface, "any");
609 printf(FMT(" %-6s ","in %s "), iface);
610
611 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
612 iface[0] = '!';
613 iface[1] = '\0';
614 }
615 else iface[0] = '\0';
616
617 if (fw->ip.outiface[0] != '\0') {
618 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000619 }
620 else if (format & FMT_NUMERIC) strcat(iface, "*");
621 else strcat(iface, "any");
622 printf(FMT("%-6s ","out %s "), iface);
623 }
624
625 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
626 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
627 printf(FMT("%-19s ","%s "), "anywhere");
628 else {
629 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100630 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000631 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100632 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.src));
633 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000634 printf(FMT("%-19s ","%s "), buf);
635 }
636
637 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
638 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000639 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000640 else {
641 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100642 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000643 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100644 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.dst));
645 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000646 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000647 }
648
649 if (format & FMT_NOTABLE)
650 fputs(" ", stdout);
651
Harald Welte72bd87e2005-11-24 17:04:05 +0000652#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000653 if(fw->ip.flags & IPT_F_GOTO)
654 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000655#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000656
Marc Bouchere6869a82000-03-20 06:03:29 +0000657 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
658
659 if (target) {
660 if (target->print)
661 /* Print the target information. */
662 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000663 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000664 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000665 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000666
667 if (!(format & FMT_NONEWLINE))
668 fputc('\n', stdout);
669}
670
671static void
672print_firewall_line(const struct ipt_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100673 struct iptc_handle *const h)
Marc Bouchere6869a82000-03-20 06:03:29 +0000674{
675 struct ipt_entry_target *t;
676
677 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000678 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000679}
680
681static int
682append_entry(const ipt_chainlabel chain,
683 struct ipt_entry *fw,
684 unsigned int nsaddrs,
685 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100686 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000687 unsigned int ndaddrs,
688 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100689 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000690 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100691 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000692{
693 unsigned int i, j;
694 int ret = 1;
695
696 for (i = 0; i < nsaddrs; i++) {
697 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100698 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000699 for (j = 0; j < ndaddrs; j++) {
700 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100701 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000702 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100703 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 ret &= iptc_append_entry(chain, fw, handle);
705 }
706 }
707
708 return ret;
709}
710
711static int
712replace_entry(const ipt_chainlabel chain,
713 struct ipt_entry *fw,
714 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100715 const struct in_addr *saddr, const struct in_addr *smask,
716 const struct in_addr *daddr, const struct in_addr *dmask,
Marc Bouchere6869a82000-03-20 06:03:29 +0000717 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100718 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000719{
720 fw->ip.src.s_addr = saddr->s_addr;
721 fw->ip.dst.s_addr = daddr->s_addr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100722 fw->ip.smsk.s_addr = smask->s_addr;
723 fw->ip.dmsk.s_addr = dmask->s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000724
725 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100726 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000727 return iptc_replace_entry(chain, fw, rulenum, handle);
728}
729
730static int
731insert_entry(const ipt_chainlabel chain,
732 struct ipt_entry *fw,
733 unsigned int rulenum,
734 unsigned int nsaddrs,
735 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100736 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000737 unsigned int ndaddrs,
738 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100739 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000740 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100741 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000742{
743 unsigned int i, j;
744 int ret = 1;
745
746 for (i = 0; i < nsaddrs; i++) {
747 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100748 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 for (j = 0; j < ndaddrs; j++) {
750 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100751 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000752 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100753 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000754 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
755 }
756 }
757
758 return ret;
759}
760
Rusty Russell2e0a3212000-04-19 11:23:18 +0000761static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100762make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100763 const struct xtables_target *target)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000764{
765 /* Establish mask for comparison */
766 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100767 const struct xtables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000768 unsigned char *mask, *mptr;
769
770 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000771 for (matchp = matches; matchp; matchp = matchp->next)
772 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000773
Jan Engelhardt630ef482009-01-27 14:58:41 +0100774 mask = xtables_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000775 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100776 + target->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000777
Rusty Russell9e1d2142000-04-23 09:11:12 +0000778 memset(mask, 0xFF, sizeof(struct ipt_entry));
779 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000780
Martin Josefsson78cafda2004-02-02 20:01:18 +0000781 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000782 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000783 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000784 + matchp->match->userspacesize);
785 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000786 }
787
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000788 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000789 IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100790 + target->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000791
792 return mask;
793}
794
Marc Bouchere6869a82000-03-20 06:03:29 +0000795static int
796delete_entry(const ipt_chainlabel chain,
797 struct ipt_entry *fw,
798 unsigned int nsaddrs,
799 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100800 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000801 unsigned int ndaddrs,
802 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100803 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000804 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100805 struct iptc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100806 struct xtables_rule_match *matches,
807 const struct xtables_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000808{
809 unsigned int i, j;
810 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000811 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000812
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100813 mask = make_delete_mask(matches, target);
Marc Bouchere6869a82000-03-20 06:03:29 +0000814 for (i = 0; i < nsaddrs; i++) {
815 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100816 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000817 for (j = 0; j < ndaddrs; j++) {
818 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100819 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100821 print_firewall_line(fw, handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000822 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000823 }
824 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000825 free(mask);
826
Marc Bouchere6869a82000-03-20 06:03:29 +0000827 return ret;
828}
829
Harald Welteae1ff9f2000-12-01 14:26:20 +0000830int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100831for_each_chain(int (*fn)(const ipt_chainlabel, int, struct iptc_handle *),
832 int verbose, int builtinstoo, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000833{
834 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000835 const char *chain;
836 char *chains;
837 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000838
Rusty Russell9e1d2142000-04-23 09:11:12 +0000839 chain = iptc_first_chain(handle);
840 while (chain) {
841 chaincount++;
842 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000843 }
844
Jan Engelhardt630ef482009-01-27 14:58:41 +0100845 chains = xtables_malloc(sizeof(ipt_chainlabel) * chaincount);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000846 i = 0;
847 chain = iptc_first_chain(handle);
848 while (chain) {
849 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
850 i++;
851 chain = iptc_next_chain(handle);
852 }
853
854 for (i = 0; i < chaincount; i++) {
855 if (!builtinstoo
856 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100857 handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000858 continue;
859 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
860 }
861
862 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000863 return ret;
864}
865
Harald Welteae1ff9f2000-12-01 14:26:20 +0000866int
Marc Bouchere6869a82000-03-20 06:03:29 +0000867flush_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100868 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000869{
870 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000871 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000872
873 if (verbose)
874 fprintf(stdout, "Flushing chain `%s'\n", chain);
875 return iptc_flush_entries(chain, handle);
876}
Marc Bouchere6869a82000-03-20 06:03:29 +0000877
878static int
879zero_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100880 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000881{
882 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000883 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000884
Marc Bouchere6869a82000-03-20 06:03:29 +0000885 if (verbose)
886 fprintf(stdout, "Zeroing chain `%s'\n", chain);
887 return iptc_zero_entries(chain, handle);
888}
889
Harald Welteae1ff9f2000-12-01 14:26:20 +0000890int
Marc Bouchere6869a82000-03-20 06:03:29 +0000891delete_chain(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100892 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000893{
Rusty Russell9e1d2142000-04-23 09:11:12 +0000894 if (!chain)
895 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000896
897 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000898 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000899 return iptc_delete_chain(chain, handle);
900}
901
902static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200903list_entries(const ipt_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100904 int expanded, int linenumbers, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000905{
906 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000907 unsigned int format;
908 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +0000909
910 format = FMT_OPTIONS;
911 if (!verbose)
912 format |= FMT_NOCOUNTS;
913 else
914 format |= FMT_VIA;
915
916 if (numeric)
917 format |= FMT_NUMERIC;
918
919 if (!expanded)
920 format |= FMT_KILOMEGAGIGA;
921
922 if (linenumbers)
923 format |= FMT_LINENUMBERS;
924
Rusty Russell9e1d2142000-04-23 09:11:12 +0000925 for (this = iptc_first_chain(handle);
926 this;
927 this = iptc_next_chain(handle)) {
928 const struct ipt_entry *i;
929 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +0000930
Marc Bouchere6869a82000-03-20 06:03:29 +0000931 if (chain && strcmp(chain, this) != 0)
932 continue;
933
934 if (found) printf("\n");
935
Henrik Nordstrombb340822008-05-13 13:09:23 +0200936 if (!rulenum)
937 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000938 i = iptc_first_rule(this, handle);
939
940 num = 0;
941 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200942 num++;
943 if (!rulenum || num == rulenum)
944 print_firewall(i,
945 iptc_get_target(i, handle),
946 num,
947 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100948 handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000949 i = iptc_next_rule(i, handle);
950 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000951 found = 1;
952 }
953
954 errno = ENOENT;
955 return found;
956}
957
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100958static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200959{
960 if (proto) {
961 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100962 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200963
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100964 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200965 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100966 printf("%s -p %s", invertstr, pent->p_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200967 return;
968 }
969
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100970 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
971 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100972 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100973 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200974 return;
975 }
976
Jan Engelhardt73866352010-12-18 02:04:59 +0100977 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200978 }
979}
980
981#define IP_PARTS_NATIVE(n) \
982(unsigned int)((n)>>24)&0xFF, \
983(unsigned int)((n)>>16)&0xFF, \
984(unsigned int)((n)>>8)&0xFF, \
985(unsigned int)((n)&0xFF)
986
987#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
988
989/* This assumes that mask is contiguous, and byte-bounded. */
990static void
991print_iface(char letter, const char *iface, const unsigned char *mask,
992 int invert)
993{
994 unsigned int i;
995
996 if (mask[0] == 0)
997 return;
998
Jan Engelhardt73866352010-12-18 02:04:59 +0100999 printf("%s -%c ", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001000
1001 for (i = 0; i < IFNAMSIZ; i++) {
1002 if (mask[i] != 0) {
1003 if (iface[i] != '\0')
1004 printf("%c", iface[i]);
1005 } else {
1006 /* we can access iface[i-1] here, because
1007 * a few lines above we make sure that mask[0] != 0 */
1008 if (iface[i-1] != '\0')
1009 printf("+");
1010 break;
1011 }
1012 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001013}
1014
1015static int print_match_save(const struct ipt_entry_match *e,
1016 const struct ipt_ip *ip)
1017{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001018 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001019 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001020
1021 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001022 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001023
1024 /* some matches don't provide a save function */
1025 if (match->save)
1026 match->save(ip, e);
1027 } else {
1028 if (e->u.match_size) {
1029 fprintf(stderr,
1030 "Can't find library for match `%s'\n",
1031 e->u.user.name);
1032 exit(1);
1033 }
1034 }
1035 return 0;
1036}
1037
1038/* print a given ip including mask if neccessary */
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001039static void print_ip(const char *prefix, uint32_t ip,
1040 uint32_t mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001041{
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001042 uint32_t bits, hmask = ntohl(mask);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001043 int i;
1044
1045 if (!mask && !ip && !invert)
1046 return;
1047
Jan Engelhardt73866352010-12-18 02:04:59 +01001048 printf("%s %s %u.%u.%u.%u",
1049 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001050 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001051 IP_PARTS(ip));
1052
1053 if (mask == 0xFFFFFFFFU) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001054 printf("/32");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001055 return;
1056 }
1057
1058 i = 32;
1059 bits = 0xFFFFFFFEU;
1060 while (--i >= 0 && hmask != bits)
1061 bits <<= 1;
1062 if (i >= 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001063 printf("/%u", i);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001064 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001065 printf("/%u.%u.%u.%u", IP_PARTS(mask));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001066}
1067
1068/* We want this to be readable, so only print out neccessary fields.
1069 * Because that's the kind of world I want to live in. */
1070void print_rule(const struct ipt_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001071 struct iptc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001072{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001073 const struct ipt_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001074 const char *target_name;
1075
1076 /* print counters for iptables-save */
1077 if (counters > 0)
1078 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1079
1080 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001081 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001082
1083 /* Print IP part. */
1084 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
1085 e->ip.invflags & IPT_INV_SRCIP);
1086
1087 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
1088 e->ip.invflags & IPT_INV_DSTIP);
1089
1090 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
1091 e->ip.invflags & IPT_INV_VIA_IN);
1092
1093 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
1094 e->ip.invflags & IPT_INV_VIA_OUT);
1095
1096 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
1097
1098 if (e->ip.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001099 printf("%s -f",
1100 e->ip.invflags & IPT_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001101
1102 /* Print matchinfo part */
1103 if (e->target_offset) {
1104 IPT_MATCH_ITERATE(e, print_match_save, &e->ip);
1105 }
1106
1107 /* print counters for iptables -R */
1108 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001109 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001110
1111 /* Print target name */
1112 target_name = iptc_get_target(e, h);
1113 if (target_name && (*target_name != '\0'))
1114#ifdef IPT_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001115 printf(" -%c %s", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001116#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001117 printf(" -j %s", target_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001118#endif
1119
1120 /* Print targinfo part */
1121 t = ipt_get_target((struct ipt_entry *)e);
1122 if (t->u.user.name[0]) {
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001123 const struct xtables_target *target =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001124 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001125
1126 if (!target) {
1127 fprintf(stderr, "Can't find library for target `%s'\n",
1128 t->u.user.name);
1129 exit(1);
1130 }
1131
1132 if (target->save)
1133 target->save(&e->ip, t);
1134 else {
1135 /* If the target size is greater than ipt_entry_target
1136 * there is something to be saved, we just don't know
1137 * how to print it */
1138 if (t->u.target_size !=
1139 sizeof(struct ipt_entry_target)) {
1140 fprintf(stderr, "Target `%s' is missing "
1141 "save function\n",
1142 t->u.user.name);
1143 exit(1);
1144 }
1145 }
1146 }
1147 printf("\n");
1148}
1149
1150static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001151list_rules(const ipt_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001152 struct iptc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001153{
1154 const char *this = NULL;
1155 int found = 0;
1156
1157 if (counters)
1158 counters = -1; /* iptables -c format */
1159
1160 /* Dump out chain names first,
1161 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001162 if (!rulenum) for (this = iptc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001163 this;
1164 this = iptc_next_chain(handle)) {
1165 if (chain && strcmp(this, chain) != 0)
1166 continue;
1167
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001168 if (iptc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001169 struct ipt_counters count;
1170 printf("-P %s %s", this, iptc_get_policy(this, &count, handle));
1171 if (counters)
1172 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1173 printf("\n");
1174 } else {
1175 printf("-N %s\n", this);
1176 }
1177 }
1178
1179 for (this = iptc_first_chain(handle);
1180 this;
1181 this = iptc_next_chain(handle)) {
1182 const struct ipt_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001183 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001184
1185 if (chain && strcmp(this, chain) != 0)
1186 continue;
1187
1188 /* Dump out rules */
1189 e = iptc_first_rule(this, handle);
1190 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001191 num++;
1192 if (!rulenum || num == rulenum)
1193 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001194 e = iptc_next_rule(e, handle);
1195 }
1196 found = 1;
1197 }
1198
1199 errno = ENOENT;
1200 return found;
1201}
1202
Marc Bouchere6869a82000-03-20 06:03:29 +00001203static struct ipt_entry *
1204generate_entry(const struct ipt_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001205 struct xtables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001206 struct ipt_entry_target *target)
1207{
1208 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001209 struct xtables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001210 struct ipt_entry *e;
1211
1212 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001213 for (matchp = matches; matchp; matchp = matchp->next)
1214 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001215
Jan Engelhardt630ef482009-01-27 14:58:41 +01001216 e = xtables_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001217 *e = *fw;
1218 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001219 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001220
1221 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001222 for (matchp = matches; matchp; matchp = matchp->next) {
1223 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1224 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001225 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001226 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001227
1228 return e;
1229}
1230
Jan Engelhardt395e4412009-02-10 10:43:08 +01001231static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001232{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001233 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001234
1235 for (matchp = *matches; matchp;) {
1236 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001237 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001238 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001239 matchp->match->m = NULL;
1240 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001241 if (matchp->match == matchp->match->next) {
1242 free(matchp->match);
1243 matchp->match = NULL;
1244 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001245 free(matchp);
1246 matchp = tmp;
1247 }
1248
1249 *matches = NULL;
1250}
1251
Phil Oester8cf65912005-09-19 15:00:33 +00001252void
1253get_kernel_version(void) {
1254 static struct utsname uts;
1255 int x = 0, y = 0, z = 0;
1256
1257 if (uname(&uts) == -1) {
1258 fprintf(stderr, "Unable to retrieve kernel version.\n");
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001259 xtables_free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001260 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001261 }
1262
1263 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1264 kernel_version = LINUX_VERSION(x, y, z);
1265}
1266
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001267static void command_default(struct iptables_command_state *cs)
1268{
1269 struct xtables_rule_match *matchp;
1270 struct xtables_match *m;
1271
Jan Engelhardtaf3d73e2011-02-11 01:45:26 +01001272 if (cs->target != NULL && cs->target->parse != NULL &&
1273 cs->c >= cs->target->option_offset &&
1274 cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001275 cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
1276 cs->invert, &cs->target->tflags, &cs->fw,
1277 &cs->target->t);
1278 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001279 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001280
1281 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1282 m = matchp->match;
1283
1284 if (matchp->completed || m->parse == NULL)
1285 continue;
1286 if (cs->c < m->option_offset ||
1287 cs->c >= m->option_offset + XT_OPTION_OFFSET_SCALE)
1288 continue;
1289 m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
1290 &m->mflags, &cs->fw, &m->m);
1291 return;
1292 }
1293
1294 /* Try loading protocol */
1295 m = load_proto(cs);
1296 if (m != NULL) {
1297 size_t size;
1298
1299 cs->proto_used = 1;
1300
1301 size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
1302
1303 m->m = xtables_calloc(1, size);
1304 m->m->u.match_size = size;
1305 strcpy(m->m->u.user.name, m->name);
1306 m->m->u.user.revision = m->revision;
1307 if (m->init != NULL)
1308 m->init(m->m);
1309
1310 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1311 m->extra_opts, &m->option_offset);
1312 if (opts == NULL)
1313 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1314
1315 optind--;
1316 return;
1317 }
1318
Jan Engelhardt58b491f2011-02-07 03:45:26 +01001319 if (cs->c == ':')
1320 xtables_error(PARAMETER_PROBLEM, "option \"%s\" "
1321 "requires an argument", cs->argv[optind-1]);
1322 if (cs->c == '?')
1323 xtables_error(PARAMETER_PROBLEM, "unknown option "
1324 "\"%s\"", cs->argv[optind-1]);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001325 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001326}
1327
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001328static void command_jump(struct iptables_command_state *cs)
1329{
1330 size_t size;
1331
1332 set_option(&cs->options, OPT_JUMP, &cs->fw.ip.invflags, cs->invert);
1333 cs->jumpto = parse_target(optarg);
1334 /* TRY_LOAD (may be chain name) */
1335 cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1336
1337 if (cs->target == NULL)
1338 return;
1339
1340 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1341 + cs->target->size;
1342
1343 cs->target->t = xtables_calloc(1, size);
1344 cs->target->t->u.target_size = size;
1345 strcpy(cs->target->t->u.user.name, cs->jumpto);
1346 cs->target->t->u.user.revision = cs->target->revision;
1347 if (cs->target->init != NULL)
1348 cs->target->init(cs->target->t);
1349 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1350 cs->target->extra_opts,
1351 &cs->target->option_offset);
1352 if (opts == NULL)
1353 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1354}
1355
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001356static void command_match(struct iptables_command_state *cs)
1357{
1358 struct xtables_match *m;
1359 size_t size;
1360
1361 if (cs->invert)
1362 xtables_error(PARAMETER_PROBLEM,
1363 "unexpected ! flag before --match");
1364
1365 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1366 size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
1367 m->m = xtables_calloc(1, size);
1368 m->m->u.match_size = size;
1369 strcpy(m->m->u.user.name, m->name);
1370 m->m->u.user.revision = m->revision;
1371 if (m->init != NULL)
1372 m->init(m->m);
1373 if (m != m->next) {
1374 /* Merge options for non-cloned matches */
1375 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1376 m->extra_opts, &m->option_offset);
1377 if (opts == NULL)
1378 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1379 }
1380}
1381
Jan Engelhardtfd187312008-11-10 16:59:27 +01001382int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001383{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001384 struct iptables_command_state cs;
1385 struct ipt_entry *e = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001386 unsigned int nsaddrs = 0, ndaddrs = 0;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001387 struct in_addr *saddrs = NULL, *smasks = NULL;
1388 struct in_addr *daddrs = NULL, *dmasks = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001389
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001390 int verbose = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001391 const char *chain = NULL;
1392 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1393 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001394 unsigned int rulenum = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001395 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001396 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001397 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001398 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001399 struct xtables_target *t;
Patrick McHardy875441e2007-10-17 08:48:58 +00001400 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001401
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001402 memset(&cs, 0, sizeof(cs));
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001403 cs.jumpto = "";
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001404 cs.argv = argv;
Marc Bouchere6869a82000-03-20 06:03:29 +00001405
Harald Welteae1ff9f2000-12-01 14:26:20 +00001406 /* re-set optind to 0 in case do_command gets called
1407 * a second time */
1408 optind = 0;
1409
1410 /* clear mflags in case do_command gets called a second time
1411 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001412 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001413 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001414
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001415 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001416 t->tflags = 0;
1417 t->used = 0;
1418 }
1419
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 /* Suppress error messages: we may add new options if we
1421 demand-load a protocol. */
1422 opterr = 0;
1423
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001424 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001425 while ((cs.c = getopt_long(argc, argv,
Jan Engelhardt58b491f2011-02-07 03:45:26 +01001426 "-:A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001427 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001428 switch (cs.c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001429 /*
1430 * Command selection
1431 */
1432 case 'A':
1433 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001434 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001435 chain = optarg;
1436 break;
1437
1438 case 'D':
1439 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001440 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001441 chain = optarg;
1442 if (optind < argc && argv[optind][0] != '-'
1443 && argv[optind][0] != '!') {
1444 rulenum = parse_rulenumber(argv[optind++]);
1445 command = CMD_DELETE_NUM;
1446 }
1447 break;
1448
Marc Bouchere6869a82000-03-20 06:03:29 +00001449 case 'R':
1450 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001451 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001452 chain = optarg;
1453 if (optind < argc && argv[optind][0] != '-'
1454 && argv[optind][0] != '!')
1455 rulenum = parse_rulenumber(argv[optind++]);
1456 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001457 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001458 "-%c requires a rule number",
1459 cmd2char(CMD_REPLACE));
1460 break;
1461
1462 case 'I':
1463 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001464 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001465 chain = optarg;
1466 if (optind < argc && argv[optind][0] != '-'
1467 && argv[optind][0] != '!')
1468 rulenum = parse_rulenumber(argv[optind++]);
1469 else rulenum = 1;
1470 break;
1471
1472 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001473 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001474 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001475 if (optarg) chain = optarg;
1476 else if (optind < argc && argv[optind][0] != '-'
1477 && argv[optind][0] != '!')
1478 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001479 if (optind < argc && argv[optind][0] != '-'
1480 && argv[optind][0] != '!')
1481 rulenum = parse_rulenumber(argv[optind++]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001482 break;
1483
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001484 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001485 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001486 CMD_ZERO|CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001487 if (optarg) chain = optarg;
1488 else if (optind < argc && argv[optind][0] != '-'
1489 && argv[optind][0] != '!')
1490 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001491 if (optind < argc && argv[optind][0] != '-'
1492 && argv[optind][0] != '!')
1493 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001494 break;
1495
Marc Bouchere6869a82000-03-20 06:03:29 +00001496 case 'F':
1497 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001498 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001499 if (optarg) chain = optarg;
1500 else if (optind < argc && argv[optind][0] != '-'
1501 && argv[optind][0] != '!')
1502 chain = argv[optind++];
1503 break;
1504
1505 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001506 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001507 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001508 if (optarg) chain = optarg;
1509 else if (optind < argc && argv[optind][0] != '-'
1510 && argv[optind][0] != '!')
1511 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001512 if (optind < argc && argv[optind][0] != '-'
1513 && argv[optind][0] != '!') {
1514 rulenum = parse_rulenumber(argv[optind++]);
1515 command = CMD_ZERO_NUM;
1516 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001517 break;
1518
1519 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001520 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001521 xtables_error(PARAMETER_PROBLEM,
Harald Welte6336bfd2002-05-07 14:41:43 +00001522 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001523 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001524 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001525 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001526 "chain name may not clash "
1527 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001528 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001529 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001530 chain = optarg;
1531 break;
1532
1533 case 'X':
1534 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001535 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001536 if (optarg) chain = optarg;
1537 else if (optind < argc && argv[optind][0] != '-'
1538 && argv[optind][0] != '!')
1539 chain = argv[optind++];
1540 break;
1541
1542 case 'E':
1543 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001544 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001545 chain = optarg;
1546 if (optind < argc && argv[optind][0] != '-'
1547 && argv[optind][0] != '!')
1548 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001549 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001550 xtables_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001551 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001552 "new-chain-name",
1553 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001554 break;
1555
1556 case 'P':
1557 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001558 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001559 chain = optarg;
1560 if (optind < argc && argv[optind][0] != '-'
1561 && argv[optind][0] != '!')
1562 policy = argv[optind++];
1563 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001564 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001565 "-%c requires a chain and a policy",
1566 cmd2char(CMD_SET_POLICY));
1567 break;
1568
1569 case 'h':
1570 if (!optarg)
1571 optarg = argv[optind];
1572
Rusty Russell2e0a3212000-04-19 11:23:18 +00001573 /* iptables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001574 if (!cs.matches && cs.protocol)
1575 xtables_find_match(cs.protocol,
1576 XTF_TRY_LOAD, &cs.matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001577
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001578 exit_printhelp(cs.matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001579
1580 /*
1581 * Option selection
1582 */
1583 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001584 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1585 set_option(&cs.options, OPT_PROTOCOL, &cs.fw.ip.invflags,
1586 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001587
1588 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001589 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1590 *cs.protocol = tolower(*cs.protocol);
Marc Bouchere6869a82000-03-20 06:03:29 +00001591
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001592 cs.protocol = optarg;
1593 cs.fw.ip.proto = xtables_parse_protocol(cs.protocol);
Marc Bouchere6869a82000-03-20 06:03:29 +00001594
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001595 if (cs.fw.ip.proto == 0
1596 && (cs.fw.ip.invflags & IPT_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001597 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001598 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001599 break;
1600
1601 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001602 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1603 set_option(&cs.options, OPT_SOURCE, &cs.fw.ip.invflags,
1604 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001605 shostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001606 break;
1607
1608 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001609 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1610 set_option(&cs.options, OPT_DESTINATION, &cs.fw.ip.invflags,
1611 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001612 dhostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001613 break;
1614
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001615#ifdef IPT_F_GOTO
1616 case 'g':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001617 set_option(&cs.options, OPT_JUMP, &cs.fw.ip.invflags,
1618 cs.invert);
1619 cs.fw.ip.flags |= IPT_F_GOTO;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001620 cs.jumpto = parse_target(optarg);
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001621 break;
1622#endif
1623
Marc Bouchere6869a82000-03-20 06:03:29 +00001624 case 'j':
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001625 command_jump(&cs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001626 break;
1627
1628
1629 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001630 if (*optarg == '\0')
1631 xtables_error(PARAMETER_PROBLEM,
1632 "Empty interface is likely to be "
1633 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001634 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1635 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw.ip.invflags,
1636 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001637 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001638 cs.fw.ip.iniface,
1639 cs.fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001640 break;
1641
1642 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001643 if (*optarg == '\0')
1644 xtables_error(PARAMETER_PROBLEM,
1645 "Empty interface is likely to be "
1646 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001647 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1648 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw.ip.invflags,
1649 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001650 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001651 cs.fw.ip.outiface,
1652 cs.fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001653 break;
1654
1655 case 'f':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001656 set_option(&cs.options, OPT_FRAGMENT, &cs.fw.ip.invflags,
1657 cs.invert);
1658 cs.fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 break;
1660
1661 case 'v':
1662 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001663 set_option(&cs.options, OPT_VERBOSE,
1664 &cs.fw.ip.invflags, cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001665 verbose++;
1666 break;
1667
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001668 case 'm':
1669 command_match(&cs);
1670 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001671
1672 case 'n':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001673 set_option(&cs.options, OPT_NUMERIC, &cs.fw.ip.invflags,
1674 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001675 break;
1676
1677 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001678 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001679 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001681 *table = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001682 break;
1683
1684 case 'x':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001685 set_option(&cs.options, OPT_EXPANDED, &cs.fw.ip.invflags,
1686 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001687 break;
1688
1689 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001690 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001691 printf("Not %s ;-)\n", prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001692 else
1693 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001694 prog_name, prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001695 exit(0);
1696
1697 case '0':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001698 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw.ip.invflags,
1699 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001700 break;
1701
Harald Welte82dd2ec2000-12-19 05:18:15 +00001702 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001703 xtables_modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001704 break;
1705
Harald Welteccd49e52001-01-23 22:54:34 +00001706 case 'c':
1707
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001708 set_option(&cs.options, OPT_COUNTERS, &cs.fw.ip.invflags,
1709 cs.invert);
Harald Welteccd49e52001-01-23 22:54:34 +00001710 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001711 bcnt = strchr(pcnt + 1, ',');
1712 if (bcnt)
1713 bcnt++;
1714 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welteccd49e52001-01-23 22:54:34 +00001715 && argv[optind][0] != '!')
1716 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001717 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001718 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001719 "-%c requires packet and byte counter",
1720 opt2char(OPT_COUNTERS));
1721
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001722 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001723 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001724 "-%c packet counter not numeric",
1725 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001726 cs.fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001727
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001728 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001729 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001730 "-%c byte counter not numeric",
1731 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001732 cs.fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001733 break;
1734
1735
Marc Bouchere6869a82000-03-20 06:03:29 +00001736 case 1: /* non option */
1737 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001738 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001739 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001740 "multiple consecutive ! not"
1741 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001742 cs.invert = TRUE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001743 optarg[0] = '\0';
1744 continue;
1745 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001746 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001747 exit_tryhelp(2);
1748
1749 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001750 command_default(&cs);
1751 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001752 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001753 cs.invert = FALSE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001754 }
1755
Jan Engelhardt1eada722008-08-13 14:41:32 +02001756 if (strcmp(*table, "nat") == 0 &&
1757 ((policy != NULL && strcmp(policy, "DROP") == 0) ||
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001758 (cs.jumpto != NULL && strcmp(cs.jumpto, "DROP") == 0)))
Jan Engelhardte0390be2009-03-15 21:22:49 +01001759 xtables_error(PARAMETER_PROBLEM,
1760 "\nThe \"nat\" table is not intended for filtering, "
1761 "the use of DROP is therefore inhibited.\n\n");
Jan Engelhardt1eada722008-08-13 14:41:32 +02001762
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001763 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001764 if (matchp->match->final_check != NULL)
1765 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001766
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001767 if (cs.target != NULL && cs.target->final_check != NULL)
1768 cs.target->final_check(cs.target->tflags);
Marc Bouchere6869a82000-03-20 06:03:29 +00001769
1770 /* Fix me: must put inverse options checking here --MN */
1771
1772 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001773 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001774 "unknown arguments found on commandline");
1775 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001776 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001777 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001778 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001779 "nothing appropriate following !");
1780
Harald Welte6336bfd2002-05-07 14:41:43 +00001781 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001782 if (!(cs.options & OPT_DESTINATION))
Marc Bouchere6869a82000-03-20 06:03:29 +00001783 dhostnetworkmask = "0.0.0.0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001784 if (!(cs.options & OPT_SOURCE))
Marc Bouchere6869a82000-03-20 06:03:29 +00001785 shostnetworkmask = "0.0.0.0/0";
1786 }
1787
1788 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001789 xtables_ipparse_multiple(shostnetworkmask, &saddrs,
1790 &smasks, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001791
1792 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001793 xtables_ipparse_multiple(dhostnetworkmask, &daddrs,
1794 &dmasks, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001795
1796 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001797 (cs.fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001798 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Marc Bouchere6869a82000-03-20 06:03:29 +00001799 " source or destination IP addresses");
1800
Marc Bouchere6869a82000-03-20 06:03:29 +00001801 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001802 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Marc Bouchere6869a82000-03-20 06:03:29 +00001803 "specify a unique address");
1804
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001805 generic_opt_check(command, cs.options);
Marc Bouchere6869a82000-03-20 06:03:29 +00001806
Jan Engelhardt5429b412010-09-13 15:45:15 +02001807 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001808 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001809 "chain name `%s' too long (must be under %u chars)",
1810 chain, XT_EXTENSION_MAXNAMELEN);
Marc Bouchere6869a82000-03-20 06:03:29 +00001811
Harald Welteae1ff9f2000-12-01 14:26:20 +00001812 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001813 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001814 *handle = iptc_init(*table);
1815
Rusty Russell8beb0492004-12-22 00:37:10 +00001816 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001817 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001818 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001819
Marc Bouchere6869a82000-03-20 06:03:29 +00001820 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001821 xtables_error(VERSION_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 "can't initialize iptables table `%s': %s",
1823 *table, iptc_strerror(errno));
1824
Harald Welte6336bfd2002-05-07 14:41:43 +00001825 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 || command == CMD_DELETE
1827 || command == CMD_INSERT
1828 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001829 if (strcmp(chain, "PREROUTING") == 0
1830 || strcmp(chain, "INPUT") == 0) {
1831 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001832 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001833 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001834 "Can't use -%c with %s\n",
1835 opt2char(OPT_VIANAMEOUT),
1836 chain);
1837 }
1838
Rusty Russella4860fd2000-06-17 16:13:02 +00001839 if (strcmp(chain, "POSTROUTING") == 0
1840 || strcmp(chain, "OUTPUT") == 0) {
1841 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001842 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001843 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001844 "Can't use -%c with %s\n",
1845 opt2char(OPT_VIANAMEIN),
1846 chain);
1847 }
1848
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001849 if (cs.target && iptc_is_chain(cs.jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001850 fprintf(stderr,
1851 "Warning: using chain %s, not extension\n",
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001852 cs.jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001853
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001854 if (cs.target->t)
1855 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001856
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001857 cs.target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 }
1859
1860 /* If they didn't specify a target, or it's a chain
1861 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001862 if (!cs.target
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001863 && (strlen(cs.jumpto) == 0
1864 || iptc_is_chain(cs.jumpto, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001865 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001866
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001867 cs.target = xtables_find_target(IPT_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001868 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001869
1870 size = sizeof(struct ipt_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001871 + cs.target->size;
1872 cs.target->t = xtables_calloc(1, size);
1873 cs.target->t->u.target_size = size;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001874 strcpy(cs.target->t->u.user.name, cs.jumpto);
1875 if (!iptc_is_chain(cs.jumpto, *handle))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001876 cs.target->t->u.user.revision = cs.target->revision;
1877 if (cs.target->init != NULL)
1878 cs.target->init(cs.target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 }
1880
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001881 if (!cs.target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001882 /* it is no chain, and we can't load a plugin.
1883 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001884 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001885 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001886#ifdef IPT_F_GOTO
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001887 if (cs.fw.ip.flags & IPT_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001888 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001889 "goto '%s' is not a chain\n",
1890 cs.jumpto);
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001891#endif
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001892 xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001893 } else {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001894 e = generate_entry(&cs.fw, cs.matches, cs.target->t);
1895 free(cs.target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001896 }
1897 }
1898
1899 switch (command) {
1900 case CMD_APPEND:
1901 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001902 nsaddrs, saddrs, smasks,
1903 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001904 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001905 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001906 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001907 case CMD_DELETE:
1908 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001909 nsaddrs, saddrs, smasks,
1910 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001911 cs.options&OPT_VERBOSE,
1912 *handle, cs.matches, cs.target);
Marc Bouchere6869a82000-03-20 06:03:29 +00001913 break;
1914 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001915 ret = iptc_delete_num_entry(chain, rulenum - 1, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001916 break;
1917 case CMD_REPLACE:
1918 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001919 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001920 cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001921 break;
1922 case CMD_INSERT:
1923 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001924 nsaddrs, saddrs, smasks,
1925 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001926 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001927 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001928 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001929 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001930 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001931 break;
1932 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001933 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001935 case CMD_ZERO_NUM:
1936 ret = iptc_zero_counter(chain, rulenum, *handle);
1937 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001938 case CMD_LIST:
Marc Bouchere6869a82000-03-20 06:03:29 +00001939 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001940 case CMD_LIST|CMD_ZERO_NUM:
Marc Bouchere6869a82000-03-20 06:03:29 +00001941 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001942 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001943 cs.options&OPT_VERBOSE,
1944 cs.options&OPT_NUMERIC,
1945 cs.options&OPT_EXPANDED,
1946 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001947 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001948 if (ret && (command & CMD_ZERO))
1949 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001950 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001951 if (ret && (command & CMD_ZERO_NUM))
1952 ret = iptc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001953 break;
1954 case CMD_LIST_RULES:
1955 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001956 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001957 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001958 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001959 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001960 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001961 if (ret && (command & CMD_ZERO))
Marc Bouchere6869a82000-03-20 06:03:29 +00001962 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001963 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001964 if (ret && (command & CMD_ZERO_NUM))
1965 ret = iptc_zero_counter(chain, rulenum, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001966 break;
1967 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001968 ret = iptc_create_chain(chain, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 break;
1970 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001971 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001972 break;
1973 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001974 ret = iptc_rename_chain(chain, newname, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001975 break;
1976 case CMD_SET_POLICY:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001977 ret = iptc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw.counters : NULL, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001978 break;
1979 default:
1980 /* We should never reach this... */
1981 exit_tryhelp(2);
1982 }
1983
1984 if (verbose > 1)
1985 dump_entries(*handle);
1986
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001987 clear_rule_matches(&cs.matches);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001988
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001989 if (e != NULL) {
1990 free(e);
1991 e = NULL;
1992 }
1993
keso6997cdf2004-07-04 15:20:53 +00001994 free(saddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01001995 free(smasks);
keso6997cdf2004-07-04 15:20:53 +00001996 free(daddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01001997 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001998 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001999
Marc Bouchere6869a82000-03-20 06:03:29 +00002000 return ret;
2001}