blob: 5a550bfb400afdb5bde2806db5ee4b9e834deb64 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
85#define NUMBER_OF_CMD 15
Rusty Russell5eed48a2000-06-02 20:12:24 +000086static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070087 'Z', 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010089#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
90static const char optflags[]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000091= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +000092
93static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010094 {.name = "append", .has_arg = 1, .val = 'A'},
95 {.name = "delete", .has_arg = 1, .val = 'D'},
96 {.name = "insert", .has_arg = 1, .val = 'I'},
97 {.name = "replace", .has_arg = 1, .val = 'R'},
98 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020099 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100100 {.name = "flush", .has_arg = 2, .val = 'F'},
101 {.name = "zero", .has_arg = 2, .val = 'Z'},
102 {.name = "new-chain", .has_arg = 1, .val = 'N'},
103 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
104 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
105 {.name = "policy", .has_arg = 1, .val = 'P'},
106 {.name = "source", .has_arg = 1, .val = 's'},
107 {.name = "destination", .has_arg = 1, .val = 'd'},
108 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
109 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
110 {.name = "protocol", .has_arg = 1, .val = 'p'},
111 {.name = "in-interface", .has_arg = 1, .val = 'i'},
112 {.name = "jump", .has_arg = 1, .val = 'j'},
113 {.name = "table", .has_arg = 1, .val = 't'},
114 {.name = "match", .has_arg = 1, .val = 'm'},
115 {.name = "numeric", .has_arg = 0, .val = 'n'},
116 {.name = "out-interface", .has_arg = 1, .val = 'o'},
117 {.name = "verbose", .has_arg = 0, .val = 'v'},
118 {.name = "exact", .has_arg = 0, .val = 'x'},
119 {.name = "version", .has_arg = 0, .val = 'V'},
120 {.name = "help", .has_arg = 2, .val = 'h'},
121 {.name = "line-numbers", .has_arg = 0, .val = '0'},
122 {.name = "modprobe", .has_arg = 1, .val = 'M'},
123 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200124 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100125 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000126};
127
Harald Weltea8658ca2003-03-05 07:46:15 +0000128/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
129 * current line of the input file, in order to give a more precise error
130 * message. ip6tables itself doesn't need this, so it is initialized to the
131 * magic number of -1 */
132int line = -1;
133
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100134void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100135struct xtables_globals ip6tables_globals = {
136 .option_offset = 0,
137 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500138 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100139 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100140};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000141
142/* Table of legal combinations of commands and options. If any of the
143 * given commands make an option legal, that option is legal (applies to
144 * CMD_LIST and CMD_ZERO only).
145 * Key:
146 * + compulsory
147 * x illegal
148 * optional
149 */
150
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100151static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000152/* Well, it's better than "Re: Linux vs FreeBSD" */
153{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200154 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000155/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
156/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
157/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
158/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
159/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
160/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
161/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
162/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700163/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000164/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200166/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200167/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000169};
170
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100171static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000172{
173/* -n */ 0,
174/* -s */ IP6T_INV_SRCIP,
175/* -d */ IP6T_INV_DSTIP,
176/* -p */ IP6T_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IP6T_INV_VIA_IN,
181/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000182/*--line*/ 0,
183/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184};
185
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100186#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500187#define prog_name ip6tables_globals.program_name
188#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189/* A few hardcoded protocols for 'all' and in case the user has no
190 /etc/protocols */
191struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100192 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100193 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194};
195
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200196static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000197exit_tryhelp(int status)
198{
Harald Weltea8658ca2003-03-05 07:46:15 +0000199 if (line != -1)
200 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500202 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500203 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000204 exit(status);
205}
206
Patrick McHardy0b639362007-09-08 16:00:01 +0000207static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100208exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000209{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000210 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000211"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100212" %s -I chain [rulenum] rule-specification [options]\n"
213" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000214" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200215" %s -[LS] [chain [rulenum]] [options]\n"
216" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000217" %s -[NX] chain\n"
218" %s -E old-chain-name new-chain-name\n"
219" %s -P chain target [options]\n"
220" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500221 prog_name, prog_vers, prog_name, prog_name,
222 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100223 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000224
225 printf(
226"Commands:\n"
227"Either long or short options are allowed.\n"
228" --append -A chain Append to chain\n"
229" --delete -D chain Delete matching rule from chain\n"
230" --delete -D chain rulenum\n"
231" Delete rule rulenum (1 = first) from chain\n"
232" --insert -I chain [rulenum]\n"
233" Insert in chain as rulenum (default 1=first)\n"
234" --replace -R chain rulenum\n"
235" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200236" --list -L [chain [rulenum]]\n"
237" List the rules in a chain or all chains\n"
238" --list-rules -S [chain [rulenum]]\n"
239" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700241" --zero -Z [chain [rulenum]]\n"
242" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000243" --new -N chain Create a new user-defined chain\n"
244" --delete-chain\n"
245" -X [chain] Delete a user-defined chain\n"
246" --policy -P chain target\n"
247" Change policy on chain to target\n"
248" --rename-chain\n"
249" -E old-chain new-chain\n"
250" Change chain name, (moving any references)\n"
251
252"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200253"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100254"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100256"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000257" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200258"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000259" network interface name ([+] for wildcard)\n"
260" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000261" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200262#ifdef IP6T_F_GOTO
263" --goto -g chain\n"
264" jump to chain with no return\n"
265#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000266" --match -m match\n"
267" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000268" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200269"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270" network interface name ([+] for wildcard)\n"
271" --table -t table table to manipulate (default: `filter')\n"
272" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000273" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000275/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000276" --modprobe=<command> try to insert modules using this command\n"
277" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278"[!] --version -V print package version.\n");
279
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200280 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000281 exit(0);
282}
283
Patrick McHardy0b639362007-09-08 16:00:01 +0000284void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100285ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000286{
287 va_list args;
288
289 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500290 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000291 vfprintf(stderr, msg, args);
292 va_end(args);
293 fprintf(stderr, "\n");
294 if (status == PARAMETER_PROBLEM)
295 exit_tryhelp(status);
296 if (status == VERSION_PROBLEM)
297 fprintf(stderr,
298 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
299 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500300 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000301 exit(status);
302}
303
Rusty Russell5eed48a2000-06-02 20:12:24 +0000304static void
305generic_opt_check(int command, int options)
306{
307 int i, j, legal = 0;
308
309 /* Check that commands are valid with options. Complicated by the
310 * fact that if an option is legal with *any* command given, it is
311 * legal overall (ie. -z and -l).
312 */
313 for (i = 0; i < NUMBER_OF_OPT; i++) {
314 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
315
316 for (j = 0; j < NUMBER_OF_CMD; j++) {
317 if (!(command & (1<<j)))
318 continue;
319
320 if (!(options & (1<<i))) {
321 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100322 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000323 "You need to supply the `-%c' "
324 "option for this command\n",
325 optflags[i]);
326 } else {
327 if (commands_v_options[j][i] != 'x')
328 legal = 1;
329 else if (legal == 0)
330 legal = -1;
331 }
332 }
333 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100334 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335 "Illegal option `-%c' with this command\n",
336 optflags[i]);
337 }
338}
339
340static char
341opt2char(int option)
342{
343 const char *ptr;
344 for (ptr = optflags; option > 1; option >>= 1, ptr++);
345
346 return *ptr;
347}
348
349static char
350cmd2char(int option)
351{
352 const char *ptr;
353 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
354
355 return *ptr;
356}
357
358static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000359add_command(unsigned int *cmd, const int newcmd, const int othercmds,
360 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361{
362 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100363 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000364 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100365 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000366 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
367 *cmd |= newcmd;
368}
369
Rusty Russell5eed48a2000-06-02 20:12:24 +0000370/*
371 * All functions starting with "parse" should succeed, otherwise
372 * the program fails.
373 * Most routines return pointers to static data that may change
374 * between calls to the same or other routines with a few exceptions:
375 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
376 * return global static data.
377*/
378
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000379/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100380static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000381{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000382 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000383 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000384 proto == IPPROTO_AH ||
385 proto == IPPROTO_DSTOPTS);
386}
387
Rusty Russell5eed48a2000-06-02 20:12:24 +0000388/* Can't be zero. */
389static int
390parse_rulenumber(const char *rule)
391{
Harald Welte43ac1912002-03-03 09:43:07 +0000392 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000393
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100394 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100395 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 "Invalid rule number `%s'", rule);
397
398 return rulenum;
399}
400
401static const char *
402parse_target(const char *targetname)
403{
404 const char *ptr;
405
406 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100407 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000408 "Invalid target name (too short)");
409
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200410 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100411 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000412 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200413 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000414
415 for (ptr = targetname; *ptr; ptr++)
416 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100417 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000418 "Invalid target name `%s'", targetname);
419 return targetname;
420}
421
Rusty Russell5eed48a2000-06-02 20:12:24 +0000422static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100423set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000424 int invert)
425{
426 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100427 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000428 opt2char(option));
429 *options |= option;
430
431 if (invert) {
432 unsigned int i;
433 for (i = 0; 1 << i != option; i++);
434
435 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100436 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437 "cannot have ! before -%c",
438 opt2char(option));
439 *invflg |= inverse_for_options[i];
440 }
441}
442
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000443static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100444print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000445{
Harald Welte43ac1912002-03-03 09:43:07 +0000446 if (format & FMT_KILOMEGAGIGA) {
447 if (number > 99999) {
448 number = (number + 500) / 1000;
449 if (number > 9999) {
450 number = (number + 500) / 1000;
451 if (number > 9999) {
452 number = (number + 500) / 1000;
453 if (number > 9999) {
454 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000455 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000456 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000457 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000458 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000459 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000460 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000461 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000462 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000463 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000464 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000465 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000466}
467
Harald Welte43ac1912002-03-03 09:43:07 +0000468
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100470print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471{
472 struct ip6t_counters counters;
473 const char *pol = ip6tc_get_policy(chain, &counters, handle);
474 printf("Chain %s", chain);
475 if (pol) {
476 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000477 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000478 fputc(' ', stdout);
479 print_num(counters.pcnt, (format|FMT_NOTABLE));
480 fputs("packets, ", stdout);
481 print_num(counters.bcnt, (format|FMT_NOTABLE));
482 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000483 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484 printf(")\n");
485 } else {
486 unsigned int refs;
487 if (!ip6tc_get_references(&refs, chain, handle))
488 printf(" (ERROR obtaining refs)\n");
489 else
490 printf(" (%u references)\n", refs);
491 }
492
493 if (format & FMT_LINENUMBERS)
494 printf(FMT("%-4s ", "%s "), "num");
495 if (!(format & FMT_NOCOUNTS)) {
496 if (format & FMT_KILOMEGAGIGA) {
497 printf(FMT("%5s ","%s "), "pkts");
498 printf(FMT("%5s ","%s "), "bytes");
499 } else {
500 printf(FMT("%8s ","%s "), "pkts");
501 printf(FMT("%10s ","%s "), "bytes");
502 }
503 }
504 if (!(format & FMT_NOTARGET))
505 printf(FMT("%-9s ","%s "), "target");
506 fputs(" prot ", stdout);
507 if (format & FMT_OPTIONS)
508 fputs("opt", stdout);
509 if (format & FMT_VIA) {
510 printf(FMT(" %-6s ","%s "), "in");
511 printf(FMT("%-6s ","%s "), "out");
512 }
513 printf(FMT(" %-19s ","%s "), "source");
514 printf(FMT(" %-19s "," %s "), "destination");
515 printf("\n");
516}
517
Harald Welte43ac1912002-03-03 09:43:07 +0000518
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519static int
520print_match(const struct ip6t_entry_match *m,
521 const struct ip6t_ip6 *ip,
522 int numeric)
523{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100524 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100525 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000526
527 if (match) {
528 if (match->print)
529 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000530 else
531 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000532 } else {
533 if (m->u.user.name[0])
534 printf("UNKNOWN match `%s' ", m->u.user.name);
535 }
536 /* Don't stop iterating. */
537 return 0;
538}
539
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100540/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000541static void
542print_firewall(const struct ip6t_entry *fw,
543 const char *targname,
544 unsigned int num,
545 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100546 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000547{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100548 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000549 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100550 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551 char buf[BUFSIZ];
552
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100554 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000555 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100556 target = xtables_find_target(IP6T_STANDARD_TARGET,
557 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000558
559 t = ip6t_get_target((struct ip6t_entry *)fw);
560 flags = fw->ipv6.flags;
561
562 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200563 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000564
565 if (!(format & FMT_NOCOUNTS)) {
566 print_num(fw->counters.pcnt, format);
567 print_num(fw->counters.bcnt, format);
568 }
569
570 if (!(format & FMT_NOTARGET))
571 printf(FMT("%-9s ", "%s "), targname);
572
573 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
574 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100575 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000576 if (pname)
577 printf(FMT("%-5s", "%s "), pname);
578 else
579 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
580 }
581
582 if (format & FMT_OPTIONS) {
583 if (format & FMT_NOTABLE)
584 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000585 fputc(' ', stdout); /* Invert flag of FRAG */
586 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000587 fputc(' ', stdout);
588 }
589
590 if (format & FMT_VIA) {
591 char iface[IFNAMSIZ+2];
592
593 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
594 iface[0] = '!';
595 iface[1] = '\0';
596 }
597 else iface[0] = '\0';
598
599 if (fw->ipv6.iniface[0] != '\0') {
600 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601 }
602 else if (format & FMT_NUMERIC) strcat(iface, "*");
603 else strcat(iface, "any");
604 printf(FMT(" %-6s ","in %s "), iface);
605
606 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
607 iface[0] = '!';
608 iface[1] = '\0';
609 }
610 else iface[0] = '\0';
611
612 if (fw->ipv6.outiface[0] != '\0') {
613 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614 }
615 else if (format & FMT_NUMERIC) strcat(iface, "*");
616 else strcat(iface, "any");
617 printf(FMT("%-6s ","out %s "), iface);
618 }
619
620 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000621 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000622 && !(format & FMT_NUMERIC))
623 printf(FMT("%-19s ","%s "), "anywhere");
624 else {
625 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100626 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000627 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100628 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
629 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000630 printf(FMT("%-19s ","%s "), buf);
631 }
632
633 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
634 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
635 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200636 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000637 else {
638 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100639 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000640 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100641 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
642 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200643 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000644 }
645
646 if (format & FMT_NOTABLE)
647 fputs(" ", stdout);
648
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200649#ifdef IP6T_F_GOTO
650 if(fw->ipv6.flags & IP6T_F_GOTO)
651 printf("[goto] ");
652#endif
653
Rusty Russell5eed48a2000-06-02 20:12:24 +0000654 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
655
656 if (target) {
657 if (target->print)
658 /* Print the target information. */
659 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
660 } else if (t->u.target_size != sizeof(*t))
661 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000662 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000663
664 if (!(format & FMT_NONEWLINE))
665 fputc('\n', stdout);
666}
667
668static void
669print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100670 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000671{
672 struct ip6t_entry_target *t;
673
674 t = ip6t_get_target((struct ip6t_entry *)fw);
675 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
676}
677
678static int
679append_entry(const ip6t_chainlabel chain,
680 struct ip6t_entry *fw,
681 unsigned int nsaddrs,
682 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100683 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000684 unsigned int ndaddrs,
685 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100686 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000687 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100688 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689{
690 unsigned int i, j;
691 int ret = 1;
692
693 for (i = 0; i < nsaddrs; i++) {
694 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100695 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696 for (j = 0; j < ndaddrs; j++) {
697 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100698 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000699 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100700 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701 ret &= ip6tc_append_entry(chain, fw, handle);
702 }
703 }
704
705 return ret;
706}
707
708static int
709replace_entry(const ip6t_chainlabel chain,
710 struct ip6t_entry *fw,
711 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100712 const struct in6_addr *saddr, const struct in6_addr *smask,
713 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100715 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716{
717 fw->ipv6.src = *saddr;
718 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100719 fw->ipv6.smsk = *smask;
720 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000721
722 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100723 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724 return ip6tc_replace_entry(chain, fw, rulenum, handle);
725}
726
727static int
728insert_entry(const ip6t_chainlabel chain,
729 struct ip6t_entry *fw,
730 unsigned int rulenum,
731 unsigned int nsaddrs,
732 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100733 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000734 unsigned int ndaddrs,
735 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100736 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000737 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100738 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739{
740 unsigned int i, j;
741 int ret = 1;
742
743 for (i = 0; i < nsaddrs; i++) {
744 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100745 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746 for (j = 0; j < ndaddrs; j++) {
747 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100748 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100750 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
752 }
753 }
754
755 return ret;
756}
757
758static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100759make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100760 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761{
762 /* Establish mask for comparison */
763 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100764 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765 unsigned char *mask, *mptr;
766
767 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000768 for (matchp = matches; matchp; matchp = matchp->next)
769 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000770
Jan Engelhardt630ef482009-01-27 14:58:41 +0100771 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000772 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100773 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774
775 memset(mask, 0xFF, sizeof(struct ip6t_entry));
776 mptr = mask + sizeof(struct ip6t_entry);
777
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000778 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000779 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000780 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000781 + matchp->match->userspacesize);
782 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 }
784
Max Kellermann5b76f682008-01-29 13:42:48 +0000785 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000786 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100787 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000788
789 return mask;
790}
791
792static int
793delete_entry(const ip6t_chainlabel chain,
794 struct ip6t_entry *fw,
795 unsigned int nsaddrs,
796 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100797 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798 unsigned int ndaddrs,
799 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100800 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000801 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100802 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100803 struct xtables_rule_match *matches,
804 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805{
806 unsigned int i, j;
807 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808 unsigned char *mask;
809
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100810 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000812 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100813 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000815 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100816 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000817 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100818 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000819 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 }
821 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000822 free(mask);
823
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 return ret;
825}
826
András Kis-Szabó764316a2001-02-26 17:31:20 +0000827int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100828for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
829 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830{
Max Kellermann5b76f682008-01-29 13:42:48 +0000831 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000832 const char *chain;
833 char *chains;
834 unsigned int i, chaincount = 0;
835
836 chain = ip6tc_first_chain(handle);
837 while (chain) {
838 chaincount++;
839 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000840 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841
Jan Engelhardt630ef482009-01-27 14:58:41 +0100842 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000843 i = 0;
844 chain = ip6tc_first_chain(handle);
845 while (chain) {
846 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
847 i++;
848 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000849 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850
851 for (i = 0; i < chaincount; i++) {
852 if (!builtinstoo
853 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100854 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000856 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000857 }
858
859 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000860 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000861}
862
András Kis-Szabó764316a2001-02-26 17:31:20 +0000863int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100865 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000866{
867 if (!chain)
868 return for_each_chain(flush_entries, verbose, 1, handle);
869
870 if (verbose)
871 fprintf(stdout, "Flushing chain `%s'\n", chain);
872 return ip6tc_flush_entries(chain, handle);
873}
874
875static int
876zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100877 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878{
879 if (!chain)
880 return for_each_chain(zero_entries, verbose, 1, handle);
881
882 if (verbose)
883 fprintf(stdout, "Zeroing chain `%s'\n", chain);
884 return ip6tc_zero_entries(chain, handle);
885}
886
András Kis-Szabó764316a2001-02-26 17:31:20 +0000887int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000888delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100889 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000890{
891 if (!chain)
892 return for_each_chain(delete_chain, verbose, 0, handle);
893
894 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000895 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896 return ip6tc_delete_chain(chain, handle);
897}
898
899static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200900list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100901 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000902{
903 int found = 0;
904 unsigned int format;
905 const char *this;
906
907 format = FMT_OPTIONS;
908 if (!verbose)
909 format |= FMT_NOCOUNTS;
910 else
911 format |= FMT_VIA;
912
913 if (numeric)
914 format |= FMT_NUMERIC;
915
916 if (!expanded)
917 format |= FMT_KILOMEGAGIGA;
918
919 if (linenumbers)
920 format |= FMT_LINENUMBERS;
921
922 for (this = ip6tc_first_chain(handle);
923 this;
924 this = ip6tc_next_chain(handle)) {
925 const struct ip6t_entry *i;
926 unsigned int num;
927
928 if (chain && strcmp(chain, this) != 0)
929 continue;
930
931 if (found) printf("\n");
932
Henrik Nordstrombb340822008-05-13 13:09:23 +0200933 if (!rulenum)
934 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935 i = ip6tc_first_rule(this, handle);
936
937 num = 0;
938 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200939 num++;
940 if (!rulenum || num == rulenum)
941 print_firewall(i,
942 ip6tc_get_target(i, handle),
943 num,
944 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100945 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000946 i = ip6tc_next_rule(i, handle);
947 }
948 found = 1;
949 }
950
951 errno = ENOENT;
952 return found;
953}
954
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200955/* This assumes that mask is contiguous, and byte-bounded. */
956static void
957print_iface(char letter, const char *iface, const unsigned char *mask,
958 int invert)
959{
960 unsigned int i;
961
962 if (mask[0] == 0)
963 return;
964
Jan Engelhardt73866352010-12-18 02:04:59 +0100965 printf("%s -%c", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200966
967 for (i = 0; i < IFNAMSIZ; i++) {
968 if (mask[i] != 0) {
969 if (iface[i] != '\0')
970 printf("%c", iface[i]);
971 } else {
972 /* we can access iface[i-1] here, because
973 * a few lines above we make sure that mask[0] != 0 */
974 if (iface[i-1] != '\0')
975 printf("+");
976 break;
977 }
978 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200979}
980
981/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100982static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200983{
984 if (proto) {
985 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100986 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200987
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100988 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200989 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100990 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200991 invertstr, pent->p_name);
992 return;
993 }
994
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100995 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
996 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100997 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100998 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200999 return;
1000 }
1001
Jan Engelhardt73866352010-12-18 02:04:59 +01001002 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001003 }
1004}
1005
1006static int print_match_save(const struct ip6t_entry_match *e,
1007 const struct ip6t_ip6 *ip)
1008{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001009 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001010 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011
1012 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001013 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001014
1015 /* some matches don't provide a save function */
1016 if (match->save)
1017 match->save(ip, e);
1018 } else {
1019 if (e->u.match_size) {
1020 fprintf(stderr,
1021 "Can't find library for match `%s'\n",
1022 e->u.user.name);
1023 exit(1);
1024 }
1025 }
1026 return 0;
1027}
1028
1029/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001030static void print_ip(const char *prefix, const struct in6_addr *ip,
1031 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001032{
1033 char buf[51];
1034 int l = ipv6_prefix_length(mask);
1035
1036 if (l == 0 && !invert)
1037 return;
1038
Jan Engelhardt73866352010-12-18 02:04:59 +01001039 printf("%s %s %s",
1040 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001041 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001042 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1043
1044 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001045 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001046 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001047 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001048}
1049
1050/* We want this to be readable, so only print out neccessary fields.
1051 * Because that's the kind of world I want to live in. */
1052void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001053 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001054{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001055 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001056 const char *target_name;
1057
1058 /* print counters for iptables-save */
1059 if (counters > 0)
1060 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1061
1062 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001063 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001064
1065 /* Print IP part. */
1066 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1067 e->ipv6.invflags & IP6T_INV_SRCIP);
1068
1069 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1070 e->ipv6.invflags & IP6T_INV_DSTIP);
1071
1072 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1073 e->ipv6.invflags & IP6T_INV_VIA_IN);
1074
1075 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1076 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1077
1078 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1079
1080#if 0
1081 /* not definied in ipv6
1082 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1083 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001084 printf("%s -f",
1085 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001086#endif
1087
1088 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001089 printf("%s -? %d",
1090 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001091 e->ipv6.tos);
1092
1093 /* Print matchinfo part */
1094 if (e->target_offset) {
1095 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1096 }
1097
1098 /* print counters for iptables -R */
1099 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001100 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001101
1102 /* Print target name */
1103 target_name = ip6tc_get_target(e, h);
1104 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001105#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001106 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001107#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001108 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001109#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001110
1111 /* Print targinfo part */
1112 t = ip6t_get_target((struct ip6t_entry *)e);
1113 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001114 struct xtables_target *target =
1115 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001116
1117 if (!target) {
1118 fprintf(stderr, "Can't find library for target `%s'\n",
1119 t->u.user.name);
1120 exit(1);
1121 }
1122
1123 if (target->save)
1124 target->save(&e->ipv6, t);
1125 else {
1126 /* If the target size is greater than ip6t_entry_target
1127 * there is something to be saved, we just don't know
1128 * how to print it */
1129 if (t->u.target_size !=
1130 sizeof(struct ip6t_entry_target)) {
1131 fprintf(stderr, "Target `%s' is missing "
1132 "save function\n",
1133 t->u.user.name);
1134 exit(1);
1135 }
1136 }
1137 }
1138 printf("\n");
1139}
1140
1141static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001142list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001143 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001144{
1145 const char *this = NULL;
1146 int found = 0;
1147
1148 if (counters)
1149 counters = -1; /* iptables -c format */
1150
1151 /* Dump out chain names first,
1152 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001153 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001154 this;
1155 this = ip6tc_next_chain(handle)) {
1156 if (chain && strcmp(this, chain) != 0)
1157 continue;
1158
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001159 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001160 struct ip6t_counters count;
1161 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1162 if (counters)
1163 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1164 printf("\n");
1165 } else {
1166 printf("-N %s\n", this);
1167 }
1168 }
1169
1170 for (this = ip6tc_first_chain(handle);
1171 this;
1172 this = ip6tc_next_chain(handle)) {
1173 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001174 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001175
1176 if (chain && strcmp(this, chain) != 0)
1177 continue;
1178
1179 /* Dump out rules */
1180 e = ip6tc_first_rule(this, handle);
1181 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001182 num++;
1183 if (!rulenum || num == rulenum)
1184 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001185 e = ip6tc_next_rule(e, handle);
1186 }
1187 found = 1;
1188 }
1189
1190 errno = ENOENT;
1191 return found;
1192}
1193
Rusty Russell5eed48a2000-06-02 20:12:24 +00001194static struct ip6t_entry *
1195generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001196 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001197 struct ip6t_entry_target *target)
1198{
1199 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001200 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001201 struct ip6t_entry *e;
1202
1203 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001204 for (matchp = matches; matchp; matchp = matchp->next)
1205 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001206
Jan Engelhardt630ef482009-01-27 14:58:41 +01001207 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001208 *e = *fw;
1209 e->target_offset = size;
1210 e->next_offset = size + target->u.target_size;
1211
1212 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001213 for (matchp = matches; matchp; matchp = matchp->next) {
1214 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1215 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001216 }
1217 memcpy(e->elems + size, target, target->u.target_size);
1218
1219 return e;
1220}
1221
Jan Engelhardt395e4412009-02-10 10:43:08 +01001222static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001223{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001224 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001225
1226 for (matchp = *matches; matchp;) {
1227 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001228 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001229 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001230 matchp->match->m = NULL;
1231 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001232 if (matchp->match == matchp->match->next) {
1233 free(matchp->match);
1234 matchp->match = NULL;
1235 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001236 free(matchp);
1237 matchp = tmp;
1238 }
1239
1240 *matches = NULL;
1241}
1242
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001243static void command_default(struct iptables_command_state *cs)
1244{
1245 struct xtables_rule_match *matchp;
1246 struct xtables_match *m;
1247
1248 if (cs->target == NULL || cs->target->parse == NULL ||
1249 cs->c < cs->target->option_offset ||
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001250 cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
1251 cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
1252 cs->invert, &cs->target->tflags, &cs->fw6,
1253 &cs->target->t);
1254 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001255 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001256
1257 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1258 m = matchp->match;
1259
1260 if (matchp->completed || m->parse == NULL)
1261 continue;
1262 if (cs->c < matchp->match->option_offset ||
1263 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1264 continue;
1265 m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
1266 &m->mflags, &cs->fw6, &m->m);
1267 return;
1268 }
1269
1270 /* Try loading protocol */
1271 m = load_proto(cs);
1272 if (m != NULL) {
1273 size_t size;
1274
1275 cs->proto_used = 1;
1276
1277 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1278
1279 m->m = xtables_calloc(1, size);
1280 m->m->u.match_size = size;
1281 strcpy(m->m->u.user.name, m->name);
1282 m->m->u.user.revision = m->revision;
1283 if (m->init != NULL)
1284 m->init(m->m);
1285
1286 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1287 m->extra_opts, &m->option_offset);
1288
1289 optind--;
1290 return;
1291 }
1292
1293 if (cs->c == '?') {
1294 if (optopt)
1295 xtables_error(PARAMETER_PROBLEM,
1296 "option \"%s\" requires an argument",
1297 cs->argv[optind-1]);
1298 else
1299 xtables_error(PARAMETER_PROBLEM,
1300 "unknown option \"%s\"", cs->argv[optind-1]);
1301 }
1302 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001303}
1304
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001305static void command_jump(struct iptables_command_state *cs)
1306{
1307 size_t size;
1308
1309 set_option(&cs->options, OPT_JUMP, &cs->fw6.ipv6.invflags, cs->invert);
1310 cs->jumpto = parse_target(optarg);
1311 /* TRY_LOAD (may be chain name) */
1312 cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1313
1314 if (cs->target == NULL)
1315 return;
1316
1317 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) + cs->target->size;
1318
1319 cs->target->t = xtables_calloc(1, size);
1320 cs->target->t->u.target_size = size;
1321 strcpy(cs->target->t->u.user.name, cs->jumpto);
1322 cs->target->t->u.user.revision = cs->target->revision;
1323 if (cs->target->init != NULL)
1324 cs->target->init(cs->target->t);
1325 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1326 cs->target->extra_opts,
1327 &cs->target->option_offset);
1328 if (opts == NULL)
1329 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1330}
1331
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001332static void command_match(struct iptables_command_state *cs)
1333{
1334 struct xtables_match *m;
1335 size_t size;
1336
1337 if (cs->invert)
1338 xtables_error(PARAMETER_PROBLEM,
1339 "unexpected ! flag before --match");
1340
1341 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1342 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1343 m->m = xtables_calloc(1, size);
1344 m->m->u.match_size = size;
1345 strcpy(m->m->u.user.name, m->name);
1346 m->m->u.user.revision = m->revision;
1347 if (m->init != NULL)
1348 m->init(m->m);
1349 if (m != m->next)
1350 /* Merge options for non-cloned matches */
1351 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1352 m->extra_opts, &m->option_offset);
1353}
1354
Jan Engelhardtfd187312008-11-10 16:59:27 +01001355int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001357 struct iptables_command_state cs;
1358 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001359 unsigned int nsaddrs = 0, ndaddrs = 0;
1360 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001361 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001363 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 const char *chain = NULL;
1365 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1366 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001367 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001368 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001369 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001370 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001371 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001372 struct xtables_target *t;
Patrick McHardy875441e2007-10-17 08:48:58 +00001373 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001374
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001375 memset(&cs, 0, sizeof(cs));
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001376 cs.jumpto = "";
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001377 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378
Harald Welte43ac1912002-03-03 09:43:07 +00001379 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001380 * a second time */
1381 optind = 0;
1382
Harald Welte43ac1912002-03-03 09:43:07 +00001383 /* clear mflags in case do_command gets called a second time
1384 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001385 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001386 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001387
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001388 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001389 t->tflags = 0;
1390 t->used = 0;
1391 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001392
Rusty Russell5eed48a2000-06-02 20:12:24 +00001393 /* Suppress error messages: we may add new options if we
1394 demand-load a protocol. */
1395 opterr = 0;
1396
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001397 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001398 while ((cs.c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001399 "-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 +00001400 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001401 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001402 /*
1403 * Command selection
1404 */
1405 case 'A':
1406 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001407 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001408 chain = optarg;
1409 break;
1410
1411 case 'D':
1412 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001413 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 chain = optarg;
1415 if (optind < argc && argv[optind][0] != '-'
1416 && argv[optind][0] != '!') {
1417 rulenum = parse_rulenumber(argv[optind++]);
1418 command = CMD_DELETE_NUM;
1419 }
1420 break;
1421
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 case 'R':
1423 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001424 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001425 chain = optarg;
1426 if (optind < argc && argv[optind][0] != '-'
1427 && argv[optind][0] != '!')
1428 rulenum = parse_rulenumber(argv[optind++]);
1429 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001430 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 "-%c requires a rule number",
1432 cmd2char(CMD_REPLACE));
1433 break;
1434
1435 case 'I':
1436 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001437 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001438 chain = optarg;
1439 if (optind < argc && argv[optind][0] != '-'
1440 && argv[optind][0] != '!')
1441 rulenum = parse_rulenumber(argv[optind++]);
1442 else rulenum = 1;
1443 break;
1444
1445 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001446 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001447 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001448 if (optarg) chain = optarg;
1449 else if (optind < argc && argv[optind][0] != '-'
1450 && argv[optind][0] != '!')
1451 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001452 if (optind < argc && argv[optind][0] != '-'
1453 && argv[optind][0] != '!')
1454 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001455 break;
1456
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001457 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001458 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001459 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001460 if (optarg) chain = optarg;
1461 else if (optind < argc && argv[optind][0] != '-'
1462 && argv[optind][0] != '!')
1463 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001464 if (optind < argc && argv[optind][0] != '-'
1465 && argv[optind][0] != '!')
1466 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001467 break;
1468
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469 case 'F':
1470 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001471 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001472 if (optarg) chain = optarg;
1473 else if (optind < argc && argv[optind][0] != '-'
1474 && argv[optind][0] != '!')
1475 chain = argv[optind++];
1476 break;
1477
1478 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001479 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001480 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001481 if (optarg) chain = optarg;
1482 else if (optind < argc && argv[optind][0] != '-'
1483 && argv[optind][0] != '!')
1484 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001485 if (optind < argc && argv[optind][0] != '-'
1486 && argv[optind][0] != '!') {
1487 rulenum = parse_rulenumber(argv[optind++]);
1488 command = CMD_ZERO_NUM;
1489 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001490 break;
1491
1492 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001493 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001494 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001495 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001496 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001497 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001498 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001499 "chain name may not clash "
1500 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001501 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001502 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001503 chain = optarg;
1504 break;
1505
1506 case 'X':
1507 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001508 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 if (optarg) chain = optarg;
1510 else if (optind < argc && argv[optind][0] != '-'
1511 && argv[optind][0] != '!')
1512 chain = argv[optind++];
1513 break;
1514
1515 case 'E':
1516 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001517 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001518 chain = optarg;
1519 if (optind < argc && argv[optind][0] != '-'
1520 && argv[optind][0] != '!')
1521 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001522 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001523 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001524 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001525 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001526 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001527 break;
1528
1529 case 'P':
1530 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001531 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001532 chain = optarg;
1533 if (optind < argc && argv[optind][0] != '-'
1534 && argv[optind][0] != '!')
1535 policy = argv[optind++];
1536 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001537 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 "-%c requires a chain and a policy",
1539 cmd2char(CMD_SET_POLICY));
1540 break;
1541
1542 case 'h':
1543 if (!optarg)
1544 optarg = argv[optind];
1545
Jonas Berlin1b91e592005-04-01 06:58:38 +00001546 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001547 if (!cs.matches && cs.protocol)
1548 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1549 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001550
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001551 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001552
1553 /*
1554 * Option selection
1555 */
1556 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001557 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001558 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001559 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001560
1561 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001562 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1563 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001564
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001565 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001566 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1567 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001568
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001569 if (cs.fw6.ipv6.proto == 0
1570 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001571 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001572 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001573
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001574 if (is_exthdr(cs.fw6.ipv6.proto)
1575 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001576 fprintf(stderr,
1577 "Warning: never matched protocol: %s. "
1578 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001579 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001580 break;
1581
1582 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001583 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001584 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001585 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001586 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587 break;
1588
1589 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001590 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001591 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001592 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001593 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001594 break;
1595
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001596#ifdef IP6T_F_GOTO
1597 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001598 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001599 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001600 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001601 cs.jumpto = parse_target(optarg);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001602 break;
1603#endif
1604
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605 case 'j':
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001606 command_jump(&cs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001607 break;
1608
1609
1610 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001611 if (*optarg == '\0')
1612 xtables_error(PARAMETER_PROBLEM,
1613 "Empty interface is likely to be "
1614 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001615 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001616 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001617 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001618 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001619 cs.fw6.ipv6.iniface,
1620 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001621 break;
1622
1623 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001624 if (*optarg == '\0')
1625 xtables_error(PARAMETER_PROBLEM,
1626 "Empty interface is likely to be "
1627 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001628 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001629 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001630 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001631 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001632 cs.fw6.ipv6.outiface,
1633 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 break;
1635
1636 case 'v':
1637 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001638 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001639 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001640 verbose++;
1641 break;
1642
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001643 case 'm':
1644 command_match(&cs);
1645 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001646
1647 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001648 set_option(&cs.options, OPT_NUMERIC, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001649 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650 break;
1651
1652 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001653 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001654 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001655 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001656 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001657 break;
1658
1659 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001660 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001661 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001662 break;
1663
1664 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001665 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001666 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001667 else
1668 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001669 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001670 exit(0);
1671
1672 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001673 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001674 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001675 break;
1676
Harald Welte43ac1912002-03-03 09:43:07 +00001677 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001678 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001679 break;
1680
1681 case 'c':
1682
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001683 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001684 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001685 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001686 bcnt = strchr(pcnt + 1, ',');
1687 if (bcnt)
1688 bcnt++;
1689 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001690 && argv[optind][0] != '!')
1691 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001692 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001693 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001694 "-%c requires packet and byte counter",
1695 opt2char(OPT_COUNTERS));
1696
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001697 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001698 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001699 "-%c packet counter not numeric",
1700 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001701 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001702
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001703 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001704 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001705 "-%c byte counter not numeric",
1706 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001707 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001708 break;
1709
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 case 1: /* non option */
1711 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001712 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001713 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001714 "multiple consecutive ! not"
1715 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001716 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001717 optarg[0] = '\0';
1718 continue;
1719 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001720 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001721 exit_tryhelp(2);
1722
1723 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001724 command_default(&cs);
1725 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001726 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001727 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001728 }
1729
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001730 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001731 if (matchp->match->final_check != NULL)
1732 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001733
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001734 if (cs.target != NULL && cs.target->final_check != NULL)
1735 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001736
1737 /* Fix me: must put inverse options checking here --MN */
1738
1739 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001740 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001741 "unknown arguments found on commandline");
1742 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001743 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001744 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001745 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001746 "nothing appropriate following !");
1747
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001748 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001749 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001750 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001751 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001752 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001753 }
1754
1755 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001756 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1757 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001758
1759 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001760 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1761 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001762
1763 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001764 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001765 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001766 " source or destination IP addresses");
1767
Rusty Russell5eed48a2000-06-02 20:12:24 +00001768 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001769 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001770 "specify a unique address");
1771
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001772 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001773
Jan Engelhardt5429b412010-09-13 15:45:15 +02001774 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001775 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001776 "chain name `%s' too long (must be under %u chars)",
1777 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001778
Harald Welte43ac1912002-03-03 09:43:07 +00001779 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001780 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001781 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001782
Rusty Russell8beb0492004-12-22 00:37:10 +00001783 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001784 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001785 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001786
Harald Welte43ac1912002-03-03 09:43:07 +00001787 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001788 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001789 "can't initialize ip6tables table `%s': %s",
1790 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001791
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001792 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001793 || command == CMD_DELETE
1794 || command == CMD_INSERT
1795 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001796 if (strcmp(chain, "PREROUTING") == 0
1797 || strcmp(chain, "INPUT") == 0) {
1798 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001799 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001800 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001801 "Can't use -%c with %s\n",
1802 opt2char(OPT_VIANAMEOUT),
1803 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001804 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001805
Harald Welte43ac1912002-03-03 09:43:07 +00001806 if (strcmp(chain, "POSTROUTING") == 0
1807 || strcmp(chain, "OUTPUT") == 0) {
1808 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001809 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001810 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001811 "Can't use -%c with %s\n",
1812 opt2char(OPT_VIANAMEIN),
1813 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001814 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001816 if (cs.target && ip6tc_is_chain(cs.jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001817 fprintf(stderr,
1818 "Warning: using chain %s, not extension\n",
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001819 cs.jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001820
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001821 if (cs.target->t)
1822 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001823
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001824 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825 }
1826
1827 /* If they didn't specify a target, or it's a chain
1828 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001829 if (!cs.target
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001830 && (strlen(cs.jumpto) == 0
1831 || ip6tc_is_chain(cs.jumpto, *handle))) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 size_t size;
1833
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001834 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001835 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001836
1837 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001838 + cs.target->size;
1839 cs.target->t = xtables_calloc(1, size);
1840 cs.target->t->u.target_size = size;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001841 strcpy(cs.target->t->u.user.name, cs.jumpto);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001842 if (cs.target->init != NULL)
1843 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001844 }
1845
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001846 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001847 /* it is no chain, and we can't load a plugin.
1848 * We cannot know if the plugin is corrupt, non
1849 * existant OR if the user just misspelled a
1850 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001851#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001852 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001853 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001854 "goto '%s' is not a chain\n",
1855 cs.jumpto);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001856#endif
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001857 xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001859 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001860 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001861 }
1862 }
1863
1864 switch (command) {
1865 case CMD_APPEND:
1866 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001867 nsaddrs, saddrs, smasks,
1868 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001869 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001870 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001871 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001872 case CMD_DELETE:
1873 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001874 nsaddrs, saddrs, smasks,
1875 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001876 cs.options&OPT_VERBOSE,
1877 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878 break;
1879 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001880 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881 break;
1882 case CMD_REPLACE:
1883 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001884 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001885 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886 break;
1887 case CMD_INSERT:
1888 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001889 nsaddrs, saddrs, smasks,
1890 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001891 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001892 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001894 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001895 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001896 break;
1897 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001898 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001900 case CMD_ZERO_NUM:
1901 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1902 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001903 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001904 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001905 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001906 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001907 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001908 cs.options&OPT_VERBOSE,
1909 cs.options&OPT_NUMERIC,
1910 cs.options&OPT_EXPANDED,
1911 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001912 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001913 if (ret && (command & CMD_ZERO))
1914 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001915 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001916 if (ret && (command & CMD_ZERO_NUM))
1917 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001918 break;
1919 case CMD_LIST_RULES:
1920 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001921 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001922 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001923 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001924 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001925 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001926 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001928 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001929 if (ret && (command & CMD_ZERO_NUM))
1930 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 break;
1932 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001933 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001934 break;
1935 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001936 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001937 break;
1938 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001939 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001940 break;
1941 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001942 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001943 break;
1944 default:
1945 /* We should never reach this... */
1946 exit_tryhelp(2);
1947 }
1948
1949 if (verbose > 1)
1950 dump_entries6(*handle);
1951
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001952 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001953
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001954 if (e != NULL) {
1955 free(e);
1956 e = NULL;
1957 }
1958
Michael Granzow332e4ac2009-04-09 18:24:36 +01001959 free(saddrs);
1960 free(smasks);
1961 free(daddrs);
1962 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05001963 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001964
Rusty Russell5eed48a2000-06-02 20:12:24 +00001965 return ret;
1966}