blob: 3330420f5a365f9c2d1e7f8e1384e14a665d9208 [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 ||
1250 cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE ||
1251 !cs->target->parse(cs->c - cs->target->option_offset,
1252 cs->argv, cs->invert,
1253 &cs->target->tflags,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001254 &cs->fw6, &cs->target->t)) {
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001255 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1256 if (matchp->completed ||
1257 matchp->match->parse == NULL)
1258 continue;
1259 if (cs->c < matchp->match->option_offset ||
1260 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1261 continue;
1262 if (matchp->match->parse(cs->c - matchp->match->option_offset,
1263 cs->argv, cs->invert,
1264 &matchp->match->mflags,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001265 &cs->fw6,
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001266 &matchp->match->m))
1267 break;
1268 }
1269 m = matchp ? matchp->match : NULL;
1270
Jan Engelhardtacef6042011-02-07 03:18:53 +01001271 if (m == NULL && (m = load_proto(cs)) != NULL) {
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001272 /* Try loading protocol */
1273 size_t size;
1274
1275 cs->proto_used = 1;
1276
1277 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1278 + m->size;
1279
1280 m->m = xtables_calloc(1, size);
1281 m->m->u.match_size = size;
1282 strcpy(m->m->u.user.name, m->name);
1283 m->m->u.user.revision = m->revision;
1284 if (m->init != NULL)
1285 m->init(m->m);
1286
1287 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1288 m->extra_opts, &m->option_offset);
1289
1290 optind--;
1291 return;
1292 }
1293
1294 if (!m) {
1295 if (cs->c == '?') {
1296 if (optopt) {
1297 xtables_error(
1298 PARAMETER_PROBLEM,
1299 "option `%s' "
1300 "requires an "
1301 "argument",
1302 cs->argv[optind-1]);
1303 } else {
1304 xtables_error(
1305 PARAMETER_PROBLEM,
1306 "unknown option "
1307 "`%s'",
1308 cs->argv[optind-1]);
1309 }
1310 }
1311 xtables_error(PARAMETER_PROBLEM,
1312 "Unknown arg `%s'", optarg);
1313 }
1314 }
1315}
1316
Jan Engelhardtfd187312008-11-10 16:59:27 +01001317int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001318{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001319 struct iptables_command_state cs;
1320 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001321 unsigned int nsaddrs = 0, ndaddrs = 0;
1322 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001323 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001324
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001325 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001326 const char *chain = NULL;
1327 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1328 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001329 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001330 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001331 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001332 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001333 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001334 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001335 const char *jumpto = "";
Patrick McHardy875441e2007-10-17 08:48:58 +00001336 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001337
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001338 memset(&cs, 0, sizeof(cs));
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001339 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001340
Harald Welte43ac1912002-03-03 09:43:07 +00001341 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001342 * a second time */
1343 optind = 0;
1344
Harald Welte43ac1912002-03-03 09:43:07 +00001345 /* clear mflags in case do_command gets called a second time
1346 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001347 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001348 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001349
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001350 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001351 t->tflags = 0;
1352 t->used = 0;
1353 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001354
Rusty Russell5eed48a2000-06-02 20:12:24 +00001355 /* Suppress error messages: we may add new options if we
1356 demand-load a protocol. */
1357 opterr = 0;
1358
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001359 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001360 while ((cs.c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001361 "-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 +00001362 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001363 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 /*
1365 * Command selection
1366 */
1367 case 'A':
1368 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001369 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001370 chain = optarg;
1371 break;
1372
1373 case 'D':
1374 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001375 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001376 chain = optarg;
1377 if (optind < argc && argv[optind][0] != '-'
1378 && argv[optind][0] != '!') {
1379 rulenum = parse_rulenumber(argv[optind++]);
1380 command = CMD_DELETE_NUM;
1381 }
1382 break;
1383
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 case 'R':
1385 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001386 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387 chain = optarg;
1388 if (optind < argc && argv[optind][0] != '-'
1389 && argv[optind][0] != '!')
1390 rulenum = parse_rulenumber(argv[optind++]);
1391 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001392 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001393 "-%c requires a rule number",
1394 cmd2char(CMD_REPLACE));
1395 break;
1396
1397 case 'I':
1398 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001399 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001400 chain = optarg;
1401 if (optind < argc && argv[optind][0] != '-'
1402 && argv[optind][0] != '!')
1403 rulenum = parse_rulenumber(argv[optind++]);
1404 else rulenum = 1;
1405 break;
1406
1407 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001408 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001409 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410 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++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 break;
1418
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001419 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001420 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001421 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001422 if (optarg) chain = optarg;
1423 else if (optind < argc && argv[optind][0] != '-'
1424 && argv[optind][0] != '!')
1425 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001426 if (optind < argc && argv[optind][0] != '-'
1427 && argv[optind][0] != '!')
1428 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001429 break;
1430
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 case 'F':
1432 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001433 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001434 if (optarg) chain = optarg;
1435 else if (optind < argc && argv[optind][0] != '-'
1436 && argv[optind][0] != '!')
1437 chain = argv[optind++];
1438 break;
1439
1440 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001441 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001442 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 if (optarg) chain = optarg;
1444 else if (optind < argc && argv[optind][0] != '-'
1445 && argv[optind][0] != '!')
1446 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001447 if (optind < argc && argv[optind][0] != '-'
1448 && argv[optind][0] != '!') {
1449 rulenum = parse_rulenumber(argv[optind++]);
1450 command = CMD_ZERO_NUM;
1451 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001452 break;
1453
1454 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001455 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001456 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001457 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001458 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001459 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001460 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001461 "chain name may not clash "
1462 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001463 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001464 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001465 chain = optarg;
1466 break;
1467
1468 case 'X':
1469 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001470 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001471 if (optarg) chain = optarg;
1472 else if (optind < argc && argv[optind][0] != '-'
1473 && argv[optind][0] != '!')
1474 chain = argv[optind++];
1475 break;
1476
1477 case 'E':
1478 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001479 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001480 chain = optarg;
1481 if (optind < argc && argv[optind][0] != '-'
1482 && argv[optind][0] != '!')
1483 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001484 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001485 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001486 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001487 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001488 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001489 break;
1490
1491 case 'P':
1492 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001493 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494 chain = optarg;
1495 if (optind < argc && argv[optind][0] != '-'
1496 && argv[optind][0] != '!')
1497 policy = argv[optind++];
1498 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001499 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500 "-%c requires a chain and a policy",
1501 cmd2char(CMD_SET_POLICY));
1502 break;
1503
1504 case 'h':
1505 if (!optarg)
1506 optarg = argv[optind];
1507
Jonas Berlin1b91e592005-04-01 06:58:38 +00001508 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001509 if (!cs.matches && cs.protocol)
1510 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1511 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001512
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001513 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514
1515 /*
1516 * Option selection
1517 */
1518 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001519 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001520 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001521 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001522
1523 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001524 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1525 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001526
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001527 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001528 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1529 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001531 if (cs.fw6.ipv6.proto == 0
1532 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001533 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001535
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001536 if (is_exthdr(cs.fw6.ipv6.proto)
1537 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001538 fprintf(stderr,
1539 "Warning: never matched protocol: %s. "
1540 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001541 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001542 break;
1543
1544 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001545 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001546 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001547 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001548 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549 break;
1550
1551 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001552 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001553 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001554 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001555 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556 break;
1557
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001558#ifdef IP6T_F_GOTO
1559 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001560 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001561 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001562 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001563 jumpto = parse_target(optarg);
1564 break;
1565#endif
1566
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567 case 'j':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001568 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001569 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001571 /* TRY_LOAD (may be chain name) */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001572 cs.target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001573
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001574 if (cs.target) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001575 size_t size;
1576
Harald Welte43ac1912002-03-03 09:43:07 +00001577 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001578 + cs.target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001579
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001580 cs.target->t = xtables_calloc(1, size);
1581 cs.target->t->u.target_size = size;
1582 strcpy(cs.target->t->u.user.name, jumpto);
1583 cs.target->t->u.user.revision = cs.target->revision;
1584 if (cs.target->init != NULL)
1585 cs.target->init(cs.target->t);
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001586 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001587 cs.target->extra_opts,
1588 &cs.target->option_offset);
Patrick McHardy455559b2008-04-15 15:51:19 +02001589 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001590 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001591 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001592 }
1593 break;
1594
1595
1596 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001597 if (*optarg == '\0')
1598 xtables_error(PARAMETER_PROBLEM,
1599 "Empty interface is likely to be "
1600 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001601 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001602 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001603 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001604 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001605 cs.fw6.ipv6.iniface,
1606 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001607 break;
1608
1609 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001610 if (*optarg == '\0')
1611 xtables_error(PARAMETER_PROBLEM,
1612 "Empty interface is likely to be "
1613 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001614 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001615 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001616 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001617 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001618 cs.fw6.ipv6.outiface,
1619 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001620 break;
1621
1622 case 'v':
1623 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001624 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001625 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001626 verbose++;
1627 break;
1628
1629 case 'm': {
1630 size_t size;
1631
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001632 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001633 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 "unexpected ! flag before --match");
1635
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001636 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001637 &cs.matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001638 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1639 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001640 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001641 m->m->u.match_size = size;
1642 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001643 m->m->u.user.revision = m->revision;
Jonas Berlin1b91e592005-04-01 06:58:38 +00001644 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001645 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001646 if (m != m->next)
1647 /* Merge options for non-cloned matches */
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001648 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649 }
1650 break;
1651
1652 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001653 set_option(&cs.options, OPT_NUMERIC, &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 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001658 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001659 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001661 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001662 break;
1663
1664 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001665 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001666 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001667 break;
1668
1669 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001670 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001671 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001672 else
1673 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001674 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001675 exit(0);
1676
1677 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001678 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001679 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001680 break;
1681
Harald Welte43ac1912002-03-03 09:43:07 +00001682 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001683 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001684 break;
1685
1686 case 'c':
1687
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001688 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001689 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001690 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001691 bcnt = strchr(pcnt + 1, ',');
1692 if (bcnt)
1693 bcnt++;
1694 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001695 && argv[optind][0] != '!')
1696 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001697 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001698 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001699 "-%c requires packet and byte counter",
1700 opt2char(OPT_COUNTERS));
1701
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001702 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001703 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001704 "-%c packet counter not numeric",
1705 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001706 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001707
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001708 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001709 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001710 "-%c byte counter not numeric",
1711 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001712 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001713 break;
1714
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 case 1: /* non option */
1716 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001717 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001718 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001719 "multiple consecutive ! not"
1720 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001721 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001722 optarg[0] = '\0';
1723 continue;
1724 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001725 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001726 exit_tryhelp(2);
1727
1728 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001729 command_default(&cs);
1730 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001731 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001732 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001733 }
1734
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001735 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001736 if (matchp->match->final_check != NULL)
1737 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001738
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001739 if (cs.target != NULL && cs.target->final_check != NULL)
1740 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001741
1742 /* Fix me: must put inverse options checking here --MN */
1743
1744 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001745 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001746 "unknown arguments found on commandline");
1747 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001748 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001749 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001750 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001751 "nothing appropriate following !");
1752
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001753 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001754 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001755 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001756 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001757 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001758 }
1759
1760 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001761 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1762 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763
1764 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001765 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1766 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001767
1768 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001769 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001770 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001771 " source or destination IP addresses");
1772
Rusty Russell5eed48a2000-06-02 20:12:24 +00001773 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001774 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001775 "specify a unique address");
1776
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001777 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001778
Jan Engelhardt5429b412010-09-13 15:45:15 +02001779 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001780 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001781 "chain name `%s' too long (must be under %u chars)",
1782 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001783
Harald Welte43ac1912002-03-03 09:43:07 +00001784 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001785 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001786 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001787
Rusty Russell8beb0492004-12-22 00:37:10 +00001788 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001789 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001790 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001791
Harald Welte43ac1912002-03-03 09:43:07 +00001792 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001793 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001794 "can't initialize ip6tables table `%s': %s",
1795 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001796
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001797 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001798 || command == CMD_DELETE
1799 || command == CMD_INSERT
1800 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001801 if (strcmp(chain, "PREROUTING") == 0
1802 || strcmp(chain, "INPUT") == 0) {
1803 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001804 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001805 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001806 "Can't use -%c with %s\n",
1807 opt2char(OPT_VIANAMEOUT),
1808 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001809 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810
Harald Welte43ac1912002-03-03 09:43:07 +00001811 if (strcmp(chain, "POSTROUTING") == 0
1812 || strcmp(chain, "OUTPUT") == 0) {
1813 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001814 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001815 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001816 "Can't use -%c with %s\n",
1817 opt2char(OPT_VIANAMEIN),
1818 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001819 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001820
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001821 if (cs.target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001822 fprintf(stderr,
1823 "Warning: using chain %s, not extension\n",
1824 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001826 if (cs.target->t)
1827 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001828
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001829 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 }
1831
1832 /* If they didn't specify a target, or it's a chain
1833 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001834 if (!cs.target
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835 && (strlen(jumpto) == 0
1836 || ip6tc_is_chain(jumpto, *handle))) {
1837 size_t size;
1838
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001839 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001840 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001841
1842 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001843 + cs.target->size;
1844 cs.target->t = xtables_calloc(1, size);
1845 cs.target->t->u.target_size = size;
1846 strcpy(cs.target->t->u.user.name, jumpto);
1847 if (cs.target->init != NULL)
1848 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001849 }
1850
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001851 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001852 /* it is no chain, and we can't load a plugin.
1853 * We cannot know if the plugin is corrupt, non
1854 * existant OR if the user just misspelled a
1855 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001856#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001857 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001858 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001859 "goto '%s' is not a chain\n", jumpto);
1860#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001861 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001863 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001864 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001865 }
1866 }
1867
1868 switch (command) {
1869 case CMD_APPEND:
1870 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001871 nsaddrs, saddrs, smasks,
1872 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001873 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001874 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001875 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001876 case CMD_DELETE:
1877 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001878 nsaddrs, saddrs, smasks,
1879 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001880 cs.options&OPT_VERBOSE,
1881 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882 break;
1883 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001884 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001885 break;
1886 case CMD_REPLACE:
1887 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001888 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001889 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001890 break;
1891 case CMD_INSERT:
1892 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001893 nsaddrs, saddrs, smasks,
1894 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001895 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001896 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001899 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900 break;
1901 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001902 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001903 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001904 case CMD_ZERO_NUM:
1905 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1906 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001907 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001908 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001909 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001910 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001911 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001912 cs.options&OPT_VERBOSE,
1913 cs.options&OPT_NUMERIC,
1914 cs.options&OPT_EXPANDED,
1915 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001916 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001917 if (ret && (command & CMD_ZERO))
1918 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001919 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001920 if (ret && (command & CMD_ZERO_NUM))
1921 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001922 break;
1923 case CMD_LIST_RULES:
1924 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001925 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001926 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001927 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001928 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001929 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001930 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001932 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001933 if (ret && (command & CMD_ZERO_NUM))
1934 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935 break;
1936 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001937 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001938 break;
1939 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001940 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 break;
1942 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001943 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001944 break;
1945 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001946 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 break;
1948 default:
1949 /* We should never reach this... */
1950 exit_tryhelp(2);
1951 }
1952
1953 if (verbose > 1)
1954 dump_entries6(*handle);
1955
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001956 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001957
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001958 if (e != NULL) {
1959 free(e);
1960 e = NULL;
1961 }
1962
Michael Granzow332e4ac2009-04-09 18:24:36 +01001963 free(saddrs);
1964 free(smasks);
1965 free(daddrs);
1966 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001967 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001968
Rusty Russell5eed48a2000-06-02 20:12:24 +00001969 return ret;
1970}