blob: c475bf260fbbb0643e9b9b69c40146c52d688da6 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +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 *
Rusty Russell5eed48a2000-06-02 20:12:24 +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>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
85#define NUMBER_OF_CMD 15
Rusty Russell5eed48a2000-06-02 20:12:24 +000086static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070087 'Z', 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010089#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
90static const char optflags[]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000091= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +000092
93static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010094 {.name = "append", .has_arg = 1, .val = 'A'},
95 {.name = "delete", .has_arg = 1, .val = 'D'},
96 {.name = "insert", .has_arg = 1, .val = 'I'},
97 {.name = "replace", .has_arg = 1, .val = 'R'},
98 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020099 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100100 {.name = "flush", .has_arg = 2, .val = 'F'},
101 {.name = "zero", .has_arg = 2, .val = 'Z'},
102 {.name = "new-chain", .has_arg = 1, .val = 'N'},
103 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
104 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
105 {.name = "policy", .has_arg = 1, .val = 'P'},
106 {.name = "source", .has_arg = 1, .val = 's'},
107 {.name = "destination", .has_arg = 1, .val = 'd'},
108 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
109 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
110 {.name = "protocol", .has_arg = 1, .val = 'p'},
111 {.name = "in-interface", .has_arg = 1, .val = 'i'},
112 {.name = "jump", .has_arg = 1, .val = 'j'},
113 {.name = "table", .has_arg = 1, .val = 't'},
114 {.name = "match", .has_arg = 1, .val = 'm'},
115 {.name = "numeric", .has_arg = 0, .val = 'n'},
116 {.name = "out-interface", .has_arg = 1, .val = 'o'},
117 {.name = "verbose", .has_arg = 0, .val = 'v'},
118 {.name = "exact", .has_arg = 0, .val = 'x'},
119 {.name = "version", .has_arg = 0, .val = 'V'},
120 {.name = "help", .has_arg = 2, .val = 'h'},
121 {.name = "line-numbers", .has_arg = 0, .val = '0'},
122 {.name = "modprobe", .has_arg = 1, .val = 'M'},
123 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200124 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100125 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000126};
127
Harald Weltea8658ca2003-03-05 07:46:15 +0000128/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
129 * current line of the input file, in order to give a more precise error
130 * message. ip6tables itself doesn't need this, so it is initialized to the
131 * magic number of -1 */
132int line = -1;
133
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100134void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100135struct xtables_globals ip6tables_globals = {
136 .option_offset = 0,
137 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500138 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100139 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100140};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000141
142/* Table of legal combinations of commands and options. If any of the
143 * given commands make an option legal, that option is legal (applies to
144 * CMD_LIST and CMD_ZERO only).
145 * Key:
146 * + compulsory
147 * x illegal
148 * optional
149 */
150
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100151static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000152/* Well, it's better than "Re: Linux vs FreeBSD" */
153{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200154 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000155/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
156/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
157/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
158/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
159/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
160/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
161/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
162/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700163/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000164/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200166/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200167/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000169};
170
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100171static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000172{
173/* -n */ 0,
174/* -s */ IP6T_INV_SRCIP,
175/* -d */ IP6T_INV_DSTIP,
176/* -p */ IP6T_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IP6T_INV_VIA_IN,
181/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000182/*--line*/ 0,
183/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184};
185
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100186#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500187#define prog_name ip6tables_globals.program_name
188#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189/* A few hardcoded protocols for 'all' and in case the user has no
190 /etc/protocols */
191struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100192 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100193 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194};
195
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200196static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000197exit_tryhelp(int status)
198{
Harald Weltea8658ca2003-03-05 07:46:15 +0000199 if (line != -1)
200 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500202 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500203 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000204 exit(status);
205}
206
Patrick McHardy0b639362007-09-08 16:00:01 +0000207static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100208exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000209{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000210 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000211"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100212" %s -I chain [rulenum] rule-specification [options]\n"
213" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000214" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200215" %s -[LS] [chain [rulenum]] [options]\n"
216" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000217" %s -[NX] chain\n"
218" %s -E old-chain-name new-chain-name\n"
219" %s -P chain target [options]\n"
220" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500221 prog_name, prog_vers, prog_name, prog_name,
222 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100223 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000224
225 printf(
226"Commands:\n"
227"Either long or short options are allowed.\n"
228" --append -A chain Append to chain\n"
229" --delete -D chain Delete matching rule from chain\n"
230" --delete -D chain rulenum\n"
231" Delete rule rulenum (1 = first) from chain\n"
232" --insert -I chain [rulenum]\n"
233" Insert in chain as rulenum (default 1=first)\n"
234" --replace -R chain rulenum\n"
235" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200236" --list -L [chain [rulenum]]\n"
237" List the rules in a chain or all chains\n"
238" --list-rules -S [chain [rulenum]]\n"
239" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700241" --zero -Z [chain [rulenum]]\n"
242" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000243" --new -N chain Create a new user-defined chain\n"
244" --delete-chain\n"
245" -X [chain] Delete a user-defined chain\n"
246" --policy -P chain target\n"
247" Change policy on chain to target\n"
248" --rename-chain\n"
249" -E old-chain new-chain\n"
250" Change chain name, (moving any references)\n"
251
252"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200253"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100254"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100256"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000257" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200258"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000259" network interface name ([+] for wildcard)\n"
260" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000261" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200262#ifdef IP6T_F_GOTO
263" --goto -g chain\n"
264" jump to chain with no return\n"
265#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000266" --match -m match\n"
267" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000268" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200269"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270" network interface name ([+] for wildcard)\n"
271" --table -t table table to manipulate (default: `filter')\n"
272" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000273" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000275/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000276" --modprobe=<command> try to insert modules using this command\n"
277" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278"[!] --version -V print package version.\n");
279
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200280 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000281 exit(0);
282}
283
Patrick McHardy0b639362007-09-08 16:00:01 +0000284void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100285ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000286{
287 va_list args;
288
289 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500290 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000291 vfprintf(stderr, msg, args);
292 va_end(args);
293 fprintf(stderr, "\n");
294 if (status == PARAMETER_PROBLEM)
295 exit_tryhelp(status);
296 if (status == VERSION_PROBLEM)
297 fprintf(stderr,
298 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
299 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500300 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000301 exit(status);
302}
303
Rusty Russell5eed48a2000-06-02 20:12:24 +0000304static void
305generic_opt_check(int command, int options)
306{
307 int i, j, legal = 0;
308
309 /* Check that commands are valid with options. Complicated by the
310 * fact that if an option is legal with *any* command given, it is
311 * legal overall (ie. -z and -l).
312 */
313 for (i = 0; i < NUMBER_OF_OPT; i++) {
314 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
315
316 for (j = 0; j < NUMBER_OF_CMD; j++) {
317 if (!(command & (1<<j)))
318 continue;
319
320 if (!(options & (1<<i))) {
321 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100322 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000323 "You need to supply the `-%c' "
324 "option for this command\n",
325 optflags[i]);
326 } else {
327 if (commands_v_options[j][i] != 'x')
328 legal = 1;
329 else if (legal == 0)
330 legal = -1;
331 }
332 }
333 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100334 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335 "Illegal option `-%c' with this command\n",
336 optflags[i]);
337 }
338}
339
340static char
341opt2char(int option)
342{
343 const char *ptr;
344 for (ptr = optflags; option > 1; option >>= 1, ptr++);
345
346 return *ptr;
347}
348
349static char
350cmd2char(int option)
351{
352 const char *ptr;
353 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
354
355 return *ptr;
356}
357
358static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000359add_command(unsigned int *cmd, const int newcmd, const int othercmds,
360 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361{
362 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100363 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000364 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100365 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000366 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
367 *cmd |= newcmd;
368}
369
Rusty Russell5eed48a2000-06-02 20:12:24 +0000370/*
371 * All functions starting with "parse" should succeed, otherwise
372 * the program fails.
373 * Most routines return pointers to static data that may change
374 * between calls to the same or other routines with a few exceptions:
375 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
376 * return global static data.
377*/
378
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000379/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100380static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000381{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000382 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000383 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000384 proto == IPPROTO_AH ||
385 proto == IPPROTO_DSTOPTS);
386}
387
Rusty Russell5eed48a2000-06-02 20:12:24 +0000388/* Can't be zero. */
389static int
390parse_rulenumber(const char *rule)
391{
Harald Welte43ac1912002-03-03 09:43:07 +0000392 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000393
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100394 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100395 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 "Invalid rule number `%s'", rule);
397
398 return rulenum;
399}
400
401static const char *
402parse_target(const char *targetname)
403{
404 const char *ptr;
405
406 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100407 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000408 "Invalid target name (too short)");
409
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200410 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100411 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000412 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200413 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000414
415 for (ptr = targetname; *ptr; ptr++)
416 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100417 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000418 "Invalid target name `%s'", targetname);
419 return targetname;
420}
421
Rusty Russell5eed48a2000-06-02 20:12:24 +0000422static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100423set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000424 int invert)
425{
426 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100427 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000428 opt2char(option));
429 *options |= option;
430
431 if (invert) {
432 unsigned int i;
433 for (i = 0; 1 << i != option; i++);
434
435 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100436 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437 "cannot have ! before -%c",
438 opt2char(option));
439 *invflg |= inverse_for_options[i];
440 }
441}
442
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000443static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100444print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000445{
Harald Welte43ac1912002-03-03 09:43:07 +0000446 if (format & FMT_KILOMEGAGIGA) {
447 if (number > 99999) {
448 number = (number + 500) / 1000;
449 if (number > 9999) {
450 number = (number + 500) / 1000;
451 if (number > 9999) {
452 number = (number + 500) / 1000;
453 if (number > 9999) {
454 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000455 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000456 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000457 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000458 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000459 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000460 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000461 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000462 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000463 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000464 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000465 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000466}
467
Harald Welte43ac1912002-03-03 09:43:07 +0000468
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100470print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471{
472 struct ip6t_counters counters;
473 const char *pol = ip6tc_get_policy(chain, &counters, handle);
474 printf("Chain %s", chain);
475 if (pol) {
476 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000477 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000478 fputc(' ', stdout);
479 print_num(counters.pcnt, (format|FMT_NOTABLE));
480 fputs("packets, ", stdout);
481 print_num(counters.bcnt, (format|FMT_NOTABLE));
482 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000483 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484 printf(")\n");
485 } else {
486 unsigned int refs;
487 if (!ip6tc_get_references(&refs, chain, handle))
488 printf(" (ERROR obtaining refs)\n");
489 else
490 printf(" (%u references)\n", refs);
491 }
492
493 if (format & FMT_LINENUMBERS)
494 printf(FMT("%-4s ", "%s "), "num");
495 if (!(format & FMT_NOCOUNTS)) {
496 if (format & FMT_KILOMEGAGIGA) {
497 printf(FMT("%5s ","%s "), "pkts");
498 printf(FMT("%5s ","%s "), "bytes");
499 } else {
500 printf(FMT("%8s ","%s "), "pkts");
501 printf(FMT("%10s ","%s "), "bytes");
502 }
503 }
504 if (!(format & FMT_NOTARGET))
505 printf(FMT("%-9s ","%s "), "target");
506 fputs(" prot ", stdout);
507 if (format & FMT_OPTIONS)
508 fputs("opt", stdout);
509 if (format & FMT_VIA) {
510 printf(FMT(" %-6s ","%s "), "in");
511 printf(FMT("%-6s ","%s "), "out");
512 }
513 printf(FMT(" %-19s ","%s "), "source");
514 printf(FMT(" %-19s "," %s "), "destination");
515 printf("\n");
516}
517
Harald Welte43ac1912002-03-03 09:43:07 +0000518
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519static int
520print_match(const struct ip6t_entry_match *m,
521 const struct ip6t_ip6 *ip,
522 int numeric)
523{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100524 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100525 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000526
527 if (match) {
528 if (match->print)
529 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000530 else
531 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000532 } else {
533 if (m->u.user.name[0])
534 printf("UNKNOWN match `%s' ", m->u.user.name);
535 }
536 /* Don't stop iterating. */
537 return 0;
538}
539
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100540/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000541static void
542print_firewall(const struct ip6t_entry *fw,
543 const char *targname,
544 unsigned int num,
545 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100546 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000547{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100548 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000549 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100550 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551 char buf[BUFSIZ];
552
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100554 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000555 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100556 target = xtables_find_target(IP6T_STANDARD_TARGET,
557 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000558
559 t = ip6t_get_target((struct ip6t_entry *)fw);
560 flags = fw->ipv6.flags;
561
562 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200563 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000564
565 if (!(format & FMT_NOCOUNTS)) {
566 print_num(fw->counters.pcnt, format);
567 print_num(fw->counters.bcnt, format);
568 }
569
570 if (!(format & FMT_NOTARGET))
571 printf(FMT("%-9s ", "%s "), targname);
572
573 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
574 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100575 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000576 if (pname)
577 printf(FMT("%-5s", "%s "), pname);
578 else
579 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
580 }
581
582 if (format & FMT_OPTIONS) {
583 if (format & FMT_NOTABLE)
584 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000585 fputc(' ', stdout); /* Invert flag of FRAG */
586 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000587 fputc(' ', stdout);
588 }
589
590 if (format & FMT_VIA) {
591 char iface[IFNAMSIZ+2];
592
593 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
594 iface[0] = '!';
595 iface[1] = '\0';
596 }
597 else iface[0] = '\0';
598
599 if (fw->ipv6.iniface[0] != '\0') {
600 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601 }
602 else if (format & FMT_NUMERIC) strcat(iface, "*");
603 else strcat(iface, "any");
604 printf(FMT(" %-6s ","in %s "), iface);
605
606 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
607 iface[0] = '!';
608 iface[1] = '\0';
609 }
610 else iface[0] = '\0';
611
612 if (fw->ipv6.outiface[0] != '\0') {
613 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614 }
615 else if (format & FMT_NUMERIC) strcat(iface, "*");
616 else strcat(iface, "any");
617 printf(FMT("%-6s ","out %s "), iface);
618 }
619
620 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000621 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000622 && !(format & FMT_NUMERIC))
623 printf(FMT("%-19s ","%s "), "anywhere");
624 else {
625 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100626 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000627 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100628 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
629 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000630 printf(FMT("%-19s ","%s "), buf);
631 }
632
633 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
634 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
635 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200636 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000637 else {
638 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100639 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000640 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100641 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
642 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200643 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000644 }
645
646 if (format & FMT_NOTABLE)
647 fputs(" ", stdout);
648
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200649#ifdef IP6T_F_GOTO
650 if(fw->ipv6.flags & IP6T_F_GOTO)
651 printf("[goto] ");
652#endif
653
Rusty Russell5eed48a2000-06-02 20:12:24 +0000654 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
655
656 if (target) {
657 if (target->print)
658 /* Print the target information. */
659 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
660 } else if (t->u.target_size != sizeof(*t))
661 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000662 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000663
664 if (!(format & FMT_NONEWLINE))
665 fputc('\n', stdout);
666}
667
668static void
669print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100670 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000671{
672 struct ip6t_entry_target *t;
673
674 t = ip6t_get_target((struct ip6t_entry *)fw);
675 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
676}
677
678static int
679append_entry(const ip6t_chainlabel chain,
680 struct ip6t_entry *fw,
681 unsigned int nsaddrs,
682 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100683 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000684 unsigned int ndaddrs,
685 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100686 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000687 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100688 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689{
690 unsigned int i, j;
691 int ret = 1;
692
693 for (i = 0; i < nsaddrs; i++) {
694 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100695 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696 for (j = 0; j < ndaddrs; j++) {
697 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100698 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000699 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100700 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701 ret &= ip6tc_append_entry(chain, fw, handle);
702 }
703 }
704
705 return ret;
706}
707
708static int
709replace_entry(const ip6t_chainlabel chain,
710 struct ip6t_entry *fw,
711 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100712 const struct in6_addr *saddr, const struct in6_addr *smask,
713 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100715 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716{
717 fw->ipv6.src = *saddr;
718 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100719 fw->ipv6.smsk = *smask;
720 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000721
722 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100723 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724 return ip6tc_replace_entry(chain, fw, rulenum, handle);
725}
726
727static int
728insert_entry(const ip6t_chainlabel chain,
729 struct ip6t_entry *fw,
730 unsigned int rulenum,
731 unsigned int nsaddrs,
732 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100733 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000734 unsigned int ndaddrs,
735 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100736 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000737 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100738 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739{
740 unsigned int i, j;
741 int ret = 1;
742
743 for (i = 0; i < nsaddrs; i++) {
744 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100745 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746 for (j = 0; j < ndaddrs; j++) {
747 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100748 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100750 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
752 }
753 }
754
755 return ret;
756}
757
758static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100759make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100760 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761{
762 /* Establish mask for comparison */
763 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100764 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765 unsigned char *mask, *mptr;
766
767 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000768 for (matchp = matches; matchp; matchp = matchp->next)
769 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000770
Jan Engelhardt630ef482009-01-27 14:58:41 +0100771 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000772 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100773 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774
775 memset(mask, 0xFF, sizeof(struct ip6t_entry));
776 mptr = mask + sizeof(struct ip6t_entry);
777
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000778 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000779 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000780 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000781 + matchp->match->userspacesize);
782 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 }
784
Max Kellermann5b76f682008-01-29 13:42:48 +0000785 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000786 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100787 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000788
789 return mask;
790}
791
792static int
793delete_entry(const ip6t_chainlabel chain,
794 struct ip6t_entry *fw,
795 unsigned int nsaddrs,
796 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100797 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798 unsigned int ndaddrs,
799 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100800 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000801 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100802 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100803 struct xtables_rule_match *matches,
804 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805{
806 unsigned int i, j;
807 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808 unsigned char *mask;
809
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100810 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000812 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100813 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000815 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100816 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000817 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100818 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000819 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 }
821 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000822 free(mask);
823
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 return ret;
825}
826
András Kis-Szabó764316a2001-02-26 17:31:20 +0000827int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100828for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
829 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830{
Max Kellermann5b76f682008-01-29 13:42:48 +0000831 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000832 const char *chain;
833 char *chains;
834 unsigned int i, chaincount = 0;
835
836 chain = ip6tc_first_chain(handle);
837 while (chain) {
838 chaincount++;
839 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000840 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841
Jan Engelhardt630ef482009-01-27 14:58:41 +0100842 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000843 i = 0;
844 chain = ip6tc_first_chain(handle);
845 while (chain) {
846 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
847 i++;
848 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000849 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850
851 for (i = 0; i < chaincount; i++) {
852 if (!builtinstoo
853 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100854 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000856 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000857 }
858
859 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000860 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000861}
862
András Kis-Szabó764316a2001-02-26 17:31:20 +0000863int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100865 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000866{
867 if (!chain)
868 return for_each_chain(flush_entries, verbose, 1, handle);
869
870 if (verbose)
871 fprintf(stdout, "Flushing chain `%s'\n", chain);
872 return ip6tc_flush_entries(chain, handle);
873}
874
875static int
876zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100877 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878{
879 if (!chain)
880 return for_each_chain(zero_entries, verbose, 1, handle);
881
882 if (verbose)
883 fprintf(stdout, "Zeroing chain `%s'\n", chain);
884 return ip6tc_zero_entries(chain, handle);
885}
886
András Kis-Szabó764316a2001-02-26 17:31:20 +0000887int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000888delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100889 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000890{
891 if (!chain)
892 return for_each_chain(delete_chain, verbose, 0, handle);
893
894 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000895 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896 return ip6tc_delete_chain(chain, handle);
897}
898
899static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200900list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100901 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000902{
903 int found = 0;
904 unsigned int format;
905 const char *this;
906
907 format = FMT_OPTIONS;
908 if (!verbose)
909 format |= FMT_NOCOUNTS;
910 else
911 format |= FMT_VIA;
912
913 if (numeric)
914 format |= FMT_NUMERIC;
915
916 if (!expanded)
917 format |= FMT_KILOMEGAGIGA;
918
919 if (linenumbers)
920 format |= FMT_LINENUMBERS;
921
922 for (this = ip6tc_first_chain(handle);
923 this;
924 this = ip6tc_next_chain(handle)) {
925 const struct ip6t_entry *i;
926 unsigned int num;
927
928 if (chain && strcmp(chain, this) != 0)
929 continue;
930
931 if (found) printf("\n");
932
Henrik Nordstrombb340822008-05-13 13:09:23 +0200933 if (!rulenum)
934 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935 i = ip6tc_first_rule(this, handle);
936
937 num = 0;
938 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200939 num++;
940 if (!rulenum || num == rulenum)
941 print_firewall(i,
942 ip6tc_get_target(i, handle),
943 num,
944 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100945 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000946 i = ip6tc_next_rule(i, handle);
947 }
948 found = 1;
949 }
950
951 errno = ENOENT;
952 return found;
953}
954
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200955/* This assumes that mask is contiguous, and byte-bounded. */
956static void
957print_iface(char letter, const char *iface, const unsigned char *mask,
958 int invert)
959{
960 unsigned int i;
961
962 if (mask[0] == 0)
963 return;
964
Jan Engelhardt73866352010-12-18 02:04:59 +0100965 printf("%s -%c", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200966
967 for (i = 0; i < IFNAMSIZ; i++) {
968 if (mask[i] != 0) {
969 if (iface[i] != '\0')
970 printf("%c", iface[i]);
971 } else {
972 /* we can access iface[i-1] here, because
973 * a few lines above we make sure that mask[0] != 0 */
974 if (iface[i-1] != '\0')
975 printf("+");
976 break;
977 }
978 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200979}
980
981/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100982static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200983{
984 if (proto) {
985 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100986 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200987
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100988 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200989 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100990 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200991 invertstr, pent->p_name);
992 return;
993 }
994
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100995 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
996 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100997 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100998 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200999 return;
1000 }
1001
Jan Engelhardt73866352010-12-18 02:04:59 +01001002 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001003 }
1004}
1005
1006static int print_match_save(const struct ip6t_entry_match *e,
1007 const struct ip6t_ip6 *ip)
1008{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001009 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001010 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011
1012 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001013 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001014
1015 /* some matches don't provide a save function */
1016 if (match->save)
1017 match->save(ip, e);
1018 } else {
1019 if (e->u.match_size) {
1020 fprintf(stderr,
1021 "Can't find library for match `%s'\n",
1022 e->u.user.name);
1023 exit(1);
1024 }
1025 }
1026 return 0;
1027}
1028
1029/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001030static void print_ip(const char *prefix, const struct in6_addr *ip,
1031 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001032{
1033 char buf[51];
1034 int l = ipv6_prefix_length(mask);
1035
1036 if (l == 0 && !invert)
1037 return;
1038
Jan Engelhardt73866352010-12-18 02:04:59 +01001039 printf("%s %s %s",
1040 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001041 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001042 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1043
1044 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001045 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001046 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001047 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001048}
1049
1050/* We want this to be readable, so only print out neccessary fields.
1051 * Because that's the kind of world I want to live in. */
1052void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001053 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001054{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001055 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001056 const char *target_name;
1057
1058 /* print counters for iptables-save */
1059 if (counters > 0)
1060 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1061
1062 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001063 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001064
1065 /* Print IP part. */
1066 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1067 e->ipv6.invflags & IP6T_INV_SRCIP);
1068
1069 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1070 e->ipv6.invflags & IP6T_INV_DSTIP);
1071
1072 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1073 e->ipv6.invflags & IP6T_INV_VIA_IN);
1074
1075 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1076 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1077
1078 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1079
1080#if 0
1081 /* not definied in ipv6
1082 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1083 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001084 printf("%s -f",
1085 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001086#endif
1087
1088 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001089 printf("%s -? %d",
1090 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001091 e->ipv6.tos);
1092
1093 /* Print matchinfo part */
1094 if (e->target_offset) {
1095 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1096 }
1097
1098 /* print counters for iptables -R */
1099 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001100 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001101
1102 /* Print target name */
1103 target_name = ip6tc_get_target(e, h);
1104 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001105#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001106 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001107#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001108 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001109#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001110
1111 /* Print targinfo part */
1112 t = ip6t_get_target((struct ip6t_entry *)e);
1113 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001114 struct xtables_target *target =
1115 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001116
1117 if (!target) {
1118 fprintf(stderr, "Can't find library for target `%s'\n",
1119 t->u.user.name);
1120 exit(1);
1121 }
1122
1123 if (target->save)
1124 target->save(&e->ipv6, t);
1125 else {
1126 /* If the target size is greater than ip6t_entry_target
1127 * there is something to be saved, we just don't know
1128 * how to print it */
1129 if (t->u.target_size !=
1130 sizeof(struct ip6t_entry_target)) {
1131 fprintf(stderr, "Target `%s' is missing "
1132 "save function\n",
1133 t->u.user.name);
1134 exit(1);
1135 }
1136 }
1137 }
1138 printf("\n");
1139}
1140
1141static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001142list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001143 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001144{
1145 const char *this = NULL;
1146 int found = 0;
1147
1148 if (counters)
1149 counters = -1; /* iptables -c format */
1150
1151 /* Dump out chain names first,
1152 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001153 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001154 this;
1155 this = ip6tc_next_chain(handle)) {
1156 if (chain && strcmp(this, chain) != 0)
1157 continue;
1158
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001159 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001160 struct ip6t_counters count;
1161 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1162 if (counters)
1163 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1164 printf("\n");
1165 } else {
1166 printf("-N %s\n", this);
1167 }
1168 }
1169
1170 for (this = ip6tc_first_chain(handle);
1171 this;
1172 this = ip6tc_next_chain(handle)) {
1173 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001174 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001175
1176 if (chain && strcmp(this, chain) != 0)
1177 continue;
1178
1179 /* Dump out rules */
1180 e = ip6tc_first_rule(this, handle);
1181 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001182 num++;
1183 if (!rulenum || num == rulenum)
1184 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001185 e = ip6tc_next_rule(e, handle);
1186 }
1187 found = 1;
1188 }
1189
1190 errno = ENOENT;
1191 return found;
1192}
1193
Rusty Russell5eed48a2000-06-02 20:12:24 +00001194static struct ip6t_entry *
1195generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001196 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001197 struct ip6t_entry_target *target)
1198{
1199 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001200 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001201 struct ip6t_entry *e;
1202
1203 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001204 for (matchp = matches; matchp; matchp = matchp->next)
1205 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001206
Jan Engelhardt630ef482009-01-27 14:58:41 +01001207 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001208 *e = *fw;
1209 e->target_offset = size;
1210 e->next_offset = size + target->u.target_size;
1211
1212 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001213 for (matchp = matches; matchp; matchp = matchp->next) {
1214 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1215 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001216 }
1217 memcpy(e->elems + size, target, target->u.target_size);
1218
1219 return e;
1220}
1221
Jan Engelhardt395e4412009-02-10 10:43:08 +01001222static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001223{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001224 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001225
1226 for (matchp = *matches; matchp;) {
1227 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001228 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001229 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001230 matchp->match->m = NULL;
1231 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001232 if (matchp->match == matchp->match->next) {
1233 free(matchp->match);
1234 matchp->match = NULL;
1235 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001236 free(matchp);
1237 matchp = tmp;
1238 }
1239
1240 *matches = NULL;
1241}
1242
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001243static void command_default(struct iptables_command_state *cs)
1244{
1245 struct xtables_rule_match *matchp;
1246 struct xtables_match *m;
1247
1248 if (cs->target == NULL || cs->target->parse == NULL ||
1249 cs->c < cs->target->option_offset ||
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001250 cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
1251 cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
1252 cs->invert, &cs->target->tflags, &cs->fw6,
1253 &cs->target->t);
1254 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001255 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001256
1257 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1258 m = matchp->match;
1259
1260 if (matchp->completed || m->parse == NULL)
1261 continue;
1262 if (cs->c < matchp->match->option_offset ||
1263 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1264 continue;
1265 m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
1266 &m->mflags, &cs->fw6, &m->m);
1267 return;
1268 }
1269
1270 /* Try loading protocol */
1271 m = load_proto(cs);
1272 if (m != NULL) {
1273 size_t size;
1274
1275 cs->proto_used = 1;
1276
1277 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1278
1279 m->m = xtables_calloc(1, size);
1280 m->m->u.match_size = size;
1281 strcpy(m->m->u.user.name, m->name);
1282 m->m->u.user.revision = m->revision;
1283 if (m->init != NULL)
1284 m->init(m->m);
1285
1286 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1287 m->extra_opts, &m->option_offset);
1288
1289 optind--;
1290 return;
1291 }
1292
1293 if (cs->c == '?') {
1294 if (optopt)
1295 xtables_error(PARAMETER_PROBLEM,
1296 "option \"%s\" requires an argument",
1297 cs->argv[optind-1]);
1298 else
1299 xtables_error(PARAMETER_PROBLEM,
1300 "unknown option \"%s\"", cs->argv[optind-1]);
1301 }
1302 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001303}
1304
Jan Engelhardtfd187312008-11-10 16:59:27 +01001305int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001306{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001307 struct iptables_command_state cs;
1308 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001309 unsigned int nsaddrs = 0, ndaddrs = 0;
1310 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001311 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001312
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001313 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001314 const char *chain = NULL;
1315 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1316 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001317 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001318 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001319 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001320 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001321 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001322 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001323 const char *jumpto = "";
Patrick McHardy875441e2007-10-17 08:48:58 +00001324 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001325
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001326 memset(&cs, 0, sizeof(cs));
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001327 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001328
Harald Welte43ac1912002-03-03 09:43:07 +00001329 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001330 * a second time */
1331 optind = 0;
1332
Harald Welte43ac1912002-03-03 09:43:07 +00001333 /* clear mflags in case do_command gets called a second time
1334 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001335 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001336 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001337
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001338 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001339 t->tflags = 0;
1340 t->used = 0;
1341 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001342
Rusty Russell5eed48a2000-06-02 20:12:24 +00001343 /* Suppress error messages: we may add new options if we
1344 demand-load a protocol. */
1345 opterr = 0;
1346
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001347 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001348 while ((cs.c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001349 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001350 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001351 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001352 /*
1353 * Command selection
1354 */
1355 case 'A':
1356 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001357 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001358 chain = optarg;
1359 break;
1360
1361 case 'D':
1362 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001363 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 chain = optarg;
1365 if (optind < argc && argv[optind][0] != '-'
1366 && argv[optind][0] != '!') {
1367 rulenum = parse_rulenumber(argv[optind++]);
1368 command = CMD_DELETE_NUM;
1369 }
1370 break;
1371
Rusty Russell5eed48a2000-06-02 20:12:24 +00001372 case 'R':
1373 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001374 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375 chain = optarg;
1376 if (optind < argc && argv[optind][0] != '-'
1377 && argv[optind][0] != '!')
1378 rulenum = parse_rulenumber(argv[optind++]);
1379 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001380 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001381 "-%c requires a rule number",
1382 cmd2char(CMD_REPLACE));
1383 break;
1384
1385 case 'I':
1386 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001387 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001388 chain = optarg;
1389 if (optind < argc && argv[optind][0] != '-'
1390 && argv[optind][0] != '!')
1391 rulenum = parse_rulenumber(argv[optind++]);
1392 else rulenum = 1;
1393 break;
1394
1395 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001396 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001397 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001398 if (optarg) chain = optarg;
1399 else if (optind < argc && argv[optind][0] != '-'
1400 && argv[optind][0] != '!')
1401 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001402 if (optind < argc && argv[optind][0] != '-'
1403 && argv[optind][0] != '!')
1404 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405 break;
1406
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001407 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001408 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001409 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001410 if (optarg) chain = optarg;
1411 else if (optind < argc && argv[optind][0] != '-'
1412 && argv[optind][0] != '!')
1413 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001414 if (optind < argc && argv[optind][0] != '-'
1415 && argv[optind][0] != '!')
1416 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001417 break;
1418
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 case 'F':
1420 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001421 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 if (optarg) chain = optarg;
1423 else if (optind < argc && argv[optind][0] != '-'
1424 && argv[optind][0] != '!')
1425 chain = argv[optind++];
1426 break;
1427
1428 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001429 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001430 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 if (optarg) chain = optarg;
1432 else if (optind < argc && argv[optind][0] != '-'
1433 && argv[optind][0] != '!')
1434 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001435 if (optind < argc && argv[optind][0] != '-'
1436 && argv[optind][0] != '!') {
1437 rulenum = parse_rulenumber(argv[optind++]);
1438 command = CMD_ZERO_NUM;
1439 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440 break;
1441
1442 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001443 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001444 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001445 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001446 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001447 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001448 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001449 "chain name may not clash "
1450 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001451 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001452 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001453 chain = optarg;
1454 break;
1455
1456 case 'X':
1457 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001458 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459 if (optarg) chain = optarg;
1460 else if (optind < argc && argv[optind][0] != '-'
1461 && argv[optind][0] != '!')
1462 chain = argv[optind++];
1463 break;
1464
1465 case 'E':
1466 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001467 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001468 chain = optarg;
1469 if (optind < argc && argv[optind][0] != '-'
1470 && argv[optind][0] != '!')
1471 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001472 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001473 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001474 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001475 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001476 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001477 break;
1478
1479 case 'P':
1480 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001481 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001482 chain = optarg;
1483 if (optind < argc && argv[optind][0] != '-'
1484 && argv[optind][0] != '!')
1485 policy = argv[optind++];
1486 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001487 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001488 "-%c requires a chain and a policy",
1489 cmd2char(CMD_SET_POLICY));
1490 break;
1491
1492 case 'h':
1493 if (!optarg)
1494 optarg = argv[optind];
1495
Jonas Berlin1b91e592005-04-01 06:58:38 +00001496 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001497 if (!cs.matches && cs.protocol)
1498 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1499 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001501 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001502
1503 /*
1504 * Option selection
1505 */
1506 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001507 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001508 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001509 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001510
1511 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001512 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1513 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001515 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001516 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1517 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001518
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001519 if (cs.fw6.ipv6.proto == 0
1520 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001521 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001522 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001523
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001524 if (is_exthdr(cs.fw6.ipv6.proto)
1525 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001526 fprintf(stderr,
1527 "Warning: never matched protocol: %s. "
1528 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001529 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530 break;
1531
1532 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001533 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001534 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001535 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001536 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001537 break;
1538
1539 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001540 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001541 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001542 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001543 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544 break;
1545
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001546#ifdef IP6T_F_GOTO
1547 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001548 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001549 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001550 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001551 jumpto = parse_target(optarg);
1552 break;
1553#endif
1554
Rusty Russell5eed48a2000-06-02 20:12:24 +00001555 case 'j':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001556 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001557 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001558 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001559 /* TRY_LOAD (may be chain name) */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001560 cs.target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001562 if (cs.target) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 size_t size;
1564
Harald Welte43ac1912002-03-03 09:43:07 +00001565 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001566 + cs.target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001568 cs.target->t = xtables_calloc(1, size);
1569 cs.target->t->u.target_size = size;
1570 strcpy(cs.target->t->u.user.name, jumpto);
1571 cs.target->t->u.user.revision = cs.target->revision;
1572 if (cs.target->init != NULL)
1573 cs.target->init(cs.target->t);
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001574 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001575 cs.target->extra_opts,
1576 &cs.target->option_offset);
Patrick McHardy455559b2008-04-15 15:51:19 +02001577 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001578 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001579 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001580 }
1581 break;
1582
1583
1584 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001585 if (*optarg == '\0')
1586 xtables_error(PARAMETER_PROBLEM,
1587 "Empty interface is likely to be "
1588 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001589 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001590 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001591 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001592 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001593 cs.fw6.ipv6.iniface,
1594 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001595 break;
1596
1597 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001598 if (*optarg == '\0')
1599 xtables_error(PARAMETER_PROBLEM,
1600 "Empty interface is likely to be "
1601 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001602 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001603 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001604 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001605 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001606 cs.fw6.ipv6.outiface,
1607 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 break;
1609
1610 case 'v':
1611 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001612 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001613 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614 verbose++;
1615 break;
1616
1617 case 'm': {
1618 size_t size;
1619
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001620 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001621 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 "unexpected ! flag before --match");
1623
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001624 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001625 &cs.matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001626 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1627 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001628 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 m->m->u.match_size = size;
1630 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001631 m->m->u.user.revision = m->revision;
Jonas Berlin1b91e592005-04-01 06:58:38 +00001632 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001633 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001634 if (m != m->next)
1635 /* Merge options for non-cloned matches */
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001636 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001637 }
1638 break;
1639
1640 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001641 set_option(&cs.options, OPT_NUMERIC, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001642 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 break;
1644
1645 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001646 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001647 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001649 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650 break;
1651
1652 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001653 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001654 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001655 break;
1656
1657 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001658 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001659 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 else
1661 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001662 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001663 exit(0);
1664
1665 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001666 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001667 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 break;
1669
Harald Welte43ac1912002-03-03 09:43:07 +00001670 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001671 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001672 break;
1673
1674 case 'c':
1675
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001676 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001677 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001678 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001679 bcnt = strchr(pcnt + 1, ',');
1680 if (bcnt)
1681 bcnt++;
1682 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001683 && argv[optind][0] != '!')
1684 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001685 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001686 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001687 "-%c requires packet and byte counter",
1688 opt2char(OPT_COUNTERS));
1689
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001690 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001691 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001692 "-%c packet counter not numeric",
1693 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001694 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001695
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001696 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001697 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001698 "-%c byte counter not numeric",
1699 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001700 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001701 break;
1702
Rusty Russell5eed48a2000-06-02 20:12:24 +00001703 case 1: /* non option */
1704 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001705 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001706 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707 "multiple consecutive ! not"
1708 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001709 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 optarg[0] = '\0';
1711 continue;
1712 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001713 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001714 exit_tryhelp(2);
1715
1716 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001717 command_default(&cs);
1718 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001719 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001720 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001721 }
1722
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001723 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001724 if (matchp->match->final_check != NULL)
1725 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001726
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001727 if (cs.target != NULL && cs.target->final_check != NULL)
1728 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001729
1730 /* Fix me: must put inverse options checking here --MN */
1731
1732 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001733 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001734 "unknown arguments found on commandline");
1735 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001736 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001737 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001738 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001739 "nothing appropriate following !");
1740
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001741 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001742 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001743 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001744 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001745 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001746 }
1747
1748 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001749 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1750 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001751
1752 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001753 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1754 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001755
1756 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001757 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001758 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001759 " source or destination IP addresses");
1760
Rusty Russell5eed48a2000-06-02 20:12:24 +00001761 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001762 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763 "specify a unique address");
1764
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001765 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001766
Jan Engelhardt5429b412010-09-13 15:45:15 +02001767 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001768 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001769 "chain name `%s' too long (must be under %u chars)",
1770 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001771
Harald Welte43ac1912002-03-03 09:43:07 +00001772 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001773 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001774 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001775
Rusty Russell8beb0492004-12-22 00:37:10 +00001776 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001777 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001778 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001779
Harald Welte43ac1912002-03-03 09:43:07 +00001780 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001781 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001782 "can't initialize ip6tables table `%s': %s",
1783 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001784
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001785 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001786 || command == CMD_DELETE
1787 || command == CMD_INSERT
1788 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001789 if (strcmp(chain, "PREROUTING") == 0
1790 || strcmp(chain, "INPUT") == 0) {
1791 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001792 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001793 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001794 "Can't use -%c with %s\n",
1795 opt2char(OPT_VIANAMEOUT),
1796 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001797 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001798
Harald Welte43ac1912002-03-03 09:43:07 +00001799 if (strcmp(chain, "POSTROUTING") == 0
1800 || strcmp(chain, "OUTPUT") == 0) {
1801 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001802 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001803 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001804 "Can't use -%c with %s\n",
1805 opt2char(OPT_VIANAMEIN),
1806 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001807 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001808
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001809 if (cs.target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001810 fprintf(stderr,
1811 "Warning: using chain %s, not extension\n",
1812 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001813
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001814 if (cs.target->t)
1815 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001816
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001817 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001818 }
1819
1820 /* If they didn't specify a target, or it's a chain
1821 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001822 if (!cs.target
Rusty Russell5eed48a2000-06-02 20:12:24 +00001823 && (strlen(jumpto) == 0
1824 || ip6tc_is_chain(jumpto, *handle))) {
1825 size_t size;
1826
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001827 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001828 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001829
1830 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001831 + cs.target->size;
1832 cs.target->t = xtables_calloc(1, size);
1833 cs.target->t->u.target_size = size;
1834 strcpy(cs.target->t->u.user.name, jumpto);
1835 if (cs.target->init != NULL)
1836 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001837 }
1838
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001839 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001840 /* it is no chain, and we can't load a plugin.
1841 * We cannot know if the plugin is corrupt, non
1842 * existant OR if the user just misspelled a
1843 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001844#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001845 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001846 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001847 "goto '%s' is not a chain\n", jumpto);
1848#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001849 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001850 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001851 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001852 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001853 }
1854 }
1855
1856 switch (command) {
1857 case CMD_APPEND:
1858 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001859 nsaddrs, saddrs, smasks,
1860 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001861 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001862 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001863 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001864 case CMD_DELETE:
1865 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001866 nsaddrs, saddrs, smasks,
1867 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001868 cs.options&OPT_VERBOSE,
1869 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001870 break;
1871 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001872 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 break;
1874 case CMD_REPLACE:
1875 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001876 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001877 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878 break;
1879 case CMD_INSERT:
1880 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001881 nsaddrs, saddrs, smasks,
1882 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001883 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001884 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001885 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001887 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001888 break;
1889 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001890 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001892 case CMD_ZERO_NUM:
1893 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1894 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001895 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001896 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001897 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001899 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001900 cs.options&OPT_VERBOSE,
1901 cs.options&OPT_NUMERIC,
1902 cs.options&OPT_EXPANDED,
1903 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001904 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001905 if (ret && (command & CMD_ZERO))
1906 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001907 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001908 if (ret && (command & CMD_ZERO_NUM))
1909 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001910 break;
1911 case CMD_LIST_RULES:
1912 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001913 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001914 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001915 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001916 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001917 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001918 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001919 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001920 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001921 if (ret && (command & CMD_ZERO_NUM))
1922 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001923 break;
1924 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001925 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001926 break;
1927 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001928 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001929 break;
1930 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001931 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932 break;
1933 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001934 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935 break;
1936 default:
1937 /* We should never reach this... */
1938 exit_tryhelp(2);
1939 }
1940
1941 if (verbose > 1)
1942 dump_entries6(*handle);
1943
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001944 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001945
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001946 if (e != NULL) {
1947 free(e);
1948 e = NULL;
1949 }
1950
Michael Granzow332e4ac2009-04-09 18:24:36 +01001951 free(saddrs);
1952 free(smasks);
1953 free(daddrs);
1954 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001955 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001956
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 return ret;
1958}