blob: 1127bddc2e83a5630c56690a225bdf04b112854f [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010032#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000033#include <stdio.h>
34#include <stdlib.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000040#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000041#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Jan Engelhardtf89c1712009-06-12 20:48:52 +020043#include "xshared.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Marc Bouchere6869a82000-03-20 06:03:29 +000052#define FMT_NUMERIC 0x0001
53#define FMT_NOCOUNTS 0x0002
54#define FMT_KILOMEGAGIGA 0x0004
55#define FMT_OPTIONS 0x0008
56#define FMT_NOTABLE 0x0010
57#define FMT_NOTARGET 0x0020
58#define FMT_VIA 0x0040
59#define FMT_NONEWLINE 0x0080
60#define FMT_LINENUMBERS 0x0100
61
62#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
63 | FMT_NUMERIC | FMT_NOTABLE)
64#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
65
66
67#define CMD_NONE 0x0000U
68#define CMD_INSERT 0x0001U
69#define CMD_DELETE 0x0002U
70#define CMD_DELETE_NUM 0x0004U
71#define CMD_REPLACE 0x0008U
72#define CMD_APPEND 0x0010U
73#define CMD_LIST 0x0020U
74#define CMD_FLUSH 0x0040U
75#define CMD_ZERO 0x0080U
76#define CMD_NEW_CHAIN 0x0100U
77#define CMD_DELETE_CHAIN 0x0200U
78#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000079#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020080#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070081#define CMD_ZERO_NUM 0x2000U
82#define NUMBER_OF_CMD 15
Marc Bouchere6869a82000-03-20 06:03:29 +000083static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070084 'Z', 'N', 'X', 'P', 'E', 'S' };
Marc Bouchere6869a82000-03-20 06:03:29 +000085
Marc Bouchere6869a82000-03-20 06:03:29 +000086#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100104 {.name = "append", .has_arg = 1, .val = 'A'},
105 {.name = "delete", .has_arg = 1, .val = 'D'},
106 {.name = "insert", .has_arg = 1, .val = 'I'},
107 {.name = "replace", .has_arg = 1, .val = 'R'},
108 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200109 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100110 {.name = "flush", .has_arg = 2, .val = 'F'},
111 {.name = "zero", .has_arg = 2, .val = 'Z'},
112 {.name = "new-chain", .has_arg = 1, .val = 'N'},
113 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
114 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
115 {.name = "policy", .has_arg = 1, .val = 'P'},
116 {.name = "source", .has_arg = 1, .val = 's'},
117 {.name = "destination", .has_arg = 1, .val = 'd'},
118 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
119 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
120 {.name = "protocol", .has_arg = 1, .val = 'p'},
121 {.name = "in-interface", .has_arg = 1, .val = 'i'},
122 {.name = "jump", .has_arg = 1, .val = 'j'},
123 {.name = "table", .has_arg = 1, .val = 't'},
124 {.name = "match", .has_arg = 1, .val = 'm'},
125 {.name = "numeric", .has_arg = 0, .val = 'n'},
126 {.name = "out-interface", .has_arg = 1, .val = 'o'},
127 {.name = "verbose", .has_arg = 0, .val = 'v'},
128 {.name = "exact", .has_arg = 0, .val = 'x'},
129 {.name = "fragments", .has_arg = 0, .val = 'f'},
130 {.name = "version", .has_arg = 0, .val = 'V'},
131 {.name = "help", .has_arg = 2, .val = 'h'},
132 {.name = "line-numbers", .has_arg = 0, .val = '0'},
133 {.name = "modprobe", .has_arg = 1, .val = 'M'},
134 {.name = "set-counters", .has_arg = 1, .val = 'c'},
135 {.name = "goto", .has_arg = 1, .val = 'g'},
136 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +0000137};
138
Illes Marci63e90632003-03-03 08:08:37 +0000139/* we need this for iptables-restore. iptables-restore.c sets line to the
140 * current line of the input file, in order to give a more precise error
141 * message. iptables itself doesn't need this, so it is initialized to the
142 * magic number of -1 */
143int line = -1;
144
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100145void iptables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
146
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100147struct xtables_globals iptables_globals = {
148 .option_offset = 0,
149 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500150 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100151 .exit_err = iptables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100152};
153
Marc Bouchere6869a82000-03-20 06:03:29 +0000154/* Table of legal combinations of commands and options. If any of the
155 * given commands make an option legal, that option is legal (applies to
156 * CMD_LIST and CMD_ZERO only).
157 * Key:
158 * + compulsory
159 * x illegal
160 * optional
161 */
162
163static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
164/* Well, it's better than "Re: Linux vs FreeBSD" */
165{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200166 /* -n -s -d -p -j -v -x -i -o -f --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000167/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
168/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
169/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
170/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
171/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
172/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
173/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
174/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700175/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000176/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
177/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200178/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200179/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
180/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000181};
182
183static int inverse_for_options[NUMBER_OF_OPT] =
184{
185/* -n */ 0,
186/* -s */ IPT_INV_SRCIP,
187/* -d */ IPT_INV_DSTIP,
188/* -p */ IPT_INV_PROTO,
189/* -j */ 0,
190/* -v */ 0,
191/* -x */ 0,
192/* -i */ IPT_INV_VIA_IN,
193/* -o */ IPT_INV_VIA_OUT,
194/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000195/*--line*/ 0,
196/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000197};
198
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100199#define opts iptables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500200#define prog_name iptables_globals.program_name
201#define prog_vers iptables_globals.program_version
Marc Bouchere6869a82000-03-20 06:03:29 +0000202
Phil Oester8cf65912005-09-19 15:00:33 +0000203int kernel_version;
204
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000205/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000206/* defined in netinet/in.h */
207#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000208#ifndef IPPROTO_ESP
209#define IPPROTO_ESP 50
210#endif
211#ifndef IPPROTO_AH
212#define IPPROTO_AH 51
213#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000214#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000215
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100216static const char *
Rusty Russell28381a42000-05-10 00:19:50 +0000217proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000218{
219 unsigned int i;
220
Rusty Russell28381a42000-05-10 00:19:50 +0000221 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000222 struct protoent *pent = getprotobynumber(proto);
223 if (pent)
224 return pent->p_name;
225 }
226
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100227 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
228 if (xtables_chain_protos[i].num == proto)
229 return xtables_chain_protos[i].name;
Marc Bouchere6869a82000-03-20 06:03:29 +0000230
231 return NULL;
232}
233
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000234enum {
235 IPT_DOTTED_ADDR = 0,
236 IPT_DOTTED_MASK
237};
238
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200239static void __attribute__((noreturn))
Marc Bouchere6869a82000-03-20 06:03:29 +0000240exit_tryhelp(int status)
241{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000242 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000243 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000244 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500245 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500246 xtables_free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000247 exit(status);
248}
249
Patrick McHardy0b639362007-09-08 16:00:01 +0000250static void
Jan Engelhardt395e4412009-02-10 10:43:08 +0100251exit_printhelp(struct xtables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000252{
253 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000254"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100255" %s -I chain [rulenum] rule-specification [options]\n"
256" %s -R chain rulenum rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000257" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200258" %s -[LS] [chain [rulenum]] [options]\n"
259" %s -[FZ] [chain] [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000260" %s -[NX] chain\n"
261" %s -E old-chain-name new-chain-name\n"
262" %s -P chain target [options]\n"
263" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500264 prog_name, prog_vers, prog_name, prog_name,
265 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100266 prog_name, prog_name, prog_name, prog_name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000267
268 printf(
269"Commands:\n"
270"Either long or short options are allowed.\n"
271" --append -A chain Append to chain\n"
272" --delete -D chain Delete matching rule from chain\n"
273" --delete -D chain rulenum\n"
274" Delete rule rulenum (1 = first) from chain\n"
275" --insert -I chain [rulenum]\n"
276" Insert in chain as rulenum (default 1=first)\n"
277" --replace -R chain rulenum\n"
278" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200279" --list -L [chain [rulenum]]\n"
280" List the rules in a chain or all chains\n"
281" --list-rules -S [chain [rulenum]]\n"
282" Print the rules in a chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000283" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700284" --zero -Z [chain [rulenum]]\n"
285" Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000286" --new -N chain Create a new user-defined chain\n"
287" --delete-chain\n"
288" -X [chain] Delete a user-defined chain\n"
289" --policy -P chain target\n"
290" Change policy on chain to target\n"
291" --rename-chain\n"
292" -E old-chain new-chain\n"
293" Change chain name, (moving any references)\n"
294
295"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200296"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100297"[!] --source -s address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000298" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100299"[!] --destination -d address[/mask][...]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000300" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200301"[!] --in-interface -i input name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000302" network interface name ([+] for wildcard)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200303" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000304" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000305#ifdef IPT_F_GOTO
306" --goto -g chain\n"
307" jump to chain with no return\n"
308#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000309" --match -m match\n"
310" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000311" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200312"[!] --out-interface -o output name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000313" network interface name ([+] for wildcard)\n"
314" --table -t table table to manipulate (default: `filter')\n"
315" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000316" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000317" --exact -x expand numbers (display exact values)\n"
318"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000319" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000320" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000321"[!] --version -V print package version.\n");
322
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200323 print_extension_helps(xtables_targets, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000324 exit(0);
325}
326
Patrick McHardy0b639362007-09-08 16:00:01 +0000327void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100328iptables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000329{
330 va_list args;
331
332 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500333 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000334 vfprintf(stderr, msg, args);
335 va_end(args);
336 fprintf(stderr, "\n");
337 if (status == PARAMETER_PROBLEM)
338 exit_tryhelp(status);
339 if (status == VERSION_PROBLEM)
340 fprintf(stderr,
341 "Perhaps iptables or your kernel needs to be upgraded.\n");
342 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500343 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000344 exit(status);
345}
346
Marc Bouchere6869a82000-03-20 06:03:29 +0000347static void
348generic_opt_check(int command, int options)
349{
350 int i, j, legal = 0;
351
352 /* Check that commands are valid with options. Complicated by the
353 * fact that if an option is legal with *any* command given, it is
354 * legal overall (ie. -z and -l).
355 */
356 for (i = 0; i < NUMBER_OF_OPT; i++) {
357 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
358
359 for (j = 0; j < NUMBER_OF_CMD; j++) {
360 if (!(command & (1<<j)))
361 continue;
362
363 if (!(options & (1<<i))) {
364 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100365 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000366 "You need to supply the `-%c' "
367 "option for this command\n",
368 optflags[i]);
369 } else {
370 if (commands_v_options[j][i] != 'x')
371 legal = 1;
372 else if (legal == 0)
373 legal = -1;
374 }
375 }
376 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100377 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000378 "Illegal option `-%c' with this command\n",
379 optflags[i]);
380 }
381}
382
383static char
384opt2char(int option)
385{
386 const char *ptr;
387 for (ptr = optflags; option > 1; option >>= 1, ptr++);
388
389 return *ptr;
390}
391
392static char
393cmd2char(int option)
394{
395 const char *ptr;
396 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
397
398 return *ptr;
399}
400
401static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000402add_command(unsigned int *cmd, const int newcmd, const int othercmds,
403 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000404{
405 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100406 xtables_error(PARAMETER_PROBLEM, "unexpected ! flag");
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100408 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Marc Bouchere6869a82000-03-20 06:03:29 +0000409 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
410 *cmd |= newcmd;
411}
412
Marc Bouchere6869a82000-03-20 06:03:29 +0000413/*
414 * All functions starting with "parse" should succeed, otherwise
415 * the program fails.
416 * Most routines return pointers to static data that may change
417 * between calls to the same or other routines with a few exceptions:
418 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
419 * return global static data.
420*/
421
Rusty Russell28381a42000-05-10 00:19:50 +0000422/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200423static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100424find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100425 int nolookup, struct xtables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000426{
Harald Welteed498492001-07-23 01:24:22 +0000427 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000428
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100429 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100430 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000431
Harald Welte0b0013a2002-02-18 16:15:31 +0000432 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100433 return xtables_find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000434 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100435 return xtables_find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000436
437 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000438}
439
Marc Bouchere6869a82000-03-20 06:03:29 +0000440/* Can't be zero. */
441static int
442parse_rulenumber(const char *rule)
443{
Harald Welteed498492001-07-23 01:24:22 +0000444 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000445
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100446 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100447 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000448 "Invalid rule number `%s'", rule);
449
450 return rulenum;
451}
452
453static const char *
454parse_target(const char *targetname)
455{
456 const char *ptr;
457
458 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100459 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000460 "Invalid target name (too short)");
461
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200462 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100463 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000464 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200465 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000466
467 for (ptr = targetname; *ptr; ptr++)
468 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100469 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000470 "Invalid target name `%s'", targetname);
471 return targetname;
472}
473
Marc Bouchere6869a82000-03-20 06:03:29 +0000474static void
475set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
476 int invert)
477{
478 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100479 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Marc Bouchere6869a82000-03-20 06:03:29 +0000480 opt2char(option));
481 *options |= option;
482
483 if (invert) {
484 unsigned int i;
485 for (i = 0; 1 << i != option; i++);
486
487 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100488 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000489 "cannot have ! before -%c",
490 opt2char(option));
491 *invflg |= inverse_for_options[i];
492 }
493}
494
Marc Bouchere6869a82000-03-20 06:03:29 +0000495static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000496print_num(u_int64_t number, unsigned int format)
497{
498 if (format & FMT_KILOMEGAGIGA) {
499 if (number > 99999) {
500 number = (number + 500) / 1000;
501 if (number > 9999) {
502 number = (number + 500) / 1000;
503 if (number > 9999) {
504 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000505 if (number > 9999) {
506 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000507 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000508 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000509 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000510 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000511 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000512 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000513 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000514 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000515 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000516 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000517 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000518}
519
520
521static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100522print_header(unsigned int format, const char *chain, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000523{
524 struct ipt_counters counters;
525 const char *pol = iptc_get_policy(chain, &counters, handle);
526 printf("Chain %s", chain);
527 if (pol) {
528 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000529 if (!(format & FMT_NOCOUNTS)) {
530 fputc(' ', stdout);
531 print_num(counters.pcnt, (format|FMT_NOTABLE));
532 fputs("packets, ", stdout);
533 print_num(counters.bcnt, (format|FMT_NOTABLE));
534 fputs("bytes", stdout);
535 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000536 printf(")\n");
537 } else {
538 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000539 if (!iptc_get_references(&refs, chain, handle))
540 printf(" (ERROR obtaining refs)\n");
541 else
542 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000543 }
544
545 if (format & FMT_LINENUMBERS)
546 printf(FMT("%-4s ", "%s "), "num");
547 if (!(format & FMT_NOCOUNTS)) {
548 if (format & FMT_KILOMEGAGIGA) {
549 printf(FMT("%5s ","%s "), "pkts");
550 printf(FMT("%5s ","%s "), "bytes");
551 } else {
552 printf(FMT("%8s ","%s "), "pkts");
553 printf(FMT("%10s ","%s "), "bytes");
554 }
555 }
556 if (!(format & FMT_NOTARGET))
557 printf(FMT("%-9s ","%s "), "target");
558 fputs(" prot ", stdout);
559 if (format & FMT_OPTIONS)
560 fputs("opt", stdout);
561 if (format & FMT_VIA) {
562 printf(FMT(" %-6s ","%s "), "in");
563 printf(FMT("%-6s ","%s "), "out");
564 }
565 printf(FMT(" %-19s ","%s "), "source");
566 printf(FMT(" %-19s "," %s "), "destination");
567 printf("\n");
568}
569
Marc Bouchere6869a82000-03-20 06:03:29 +0000570
571static int
572print_match(const struct ipt_entry_match *m,
573 const struct ipt_ip *ip,
574 int numeric)
575{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100576 struct xtables_match *match =
577 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000578
579 if (match) {
580 if (match->print)
581 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000582 else
Rusty Russellb039b022000-09-01 06:04:05 +0000583 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000584 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000585 if (m->u.user.name[0])
586 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000587 }
588 /* Don't stop iterating. */
589 return 0;
590}
591
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100592/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000593static void
594print_firewall(const struct ipt_entry *fw,
595 const char *targname,
596 unsigned int num,
597 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100598 struct iptc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000599{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200600 struct xtables_target *target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000601 const struct ipt_entry_target *t;
602 u_int8_t flags;
603 char buf[BUFSIZ];
604
Marc Bouchere6869a82000-03-20 06:03:29 +0000605 if (!iptc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100606 target = xtables_find_target(targname, XTF_TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000607 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100608 target = xtables_find_target(IPT_STANDARD_TARGET,
609 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000610
611 t = ipt_get_target((struct ipt_entry *)fw);
612 flags = fw->ip.flags;
613
614 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200615 printf(FMT("%-4u ", "%u "), num);
Marc Bouchere6869a82000-03-20 06:03:29 +0000616
617 if (!(format & FMT_NOCOUNTS)) {
618 print_num(fw->counters.pcnt, format);
619 print_num(fw->counters.bcnt, format);
620 }
621
622 if (!(format & FMT_NOTARGET))
623 printf(FMT("%-9s ", "%s "), targname);
624
625 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
626 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100627 const char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000628 if (pname)
629 printf(FMT("%-5s", "%s "), pname);
630 else
631 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
632 }
633
634 if (format & FMT_OPTIONS) {
635 if (format & FMT_NOTABLE)
636 fputs("opt ", stdout);
637 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
638 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
639 fputc(' ', stdout);
640 }
641
642 if (format & FMT_VIA) {
643 char iface[IFNAMSIZ+2];
644
645 if (fw->ip.invflags & IPT_INV_VIA_IN) {
646 iface[0] = '!';
647 iface[1] = '\0';
648 }
649 else iface[0] = '\0';
650
651 if (fw->ip.iniface[0] != '\0') {
652 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000653 }
654 else if (format & FMT_NUMERIC) strcat(iface, "*");
655 else strcat(iface, "any");
656 printf(FMT(" %-6s ","in %s "), iface);
657
658 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
659 iface[0] = '!';
660 iface[1] = '\0';
661 }
662 else iface[0] = '\0';
663
664 if (fw->ip.outiface[0] != '\0') {
665 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000666 }
667 else if (format & FMT_NUMERIC) strcat(iface, "*");
668 else strcat(iface, "any");
669 printf(FMT("%-6s ","out %s "), iface);
670 }
671
672 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
673 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
674 printf(FMT("%-19s ","%s "), "anywhere");
675 else {
676 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100677 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000678 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100679 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.src));
680 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000681 printf(FMT("%-19s ","%s "), buf);
682 }
683
684 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
685 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000686 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000687 else {
688 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100689 strcpy(buf, xtables_ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000690 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100691 strcpy(buf, xtables_ipaddr_to_anyname(&fw->ip.dst));
692 strcat(buf, xtables_ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000693 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000694 }
695
696 if (format & FMT_NOTABLE)
697 fputs(" ", stdout);
698
Harald Welte72bd87e2005-11-24 17:04:05 +0000699#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000700 if(fw->ip.flags & IPT_F_GOTO)
701 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000702#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000703
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
705
706 if (target) {
707 if (target->print)
708 /* Print the target information. */
709 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000710 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000711 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000712 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000713
714 if (!(format & FMT_NONEWLINE))
715 fputc('\n', stdout);
716}
717
718static void
719print_firewall_line(const struct ipt_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100720 struct iptc_handle *const h)
Marc Bouchere6869a82000-03-20 06:03:29 +0000721{
722 struct ipt_entry_target *t;
723
724 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000725 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000726}
727
728static int
729append_entry(const ipt_chainlabel chain,
730 struct ipt_entry *fw,
731 unsigned int nsaddrs,
732 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100733 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000734 unsigned int ndaddrs,
735 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100736 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000737 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100738 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000739{
740 unsigned int i, j;
741 int ret = 1;
742
743 for (i = 0; i < nsaddrs; i++) {
744 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100745 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000746 for (j = 0; j < ndaddrs; j++) {
747 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100748 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100750 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000751 ret &= iptc_append_entry(chain, fw, handle);
752 }
753 }
754
755 return ret;
756}
757
758static int
759replace_entry(const ipt_chainlabel chain,
760 struct ipt_entry *fw,
761 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100762 const struct in_addr *saddr, const struct in_addr *smask,
763 const struct in_addr *daddr, const struct in_addr *dmask,
Marc Bouchere6869a82000-03-20 06:03:29 +0000764 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100765 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000766{
767 fw->ip.src.s_addr = saddr->s_addr;
768 fw->ip.dst.s_addr = daddr->s_addr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100769 fw->ip.smsk.s_addr = smask->s_addr;
770 fw->ip.dmsk.s_addr = dmask->s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771
772 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100773 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000774 return iptc_replace_entry(chain, fw, rulenum, handle);
775}
776
777static int
778insert_entry(const ipt_chainlabel chain,
779 struct ipt_entry *fw,
780 unsigned int rulenum,
781 unsigned int nsaddrs,
782 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100783 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000784 unsigned int ndaddrs,
785 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100786 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000787 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100788 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000789{
790 unsigned int i, j;
791 int ret = 1;
792
793 for (i = 0; i < nsaddrs; i++) {
794 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100795 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 for (j = 0; j < ndaddrs; j++) {
797 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100798 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000799 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100800 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000801 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
802 }
803 }
804
805 return ret;
806}
807
Rusty Russell2e0a3212000-04-19 11:23:18 +0000808static unsigned char *
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100809make_delete_mask(struct xtables_rule_match *matches,
810 const struct xtables_target *target)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000811{
812 /* Establish mask for comparison */
813 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100814 struct xtables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000815 unsigned char *mask, *mptr;
816
817 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000818 for (matchp = matches; matchp; matchp = matchp->next)
819 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000820
Jan Engelhardt630ef482009-01-27 14:58:41 +0100821 mask = xtables_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000822 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100823 + target->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000824
Rusty Russell9e1d2142000-04-23 09:11:12 +0000825 memset(mask, 0xFF, sizeof(struct ipt_entry));
826 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000827
Martin Josefsson78cafda2004-02-02 20:01:18 +0000828 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000829 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000830 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000831 + matchp->match->userspacesize);
832 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000833 }
834
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000835 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000836 IPT_ALIGN(sizeof(struct ipt_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100837 + target->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000838
839 return mask;
840}
841
Marc Bouchere6869a82000-03-20 06:03:29 +0000842static int
843delete_entry(const ipt_chainlabel chain,
844 struct ipt_entry *fw,
845 unsigned int nsaddrs,
846 const struct in_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100847 const struct in_addr smasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000848 unsigned int ndaddrs,
849 const struct in_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100850 const struct in_addr dmasks[],
Marc Bouchere6869a82000-03-20 06:03:29 +0000851 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100852 struct iptc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100853 struct xtables_rule_match *matches,
854 const struct xtables_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000855{
856 unsigned int i, j;
857 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000858 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000859
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100860 mask = make_delete_mask(matches, target);
Marc Bouchere6869a82000-03-20 06:03:29 +0000861 for (i = 0; i < nsaddrs; i++) {
862 fw->ip.src.s_addr = saddrs[i].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100863 fw->ip.smsk.s_addr = smasks[i].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000864 for (j = 0; j < ndaddrs; j++) {
865 fw->ip.dst.s_addr = daddrs[j].s_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +0100866 fw->ip.dmsk.s_addr = dmasks[j].s_addr;
Marc Bouchere6869a82000-03-20 06:03:29 +0000867 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100868 print_firewall_line(fw, handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000869 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000870 }
871 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000872 free(mask);
873
Marc Bouchere6869a82000-03-20 06:03:29 +0000874 return ret;
875}
876
Harald Welteae1ff9f2000-12-01 14:26:20 +0000877int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100878for_each_chain(int (*fn)(const ipt_chainlabel, int, struct iptc_handle *),
879 int verbose, int builtinstoo, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000880{
881 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000882 const char *chain;
883 char *chains;
884 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000885
Rusty Russell9e1d2142000-04-23 09:11:12 +0000886 chain = iptc_first_chain(handle);
887 while (chain) {
888 chaincount++;
889 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000890 }
891
Jan Engelhardt630ef482009-01-27 14:58:41 +0100892 chains = xtables_malloc(sizeof(ipt_chainlabel) * chaincount);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000893 i = 0;
894 chain = iptc_first_chain(handle);
895 while (chain) {
896 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
897 i++;
898 chain = iptc_next_chain(handle);
899 }
900
901 for (i = 0; i < chaincount; i++) {
902 if (!builtinstoo
903 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100904 handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000905 continue;
906 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
907 }
908
909 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +0000910 return ret;
911}
912
Harald Welteae1ff9f2000-12-01 14:26:20 +0000913int
Marc Bouchere6869a82000-03-20 06:03:29 +0000914flush_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100915 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000916{
917 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000918 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000919
920 if (verbose)
921 fprintf(stdout, "Flushing chain `%s'\n", chain);
922 return iptc_flush_entries(chain, handle);
923}
Marc Bouchere6869a82000-03-20 06:03:29 +0000924
925static int
926zero_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100927 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000928{
929 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +0000930 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000931
Marc Bouchere6869a82000-03-20 06:03:29 +0000932 if (verbose)
933 fprintf(stdout, "Zeroing chain `%s'\n", chain);
934 return iptc_zero_entries(chain, handle);
935}
936
Harald Welteae1ff9f2000-12-01 14:26:20 +0000937int
Marc Bouchere6869a82000-03-20 06:03:29 +0000938delete_chain(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100939 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000940{
Rusty Russell9e1d2142000-04-23 09:11:12 +0000941 if (!chain)
942 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000943
944 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000945 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +0000946 return iptc_delete_chain(chain, handle);
947}
948
949static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200950list_entries(const ipt_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100951 int expanded, int linenumbers, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000952{
953 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000954 unsigned int format;
955 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +0000956
957 format = FMT_OPTIONS;
958 if (!verbose)
959 format |= FMT_NOCOUNTS;
960 else
961 format |= FMT_VIA;
962
963 if (numeric)
964 format |= FMT_NUMERIC;
965
966 if (!expanded)
967 format |= FMT_KILOMEGAGIGA;
968
969 if (linenumbers)
970 format |= FMT_LINENUMBERS;
971
Rusty Russell9e1d2142000-04-23 09:11:12 +0000972 for (this = iptc_first_chain(handle);
973 this;
974 this = iptc_next_chain(handle)) {
975 const struct ipt_entry *i;
976 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +0000977
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 if (chain && strcmp(chain, this) != 0)
979 continue;
980
981 if (found) printf("\n");
982
Henrik Nordstrombb340822008-05-13 13:09:23 +0200983 if (!rulenum)
984 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000985 i = iptc_first_rule(this, handle);
986
987 num = 0;
988 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200989 num++;
990 if (!rulenum || num == rulenum)
991 print_firewall(i,
992 iptc_get_target(i, handle),
993 num,
994 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100995 handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000996 i = iptc_next_rule(i, handle);
997 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000998 found = 1;
999 }
1000
1001 errno = ENOENT;
1002 return found;
1003}
1004
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001005static void print_proto(u_int16_t proto, int invert)
1006{
1007 if (proto) {
1008 unsigned int i;
1009 const char *invertstr = invert ? "! " : "";
1010
1011 struct protoent *pent = getprotobynumber(proto);
1012 if (pent) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001013 printf("%s-p %s ", invertstr, pent->p_name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001014 return;
1015 }
1016
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001017 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1018 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001019 printf("%s-p %s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001020 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001021 return;
1022 }
1023
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001024 printf("%s-p %u ", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001025 }
1026}
1027
1028#define IP_PARTS_NATIVE(n) \
1029(unsigned int)((n)>>24)&0xFF, \
1030(unsigned int)((n)>>16)&0xFF, \
1031(unsigned int)((n)>>8)&0xFF, \
1032(unsigned int)((n)&0xFF)
1033
1034#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
1035
1036/* This assumes that mask is contiguous, and byte-bounded. */
1037static void
1038print_iface(char letter, const char *iface, const unsigned char *mask,
1039 int invert)
1040{
1041 unsigned int i;
1042
1043 if (mask[0] == 0)
1044 return;
1045
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001046 printf("%s-%c ", invert ? "! " : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001047
1048 for (i = 0; i < IFNAMSIZ; i++) {
1049 if (mask[i] != 0) {
1050 if (iface[i] != '\0')
1051 printf("%c", iface[i]);
1052 } else {
1053 /* we can access iface[i-1] here, because
1054 * a few lines above we make sure that mask[0] != 0 */
1055 if (iface[i-1] != '\0')
1056 printf("+");
1057 break;
1058 }
1059 }
1060
1061 printf(" ");
1062}
1063
1064static int print_match_save(const struct ipt_entry_match *e,
1065 const struct ipt_ip *ip)
1066{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001067 struct xtables_match *match =
1068 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001069
1070 if (match) {
1071 printf("-m %s ", e->u.user.name);
1072
1073 /* some matches don't provide a save function */
1074 if (match->save)
1075 match->save(ip, e);
1076 } else {
1077 if (e->u.match_size) {
1078 fprintf(stderr,
1079 "Can't find library for match `%s'\n",
1080 e->u.user.name);
1081 exit(1);
1082 }
1083 }
1084 return 0;
1085}
1086
1087/* print a given ip including mask if neccessary */
1088static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert)
1089{
1090 u_int32_t bits, hmask = ntohl(mask);
1091 int i;
1092
1093 if (!mask && !ip && !invert)
1094 return;
1095
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001096 printf("%s%s %u.%u.%u.%u",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001097 invert ? "! " : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001098 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001099 IP_PARTS(ip));
1100
1101 if (mask == 0xFFFFFFFFU) {
1102 printf("/32 ");
1103 return;
1104 }
1105
1106 i = 32;
1107 bits = 0xFFFFFFFEU;
1108 while (--i >= 0 && hmask != bits)
1109 bits <<= 1;
1110 if (i >= 0)
1111 printf("/%u ", i);
1112 else
1113 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
1114}
1115
1116/* We want this to be readable, so only print out neccessary fields.
1117 * Because that's the kind of world I want to live in. */
1118void print_rule(const struct ipt_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001119 struct iptc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001120{
1121 struct ipt_entry_target *t;
1122 const char *target_name;
1123
1124 /* print counters for iptables-save */
1125 if (counters > 0)
1126 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1127
1128 /* print chain name */
1129 printf("-A %s ", chain);
1130
1131 /* Print IP part. */
1132 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
1133 e->ip.invflags & IPT_INV_SRCIP);
1134
1135 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
1136 e->ip.invflags & IPT_INV_DSTIP);
1137
1138 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
1139 e->ip.invflags & IPT_INV_VIA_IN);
1140
1141 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
1142 e->ip.invflags & IPT_INV_VIA_OUT);
1143
1144 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
1145
1146 if (e->ip.flags & IPT_F_FRAG)
1147 printf("%s-f ",
1148 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
1149
1150 /* Print matchinfo part */
1151 if (e->target_offset) {
1152 IPT_MATCH_ITERATE(e, print_match_save, &e->ip);
1153 }
1154
1155 /* print counters for iptables -R */
1156 if (counters < 0)
1157 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1158
1159 /* Print target name */
1160 target_name = iptc_get_target(e, h);
1161 if (target_name && (*target_name != '\0'))
1162#ifdef IPT_F_GOTO
1163 printf("-%c %s ", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
1164#else
1165 printf("-j %s ", target_name);
1166#endif
1167
1168 /* Print targinfo part */
1169 t = ipt_get_target((struct ipt_entry *)e);
1170 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001171 struct xtables_target *target =
1172 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001173
1174 if (!target) {
1175 fprintf(stderr, "Can't find library for target `%s'\n",
1176 t->u.user.name);
1177 exit(1);
1178 }
1179
1180 if (target->save)
1181 target->save(&e->ip, t);
1182 else {
1183 /* If the target size is greater than ipt_entry_target
1184 * there is something to be saved, we just don't know
1185 * how to print it */
1186 if (t->u.target_size !=
1187 sizeof(struct ipt_entry_target)) {
1188 fprintf(stderr, "Target `%s' is missing "
1189 "save function\n",
1190 t->u.user.name);
1191 exit(1);
1192 }
1193 }
1194 }
1195 printf("\n");
1196}
1197
1198static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001199list_rules(const ipt_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001200 struct iptc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001201{
1202 const char *this = NULL;
1203 int found = 0;
1204
1205 if (counters)
1206 counters = -1; /* iptables -c format */
1207
1208 /* Dump out chain names first,
1209 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001210 if (!rulenum) for (this = iptc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001211 this;
1212 this = iptc_next_chain(handle)) {
1213 if (chain && strcmp(this, chain) != 0)
1214 continue;
1215
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001216 if (iptc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001217 struct ipt_counters count;
1218 printf("-P %s %s", this, iptc_get_policy(this, &count, handle));
1219 if (counters)
1220 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1221 printf("\n");
1222 } else {
1223 printf("-N %s\n", this);
1224 }
1225 }
1226
1227 for (this = iptc_first_chain(handle);
1228 this;
1229 this = iptc_next_chain(handle)) {
1230 const struct ipt_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001231 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001232
1233 if (chain && strcmp(this, chain) != 0)
1234 continue;
1235
1236 /* Dump out rules */
1237 e = iptc_first_rule(this, handle);
1238 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001239 num++;
1240 if (!rulenum || num == rulenum)
1241 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001242 e = iptc_next_rule(e, handle);
1243 }
1244 found = 1;
1245 }
1246
1247 errno = ENOENT;
1248 return found;
1249}
1250
Marc Bouchere6869a82000-03-20 06:03:29 +00001251static struct ipt_entry *
1252generate_entry(const struct ipt_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001253 struct xtables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001254 struct ipt_entry_target *target)
1255{
1256 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001257 struct xtables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001258 struct ipt_entry *e;
1259
1260 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001261 for (matchp = matches; matchp; matchp = matchp->next)
1262 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001263
Jan Engelhardt630ef482009-01-27 14:58:41 +01001264 e = xtables_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001265 *e = *fw;
1266 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001267 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001268
1269 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001270 for (matchp = matches; matchp; matchp = matchp->next) {
1271 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1272 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001273 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001274 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001275
1276 return e;
1277}
1278
Jan Engelhardt395e4412009-02-10 10:43:08 +01001279static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001280{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001281 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001282
1283 for (matchp = *matches; matchp;) {
1284 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001285 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001286 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001287 matchp->match->m = NULL;
1288 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001289 if (matchp->match == matchp->match->next) {
1290 free(matchp->match);
1291 matchp->match = NULL;
1292 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001293 free(matchp);
1294 matchp = tmp;
1295 }
1296
1297 *matches = NULL;
1298}
1299
Phil Oester8cf65912005-09-19 15:00:33 +00001300void
1301get_kernel_version(void) {
1302 static struct utsname uts;
1303 int x = 0, y = 0, z = 0;
1304
1305 if (uname(&uts) == -1) {
1306 fprintf(stderr, "Unable to retrieve kernel version.\n");
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001307 xtables_free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001308 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001309 }
1310
1311 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1312 kernel_version = LINUX_VERSION(x, y, z);
1313}
1314
Jan Engelhardtfd187312008-11-10 16:59:27 +01001315int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001316{
1317 struct ipt_entry fw, *e = NULL;
1318 int invert = 0;
1319 unsigned int nsaddrs = 0, ndaddrs = 0;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001320 struct in_addr *saddrs = NULL, *smasks = NULL;
1321 struct in_addr *daddrs = NULL, *dmasks = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001322
1323 int c, verbose = 0;
1324 const char *chain = NULL;
1325 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1326 const char *policy = NULL, *newname = NULL;
1327 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001328 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001330 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001331 struct xtables_rule_match *matches = NULL;
1332 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001333 struct xtables_target *target = NULL;
1334 struct xtables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001335 const char *jumpto = "";
1336 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001337 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001338 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001339
1340 memset(&fw, 0, sizeof(fw));
1341
Harald Welteae1ff9f2000-12-01 14:26:20 +00001342 /* re-set optind to 0 in case do_command gets called
1343 * a second time */
1344 optind = 0;
1345
1346 /* clear mflags in case do_command gets called a second time
1347 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001348 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001349 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001350
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001351 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001352 t->tflags = 0;
1353 t->used = 0;
1354 }
1355
Marc Bouchere6869a82000-03-20 06:03:29 +00001356 /* Suppress error messages: we may add new options if we
1357 demand-load a protocol. */
1358 opterr = 0;
1359
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001360 opts = xt_params->orig_opts;
Marc Bouchere6869a82000-03-20 06:03:29 +00001361 while ((c = getopt_long(argc, argv,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001362 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001363 opts, NULL)) != -1) {
1364 switch (c) {
1365 /*
1366 * Command selection
1367 */
1368 case 'A':
1369 add_command(&command, CMD_APPEND, CMD_NONE,
1370 invert);
1371 chain = optarg;
1372 break;
1373
1374 case 'D':
1375 add_command(&command, CMD_DELETE, CMD_NONE,
1376 invert);
1377 chain = optarg;
1378 if (optind < argc && argv[optind][0] != '-'
1379 && argv[optind][0] != '!') {
1380 rulenum = parse_rulenumber(argv[optind++]);
1381 command = CMD_DELETE_NUM;
1382 }
1383 break;
1384
Marc Bouchere6869a82000-03-20 06:03:29 +00001385 case 'R':
1386 add_command(&command, CMD_REPLACE, CMD_NONE,
1387 invert);
1388 chain = optarg;
1389 if (optind < argc && argv[optind][0] != '-'
1390 && argv[optind][0] != '!')
1391 rulenum = parse_rulenumber(argv[optind++]);
1392 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001393 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001394 "-%c requires a rule number",
1395 cmd2char(CMD_REPLACE));
1396 break;
1397
1398 case 'I':
1399 add_command(&command, CMD_INSERT, CMD_NONE,
1400 invert);
1401 chain = optarg;
1402 if (optind < argc && argv[optind][0] != '-'
1403 && argv[optind][0] != '!')
1404 rulenum = parse_rulenumber(argv[optind++]);
1405 else rulenum = 1;
1406 break;
1407
1408 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001409 add_command(&command, CMD_LIST,
1410 CMD_ZERO | CMD_ZERO_NUM, invert);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 if (optarg) chain = optarg;
1412 else if (optind < argc && argv[optind][0] != '-'
1413 && argv[optind][0] != '!')
1414 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001415 if (optind < argc && argv[optind][0] != '-'
1416 && argv[optind][0] != '!')
1417 rulenum = parse_rulenumber(argv[optind++]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001418 break;
1419
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001420 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001421 add_command(&command, CMD_LIST_RULES,
1422 CMD_ZERO|CMD_ZERO_NUM, invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001423 if (optarg) chain = optarg;
1424 else if (optind < argc && argv[optind][0] != '-'
1425 && argv[optind][0] != '!')
1426 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001427 if (optind < argc && argv[optind][0] != '-'
1428 && argv[optind][0] != '!')
1429 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001430 break;
1431
Marc Bouchere6869a82000-03-20 06:03:29 +00001432 case 'F':
1433 add_command(&command, CMD_FLUSH, CMD_NONE,
1434 invert);
1435 if (optarg) chain = optarg;
1436 else if (optind < argc && argv[optind][0] != '-'
1437 && argv[optind][0] != '!')
1438 chain = argv[optind++];
1439 break;
1440
1441 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001442 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Marc Bouchere6869a82000-03-20 06:03:29 +00001443 invert);
1444 if (optarg) chain = optarg;
1445 else if (optind < argc && argv[optind][0] != '-'
1446 && argv[optind][0] != '!')
1447 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001448 if (optind < argc && argv[optind][0] != '-'
1449 && argv[optind][0] != '!') {
1450 rulenum = parse_rulenumber(argv[optind++]);
1451 command = CMD_ZERO_NUM;
1452 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001453 break;
1454
1455 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001456 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001457 xtables_error(PARAMETER_PROBLEM,
Harald Welte6336bfd2002-05-07 14:41:43 +00001458 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001459 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001460 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001461 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001462 "chain name may not clash "
1463 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001464 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1465 invert);
1466 chain = optarg;
1467 break;
1468
1469 case 'X':
1470 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1471 invert);
1472 if (optarg) chain = optarg;
1473 else if (optind < argc && argv[optind][0] != '-'
1474 && argv[optind][0] != '!')
1475 chain = argv[optind++];
1476 break;
1477
1478 case 'E':
1479 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1480 invert);
1481 chain = optarg;
1482 if (optind < argc && argv[optind][0] != '-'
1483 && argv[optind][0] != '!')
1484 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001485 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001486 xtables_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001487 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001488 "new-chain-name",
1489 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001490 break;
1491
1492 case 'P':
1493 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1494 invert);
1495 chain = optarg;
1496 if (optind < argc && argv[optind][0] != '-'
1497 && argv[optind][0] != '!')
1498 policy = argv[optind++];
1499 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001500 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001501 "-%c requires a chain and a policy",
1502 cmd2char(CMD_SET_POLICY));
1503 break;
1504
1505 case 'h':
1506 if (!optarg)
1507 optarg = argv[optind];
1508
Rusty Russell2e0a3212000-04-19 11:23:18 +00001509 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001510 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001511 xtables_find_match(protocol,
1512 XTF_TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001513
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001514 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001515
1516 /*
1517 * Option selection
1518 */
1519 case 'p':
Jan Engelhardtbf971282009-11-03 19:55:11 +01001520 xtables_check_inverse(optarg, &invert, &optind, argc, argv);
Marc Bouchere6869a82000-03-20 06:03:29 +00001521 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1522 invert);
1523
1524 /* Canonicalize into lower case */
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001525 for (protocol = optarg; *protocol; protocol++)
Marc Bouchere6869a82000-03-20 06:03:29 +00001526 *protocol = tolower(*protocol);
1527
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001528 protocol = optarg;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001529 fw.ip.proto = xtables_parse_protocol(protocol);
Marc Bouchere6869a82000-03-20 06:03:29 +00001530
1531 if (fw.ip.proto == 0
1532 && (fw.ip.invflags & IPT_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001533 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001534 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001535 break;
1536
1537 case 's':
Jan Engelhardtbf971282009-11-03 19:55:11 +01001538 xtables_check_inverse(optarg, &invert, &optind, argc, argv);
Marc Bouchere6869a82000-03-20 06:03:29 +00001539 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1540 invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001541 shostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001542 break;
1543
1544 case 'd':
Jan Engelhardtbf971282009-11-03 19:55:11 +01001545 xtables_check_inverse(optarg, &invert, &optind, argc, argv);
Marc Bouchere6869a82000-03-20 06:03:29 +00001546 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1547 invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001548 dhostnetworkmask = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001549 break;
1550
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001551#ifdef IPT_F_GOTO
1552 case 'g':
1553 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1554 invert);
1555 fw.ip.flags |= IPT_F_GOTO;
1556 jumpto = parse_target(optarg);
1557 break;
1558#endif
1559
Marc Bouchere6869a82000-03-20 06:03:29 +00001560 case 'j':
1561 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1562 invert);
1563 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001564 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001565 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001566
1567 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001568 size_t size;
1569
Rusty Russell73f72f52000-07-03 10:17:57 +00001570 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1571 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001572
Jan Engelhardt630ef482009-01-27 14:58:41 +01001573 target->t = xtables_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001574 target->t->u.target_size = size;
1575 strcpy(target->t->u.user.name, jumpto);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001576 target->t->u.user.revision = target->revision;
Pablo Neira8115e542005-02-14 13:13:04 +00001577 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001578 target->init(target->t);
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001579 opts = xtables_merge_options(
1580 iptables_globals.orig_opts,
1581 opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001582 target->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001583 &target->option_offset);
1584 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001585 xtables_error(OTHER_PROBLEM,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001586 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001587 }
1588 break;
1589
1590
1591 case 'i':
Jan Engelhardtbf971282009-11-03 19:55:11 +01001592 xtables_check_inverse(optarg, &invert, &optind, argc, argv);
Marc Bouchere6869a82000-03-20 06:03:29 +00001593 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1594 invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001595 xtables_parse_interface(optarg,
Marc Bouchere6869a82000-03-20 06:03:29 +00001596 fw.ip.iniface,
1597 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001598 break;
1599
1600 case 'o':
Jan Engelhardtbf971282009-11-03 19:55:11 +01001601 xtables_check_inverse(optarg, &invert, &optind, argc, argv);
Marc Bouchere6869a82000-03-20 06:03:29 +00001602 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1603 invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001604 xtables_parse_interface(optarg,
Marc Bouchere6869a82000-03-20 06:03:29 +00001605 fw.ip.outiface,
1606 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001607 break;
1608
1609 case 'f':
1610 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1611 invert);
1612 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001613 break;
1614
1615 case 'v':
1616 if (!verbose)
1617 set_option(&options, OPT_VERBOSE,
1618 &fw.ip.invflags, invert);
1619 verbose++;
1620 break;
1621
Rusty Russell52a51492000-05-02 16:44:29 +00001622 case 'm': {
1623 size_t size;
1624
Marc Bouchere6869a82000-03-20 06:03:29 +00001625 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001626 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001627 "unexpected ! flag before --match");
1628
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001629 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1630 &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001631 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1632 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001633 m->m = xtables_calloc(1, size);
Rusty Russell52a51492000-05-02 16:44:29 +00001634 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001635 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001636 m->m->u.user.revision = m->revision;
Pablo Neira8115e542005-02-14 13:13:04 +00001637 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001638 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001639 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001640 /* Merge options for non-cloned matches */
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001641 opts = xtables_merge_options(
1642 iptables_globals.orig_opts,
1643 opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001644 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001645 &m->option_offset);
1646 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001647 xtables_error(OTHER_PROBLEM,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001648 "can't alloc memory!");
1649 }
Rusty Russell52a51492000-05-02 16:44:29 +00001650 }
1651 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001652
1653 case 'n':
1654 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1655 invert);
1656 break;
1657
1658 case 't':
1659 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001660 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001661 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001662 *table = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001663 break;
1664
1665 case 'x':
1666 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1667 invert);
1668 break;
1669
1670 case 'V':
1671 if (invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001672 printf("Not %s ;-)\n", prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001673 else
1674 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001675 prog_name, prog_vers);
Marc Bouchere6869a82000-03-20 06:03:29 +00001676 exit(0);
1677
1678 case '0':
1679 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1680 invert);
1681 break;
1682
Harald Welte82dd2ec2000-12-19 05:18:15 +00001683 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001684 xtables_modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001685 break;
1686
Harald Welteccd49e52001-01-23 22:54:34 +00001687 case 'c':
1688
1689 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1690 invert);
1691 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001692 bcnt = strchr(pcnt + 1, ',');
1693 if (bcnt)
1694 bcnt++;
1695 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welteccd49e52001-01-23 22:54:34 +00001696 && argv[optind][0] != '!')
1697 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001698 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001699 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001700 "-%c requires packet and byte counter",
1701 opt2char(OPT_COUNTERS));
1702
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001703 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001704 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001705 "-%c packet counter not numeric",
1706 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001707 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001708
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001709 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001710 xtables_error(PARAMETER_PROBLEM,
Harald Welteccd49e52001-01-23 22:54:34 +00001711 "-%c byte counter not numeric",
1712 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001713 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001714 break;
1715
1716
Marc Bouchere6869a82000-03-20 06:03:29 +00001717 case 1: /* non option */
1718 if (optarg[0] == '!' && optarg[1] == '\0') {
1719 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001720 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001721 "multiple consecutive ! not"
1722 " allowed");
1723 invert = TRUE;
1724 optarg[0] = '\0';
1725 continue;
1726 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001727 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001728 exit_tryhelp(2);
1729
1730 default:
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001731 if (target == NULL || target->parse == NULL ||
1732 !target->parse(c - target->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001733 argv, invert,
1734 &target->tflags,
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001735 &fw, &target->t)) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001736 for (matchp = matches; matchp; matchp = matchp->next) {
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001737 if (matchp->completed ||
1738 matchp->match->parse == NULL)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001739 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001740 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001741 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001742 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001743 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001744 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001745 break;
1746 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001747 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001748
1749 /* If you listen carefully, you can
1750 actually hear this code suck. */
1751
1752 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001753 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001754 * parameter, that has not been parsed yet,
1755 * it's not an option of an explicitly loaded
1756 * match or a target. However, we support
1757 * implicit loading of the protocol match
1758 * extension. '-p tcp' means 'l4 proto 6' and
1759 * at the same time 'load tcp protocol match on
1760 * demand if we specify --dport'.
1761 *
1762 * To make this work, we need to make sure:
1763 * - the parameter has not been parsed by
1764 * a match (m above)
1765 * - a protocol has been specified
1766 * - the protocol extension has not been
1767 * loaded yet, or is loaded and unused
1768 * [think of iptables-restore!]
1769 * - the protocol extension can be successively
1770 * loaded
1771 */
1772 if (m == NULL
1773 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001774 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001775 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001776 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001777 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001778 && (proto_used == 0))
1779 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001780 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001781 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001782 /* Try loading protocol */
1783 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001784
Harald Welte2d86b772002-08-26 12:21:44 +00001785 proto_used = 1;
1786
1787 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1788 + m->size;
1789
Jan Engelhardt630ef482009-01-27 14:58:41 +01001790 m->m = xtables_calloc(1, size);
Harald Welte2d86b772002-08-26 12:21:44 +00001791 m->m->u.match_size = size;
1792 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001793 m->m->u.user.revision = m->revision;
Pablo Neira8115e542005-02-14 13:13:04 +00001794 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001795 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001796
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001797 opts = xtables_merge_options(
1798 iptables_globals.orig_opts,
1799 opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001800 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001801 &m->option_offset);
1802 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001803 xtables_error(OTHER_PROBLEM,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001804 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001805
1806 optind--;
1807 continue;
1808 }
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001809 if (!m) {
1810 if (c == '?') {
1811 if (optopt) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001812 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001813 PARAMETER_PROBLEM,
1814 "option `%s' "
1815 "requires an "
1816 "argument",
1817 argv[optind-1]);
1818 } else {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001819 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001820 PARAMETER_PROBLEM,
1821 "unknown option "
1822 "`%s'",
1823 argv[optind-1]);
1824 }
1825 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001826 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001827 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001828 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001829 }
1830 }
1831 invert = FALSE;
1832 }
1833
Jan Engelhardt1eada722008-08-13 14:41:32 +02001834 if (strcmp(*table, "nat") == 0 &&
1835 ((policy != NULL && strcmp(policy, "DROP") == 0) ||
1836 (jumpto != NULL && strcmp(jumpto, "DROP") == 0)))
Jan Engelhardte0390be2009-03-15 21:22:49 +01001837 xtables_error(PARAMETER_PROBLEM,
1838 "\nThe \"nat\" table is not intended for filtering, "
1839 "the use of DROP is therefore inhibited.\n\n");
Jan Engelhardt1eada722008-08-13 14:41:32 +02001840
Martin Josefsson78cafda2004-02-02 20:01:18 +00001841 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001842 if (matchp->match->final_check != NULL)
1843 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001844
Jan Engelhardt830132a2007-10-04 16:24:50 +00001845 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001846 target->final_check(target->tflags);
1847
1848 /* Fix me: must put inverse options checking here --MN */
1849
1850 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001851 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001852 "unknown arguments found on commandline");
1853 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001854 xtables_error(PARAMETER_PROBLEM, "no command specified");
Marc Bouchere6869a82000-03-20 06:03:29 +00001855 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001856 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001857 "nothing appropriate following !");
1858
Harald Welte6336bfd2002-05-07 14:41:43 +00001859 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 if (!(options & OPT_DESTINATION))
1861 dhostnetworkmask = "0.0.0.0/0";
1862 if (!(options & OPT_SOURCE))
1863 shostnetworkmask = "0.0.0.0/0";
1864 }
1865
1866 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001867 xtables_ipparse_multiple(shostnetworkmask, &saddrs,
1868 &smasks, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001869
1870 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001871 xtables_ipparse_multiple(dhostnetworkmask, &daddrs,
1872 &dmasks, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001873
1874 if ((nsaddrs > 1 || ndaddrs > 1) &&
1875 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001876 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Marc Bouchere6869a82000-03-20 06:03:29 +00001877 " source or destination IP addresses");
1878
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001880 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Marc Bouchere6869a82000-03-20 06:03:29 +00001881 "specify a unique address");
1882
1883 generic_opt_check(command, options);
1884
Jan Engelhardt5429b412010-09-13 15:45:15 +02001885 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001886 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001887 "chain name `%s' too long (must be under %u chars)",
1888 chain, XT_EXTENSION_MAXNAMELEN);
Marc Bouchere6869a82000-03-20 06:03:29 +00001889
Harald Welteae1ff9f2000-12-01 14:26:20 +00001890 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001891 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001892 *handle = iptc_init(*table);
1893
Rusty Russell8beb0492004-12-22 00:37:10 +00001894 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001895 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001896 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001897
Marc Bouchere6869a82000-03-20 06:03:29 +00001898 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001899 xtables_error(VERSION_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001900 "can't initialize iptables table `%s': %s",
1901 *table, iptc_strerror(errno));
1902
Harald Welte6336bfd2002-05-07 14:41:43 +00001903 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001904 || command == CMD_DELETE
1905 || command == CMD_INSERT
1906 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001907 if (strcmp(chain, "PREROUTING") == 0
1908 || strcmp(chain, "INPUT") == 0) {
1909 /* -o not valid with incoming packets. */
1910 if (options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001911 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 "Can't use -%c with %s\n",
1913 opt2char(OPT_VIANAMEOUT),
1914 chain);
1915 }
1916
Rusty Russella4860fd2000-06-17 16:13:02 +00001917 if (strcmp(chain, "POSTROUTING") == 0
1918 || strcmp(chain, "OUTPUT") == 0) {
1919 /* -i not valid with outgoing packets */
1920 if (options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001921 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +00001922 "Can't use -%c with %s\n",
1923 opt2char(OPT_VIANAMEIN),
1924 chain);
1925 }
1926
1927 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001928 fprintf(stderr,
1929 "Warning: using chain %s, not extension\n",
1930 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001931
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001932 if (target->t)
1933 free(target->t);
1934
Marc Bouchere6869a82000-03-20 06:03:29 +00001935 target = NULL;
1936 }
1937
1938 /* If they didn't specify a target, or it's a chain
1939 name, use standard. */
1940 if (!target
1941 && (strlen(jumpto) == 0
1942 || iptc_is_chain(jumpto, *handle))) {
1943 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001944
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001945 target = xtables_find_target(IPT_STANDARD_TARGET,
1946 XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001947
1948 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001949 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001950 target->t = xtables_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001951 target->t->u.target_size = size;
1952 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00001953 if (!iptc_is_chain(jumpto, *handle))
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001954 target->t->u.user.revision = target->revision;
Pablo Neira8115e542005-02-14 13:13:04 +00001955 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001956 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001957 }
1958
Rusty Russell7e53bf92000-03-20 07:03:28 +00001959 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001960 /* it is no chain, and we can't load a plugin.
1961 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001962 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001963 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001964#ifdef IPT_F_GOTO
1965 if (fw.ip.flags & IPT_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001966 xtables_error(PARAMETER_PROBLEM,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001967 "goto '%s' is not a chain\n", jumpto);
1968#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001969 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001970 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001971 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001972 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001973 }
1974 }
1975
1976 switch (command) {
1977 case CMD_APPEND:
1978 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001979 nsaddrs, saddrs, smasks,
1980 ndaddrs, daddrs, dmasks,
Marc Bouchere6869a82000-03-20 06:03:29 +00001981 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001982 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001983 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001984 case CMD_DELETE:
1985 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001986 nsaddrs, saddrs, smasks,
1987 ndaddrs, daddrs, dmasks,
Marc Bouchere6869a82000-03-20 06:03:29 +00001988 options&OPT_VERBOSE,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +01001989 *handle, matches, target);
Marc Bouchere6869a82000-03-20 06:03:29 +00001990 break;
1991 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001992 ret = iptc_delete_num_entry(chain, rulenum - 1, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001993 break;
1994 case CMD_REPLACE:
1995 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001996 saddrs, smasks, daddrs, dmasks,
1997 options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 break;
1999 case CMD_INSERT:
2000 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01002001 nsaddrs, saddrs, smasks,
2002 ndaddrs, daddrs, dmasks,
Marc Bouchere6869a82000-03-20 06:03:29 +00002003 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002004 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002005 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002007 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 break;
2009 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002010 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002011 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07002012 case CMD_ZERO_NUM:
2013 ret = iptc_zero_counter(chain, rulenum, *handle);
2014 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002015 case CMD_LIST:
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07002017 case CMD_LIST|CMD_ZERO_NUM:
Marc Bouchere6869a82000-03-20 06:03:29 +00002018 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002019 rulenum,
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 options&OPT_VERBOSE,
2021 options&OPT_NUMERIC,
2022 options&OPT_EXPANDED,
2023 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002024 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002025 if (ret && (command & CMD_ZERO))
2026 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002027 options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07002028 if (ret && (command & CMD_ZERO_NUM))
2029 ret = iptc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002030 break;
2031 case CMD_LIST_RULES:
2032 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07002033 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002034 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002035 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002036 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002037 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002038 if (ret && (command & CMD_ZERO))
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002040 options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07002041 if (ret && (command & CMD_ZERO_NUM))
2042 ret = iptc_zero_counter(chain, rulenum, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002043 break;
2044 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002045 ret = iptc_create_chain(chain, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002046 break;
2047 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002048 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002049 break;
2050 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002051 ret = iptc_rename_chain(chain, newname, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002052 break;
2053 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002054 ret = iptc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002055 break;
2056 default:
2057 /* We should never reach this... */
2058 exit_tryhelp(2);
2059 }
2060
2061 if (verbose > 1)
2062 dump_entries(*handle);
2063
Martin Josefsson78cafda2004-02-02 20:01:18 +00002064 clear_rule_matches(&matches);
2065
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002066 if (e != NULL) {
2067 free(e);
2068 e = NULL;
2069 }
2070
keso6997cdf2004-07-04 15:20:53 +00002071 free(saddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01002072 free(smasks);
keso6997cdf2004-07-04 15:20:53 +00002073 free(daddrs);
Michael Granzow332e4ac2009-04-09 18:24:36 +01002074 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002075 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002076
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 return ret;
2078}