blob: cff4a7b3d41d09eb1584dde61c34fe341c215f76 [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
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010082#define CMD_CHECK 0x4000U
83#define NUMBER_OF_CMD 16
Marc Bouchere6869a82000-03-20 06:03:29 +000084static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010085 'Z', 'N', 'X', 'P', 'E', 'S', 'C' };
Marc Bouchere6869a82000-03-20 06:03:29 +000086
Jan Engelhardtf1e71012011-02-07 03:13:43 +010087#define OPT_FRAGMENT 0x00800U
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010088#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
89static const char optflags[]
Jan Engelhardtf1e71012011-02-07 03:13:43 +010090= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c', 'f'};
Marc Bouchere6869a82000-03-20 06:03:29 +000091
92static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010093 {.name = "append", .has_arg = 1, .val = 'A'},
94 {.name = "delete", .has_arg = 1, .val = 'D'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010095 {.name = "check", .has_arg = 1, .val = 'C'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010096 {.name = "insert", .has_arg = 1, .val = 'I'},
97 {.name = "replace", .has_arg = 1, .val = 'R'},
98 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020099 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100100 {.name = "flush", .has_arg = 2, .val = 'F'},
101 {.name = "zero", .has_arg = 2, .val = 'Z'},
102 {.name = "new-chain", .has_arg = 1, .val = 'N'},
103 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
104 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
105 {.name = "policy", .has_arg = 1, .val = 'P'},
106 {.name = "source", .has_arg = 1, .val = 's'},
107 {.name = "destination", .has_arg = 1, .val = 'd'},
108 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
109 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
110 {.name = "protocol", .has_arg = 1, .val = 'p'},
111 {.name = "in-interface", .has_arg = 1, .val = 'i'},
112 {.name = "jump", .has_arg = 1, .val = 'j'},
113 {.name = "table", .has_arg = 1, .val = 't'},
114 {.name = "match", .has_arg = 1, .val = 'm'},
115 {.name = "numeric", .has_arg = 0, .val = 'n'},
116 {.name = "out-interface", .has_arg = 1, .val = 'o'},
117 {.name = "verbose", .has_arg = 0, .val = 'v'},
118 {.name = "exact", .has_arg = 0, .val = 'x'},
119 {.name = "fragments", .has_arg = 0, .val = 'f'},
120 {.name = "version", .has_arg = 0, .val = 'V'},
121 {.name = "help", .has_arg = 2, .val = 'h'},
122 {.name = "line-numbers", .has_arg = 0, .val = '0'},
123 {.name = "modprobe", .has_arg = 1, .val = 'M'},
124 {.name = "set-counters", .has_arg = 1, .val = 'c'},
125 {.name = "goto", .has_arg = 1, .val = 'g'},
126 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +0000127};
128
Illes Marci63e90632003-03-03 08:08:37 +0000129/* we need this for iptables-restore. iptables-restore.c sets line to the
130 * current line of the input file, in order to give a more precise error
131 * message. iptables itself doesn't need this, so it is initialized to the
132 * magic number of -1 */
133int line = -1;
134
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100135void iptables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
136
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100137struct xtables_globals iptables_globals = {
138 .option_offset = 0,
139 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500140 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100141 .exit_err = iptables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100142};
143
Marc Bouchere6869a82000-03-20 06:03:29 +0000144/* Table of legal combinations of commands and options. If any of the
145 * given commands make an option legal, that option is legal (applies to
146 * CMD_LIST and CMD_ZERO only).
147 * Key:
148 * + compulsory
149 * x illegal
150 * optional
151 */
152
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100153static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Marc Bouchere6869a82000-03-20 06:03:29 +0000154/* Well, it's better than "Re: Linux vs FreeBSD" */
155{
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100156 /* -n -s -d -p -j -v -x -i -o --line -c -f */
157/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
158/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000159/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100160/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
161/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' ',' '},
162/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000163/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
164/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700165/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Jan Engelhardtf1e71012011-02-07 03:13:43 +0100168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' ','x'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100170/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
171/*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x',' '},
Marc Bouchere6869a82000-03-20 06:03:29 +0000172};
173
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100174static const int inverse_for_options[NUMBER_OF_OPT] =
Marc Bouchere6869a82000-03-20 06:03:29 +0000175{
176/* -n */ 0,
177/* -s */ IPT_INV_SRCIP,
178/* -d */ IPT_INV_DSTIP,
179/* -p */ IPT_INV_PROTO,
180/* -j */ 0,
181/* -v */ 0,
182/* -x */ 0,
183/* -i */ IPT_INV_VIA_IN,
184/* -o */ IPT_INV_VIA_OUT,
185/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000186/*--line*/ 0,
187/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000188};
189
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100190#define opts iptables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500191#define prog_name iptables_globals.program_name
192#define prog_vers iptables_globals.program_version
Marc Bouchere6869a82000-03-20 06:03:29 +0000193
Phil Oester8cf65912005-09-19 15:00:33 +0000194int kernel_version;
195
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000196/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000197/* defined in netinet/in.h */
198#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000199#ifndef IPPROTO_ESP
200#define IPPROTO_ESP 50
201#endif
202#ifndef IPPROTO_AH
203#define IPPROTO_AH 51
204#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000205#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000206
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000207enum {
208 IPT_DOTTED_ADDR = 0,
209 IPT_DOTTED_MASK
210};
211
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200212static void __attribute__((noreturn))
Marc Bouchere6869a82000-03-20 06:03:29 +0000213exit_tryhelp(int status)
214{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000215 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000216 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000217 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500218 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500219 xtables_free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000220 exit(status);
221}
222
Patrick McHardy0b639362007-09-08 16:00:01 +0000223static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100224exit_printhelp(const struct xtables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000225{
226 printf("%s v%s\n\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100227"Usage: %s -[ACD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100228" %s -I chain [rulenum] rule-specification [options]\n"
229" %s -R chain rulenum rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000230" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200231" %s -[LS] [chain [rulenum]] [options]\n"
232" %s -[FZ] [chain] [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000233" %s -[NX] chain\n"
234" %s -E old-chain-name new-chain-name\n"
235" %s -P chain target [options]\n"
236" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500237 prog_name, prog_vers, prog_name, prog_name,
238 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100239 prog_name, prog_name, prog_name, prog_name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000240
241 printf(
242"Commands:\n"
243"Either long or short options are allowed.\n"
244" --append -A chain Append to chain\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100245" --check -C chain Check for the existence of a rule\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000246" --delete -D chain Delete matching rule from chain\n"
247" --delete -D chain rulenum\n"
248" Delete rule rulenum (1 = first) from chain\n"
249" --insert -I chain [rulenum]\n"
250" Insert in chain as rulenum (default 1=first)\n"
251" --replace -R chain rulenum\n"
252" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200253" --list -L [chain [rulenum]]\n"
254" List the rules in a chain or all chains\n"
255" --list-rules -S [chain [rulenum]]\n"
256" Print the rules in a chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000257" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700258" --zero -Z [chain [rulenum]]\n"
259" Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000260" --new -N chain Create a new user-defined chain\n"
261" --delete-chain\n"
262" -X [chain] Delete a user-defined chain\n"
263" --policy -P chain target\n"
264" Change policy on chain to target\n"
265" --rename-chain\n"
266" -E old-chain new-chain\n"
267" Change chain name, (moving any references)\n"
268
269"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200270"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100271"[!] --source -s address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000272" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100273"[!] --destination -d address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000274" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200275"[!] --in-interface -i input name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000276" network interface name ([+] for wildcard)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200277" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000278" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000279#ifdef IPT_F_GOTO
280" --goto -g chain\n"
281" jump to chain with no return\n"
282#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000283" --match -m match\n"
284" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000285" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200286"[!] --out-interface -o output name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000287" network interface name ([+] for wildcard)\n"
288" --table -t table table to manipulate (default: `filter')\n"
289" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000290" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000291" --exact -x expand numbers (display exact values)\n"
292"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000293" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000294" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000295"[!] --version -V print package version.\n");
296
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200297 print_extension_helps(xtables_targets, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 exit(0);
299}
300
Patrick McHardy0b639362007-09-08 16:00:01 +0000301void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100302iptables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000303{
304 va_list args;
305
306 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500307 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000308 vfprintf(stderr, msg, args);
309 va_end(args);
310 fprintf(stderr, "\n");
311 if (status == PARAMETER_PROBLEM)
312 exit_tryhelp(status);
313 if (status == VERSION_PROBLEM)
314 fprintf(stderr,
315 "Perhaps iptables or your kernel needs to be upgraded.\n");
316 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500317 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000318 exit(status);
319}
320
Marc Bouchere6869a82000-03-20 06:03:29 +0000321static void
322generic_opt_check(int command, int options)
323{
324 int i, j, legal = 0;
325
326 /* Check that commands are valid with options. Complicated by the
327 * fact that if an option is legal with *any* command given, it is
328 * legal overall (ie. -z and -l).
329 */
330 for (i = 0; i < NUMBER_OF_OPT; i++) {
331 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
332
333 for (j = 0; j < NUMBER_OF_CMD; j++) {
334 if (!(command & (1<<j)))
335 continue;
336
337 if (!(options & (1<<i))) {
338 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100339 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000340 "You need to supply the `-%c' "
341 "option for this command\n",
342 optflags[i]);
343 } else {
344 if (commands_v_options[j][i] != 'x')
345 legal = 1;
346 else if (legal == 0)
347 legal = -1;
348 }
349 }
350 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100351 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000352 "Illegal option `-%c' with this command\n",
353 optflags[i]);
354 }
355}
356
357static char
358opt2char(int option)
359{
360 const char *ptr;
361 for (ptr = optflags; option > 1; option >>= 1, ptr++);
362
363 return *ptr;
364}
365
366static char
367cmd2char(int option)
368{
369 const char *ptr;
370 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
371
372 return *ptr;
373}
374
375static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000376add_command(unsigned int *cmd, const int newcmd, const int othercmds,
377 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000378{
379 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100380 xtables_error(PARAMETER_PROBLEM, "unexpected ! flag");
Marc Bouchere6869a82000-03-20 06:03:29 +0000381 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100382 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Marc Bouchere6869a82000-03-20 06:03:29 +0000383 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
384 *cmd |= newcmd;
385}
386
Marc Bouchere6869a82000-03-20 06:03:29 +0000387/*
388 * All functions starting with "parse" should succeed, otherwise
389 * the program fails.
390 * Most routines return pointers to static data that may change
391 * between calls to the same or other routines with a few exceptions:
392 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
393 * return global static data.
394*/
395
Rusty Russell28381a42000-05-10 00:19:50 +0000396/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000397/* Can't be zero. */
398static int
399parse_rulenumber(const char *rule)
400{
Harald Welteed498492001-07-23 01:24:22 +0000401 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000402
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100403 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100404 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 "Invalid rule number `%s'", rule);
406
407 return rulenum;
408}
409
410static const char *
411parse_target(const char *targetname)
412{
413 const char *ptr;
414
415 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100416 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000417 "Invalid target name (too short)");
418
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200419 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100420 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000421 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200422 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000423
424 for (ptr = targetname; *ptr; ptr++)
425 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100426 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000427 "Invalid target name `%s'", targetname);
428 return targetname;
429}
430
Marc Bouchere6869a82000-03-20 06:03:29 +0000431static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100432set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Marc Bouchere6869a82000-03-20 06:03:29 +0000433 int invert)
434{
435 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100436 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Marc Bouchere6869a82000-03-20 06:03:29 +0000437 opt2char(option));
438 *options |= option;
439
440 if (invert) {
441 unsigned int i;
442 for (i = 0; 1 << i != option; i++);
443
444 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100445 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000446 "cannot have ! before -%c",
447 opt2char(option));
448 *invflg |= inverse_for_options[i];
449 }
450}
451
Marc Bouchere6869a82000-03-20 06:03:29 +0000452static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100453print_num(uint64_t number, unsigned int format)
Harald Weltea0b4f792001-03-25 19:55:04 +0000454{
455 if (format & FMT_KILOMEGAGIGA) {
456 if (number > 99999) {
457 number = (number + 500) / 1000;
458 if (number > 9999) {
459 number = (number + 500) / 1000;
460 if (number > 9999) {
461 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000462 if (number > 9999) {
463 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000464 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000465 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000466 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000467 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000468 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000469 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000470 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000471 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000472 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000473 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000474 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000475}
476
477
478static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100479print_header(unsigned int format, const char *chain, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000480{
481 struct ipt_counters counters;
482 const char *pol = iptc_get_policy(chain, &counters, handle);
483 printf("Chain %s", chain);
484 if (pol) {
485 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000486 if (!(format & FMT_NOCOUNTS)) {
487 fputc(' ', stdout);
488 print_num(counters.pcnt, (format|FMT_NOTABLE));
489 fputs("packets, ", stdout);
490 print_num(counters.bcnt, (format|FMT_NOTABLE));
491 fputs("bytes", stdout);
492 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000493 printf(")\n");
494 } else {
495 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000496 if (!iptc_get_references(&refs, chain, handle))
497 printf(" (ERROR obtaining refs)\n");
498 else
499 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000500 }
501
502 if (format & FMT_LINENUMBERS)
503 printf(FMT("%-4s ", "%s "), "num");
504 if (!(format & FMT_NOCOUNTS)) {
505 if (format & FMT_KILOMEGAGIGA) {
506 printf(FMT("%5s ","%s "), "pkts");
507 printf(FMT("%5s ","%s "), "bytes");
508 } else {
509 printf(FMT("%8s ","%s "), "pkts");
510 printf(FMT("%10s ","%s "), "bytes");
511 }
512 }
513 if (!(format & FMT_NOTARGET))
514 printf(FMT("%-9s ","%s "), "target");
515 fputs(" prot ", stdout);
516 if (format & FMT_OPTIONS)
517 fputs("opt", stdout);
518 if (format & FMT_VIA) {
519 printf(FMT(" %-6s ","%s "), "in");
520 printf(FMT("%-6s ","%s "), "out");
521 }
522 printf(FMT(" %-19s ","%s "), "source");
523 printf(FMT(" %-19s "," %s "), "destination");
524 printf("\n");
525}
526
Marc Bouchere6869a82000-03-20 06:03:29 +0000527
528static int
529print_match(const struct ipt_entry_match *m,
530 const struct ipt_ip *ip,
531 int numeric)
532{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100533 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100534 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000535
536 if (match) {
537 if (match->print)
538 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000539 else
Rusty Russellb039b022000-09-01 06:04:05 +0000540 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000541 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000542 if (m->u.user.name[0])
543 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000544 }
545 /* Don't stop iterating. */
546 return 0;
547}
548
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100549/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000550static void
551print_firewall(const struct ipt_entry *fw,
552 const char *targname,
553 unsigned int num,
554 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100555 struct iptc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000556{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100557 const struct xtables_target *target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000558 const struct ipt_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100559 uint8_t flags;
Marc Bouchere6869a82000-03-20 06:03:29 +0000560 char buf[BUFSIZ];
561
Marc Bouchere6869a82000-03-20 06:03:29 +0000562 if (!iptc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100563 target = xtables_find_target(targname, XTF_TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000564 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100565 target = xtables_find_target(IPT_STANDARD_TARGET,
566 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000567
568 t = ipt_get_target((struct ipt_entry *)fw);
569 flags = fw->ip.flags;
570
571 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200572 printf(FMT("%-4u ", "%u "), num);
Marc Bouchere6869a82000-03-20 06:03:29 +0000573
574 if (!(format & FMT_NOCOUNTS)) {
575 print_num(fw->counters.pcnt, format);
576 print_num(fw->counters.bcnt, format);
577 }
578
579 if (!(format & FMT_NOTARGET))
580 printf(FMT("%-9s ", "%s "), targname);
581
582 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
583 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100584 const char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000585 if (pname)
586 printf(FMT("%-5s", "%s "), pname);
587 else
588 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
589 }
590
591 if (format & FMT_OPTIONS) {
592 if (format & FMT_NOTABLE)
593 fputs("opt ", stdout);
594 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
595 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
596 fputc(' ', stdout);
597 }
598
599 if (format & FMT_VIA) {
600 char iface[IFNAMSIZ+2];
601
602 if (fw->ip.invflags & IPT_INV_VIA_IN) {
603 iface[0] = '!';
604 iface[1] = '\0';
605 }
606 else iface[0] = '\0';
607
608 if (fw->ip.iniface[0] != '\0') {
609 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000610 }
611 else if (format & FMT_NUMERIC) strcat(iface, "*");
612 else strcat(iface, "any");
613 printf(FMT(" %-6s ","in %s "), iface);
614
615 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
616 iface[0] = '!';
617 iface[1] = '\0';
618 }
619 else iface[0] = '\0';
620
621 if (fw->ip.outiface[0] != '\0') {
622 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000623 }
624 else if (format & FMT_NUMERIC) strcat(iface, "*");
625 else strcat(iface, "any");
626 printf(FMT("%-6s ","out %s "), iface);
627 }
628
629 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
630 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
631 printf(FMT("%-19s ","%s "), "anywhere");
632 else {
633 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100634 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000635 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100636 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.src));
637 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000638 printf(FMT("%-19s ","%s "), buf);
639 }
640
641 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
642 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000643 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000644 else {
645 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100646 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000647 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100648 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.dst));
649 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000650 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000651 }
652
653 if (format & FMT_NOTABLE)
654 fputs(" ", stdout);
655
Harald Welte72bd87e2005-11-24 17:04:05 +0000656#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000657 if(fw->ip.flags & IPT_F_GOTO)
658 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000659#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000660
Marc Bouchere6869a82000-03-20 06:03:29 +0000661 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
662
663 if (target) {
664 if (target->print)
665 /* Print the target information. */
666 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000667 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000668 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000669 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000670
671 if (!(format & FMT_NONEWLINE))
672 fputc('\n', stdout);
673}
674
675static void
676print_firewall_line(const struct ipt_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100677 struct iptc_handle *const h)
Marc Bouchere6869a82000-03-20 06:03:29 +0000678{
679 struct ipt_entry_target *t;
680
681 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000682 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000683}
684
685static int
686append_entry(const ipt_chainlabel chain,
687 struct ipt_entry *fw,
688 unsigned int nsaddrs,
689 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100690 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000691 unsigned int ndaddrs,
692 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100693 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000694 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100695 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000696{
697 unsigned int i, j;
698 int ret = 1;
699
700 for (i = 0; i < nsaddrs; i++) {
701 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100702 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 for (j = 0; j < ndaddrs; j++) {
704 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100705 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000706 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100707 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000708 ret &= iptc_append_entry(chain, fw, handle);
709 }
710 }
711
712 return ret;
713}
714
715static int
716replace_entry(const ipt_chainlabel chain,
717 struct ipt_entry *fw,
718 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100719 const struct in_addr *saddr, const struct in_addr *smask,
720 const struct in_addr *daddr, const struct in_addr *dmask,
Marc Bouchere6869a82000-03-20 06:03:29 +0000721 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100722 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000723{
724 fw->ip.src.s_addr = saddr->s_addr;
725 fw->ip.dst.s_addr = daddr->s_addr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100726 fw->ip.smsk.s_addr = smask->s_addr;
727 fw->ip.dmsk.s_addr = dmask->s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000728
729 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100730 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000731 return iptc_replace_entry(chain, fw, rulenum, handle);
732}
733
734static int
735insert_entry(const ipt_chainlabel chain,
736 struct ipt_entry *fw,
737 unsigned int rulenum,
738 unsigned int nsaddrs,
739 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100740 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000741 unsigned int ndaddrs,
742 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100743 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000744 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100745 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000746{
747 unsigned int i, j;
748 int ret = 1;
749
750 for (i = 0; i < nsaddrs; i++) {
751 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100752 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000753 for (j = 0; j < ndaddrs; j++) {
754 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100755 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000756 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100757 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000758 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
759 }
760 }
761
762 return ret;
763}
764
Rusty Russell2e0a3212000-04-19 11:23:18 +0000765static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100766make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100767 const struct xtables_target *target)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000768{
769 /* Establish mask for comparison */
770 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100771 const struct xtables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000772 unsigned char *mask, *mptr;
773
774 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000775 for (matchp = matches; matchp; matchp = matchp->next)
776 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000777
Jan Engelhardt630ef482009-01-27 14:58:41 +0100778 mask = xtables_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000779 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100780 + target->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000781
Rusty Russell9e1d2142000-04-23 09:11:12 +0000782 memset(mask, 0xFF, sizeof(struct ipt_entry));
783 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000784
Martin Josefsson78cafda2004-02-02 20:01:18 +0000785 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000786 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000787 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000788 + matchp->match->userspacesize);
789 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000790 }
791
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000792 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000793 IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100794 + target->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000795
796 return mask;
797}
798
Marc Bouchere6869a82000-03-20 06:03:29 +0000799static int
800delete_entry(const ipt_chainlabel chain,
801 struct ipt_entry *fw,
802 unsigned int nsaddrs,
803 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100804 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000805 unsigned int ndaddrs,
806 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100807 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000808 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100809 struct iptc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100810 struct xtables_rule_match *matches,
811 const struct xtables_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000812{
813 unsigned int i, j;
814 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000815 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000816
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100817 mask = make_delete_mask(matches, target);
Marc Bouchere6869a82000-03-20 06:03:29 +0000818 for (i = 0; i < nsaddrs; i++) {
819 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100820 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000821 for (j = 0; j < ndaddrs; j++) {
822 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100823 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000824 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100825 print_firewall_line(fw, handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000826 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000827 }
828 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000829 free(mask);
830
Marc Bouchere6869a82000-03-20 06:03:29 +0000831 return ret;
832}
833
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100834static int
835check_entry(const ipt_chainlabel chain, struct ipt_entry *fw,
836 unsigned int nsaddrs, const struct in_addr *saddrs,
837 const struct in_addr *smasks, unsigned int ndaddrs,
838 const struct in_addr *daddrs, const struct in_addr *dmasks,
839 bool verbose, struct iptc_handle *handle,
840 struct xtables_rule_match *matches,
841 const struct xtables_target *target)
842{
843 unsigned int i, j;
844 int ret = 1;
845 unsigned char *mask;
846
847 mask = make_delete_mask(matches, target);
848 for (i = 0; i < nsaddrs; i++) {
849 fw->ip.src.s_addr = saddrs[i].s_addr;
850 fw->ip.smsk.s_addr = smasks[i].s_addr;
851 for (j = 0; j < ndaddrs; j++) {
852 fw->ip.dst.s_addr = daddrs[j].s_addr;
853 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
854 if (verbose)
855 print_firewall_line(fw, handle);
856 ret &= iptc_check_entry(chain, fw, mask, handle);
857 }
858 }
859
860 free(mask);
861 return ret;
862}
863
Harald Welteae1ff9f2000-12-01 14:26:20 +0000864int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100865for_each_chain(int (*fn)(const ipt_chainlabel, int, struct iptc_handle *),
866 int verbose, int builtinstoo, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000867{
868 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000869 const char *chain;
870 char *chains;
871 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000872
Rusty Russell9e1d2142000-04-23 09:11:12 +0000873 chain = iptc_first_chain(handle);
874 while (chain) {
875 chaincount++;
876 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000877 }
878
Jan Engelhardt630ef482009-01-27 14:58:41 +0100879 chains = xtables_malloc(sizeof(ipt_chainlabel) * chaincount);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000880 i = 0;
881 chain = iptc_first_chain(handle);
882 while (chain) {
883 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
884 i++;
885 chain = iptc_next_chain(handle);
886 }
887
888 for (i = 0; i < chaincount; i++) {
889 if (!builtinstoo
890 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100891 handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000892 continue;
893 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
894 }
895
896 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000897 return ret;
898}
899
Harald Welteae1ff9f2000-12-01 14:26:20 +0000900int
Marc Bouchere6869a82000-03-20 06:03:29 +0000901flush_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100902 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000903{
904 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000905 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000906
907 if (verbose)
908 fprintf(stdout, "Flushing chain `%s'\n", chain);
909 return iptc_flush_entries(chain, handle);
910}
Marc Bouchere6869a82000-03-20 06:03:29 +0000911
912static int
913zero_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100914 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000915{
916 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000917 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000918
Marc Bouchere6869a82000-03-20 06:03:29 +0000919 if (verbose)
920 fprintf(stdout, "Zeroing chain `%s'\n", chain);
921 return iptc_zero_entries(chain, handle);
922}
923
Harald Welteae1ff9f2000-12-01 14:26:20 +0000924int
Marc Bouchere6869a82000-03-20 06:03:29 +0000925delete_chain(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100926 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000927{
Rusty Russell9e1d2142000-04-23 09:11:12 +0000928 if (!chain)
929 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000930
931 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000932 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000933 return iptc_delete_chain(chain, handle);
934}
935
936static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200937list_entries(const ipt_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100938 int expanded, int linenumbers, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000939{
940 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000941 unsigned int format;
942 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +0000943
944 format = FMT_OPTIONS;
945 if (!verbose)
946 format |= FMT_NOCOUNTS;
947 else
948 format |= FMT_VIA;
949
950 if (numeric)
951 format |= FMT_NUMERIC;
952
953 if (!expanded)
954 format |= FMT_KILOMEGAGIGA;
955
956 if (linenumbers)
957 format |= FMT_LINENUMBERS;
958
Rusty Russell9e1d2142000-04-23 09:11:12 +0000959 for (this = iptc_first_chain(handle);
960 this;
961 this = iptc_next_chain(handle)) {
962 const struct ipt_entry *i;
963 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +0000964
Marc Bouchere6869a82000-03-20 06:03:29 +0000965 if (chain && strcmp(chain, this) != 0)
966 continue;
967
968 if (found) printf("\n");
969
Henrik Nordstrombb340822008-05-13 13:09:23 +0200970 if (!rulenum)
971 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000972 i = iptc_first_rule(this, handle);
973
974 num = 0;
975 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200976 num++;
977 if (!rulenum || num == rulenum)
978 print_firewall(i,
979 iptc_get_target(i, handle),
980 num,
981 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100982 handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000983 i = iptc_next_rule(i, handle);
984 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000985 found = 1;
986 }
987
988 errno = ENOENT;
989 return found;
990}
991
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100992static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200993{
994 if (proto) {
995 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100996 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200997
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100998 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200999 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001000 printf("%s -p %s", invertstr, pent->p_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001001 return;
1002 }
1003
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001004 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1005 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001006 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001007 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001008 return;
1009 }
1010
Jan Engelhardt73866352010-12-18 02:04:59 +01001011 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001012 }
1013}
1014
1015#define IP_PARTS_NATIVE(n) \
1016(unsigned int)((n)>>24)&0xFF, \
1017(unsigned int)((n)>>16)&0xFF, \
1018(unsigned int)((n)>>8)&0xFF, \
1019(unsigned int)((n)&0xFF)
1020
1021#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
1022
1023/* This assumes that mask is contiguous, and byte-bounded. */
1024static void
1025print_iface(char letter, const char *iface, const unsigned char *mask,
1026 int invert)
1027{
1028 unsigned int i;
1029
1030 if (mask[0] == 0)
1031 return;
1032
Jan Engelhardt73866352010-12-18 02:04:59 +01001033 printf("%s -%c ", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001034
1035 for (i = 0; i < IFNAMSIZ; i++) {
1036 if (mask[i] != 0) {
1037 if (iface[i] != '\0')
1038 printf("%c", iface[i]);
1039 } else {
1040 /* we can access iface[i-1] here, because
1041 * a few lines above we make sure that mask[0] != 0 */
1042 if (iface[i-1] != '\0')
1043 printf("+");
1044 break;
1045 }
1046 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001047}
1048
1049static int print_match_save(const struct ipt_entry_match *e,
1050 const struct ipt_ip *ip)
1051{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001052 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001053 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001054
1055 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001056 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001057
1058 /* some matches don't provide a save function */
1059 if (match->save)
1060 match->save(ip, e);
1061 } else {
1062 if (e->u.match_size) {
1063 fprintf(stderr,
1064 "Can't find library for match `%s'\n",
1065 e->u.user.name);
1066 exit(1);
1067 }
1068 }
1069 return 0;
1070}
1071
1072/* print a given ip including mask if neccessary */
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001073static void print_ip(const char *prefix, uint32_t ip,
1074 uint32_t mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001075{
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001076 uint32_t bits, hmask = ntohl(mask);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001077 int i;
1078
1079 if (!mask && !ip && !invert)
1080 return;
1081
Jan Engelhardt73866352010-12-18 02:04:59 +01001082 printf("%s %s %u.%u.%u.%u",
1083 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001084 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001085 IP_PARTS(ip));
1086
1087 if (mask == 0xFFFFFFFFU) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001088 printf("/32");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001089 return;
1090 }
1091
1092 i = 32;
1093 bits = 0xFFFFFFFEU;
1094 while (--i >= 0 && hmask != bits)
1095 bits <<= 1;
1096 if (i >= 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001097 printf("/%u", i);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001098 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001099 printf("/%u.%u.%u.%u", IP_PARTS(mask));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001100}
1101
1102/* We want this to be readable, so only print out neccessary fields.
1103 * Because that's the kind of world I want to live in. */
1104void print_rule(const struct ipt_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001105 struct iptc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001106{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001107 const struct ipt_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001108 const char *target_name;
1109
1110 /* print counters for iptables-save */
1111 if (counters > 0)
1112 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1113
1114 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001115 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001116
1117 /* Print IP part. */
1118 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
1119 e->ip.invflags & IPT_INV_SRCIP);
1120
1121 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
1122 e->ip.invflags & IPT_INV_DSTIP);
1123
1124 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
1125 e->ip.invflags & IPT_INV_VIA_IN);
1126
1127 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
1128 e->ip.invflags & IPT_INV_VIA_OUT);
1129
1130 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
1131
1132 if (e->ip.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001133 printf("%s -f",
1134 e->ip.invflags & IPT_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001135
1136 /* Print matchinfo part */
1137 if (e->target_offset) {
1138 IPT_MATCH_ITERATE(e, print_match_save, &e->ip);
1139 }
1140
1141 /* print counters for iptables -R */
1142 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001143 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001144
1145 /* Print target name */
1146 target_name = iptc_get_target(e, h);
1147 if (target_name && (*target_name != '\0'))
1148#ifdef IPT_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001149 printf(" -%c %s", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001150#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001151 printf(" -j %s", target_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001152#endif
1153
1154 /* Print targinfo part */
1155 t = ipt_get_target((struct ipt_entry *)e);
1156 if (t->u.user.name[0]) {
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001157 const struct xtables_target *target =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001158 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001159
1160 if (!target) {
1161 fprintf(stderr, "Can't find library for target `%s'\n",
1162 t->u.user.name);
1163 exit(1);
1164 }
1165
1166 if (target->save)
1167 target->save(&e->ip, t);
1168 else {
1169 /* If the target size is greater than ipt_entry_target
1170 * there is something to be saved, we just don't know
1171 * how to print it */
1172 if (t->u.target_size !=
1173 sizeof(struct ipt_entry_target)) {
1174 fprintf(stderr, "Target `%s' is missing "
1175 "save function\n",
1176 t->u.user.name);
1177 exit(1);
1178 }
1179 }
1180 }
1181 printf("\n");
1182}
1183
1184static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001185list_rules(const ipt_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001186 struct iptc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001187{
1188 const char *this = NULL;
1189 int found = 0;
1190
1191 if (counters)
1192 counters = -1; /* iptables -c format */
1193
1194 /* Dump out chain names first,
1195 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001196 if (!rulenum) for (this = iptc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001197 this;
1198 this = iptc_next_chain(handle)) {
1199 if (chain && strcmp(this, chain) != 0)
1200 continue;
1201
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001202 if (iptc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001203 struct ipt_counters count;
1204 printf("-P %s %s", this, iptc_get_policy(this, &count, handle));
1205 if (counters)
1206 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1207 printf("\n");
1208 } else {
1209 printf("-N %s\n", this);
1210 }
1211 }
1212
1213 for (this = iptc_first_chain(handle);
1214 this;
1215 this = iptc_next_chain(handle)) {
1216 const struct ipt_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001217 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001218
1219 if (chain && strcmp(this, chain) != 0)
1220 continue;
1221
1222 /* Dump out rules */
1223 e = iptc_first_rule(this, handle);
1224 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001225 num++;
1226 if (!rulenum || num == rulenum)
1227 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001228 e = iptc_next_rule(e, handle);
1229 }
1230 found = 1;
1231 }
1232
1233 errno = ENOENT;
1234 return found;
1235}
1236
Marc Bouchere6869a82000-03-20 06:03:29 +00001237static struct ipt_entry *
1238generate_entry(const struct ipt_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001239 struct xtables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001240 struct ipt_entry_target *target)
1241{
1242 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001243 struct xtables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001244 struct ipt_entry *e;
1245
1246 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001247 for (matchp = matches; matchp; matchp = matchp->next)
1248 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001249
Jan Engelhardt630ef482009-01-27 14:58:41 +01001250 e = xtables_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001251 *e = *fw;
1252 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001253 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001254
1255 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001256 for (matchp = matches; matchp; matchp = matchp->next) {
1257 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1258 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001259 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001260 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001261
1262 return e;
1263}
1264
Jan Engelhardt395e4412009-02-10 10:43:08 +01001265static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001266{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001267 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001268
1269 for (matchp = *matches; matchp;) {
1270 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001271 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001272 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001273 matchp->match->m = NULL;
1274 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001275 if (matchp->match == matchp->match->next) {
1276 free(matchp->match);
1277 matchp->match = NULL;
1278 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001279 free(matchp);
1280 matchp = tmp;
1281 }
1282
1283 *matches = NULL;
1284}
1285
Phil Oester8cf65912005-09-19 15:00:33 +00001286void
1287get_kernel_version(void) {
1288 static struct utsname uts;
1289 int x = 0, y = 0, z = 0;
1290
1291 if (uname(&uts) == -1) {
1292 fprintf(stderr, "Unable to retrieve kernel version.\n");
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001293 xtables_free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001294 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001295 }
1296
1297 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1298 kernel_version = LINUX_VERSION(x, y, z);
1299}
1300
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001301static void command_default(struct iptables_command_state *cs)
1302{
1303 struct xtables_rule_match *matchp;
1304 struct xtables_match *m;
1305
Jan Engelhardtaf3d73e2011-02-11 01:45:26 +01001306 if (cs->target != NULL && cs->target->parse != NULL &&
1307 cs->c >= cs->target->option_offset &&
1308 cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001309 cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
1310 cs->invert, &cs->target->tflags, &cs->fw,
1311 &cs->target->t);
1312 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001313 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001314
1315 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1316 m = matchp->match;
1317
1318 if (matchp->completed || m->parse == NULL)
1319 continue;
1320 if (cs->c < m->option_offset ||
1321 cs->c >= m->option_offset + XT_OPTION_OFFSET_SCALE)
1322 continue;
1323 m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
1324 &m->mflags, &cs->fw, &m->m);
1325 return;
1326 }
1327
1328 /* Try loading protocol */
1329 m = load_proto(cs);
1330 if (m != NULL) {
1331 size_t size;
1332
1333 cs->proto_used = 1;
1334
1335 size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
1336
1337 m->m = xtables_calloc(1, size);
1338 m->m->u.match_size = size;
1339 strcpy(m->m->u.user.name, m->name);
1340 m->m->u.user.revision = m->revision;
1341 if (m->init != NULL)
1342 m->init(m->m);
1343
1344 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1345 m->extra_opts, &m->option_offset);
1346 if (opts == NULL)
1347 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1348
1349 optind--;
1350 return;
1351 }
1352
Jan Engelhardt58b491f2011-02-07 03:45:26 +01001353 if (cs->c == ':')
1354 xtables_error(PARAMETER_PROBLEM, "option \"%s\" "
1355 "requires an argument", cs->argv[optind-1]);
1356 if (cs->c == '?')
1357 xtables_error(PARAMETER_PROBLEM, "unknown option "
1358 "\"%s\"", cs->argv[optind-1]);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001359 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001360}
1361
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001362static void command_jump(struct iptables_command_state *cs)
1363{
1364 size_t size;
1365
1366 set_option(&cs->options, OPT_JUMP, &cs->fw.ip.invflags, cs->invert);
1367 cs->jumpto = parse_target(optarg);
1368 /* TRY_LOAD (may be chain name) */
1369 cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1370
1371 if (cs->target == NULL)
1372 return;
1373
1374 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1375 + cs->target->size;
1376
1377 cs->target->t = xtables_calloc(1, size);
1378 cs->target->t->u.target_size = size;
1379 strcpy(cs->target->t->u.user.name, cs->jumpto);
1380 cs->target->t->u.user.revision = cs->target->revision;
1381 if (cs->target->init != NULL)
1382 cs->target->init(cs->target->t);
1383 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1384 cs->target->extra_opts,
1385 &cs->target->option_offset);
1386 if (opts == NULL)
1387 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1388}
1389
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001390static void command_match(struct iptables_command_state *cs)
1391{
1392 struct xtables_match *m;
1393 size_t size;
1394
1395 if (cs->invert)
1396 xtables_error(PARAMETER_PROBLEM,
1397 "unexpected ! flag before --match");
1398
1399 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1400 size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
1401 m->m = xtables_calloc(1, size);
1402 m->m->u.match_size = size;
1403 strcpy(m->m->u.user.name, m->name);
1404 m->m->u.user.revision = m->revision;
1405 if (m->init != NULL)
1406 m->init(m->m);
1407 if (m != m->next) {
1408 /* Merge options for non-cloned matches */
1409 opts = xtables_merge_options(iptables_globals.orig_opts, opts,
1410 m->extra_opts, &m->option_offset);
1411 if (opts == NULL)
1412 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1413 }
1414}
1415
Jan Engelhardtfd187312008-11-10 16:59:27 +01001416int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001417{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001418 struct iptables_command_state cs;
1419 struct ipt_entry *e = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001420 unsigned int nsaddrs = 0, ndaddrs = 0;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001421 struct in_addr *saddrs = NULL, *smasks = NULL;
1422 struct in_addr *daddrs = NULL, *dmasks = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001423
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001424 int verbose = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001425 const char *chain = NULL;
1426 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1427 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001428 unsigned int rulenum = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001429 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001431 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001432 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001433 struct xtables_target *t;
Patrick McHardy875441e2007-10-17 08:48:58 +00001434 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001435
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001436 memset(&cs, 0, sizeof(cs));
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001437 cs.jumpto = "";
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001438 cs.argv = argv;
Marc Bouchere6869a82000-03-20 06:03:29 +00001439
Harald Welteae1ff9f2000-12-01 14:26:20 +00001440 /* re-set optind to 0 in case do_command gets called
1441 * a second time */
1442 optind = 0;
1443
1444 /* clear mflags in case do_command gets called a second time
1445 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001446 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001447 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001448
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001449 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001450 t->tflags = 0;
1451 t->used = 0;
1452 }
1453
Marc Bouchere6869a82000-03-20 06:03:29 +00001454 /* Suppress error messages: we may add new options if we
1455 demand-load a protocol. */
1456 opterr = 0;
1457
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001458 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001459 while ((cs.c = getopt_long(argc, argv,
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001460 "-:A:C: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 +00001461 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001462 switch (cs.c) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001463 /*
1464 * Command selection
1465 */
1466 case 'A':
1467 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001468 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001469 chain = optarg;
1470 break;
1471
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001472 case 'C':
1473 add_command(&command, CMD_CHECK, CMD_NONE,
1474 cs.invert);
1475 chain = optarg;
1476 break;
1477
Marc Bouchere6869a82000-03-20 06:03:29 +00001478 case 'D':
1479 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001480 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001481 chain = optarg;
1482 if (optind < argc && argv[optind][0] != '-'
1483 && argv[optind][0] != '!') {
1484 rulenum = parse_rulenumber(argv[optind++]);
1485 command = CMD_DELETE_NUM;
1486 }
1487 break;
1488
Marc Bouchere6869a82000-03-20 06:03:29 +00001489 case 'R':
1490 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001491 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001492 chain = optarg;
1493 if (optind < argc && argv[optind][0] != '-'
1494 && argv[optind][0] != '!')
1495 rulenum = parse_rulenumber(argv[optind++]);
1496 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001497 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001498 "-%c requires a rule number",
1499 cmd2char(CMD_REPLACE));
1500 break;
1501
1502 case 'I':
1503 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001504 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001505 chain = optarg;
1506 if (optind < argc && argv[optind][0] != '-'
1507 && argv[optind][0] != '!')
1508 rulenum = parse_rulenumber(argv[optind++]);
1509 else rulenum = 1;
1510 break;
1511
1512 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001513 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001514 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001515 if (optarg) chain = optarg;
1516 else if (optind < argc && argv[optind][0] != '-'
1517 && argv[optind][0] != '!')
1518 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001519 if (optind < argc && argv[optind][0] != '-'
1520 && argv[optind][0] != '!')
1521 rulenum = parse_rulenumber(argv[optind++]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001522 break;
1523
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001524 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001525 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001526 CMD_ZERO|CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001527 if (optarg) chain = optarg;
1528 else if (optind < argc && argv[optind][0] != '-'
1529 && argv[optind][0] != '!')
1530 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001531 if (optind < argc && argv[optind][0] != '-'
1532 && argv[optind][0] != '!')
1533 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001534 break;
1535
Marc Bouchere6869a82000-03-20 06:03:29 +00001536 case 'F':
1537 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001538 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001539 if (optarg) chain = optarg;
1540 else if (optind < argc && argv[optind][0] != '-'
1541 && argv[optind][0] != '!')
1542 chain = argv[optind++];
1543 break;
1544
1545 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001546 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001547 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001548 if (optarg) chain = optarg;
1549 else if (optind < argc && argv[optind][0] != '-'
1550 && argv[optind][0] != '!')
1551 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001552 if (optind < argc && argv[optind][0] != '-'
1553 && argv[optind][0] != '!') {
1554 rulenum = parse_rulenumber(argv[optind++]);
1555 command = CMD_ZERO_NUM;
1556 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001557 break;
1558
1559 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001560 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001561 xtables_error(PARAMETER_PROBLEM,
Harald Welte6336bfd2002-05-07 14:41:43 +00001562 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001563 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001564 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001565 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001566 "chain name may not clash "
1567 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001568 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001569 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001570 chain = optarg;
1571 break;
1572
1573 case 'X':
1574 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001575 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001576 if (optarg) chain = optarg;
1577 else if (optind < argc && argv[optind][0] != '-'
1578 && argv[optind][0] != '!')
1579 chain = argv[optind++];
1580 break;
1581
1582 case 'E':
1583 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001584 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001585 chain = optarg;
1586 if (optind < argc && argv[optind][0] != '-'
1587 && argv[optind][0] != '!')
1588 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001589 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001590 xtables_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001591 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001592 "new-chain-name",
1593 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001594 break;
1595
1596 case 'P':
1597 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001598 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001599 chain = optarg;
1600 if (optind < argc && argv[optind][0] != '-'
1601 && argv[optind][0] != '!')
1602 policy = argv[optind++];
1603 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001604 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001605 "-%c requires a chain and a policy",
1606 cmd2char(CMD_SET_POLICY));
1607 break;
1608
1609 case 'h':
1610 if (!optarg)
1611 optarg = argv[optind];
1612
Rusty Russell2e0a3212000-04-19 11:23:18 +00001613 /* iptables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001614 if (!cs.matches && cs.protocol)
1615 xtables_find_match(cs.protocol,
1616 XTF_TRY_LOAD, &cs.matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001617
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001618 exit_printhelp(cs.matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001619
1620 /*
1621 * Option selection
1622 */
1623 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001624 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1625 set_option(&cs.options, OPT_PROTOCOL, &cs.fw.ip.invflags,
1626 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001627
1628 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001629 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1630 *cs.protocol = tolower(*cs.protocol);
Marc Bouchere6869a82000-03-20 06:03:29 +00001631
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001632 cs.protocol = optarg;
1633 cs.fw.ip.proto = xtables_parse_protocol(cs.protocol);
Marc Bouchere6869a82000-03-20 06:03:29 +00001634
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001635 if (cs.fw.ip.proto == 0
1636 && (cs.fw.ip.invflags & IPT_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001637 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001638 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001639 break;
1640
1641 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001642 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1643 set_option(&cs.options, OPT_SOURCE, &cs.fw.ip.invflags,
1644 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001645 shostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001646 break;
1647
1648 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001649 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1650 set_option(&cs.options, OPT_DESTINATION, &cs.fw.ip.invflags,
1651 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001652 dhostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001653 break;
1654
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001655#ifdef IPT_F_GOTO
1656 case 'g':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001657 set_option(&cs.options, OPT_JUMP, &cs.fw.ip.invflags,
1658 cs.invert);
1659 cs.fw.ip.flags |= IPT_F_GOTO;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001660 cs.jumpto = parse_target(optarg);
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001661 break;
1662#endif
1663
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 case 'j':
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001665 command_jump(&cs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001666 break;
1667
1668
1669 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001670 if (*optarg == '\0')
1671 xtables_error(PARAMETER_PROBLEM,
1672 "Empty interface is likely to be "
1673 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001674 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1675 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw.ip.invflags,
1676 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001677 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001678 cs.fw.ip.iniface,
1679 cs.fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 break;
1681
1682 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001683 if (*optarg == '\0')
1684 xtables_error(PARAMETER_PROBLEM,
1685 "Empty interface is likely to be "
1686 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001687 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1688 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw.ip.invflags,
1689 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001690 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001691 cs.fw.ip.outiface,
1692 cs.fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001693 break;
1694
1695 case 'f':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001696 set_option(&cs.options, OPT_FRAGMENT, &cs.fw.ip.invflags,
1697 cs.invert);
1698 cs.fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001699 break;
1700
1701 case 'v':
1702 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001703 set_option(&cs.options, OPT_VERBOSE,
1704 &cs.fw.ip.invflags, cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 verbose++;
1706 break;
1707
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001708 case 'm':
1709 command_match(&cs);
1710 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001711
1712 case 'n':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001713 set_option(&cs.options, OPT_NUMERIC, &cs.fw.ip.invflags,
1714 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001715 break;
1716
1717 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001718 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001719 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001720 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001721 *table = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001722 break;
1723
1724 case 'x':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001725 set_option(&cs.options, OPT_EXPANDED, &cs.fw.ip.invflags,
1726 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001727 break;
1728
1729 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001730 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001731 printf("Not %s ;-)\n", prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001732 else
1733 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001734 prog_name, prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001735 exit(0);
1736
1737 case '0':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001738 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw.ip.invflags,
1739 cs.invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001740 break;
1741
Harald Welte82dd2ec2000-12-19 05:18:15 +00001742 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001743 xtables_modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001744 break;
1745
Harald Welteccd49e52001-01-23 22:54:34 +00001746 case 'c':
1747
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001748 set_option(&cs.options, OPT_COUNTERS, &cs.fw.ip.invflags,
1749 cs.invert);
Harald Welteccd49e52001-01-23 22:54:34 +00001750 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001751 bcnt = strchr(pcnt + 1, ',');
1752 if (bcnt)
1753 bcnt++;
1754 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welteccd49e52001-01-23 22:54:34 +00001755 && argv[optind][0] != '!')
1756 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001757 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001758 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001759 "-%c requires packet and byte counter",
1760 opt2char(OPT_COUNTERS));
1761
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001762 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001763 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001764 "-%c packet counter not numeric",
1765 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001766 cs.fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001767
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001768 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001769 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001770 "-%c byte counter not numeric",
1771 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001772 cs.fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001773 break;
1774
1775
Marc Bouchere6869a82000-03-20 06:03:29 +00001776 case 1: /* non option */
1777 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001778 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001779 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001780 "multiple consecutive ! not"
1781 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001782 cs.invert = TRUE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001783 optarg[0] = '\0';
1784 continue;
1785 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001786 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001787 exit_tryhelp(2);
1788
1789 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001790 command_default(&cs);
1791 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001792 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001793 cs.invert = FALSE;
Marc Bouchere6869a82000-03-20 06:03:29 +00001794 }
1795
Jan Engelhardt1eada722008-08-13 14:41:32 +02001796 if (strcmp(*table, "nat") == 0 &&
1797 ((policy != NULL && strcmp(policy, "DROP") == 0) ||
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001798 (cs.jumpto != NULL && strcmp(cs.jumpto, "DROP") == 0)))
Jan Engelhardte0390be2009-03-15 21:22:49 +01001799 xtables_error(PARAMETER_PROBLEM,
1800 "\nThe \"nat\" table is not intended for filtering, "
1801 "the use of DROP is therefore inhibited.\n\n");
Jan Engelhardt1eada722008-08-13 14:41:32 +02001802
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001803 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001804 if (matchp->match->final_check != NULL)
1805 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001806
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001807 if (cs.target != NULL && cs.target->final_check != NULL)
1808 cs.target->final_check(cs.target->tflags);
Marc Bouchere6869a82000-03-20 06:03:29 +00001809
1810 /* Fix me: must put inverse options checking here --MN */
1811
1812 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001813 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001814 "unknown arguments found on commandline");
1815 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001816 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001817 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001818 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001819 "nothing appropriate following !");
1820
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001821 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND | CMD_CHECK)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001822 if (!(cs.options & OPT_DESTINATION))
Marc Bouchere6869a82000-03-20 06:03:29 +00001823 dhostnetworkmask = "0.0.0.0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001824 if (!(cs.options & OPT_SOURCE))
Marc Bouchere6869a82000-03-20 06:03:29 +00001825 shostnetworkmask = "0.0.0.0/0";
1826 }
1827
1828 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001829 xtables_ipparse_multiple(shostnetworkmask, &saddrs,
1830 &smasks, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001831
1832 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001833 xtables_ipparse_multiple(dhostnetworkmask, &daddrs,
1834 &dmasks, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001835
1836 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001837 (cs.fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001838 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Marc Bouchere6869a82000-03-20 06:03:29 +00001839 " source or destination IP addresses");
1840
Marc Bouchere6869a82000-03-20 06:03:29 +00001841 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001842 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Marc Bouchere6869a82000-03-20 06:03:29 +00001843 "specify a unique address");
1844
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001845 generic_opt_check(command, cs.options);
Marc Bouchere6869a82000-03-20 06:03:29 +00001846
Jan Engelhardt5429b412010-09-13 15:45:15 +02001847 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001848 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001849 "chain name `%s' too long (must be under %u chars)",
1850 chain, XT_EXTENSION_MAXNAMELEN);
Marc Bouchere6869a82000-03-20 06:03:29 +00001851
Harald Welteae1ff9f2000-12-01 14:26:20 +00001852 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001853 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001854 *handle = iptc_init(*table);
1855
Rusty Russell8beb0492004-12-22 00:37:10 +00001856 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001857 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001858 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001859
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001861 xtables_error(VERSION_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001862 "can't initialize iptables table `%s': %s",
1863 *table, iptc_strerror(errno));
1864
Harald Welte6336bfd2002-05-07 14:41:43 +00001865 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001866 || command == CMD_DELETE
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001867 || command == CMD_CHECK
Marc Bouchere6869a82000-03-20 06:03:29 +00001868 || command == CMD_INSERT
1869 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001870 if (strcmp(chain, "PREROUTING") == 0
1871 || strcmp(chain, "INPUT") == 0) {
1872 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001873 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001874 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001875 "Can't use -%c with %s\n",
1876 opt2char(OPT_VIANAMEOUT),
1877 chain);
1878 }
1879
Rusty Russella4860fd2000-06-17 16:13:02 +00001880 if (strcmp(chain, "POSTROUTING") == 0
1881 || strcmp(chain, "OUTPUT") == 0) {
1882 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001883 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001884 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 "Can't use -%c with %s\n",
1886 opt2char(OPT_VIANAMEIN),
1887 chain);
1888 }
1889
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001890 if (cs.target && iptc_is_chain(cs.jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001891 fprintf(stderr,
1892 "Warning: using chain %s, not extension\n",
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001893 cs.jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001894
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001895 if (cs.target->t)
1896 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001897
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001898 cs.target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001899 }
1900
1901 /* If they didn't specify a target, or it's a chain
1902 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001903 if (!cs.target
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001904 && (strlen(cs.jumpto) == 0
1905 || iptc_is_chain(cs.jumpto, *handle))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001906 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001907
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001908 cs.target = xtables_find_target(IPT_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001909 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001910
1911 size = sizeof(struct ipt_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001912 + cs.target->size;
1913 cs.target->t = xtables_calloc(1, size);
1914 cs.target->t->u.target_size = size;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001915 strcpy(cs.target->t->u.user.name, cs.jumpto);
1916 if (!iptc_is_chain(cs.jumpto, *handle))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001917 cs.target->t->u.user.revision = cs.target->revision;
1918 if (cs.target->init != NULL)
1919 cs.target->init(cs.target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001920 }
1921
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001922 if (!cs.target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001923 /* it is no chain, and we can't load a plugin.
1924 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001925 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001926 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001927#ifdef IPT_F_GOTO
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001928 if (cs.fw.ip.flags & IPT_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001929 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001930 "goto '%s' is not a chain\n",
1931 cs.jumpto);
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001932#endif
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001933 xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001934 } else {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001935 e = generate_entry(&cs.fw, cs.matches, cs.target->t);
1936 free(cs.target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001937 }
1938 }
1939
1940 switch (command) {
1941 case CMD_APPEND:
1942 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001943 nsaddrs, saddrs, smasks,
1944 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001945 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001946 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001947 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001948 case CMD_DELETE:
1949 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001950 nsaddrs, saddrs, smasks,
1951 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001952 cs.options&OPT_VERBOSE,
1953 *handle, cs.matches, cs.target);
Marc Bouchere6869a82000-03-20 06:03:29 +00001954 break;
1955 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001956 ret = iptc_delete_num_entry(chain, rulenum - 1, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001957 break;
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001958 case CMD_CHECK:
1959 ret = check_entry(chain, e,
1960 nsaddrs, saddrs, smasks,
1961 ndaddrs, daddrs, dmasks,
1962 cs.options&OPT_VERBOSE,
1963 *handle, cs.matches, cs.target);
1964 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001965 case CMD_REPLACE:
1966 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001967 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001968 cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 break;
1970 case CMD_INSERT:
1971 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001972 nsaddrs, saddrs, smasks,
1973 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001974 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001975 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001976 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001977 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001978 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001979 break;
1980 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001981 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001982 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001983 case CMD_ZERO_NUM:
1984 ret = iptc_zero_counter(chain, rulenum, *handle);
1985 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001986 case CMD_LIST:
Marc Bouchere6869a82000-03-20 06:03:29 +00001987 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001988 case CMD_LIST|CMD_ZERO_NUM:
Marc Bouchere6869a82000-03-20 06:03:29 +00001989 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001990 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001991 cs.options&OPT_VERBOSE,
1992 cs.options&OPT_NUMERIC,
1993 cs.options&OPT_EXPANDED,
1994 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001995 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001996 if (ret && (command & CMD_ZERO))
1997 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001998 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001999 if (ret && (command & CMD_ZERO_NUM))
2000 ret = iptc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002001 break;
2002 case CMD_LIST_RULES:
2003 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07002004 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002005 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002006 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002007 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002008 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002009 if (ret && (command & CMD_ZERO))
Marc Bouchere6869a82000-03-20 06:03:29 +00002010 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002011 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07002012 if (ret && (command & CMD_ZERO_NUM))
2013 ret = iptc_zero_counter(chain, rulenum, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002014 break;
2015 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002016 ret = iptc_create_chain(chain, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002017 break;
2018 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002019 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 break;
2021 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002022 ret = iptc_rename_chain(chain, newname, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002023 break;
2024 case CMD_SET_POLICY:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002025 ret = iptc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw.counters : NULL, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 break;
2027 default:
2028 /* We should never reach this... */
2029 exit_tryhelp(2);
2030 }
2031
2032 if (verbose > 1)
2033 dump_entries(*handle);
2034
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002035 clear_rule_matches(&cs.matches);
Martin Josefsson78cafda2004-02-02 20:01:18 +00002036
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002037 if (e != NULL) {
2038 free(e);
2039 e = NULL;
2040 }
2041
keso6997cdf2004-07-04 15:20:53 +00002042 free(saddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01002043 free(smasks);
keso6997cdf2004-07-04 15:20:53 +00002044 free(daddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01002045 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002046 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002047
Marc Bouchere6869a82000-03-20 06:03:29 +00002048 return ret;
2049}