blob: 96a0fdcf979da5306c55fe84f085dc25aa9d003f [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
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010085#define CMD_CHECK 0x4000U
86#define NUMBER_OF_CMD 16
Rusty Russell5eed48a2000-06-02 20:12:24 +000087static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010088 'Z', 'N', 'X', 'P', 'E', 'S', 'C' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000089
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010090#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
91static const char optflags[]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000092= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010095 {.name = "append", .has_arg = 1, .val = 'A'},
96 {.name = "delete", .has_arg = 1, .val = 'D'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010097 {.name = "check" , .has_arg = 1, .val = 'C'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010098 {.name = "insert", .has_arg = 1, .val = 'I'},
99 {.name = "replace", .has_arg = 1, .val = 'R'},
100 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200101 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100102 {.name = "flush", .has_arg = 2, .val = 'F'},
103 {.name = "zero", .has_arg = 2, .val = 'Z'},
104 {.name = "new-chain", .has_arg = 1, .val = 'N'},
105 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
106 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
107 {.name = "policy", .has_arg = 1, .val = 'P'},
108 {.name = "source", .has_arg = 1, .val = 's'},
109 {.name = "destination", .has_arg = 1, .val = 'd'},
110 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
111 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
112 {.name = "protocol", .has_arg = 1, .val = 'p'},
113 {.name = "in-interface", .has_arg = 1, .val = 'i'},
114 {.name = "jump", .has_arg = 1, .val = 'j'},
115 {.name = "table", .has_arg = 1, .val = 't'},
116 {.name = "match", .has_arg = 1, .val = 'm'},
117 {.name = "numeric", .has_arg = 0, .val = 'n'},
118 {.name = "out-interface", .has_arg = 1, .val = 'o'},
119 {.name = "verbose", .has_arg = 0, .val = 'v'},
120 {.name = "exact", .has_arg = 0, .val = 'x'},
121 {.name = "version", .has_arg = 0, .val = 'V'},
122 {.name = "help", .has_arg = 2, .val = 'h'},
123 {.name = "line-numbers", .has_arg = 0, .val = '0'},
124 {.name = "modprobe", .has_arg = 1, .val = 'M'},
125 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200126 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100127 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000128};
129
Harald Weltea8658ca2003-03-05 07:46:15 +0000130/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
131 * current line of the input file, in order to give a more precise error
132 * message. ip6tables itself doesn't need this, so it is initialized to the
133 * magic number of -1 */
134int line = -1;
135
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100136void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100137struct xtables_globals ip6tables_globals = {
138 .option_offset = 0,
139 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500140 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100141 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100142};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000143
144/* Table of legal combinations of commands and options. If any of the
145 * given commands make an option legal, that option is legal (applies to
146 * CMD_LIST and CMD_ZERO only).
147 * Key:
148 * + compulsory
149 * x illegal
150 * optional
151 */
152
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100153static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000154/* Well, it's better than "Re: Linux vs FreeBSD" */
155{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200156 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
158/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
159/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
160/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
161/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
163/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
164/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700165/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100170/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'},
171/*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000172};
173
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100174static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175{
176/* -n */ 0,
177/* -s */ IP6T_INV_SRCIP,
178/* -d */ IP6T_INV_DSTIP,
179/* -p */ IP6T_INV_PROTO,
180/* -j */ 0,
181/* -v */ 0,
182/* -x */ 0,
183/* -i */ IP6T_INV_VIA_IN,
184/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000185/*--line*/ 0,
186/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000187};
188
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100189#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500190#define prog_name ip6tables_globals.program_name
191#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000192/* A few hardcoded protocols for 'all' and in case the user has no
193 /etc/protocols */
194struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100195 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100196 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000197};
198
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200199static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000200exit_tryhelp(int status)
201{
Harald Weltea8658ca2003-03-05 07:46:15 +0000202 if (line != -1)
203 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000204 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500205 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500206 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000207 exit(status);
208}
209
Patrick McHardy0b639362007-09-08 16:00:01 +0000210static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100211exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000212{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000213 printf("%s v%s\n\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100214"Usage: %s -[ACD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100215" %s -I chain [rulenum] rule-specification [options]\n"
216" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000217" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200218" %s -[LS] [chain [rulenum]] [options]\n"
219" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000220" %s -[NX] chain\n"
221" %s -E old-chain-name new-chain-name\n"
222" %s -P chain target [options]\n"
223" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500224 prog_name, prog_vers, prog_name, prog_name,
225 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100226 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227
228 printf(
229"Commands:\n"
230"Either long or short options are allowed.\n"
231" --append -A chain Append to chain\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100232" --check -C chain Check for the existence of a rule\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000233" --delete -D chain Delete matching rule from chain\n"
234" --delete -D chain rulenum\n"
235" Delete rule rulenum (1 = first) from chain\n"
236" --insert -I chain [rulenum]\n"
237" Insert in chain as rulenum (default 1=first)\n"
238" --replace -R chain rulenum\n"
239" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200240" --list -L [chain [rulenum]]\n"
241" List the rules in a chain or all chains\n"
242" --list-rules -S [chain [rulenum]]\n"
243" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000244" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700245" --zero -Z [chain [rulenum]]\n"
246" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000247" --new -N chain Create a new user-defined chain\n"
248" --delete-chain\n"
249" -X [chain] Delete a user-defined chain\n"
250" --policy -P chain target\n"
251" Change policy on chain to target\n"
252" --rename-chain\n"
253" -E old-chain new-chain\n"
254" Change chain name, (moving any references)\n"
255
256"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200257"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100258"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000259" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100260"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000261" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200262"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000263" network interface name ([+] for wildcard)\n"
264" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000265" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200266#ifdef IP6T_F_GOTO
267" --goto -g chain\n"
268" jump to chain with no return\n"
269#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000270" --match -m match\n"
271" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200273"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274" network interface name ([+] for wildcard)\n"
275" --table -t table table to manipulate (default: `filter')\n"
276" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000277" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000279/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000280" --modprobe=<command> try to insert modules using this command\n"
281" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282"[!] --version -V print package version.\n");
283
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200284 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285 exit(0);
286}
287
Patrick McHardy0b639362007-09-08 16:00:01 +0000288void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100289ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000290{
291 va_list args;
292
293 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500294 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000295 vfprintf(stderr, msg, args);
296 va_end(args);
297 fprintf(stderr, "\n");
298 if (status == PARAMETER_PROBLEM)
299 exit_tryhelp(status);
300 if (status == VERSION_PROBLEM)
301 fprintf(stderr,
302 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
303 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500304 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000305 exit(status);
306}
307
Rusty Russell5eed48a2000-06-02 20:12:24 +0000308static void
309generic_opt_check(int command, int options)
310{
311 int i, j, legal = 0;
312
313 /* Check that commands are valid with options. Complicated by the
314 * fact that if an option is legal with *any* command given, it is
315 * legal overall (ie. -z and -l).
316 */
317 for (i = 0; i < NUMBER_OF_OPT; i++) {
318 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
319
320 for (j = 0; j < NUMBER_OF_CMD; j++) {
321 if (!(command & (1<<j)))
322 continue;
323
324 if (!(options & (1<<i))) {
325 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100326 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327 "You need to supply the `-%c' "
328 "option for this command\n",
329 optflags[i]);
330 } else {
331 if (commands_v_options[j][i] != 'x')
332 legal = 1;
333 else if (legal == 0)
334 legal = -1;
335 }
336 }
337 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100338 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000339 "Illegal option `-%c' with this command\n",
340 optflags[i]);
341 }
342}
343
344static char
345opt2char(int option)
346{
347 const char *ptr;
348 for (ptr = optflags; option > 1; option >>= 1, ptr++);
349
350 return *ptr;
351}
352
353static char
354cmd2char(int option)
355{
356 const char *ptr;
357 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
358
359 return *ptr;
360}
361
362static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000363add_command(unsigned int *cmd, const int newcmd, const int othercmds,
364 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000365{
366 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100367 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000368 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100369 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000370 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
371 *cmd |= newcmd;
372}
373
Rusty Russell5eed48a2000-06-02 20:12:24 +0000374/*
375 * All functions starting with "parse" should succeed, otherwise
376 * the program fails.
377 * Most routines return pointers to static data that may change
378 * between calls to the same or other routines with a few exceptions:
379 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
380 * return global static data.
381*/
382
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000383/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100384static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000385{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000386 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000387 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000388 proto == IPPROTO_AH ||
389 proto == IPPROTO_DSTOPTS);
390}
391
Rusty Russell5eed48a2000-06-02 20:12:24 +0000392/* Can't be zero. */
393static int
394parse_rulenumber(const char *rule)
395{
Harald Welte43ac1912002-03-03 09:43:07 +0000396 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000397
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100398 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100399 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000400 "Invalid rule number `%s'", rule);
401
402 return rulenum;
403}
404
405static const char *
406parse_target(const char *targetname)
407{
408 const char *ptr;
409
410 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100411 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000412 "Invalid target name (too short)");
413
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200414 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100415 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000416 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200417 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000418
419 for (ptr = targetname; *ptr; ptr++)
420 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100421 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000422 "Invalid target name `%s'", targetname);
423 return targetname;
424}
425
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100427set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000428 int invert)
429{
430 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100431 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000432 opt2char(option));
433 *options |= option;
434
435 if (invert) {
436 unsigned int i;
437 for (i = 0; 1 << i != option; i++);
438
439 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100440 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441 "cannot have ! before -%c",
442 opt2char(option));
443 *invflg |= inverse_for_options[i];
444 }
445}
446
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000447static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100448print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000449{
Harald Welte43ac1912002-03-03 09:43:07 +0000450 if (format & FMT_KILOMEGAGIGA) {
451 if (number > 99999) {
452 number = (number + 500) / 1000;
453 if (number > 9999) {
454 number = (number + 500) / 1000;
455 if (number > 9999) {
456 number = (number + 500) / 1000;
457 if (number > 9999) {
458 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000459 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000460 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000461 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000462 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000463 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000464 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000465 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000466 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000467 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000468 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000469 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470}
471
Harald Welte43ac1912002-03-03 09:43:07 +0000472
Rusty Russell5eed48a2000-06-02 20:12:24 +0000473static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100474print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000475{
476 struct ip6t_counters counters;
477 const char *pol = ip6tc_get_policy(chain, &counters, handle);
478 printf("Chain %s", chain);
479 if (pol) {
480 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000481 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000482 fputc(' ', stdout);
483 print_num(counters.pcnt, (format|FMT_NOTABLE));
484 fputs("packets, ", stdout);
485 print_num(counters.bcnt, (format|FMT_NOTABLE));
486 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000487 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488 printf(")\n");
489 } else {
490 unsigned int refs;
491 if (!ip6tc_get_references(&refs, chain, handle))
492 printf(" (ERROR obtaining refs)\n");
493 else
494 printf(" (%u references)\n", refs);
495 }
496
497 if (format & FMT_LINENUMBERS)
498 printf(FMT("%-4s ", "%s "), "num");
499 if (!(format & FMT_NOCOUNTS)) {
500 if (format & FMT_KILOMEGAGIGA) {
501 printf(FMT("%5s ","%s "), "pkts");
502 printf(FMT("%5s ","%s "), "bytes");
503 } else {
504 printf(FMT("%8s ","%s "), "pkts");
505 printf(FMT("%10s ","%s "), "bytes");
506 }
507 }
508 if (!(format & FMT_NOTARGET))
509 printf(FMT("%-9s ","%s "), "target");
510 fputs(" prot ", stdout);
511 if (format & FMT_OPTIONS)
512 fputs("opt", stdout);
513 if (format & FMT_VIA) {
514 printf(FMT(" %-6s ","%s "), "in");
515 printf(FMT("%-6s ","%s "), "out");
516 }
517 printf(FMT(" %-19s ","%s "), "source");
518 printf(FMT(" %-19s "," %s "), "destination");
519 printf("\n");
520}
521
Harald Welte43ac1912002-03-03 09:43:07 +0000522
Rusty Russell5eed48a2000-06-02 20:12:24 +0000523static int
524print_match(const struct ip6t_entry_match *m,
525 const struct ip6t_ip6 *ip,
526 int numeric)
527{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100528 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100529 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000530
531 if (match) {
532 if (match->print)
533 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000534 else
535 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000536 } else {
537 if (m->u.user.name[0])
538 printf("UNKNOWN match `%s' ", m->u.user.name);
539 }
540 /* Don't stop iterating. */
541 return 0;
542}
543
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100544/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000545static void
546print_firewall(const struct ip6t_entry *fw,
547 const char *targname,
548 unsigned int num,
549 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100550 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100552 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100554 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000555 char buf[BUFSIZ];
556
Rusty Russell5eed48a2000-06-02 20:12:24 +0000557 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100558 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000559 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100560 target = xtables_find_target(IP6T_STANDARD_TARGET,
561 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000562
563 t = ip6t_get_target((struct ip6t_entry *)fw);
564 flags = fw->ipv6.flags;
565
566 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200567 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568
569 if (!(format & FMT_NOCOUNTS)) {
570 print_num(fw->counters.pcnt, format);
571 print_num(fw->counters.bcnt, format);
572 }
573
574 if (!(format & FMT_NOTARGET))
575 printf(FMT("%-9s ", "%s "), targname);
576
577 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
578 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100579 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000580 if (pname)
581 printf(FMT("%-5s", "%s "), pname);
582 else
583 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
584 }
585
586 if (format & FMT_OPTIONS) {
587 if (format & FMT_NOTABLE)
588 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000589 fputc(' ', stdout); /* Invert flag of FRAG */
590 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000591 fputc(' ', stdout);
592 }
593
594 if (format & FMT_VIA) {
595 char iface[IFNAMSIZ+2];
596
597 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
598 iface[0] = '!';
599 iface[1] = '\0';
600 }
601 else iface[0] = '\0';
602
603 if (fw->ipv6.iniface[0] != '\0') {
604 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000605 }
606 else if (format & FMT_NUMERIC) strcat(iface, "*");
607 else strcat(iface, "any");
608 printf(FMT(" %-6s ","in %s "), iface);
609
610 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
611 iface[0] = '!';
612 iface[1] = '\0';
613 }
614 else iface[0] = '\0';
615
616 if (fw->ipv6.outiface[0] != '\0') {
617 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000618 }
619 else if (format & FMT_NUMERIC) strcat(iface, "*");
620 else strcat(iface, "any");
621 printf(FMT("%-6s ","out %s "), iface);
622 }
623
624 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000625 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000626 && !(format & FMT_NUMERIC))
627 printf(FMT("%-19s ","%s "), "anywhere");
628 else {
629 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100630 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000631 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100632 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
633 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000634 printf(FMT("%-19s ","%s "), buf);
635 }
636
637 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
638 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
639 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200640 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000641 else {
642 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100643 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000644 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100645 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
646 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200647 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000648 }
649
650 if (format & FMT_NOTABLE)
651 fputs(" ", stdout);
652
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200653#ifdef IP6T_F_GOTO
654 if(fw->ipv6.flags & IP6T_F_GOTO)
655 printf("[goto] ");
656#endif
657
Rusty Russell5eed48a2000-06-02 20:12:24 +0000658 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
659
660 if (target) {
661 if (target->print)
662 /* Print the target information. */
663 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
664 } else if (t->u.target_size != sizeof(*t))
665 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000666 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000667
668 if (!(format & FMT_NONEWLINE))
669 fputc('\n', stdout);
670}
671
672static void
673print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100674 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000675{
676 struct ip6t_entry_target *t;
677
678 t = ip6t_get_target((struct ip6t_entry *)fw);
679 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
680}
681
682static int
683append_entry(const ip6t_chainlabel chain,
684 struct ip6t_entry *fw,
685 unsigned int nsaddrs,
686 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100687 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000688 unsigned int ndaddrs,
689 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100690 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000691 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100692 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000693{
694 unsigned int i, j;
695 int ret = 1;
696
697 for (i = 0; i < nsaddrs; i++) {
698 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100699 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700 for (j = 0; j < ndaddrs; j++) {
701 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100702 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100704 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000705 ret &= ip6tc_append_entry(chain, fw, handle);
706 }
707 }
708
709 return ret;
710}
711
712static int
713replace_entry(const ip6t_chainlabel chain,
714 struct ip6t_entry *fw,
715 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100716 const struct in6_addr *saddr, const struct in6_addr *smask,
717 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000718 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100719 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720{
721 fw->ipv6.src = *saddr;
722 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100723 fw->ipv6.smsk = *smask;
724 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000725
726 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100727 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 return ip6tc_replace_entry(chain, fw, rulenum, handle);
729}
730
731static int
732insert_entry(const ip6t_chainlabel chain,
733 struct ip6t_entry *fw,
734 unsigned int rulenum,
735 unsigned int nsaddrs,
736 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100737 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738 unsigned int ndaddrs,
739 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100740 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000741 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100742 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743{
744 unsigned int i, j;
745 int ret = 1;
746
747 for (i = 0; i < nsaddrs; i++) {
748 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100749 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000750 for (j = 0; j < ndaddrs; j++) {
751 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100752 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000753 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100754 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000755 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
756 }
757 }
758
759 return ret;
760}
761
762static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100763make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100764 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765{
766 /* Establish mask for comparison */
767 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100768 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769 unsigned char *mask, *mptr;
770
771 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000772 for (matchp = matches; matchp; matchp = matchp->next)
773 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774
Jan Engelhardt630ef482009-01-27 14:58:41 +0100775 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000776 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100777 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778
779 memset(mask, 0xFF, sizeof(struct ip6t_entry));
780 mptr = mask + sizeof(struct ip6t_entry);
781
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000782 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000784 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000785 + matchp->match->userspacesize);
786 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000787 }
788
Max Kellermann5b76f682008-01-29 13:42:48 +0000789 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000790 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100791 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792
793 return mask;
794}
795
796static int
797delete_entry(const ip6t_chainlabel chain,
798 struct ip6t_entry *fw,
799 unsigned int nsaddrs,
800 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100801 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000802 unsigned int ndaddrs,
803 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100804 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100806 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100807 struct xtables_rule_match *matches,
808 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809{
810 unsigned int i, j;
811 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000812 unsigned char *mask;
813
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100814 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000815 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000816 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100817 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000819 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100820 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000821 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100822 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000823 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 }
825 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000826 free(mask);
827
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828 return ret;
829}
830
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100831static int
832check_entry(const ip6t_chainlabel chain, struct ip6t_entry *fw,
833 unsigned int nsaddrs, const struct in6_addr *saddrs,
834 const struct in6_addr *smasks, unsigned int ndaddrs,
835 const struct in6_addr *daddrs, const struct in6_addr *dmasks,
836 bool verbose, struct ip6tc_handle *handle,
837 struct xtables_rule_match *matches,
838 const struct xtables_target *target)
839{
840 unsigned int i, j;
841 int ret = 1;
842 unsigned char *mask;
843
844 mask = make_delete_mask(matches, target);
845 for (i = 0; i < nsaddrs; i++) {
846 fw->ipv6.src = saddrs[i];
847 fw->ipv6.smsk = smasks[i];
848 for (j = 0; j < ndaddrs; j++) {
849 fw->ipv6.dst = daddrs[j];
850 fw->ipv6.dmsk = dmasks[j];
851 if (verbose)
852 print_firewall_line(fw, handle);
853 ret &= ip6tc_check_entry(chain, fw, mask, handle);
854 }
855 }
856
857 free(mask);
858 return ret;
859}
860
András Kis-Szabó764316a2001-02-26 17:31:20 +0000861int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100862for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
863 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864{
Max Kellermann5b76f682008-01-29 13:42:48 +0000865 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000866 const char *chain;
867 char *chains;
868 unsigned int i, chaincount = 0;
869
870 chain = ip6tc_first_chain(handle);
871 while (chain) {
872 chaincount++;
873 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000874 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875
Jan Engelhardt630ef482009-01-27 14:58:41 +0100876 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000877 i = 0;
878 chain = ip6tc_first_chain(handle);
879 while (chain) {
880 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
881 i++;
882 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000883 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000884
885 for (i = 0; i < chaincount; i++) {
886 if (!builtinstoo
887 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100888 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000890 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000891 }
892
893 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000894 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000895}
896
András Kis-Szabó764316a2001-02-26 17:31:20 +0000897int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100899 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000900{
901 if (!chain)
902 return for_each_chain(flush_entries, verbose, 1, handle);
903
904 if (verbose)
905 fprintf(stdout, "Flushing chain `%s'\n", chain);
906 return ip6tc_flush_entries(chain, handle);
907}
908
909static int
910zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100911 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000912{
913 if (!chain)
914 return for_each_chain(zero_entries, verbose, 1, handle);
915
916 if (verbose)
917 fprintf(stdout, "Zeroing chain `%s'\n", chain);
918 return ip6tc_zero_entries(chain, handle);
919}
920
András Kis-Szabó764316a2001-02-26 17:31:20 +0000921int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000922delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100923 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000924{
925 if (!chain)
926 return for_each_chain(delete_chain, verbose, 0, handle);
927
928 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000929 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000930 return ip6tc_delete_chain(chain, handle);
931}
932
933static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200934list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100935 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936{
937 int found = 0;
938 unsigned int format;
939 const char *this;
940
941 format = FMT_OPTIONS;
942 if (!verbose)
943 format |= FMT_NOCOUNTS;
944 else
945 format |= FMT_VIA;
946
947 if (numeric)
948 format |= FMT_NUMERIC;
949
950 if (!expanded)
951 format |= FMT_KILOMEGAGIGA;
952
953 if (linenumbers)
954 format |= FMT_LINENUMBERS;
955
956 for (this = ip6tc_first_chain(handle);
957 this;
958 this = ip6tc_next_chain(handle)) {
959 const struct ip6t_entry *i;
960 unsigned int num;
961
962 if (chain && strcmp(chain, this) != 0)
963 continue;
964
965 if (found) printf("\n");
966
Henrik Nordstrombb340822008-05-13 13:09:23 +0200967 if (!rulenum)
968 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000969 i = ip6tc_first_rule(this, handle);
970
971 num = 0;
972 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200973 num++;
974 if (!rulenum || num == rulenum)
975 print_firewall(i,
976 ip6tc_get_target(i, handle),
977 num,
978 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100979 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 i = ip6tc_next_rule(i, handle);
981 }
982 found = 1;
983 }
984
985 errno = ENOENT;
986 return found;
987}
988
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200989/* This assumes that mask is contiguous, and byte-bounded. */
990static void
991print_iface(char letter, const char *iface, const unsigned char *mask,
992 int invert)
993{
994 unsigned int i;
995
996 if (mask[0] == 0)
997 return;
998
Jan Engelhardtaa66aed2011-02-16 02:41:22 +0100999 printf("%s -%c ", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001000
1001 for (i = 0; i < IFNAMSIZ; i++) {
1002 if (mask[i] != 0) {
1003 if (iface[i] != '\0')
1004 printf("%c", iface[i]);
1005 } else {
1006 /* we can access iface[i-1] here, because
1007 * a few lines above we make sure that mask[0] != 0 */
1008 if (iface[i-1] != '\0')
1009 printf("+");
1010 break;
1011 }
1012 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001013}
1014
1015/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001016static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001017{
1018 if (proto) {
1019 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +01001020 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001021
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001022 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001023 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001024 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001025 invertstr, pent->p_name);
1026 return;
1027 }
1028
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001029 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1030 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001031 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001032 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001033 return;
1034 }
1035
Jan Engelhardt73866352010-12-18 02:04:59 +01001036 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001037 }
1038}
1039
1040static int print_match_save(const struct ip6t_entry_match *e,
1041 const struct ip6t_ip6 *ip)
1042{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001043 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001044 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001045
1046 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001047 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001048
1049 /* some matches don't provide a save function */
1050 if (match->save)
1051 match->save(ip, e);
1052 } else {
1053 if (e->u.match_size) {
1054 fprintf(stderr,
1055 "Can't find library for match `%s'\n",
1056 e->u.user.name);
1057 exit(1);
1058 }
1059 }
1060 return 0;
1061}
1062
1063/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001064static void print_ip(const char *prefix, const struct in6_addr *ip,
1065 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001066{
1067 char buf[51];
1068 int l = ipv6_prefix_length(mask);
1069
1070 if (l == 0 && !invert)
1071 return;
1072
Jan Engelhardt73866352010-12-18 02:04:59 +01001073 printf("%s %s %s",
1074 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001075 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001076 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1077
1078 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001079 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001080 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001081 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001082}
1083
1084/* We want this to be readable, so only print out neccessary fields.
1085 * Because that's the kind of world I want to live in. */
1086void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001087 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001088{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001089 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001090 const char *target_name;
1091
1092 /* print counters for iptables-save */
1093 if (counters > 0)
1094 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1095
1096 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001097 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001098
1099 /* Print IP part. */
1100 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1101 e->ipv6.invflags & IP6T_INV_SRCIP);
1102
1103 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1104 e->ipv6.invflags & IP6T_INV_DSTIP);
1105
1106 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1107 e->ipv6.invflags & IP6T_INV_VIA_IN);
1108
1109 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1110 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1111
1112 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1113
1114#if 0
1115 /* not definied in ipv6
1116 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1117 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001118 printf("%s -f",
1119 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001120#endif
1121
1122 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001123 printf("%s -? %d",
1124 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001125 e->ipv6.tos);
1126
1127 /* Print matchinfo part */
1128 if (e->target_offset) {
1129 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1130 }
1131
1132 /* print counters for iptables -R */
1133 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001134 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001135
1136 /* Print target name */
1137 target_name = ip6tc_get_target(e, h);
1138 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001139#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001140 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001141#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001142 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001143#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001144
1145 /* Print targinfo part */
1146 t = ip6t_get_target((struct ip6t_entry *)e);
1147 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001148 struct xtables_target *target =
1149 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001150
1151 if (!target) {
1152 fprintf(stderr, "Can't find library for target `%s'\n",
1153 t->u.user.name);
1154 exit(1);
1155 }
1156
1157 if (target->save)
1158 target->save(&e->ipv6, t);
1159 else {
1160 /* If the target size is greater than ip6t_entry_target
1161 * there is something to be saved, we just don't know
1162 * how to print it */
1163 if (t->u.target_size !=
1164 sizeof(struct ip6t_entry_target)) {
1165 fprintf(stderr, "Target `%s' is missing "
1166 "save function\n",
1167 t->u.user.name);
1168 exit(1);
1169 }
1170 }
1171 }
1172 printf("\n");
1173}
1174
1175static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001176list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001177 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001178{
1179 const char *this = NULL;
1180 int found = 0;
1181
1182 if (counters)
1183 counters = -1; /* iptables -c format */
1184
1185 /* Dump out chain names first,
1186 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001187 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001188 this;
1189 this = ip6tc_next_chain(handle)) {
1190 if (chain && strcmp(this, chain) != 0)
1191 continue;
1192
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001193 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001194 struct ip6t_counters count;
1195 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1196 if (counters)
1197 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1198 printf("\n");
1199 } else {
1200 printf("-N %s\n", this);
1201 }
1202 }
1203
1204 for (this = ip6tc_first_chain(handle);
1205 this;
1206 this = ip6tc_next_chain(handle)) {
1207 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001208 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001209
1210 if (chain && strcmp(this, chain) != 0)
1211 continue;
1212
1213 /* Dump out rules */
1214 e = ip6tc_first_rule(this, handle);
1215 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001216 num++;
1217 if (!rulenum || num == rulenum)
1218 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001219 e = ip6tc_next_rule(e, handle);
1220 }
1221 found = 1;
1222 }
1223
1224 errno = ENOENT;
1225 return found;
1226}
1227
Rusty Russell5eed48a2000-06-02 20:12:24 +00001228static struct ip6t_entry *
1229generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001230 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001231 struct ip6t_entry_target *target)
1232{
1233 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001234 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001235 struct ip6t_entry *e;
1236
1237 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001238 for (matchp = matches; matchp; matchp = matchp->next)
1239 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240
Jan Engelhardt630ef482009-01-27 14:58:41 +01001241 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242 *e = *fw;
1243 e->target_offset = size;
1244 e->next_offset = size + target->u.target_size;
1245
1246 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001247 for (matchp = matches; matchp; matchp = matchp->next) {
1248 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1249 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001250 }
1251 memcpy(e->elems + size, target, target->u.target_size);
1252
1253 return e;
1254}
1255
Jan Engelhardt395e4412009-02-10 10:43:08 +01001256static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001257{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001258 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001259
1260 for (matchp = *matches; matchp;) {
1261 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001262 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001263 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001264 matchp->match->m = NULL;
1265 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001266 if (matchp->match == matchp->match->next) {
1267 free(matchp->match);
1268 matchp->match = NULL;
1269 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001270 free(matchp);
1271 matchp = tmp;
1272 }
1273
1274 *matches = NULL;
1275}
1276
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001277static void command_default(struct iptables_command_state *cs)
1278{
1279 struct xtables_rule_match *matchp;
1280 struct xtables_match *m;
1281
Jan Engelhardtaf3d73e2011-02-11 01:45:26 +01001282 if (cs->target != NULL && cs->target->parse != NULL &&
1283 cs->c >= cs->target->option_offset &&
1284 cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001285 cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
1286 cs->invert, &cs->target->tflags, &cs->fw6,
1287 &cs->target->t);
1288 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001289 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001290
1291 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1292 m = matchp->match;
1293
1294 if (matchp->completed || m->parse == NULL)
1295 continue;
1296 if (cs->c < matchp->match->option_offset ||
1297 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1298 continue;
1299 m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
1300 &m->mflags, &cs->fw6, &m->m);
1301 return;
1302 }
1303
1304 /* Try loading protocol */
1305 m = load_proto(cs);
1306 if (m != NULL) {
1307 size_t size;
1308
1309 cs->proto_used = 1;
1310
1311 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1312
1313 m->m = xtables_calloc(1, size);
1314 m->m->u.match_size = size;
1315 strcpy(m->m->u.user.name, m->name);
1316 m->m->u.user.revision = m->revision;
1317 if (m->init != NULL)
1318 m->init(m->m);
1319
1320 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1321 m->extra_opts, &m->option_offset);
1322
1323 optind--;
1324 return;
1325 }
1326
Jan Engelhardt58b491f2011-02-07 03:45:26 +01001327 if (cs->c == ':')
1328 xtables_error(PARAMETER_PROBLEM, "option \"%s\" "
1329 "requires an argument", cs->argv[optind-1]);
1330 if (cs->c == '?')
1331 xtables_error(PARAMETER_PROBLEM, "unknown option "
1332 "\"%s\"", cs->argv[optind-1]);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001333 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001334}
1335
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001336static void command_jump(struct iptables_command_state *cs)
1337{
1338 size_t size;
1339
1340 set_option(&cs->options, OPT_JUMP, &cs->fw6.ipv6.invflags, cs->invert);
1341 cs->jumpto = parse_target(optarg);
1342 /* TRY_LOAD (may be chain name) */
1343 cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1344
1345 if (cs->target == NULL)
1346 return;
1347
1348 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) + cs->target->size;
1349
1350 cs->target->t = xtables_calloc(1, size);
1351 cs->target->t->u.target_size = size;
1352 strcpy(cs->target->t->u.user.name, cs->jumpto);
1353 cs->target->t->u.user.revision = cs->target->revision;
1354 if (cs->target->init != NULL)
1355 cs->target->init(cs->target->t);
1356 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1357 cs->target->extra_opts,
1358 &cs->target->option_offset);
1359 if (opts == NULL)
1360 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1361}
1362
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001363static void command_match(struct iptables_command_state *cs)
1364{
1365 struct xtables_match *m;
1366 size_t size;
1367
1368 if (cs->invert)
1369 xtables_error(PARAMETER_PROBLEM,
1370 "unexpected ! flag before --match");
1371
1372 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1373 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1374 m->m = xtables_calloc(1, size);
1375 m->m->u.match_size = size;
1376 strcpy(m->m->u.user.name, m->name);
1377 m->m->u.user.revision = m->revision;
1378 if (m->init != NULL)
1379 m->init(m->m);
1380 if (m != m->next)
1381 /* Merge options for non-cloned matches */
1382 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1383 m->extra_opts, &m->option_offset);
1384}
1385
Jan Engelhardtfd187312008-11-10 16:59:27 +01001386int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001388 struct iptables_command_state cs;
1389 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001390 unsigned int nsaddrs = 0, ndaddrs = 0;
1391 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001392 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001393
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001394 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001395 const char *chain = NULL;
1396 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1397 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001398 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001399 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001400 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001401 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001402 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001403 struct xtables_target *t;
Patrick McHardy875441e2007-10-17 08:48:58 +00001404 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001406 memset(&cs, 0, sizeof(cs));
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001407 cs.jumpto = "";
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001408 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001409
Harald Welte43ac1912002-03-03 09:43:07 +00001410 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001411 * a second time */
1412 optind = 0;
1413
Harald Welte43ac1912002-03-03 09:43:07 +00001414 /* clear mflags in case do_command gets called a second time
1415 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001416 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001417 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001418
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001419 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001420 t->tflags = 0;
1421 t->used = 0;
1422 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001423
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424 /* Suppress error messages: we may add new options if we
1425 demand-load a protocol. */
1426 opterr = 0;
1427
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001428 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001429 while ((cs.c = getopt_long(argc, argv,
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001430 "-:A:C: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 +00001431 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001432 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433 /*
1434 * Command selection
1435 */
1436 case 'A':
1437 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001438 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001439 chain = optarg;
1440 break;
1441
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001442 case 'C':
1443 add_command(&command, CMD_CHECK, CMD_NONE,
1444 cs.invert);
1445 chain = optarg;
1446 break;
1447
Rusty Russell5eed48a2000-06-02 20:12:24 +00001448 case 'D':
1449 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001450 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001451 chain = optarg;
1452 if (optind < argc && argv[optind][0] != '-'
1453 && argv[optind][0] != '!') {
1454 rulenum = parse_rulenumber(argv[optind++]);
1455 command = CMD_DELETE_NUM;
1456 }
1457 break;
1458
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459 case 'R':
1460 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001461 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001462 chain = optarg;
1463 if (optind < argc && argv[optind][0] != '-'
1464 && argv[optind][0] != '!')
1465 rulenum = parse_rulenumber(argv[optind++]);
1466 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001467 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001468 "-%c requires a rule number",
1469 cmd2char(CMD_REPLACE));
1470 break;
1471
1472 case 'I':
1473 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001474 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001475 chain = optarg;
1476 if (optind < argc && argv[optind][0] != '-'
1477 && argv[optind][0] != '!')
1478 rulenum = parse_rulenumber(argv[optind++]);
1479 else rulenum = 1;
1480 break;
1481
1482 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001483 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001484 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001485 if (optarg) chain = optarg;
1486 else if (optind < argc && argv[optind][0] != '-'
1487 && argv[optind][0] != '!')
1488 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001489 if (optind < argc && argv[optind][0] != '-'
1490 && argv[optind][0] != '!')
1491 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001492 break;
1493
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001494 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001495 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001496 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001497 if (optarg) chain = optarg;
1498 else if (optind < argc && argv[optind][0] != '-'
1499 && argv[optind][0] != '!')
1500 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001501 if (optind < argc && argv[optind][0] != '-'
1502 && argv[optind][0] != '!')
1503 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001504 break;
1505
Rusty Russell5eed48a2000-06-02 20:12:24 +00001506 case 'F':
1507 add_command(&command, CMD_FLUSH, 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 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001516 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001517 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001518 if (optarg) chain = optarg;
1519 else if (optind < argc && argv[optind][0] != '-'
1520 && argv[optind][0] != '!')
1521 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001522 if (optind < argc && argv[optind][0] != '-'
1523 && argv[optind][0] != '!') {
1524 rulenum = parse_rulenumber(argv[optind++]);
1525 command = CMD_ZERO_NUM;
1526 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001527 break;
1528
1529 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001530 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001531 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001532 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001533 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001534 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001535 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001536 "chain name may not clash "
1537 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001539 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001540 chain = optarg;
1541 break;
1542
1543 case 'X':
1544 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001545 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546 if (optarg) chain = optarg;
1547 else if (optind < argc && argv[optind][0] != '-'
1548 && argv[optind][0] != '!')
1549 chain = argv[optind++];
1550 break;
1551
1552 case 'E':
1553 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001554 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001555 chain = optarg;
1556 if (optind < argc && argv[optind][0] != '-'
1557 && argv[optind][0] != '!')
1558 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001559 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001560 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001561 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001562 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001563 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001564 break;
1565
1566 case 'P':
1567 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001568 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001569 chain = optarg;
1570 if (optind < argc && argv[optind][0] != '-'
1571 && argv[optind][0] != '!')
1572 policy = argv[optind++];
1573 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001574 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001575 "-%c requires a chain and a policy",
1576 cmd2char(CMD_SET_POLICY));
1577 break;
1578
1579 case 'h':
1580 if (!optarg)
1581 optarg = argv[optind];
1582
Jonas Berlin1b91e592005-04-01 06:58:38 +00001583 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001584 if (!cs.matches && cs.protocol)
1585 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1586 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001588 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001589
1590 /*
1591 * Option selection
1592 */
1593 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001594 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001595 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001596 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001597
1598 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001599 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1600 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001602 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001603 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1604 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001606 if (cs.fw6.ipv6.proto == 0
1607 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001608 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001609 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001610
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001611 if (is_exthdr(cs.fw6.ipv6.proto)
1612 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001613 fprintf(stderr,
1614 "Warning: never matched protocol: %s. "
1615 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001616 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617 break;
1618
1619 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001620 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001621 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001622 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001623 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001624 break;
1625
1626 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001627 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001628 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001629 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001630 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001631 break;
1632
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001633#ifdef IP6T_F_GOTO
1634 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001635 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001636 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001637 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001638 cs.jumpto = parse_target(optarg);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001639 break;
1640#endif
1641
Rusty Russell5eed48a2000-06-02 20:12:24 +00001642 case 'j':
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001643 command_jump(&cs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001644 break;
1645
1646
1647 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001648 if (*optarg == '\0')
1649 xtables_error(PARAMETER_PROBLEM,
1650 "Empty interface is likely to be "
1651 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001652 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001653 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001654 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001655 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001656 cs.fw6.ipv6.iniface,
1657 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 break;
1659
1660 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001661 if (*optarg == '\0')
1662 xtables_error(PARAMETER_PROBLEM,
1663 "Empty interface is likely to be "
1664 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001665 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001666 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001667 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001668 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001669 cs.fw6.ipv6.outiface,
1670 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001671 break;
1672
1673 case 'v':
1674 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001675 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001676 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001677 verbose++;
1678 break;
1679
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001680 case 'm':
1681 command_match(&cs);
1682 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683
1684 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001685 set_option(&cs.options, OPT_NUMERIC, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001686 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001687 break;
1688
1689 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001690 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001691 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001692 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001693 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001694 break;
1695
1696 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001697 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001698 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699 break;
1700
1701 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001702 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001703 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704 else
1705 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001706 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707 exit(0);
1708
1709 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001710 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001711 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712 break;
1713
Harald Welte43ac1912002-03-03 09:43:07 +00001714 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001715 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001716 break;
1717
1718 case 'c':
1719
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001720 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001721 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001722 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001723 bcnt = strchr(pcnt + 1, ',');
1724 if (bcnt)
1725 bcnt++;
1726 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001727 && argv[optind][0] != '!')
1728 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001729 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001730 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001731 "-%c requires packet and byte counter",
1732 opt2char(OPT_COUNTERS));
1733
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001734 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001735 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001736 "-%c packet counter not numeric",
1737 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001738 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001739
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001740 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001741 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001742 "-%c byte counter not numeric",
1743 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001744 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001745 break;
1746
Rusty Russell5eed48a2000-06-02 20:12:24 +00001747 case 1: /* non option */
1748 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001749 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001750 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001751 "multiple consecutive ! not"
1752 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001753 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001754 optarg[0] = '\0';
1755 continue;
1756 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001757 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001758 exit_tryhelp(2);
1759
1760 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001761 command_default(&cs);
1762 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001764 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001765 }
1766
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001767 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001768 if (matchp->match->final_check != NULL)
1769 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001770
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001771 if (cs.target != NULL && cs.target->final_check != NULL)
1772 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001773
1774 /* Fix me: must put inverse options checking here --MN */
1775
1776 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001777 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001778 "unknown arguments found on commandline");
1779 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001780 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001781 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001782 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001783 "nothing appropriate following !");
1784
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001785 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND | CMD_CHECK)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001786 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001787 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001788 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001789 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001790 }
1791
1792 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001793 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1794 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001795
1796 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001797 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1798 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001799
1800 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001801 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001802 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001803 " source or destination IP addresses");
1804
Rusty Russell5eed48a2000-06-02 20:12:24 +00001805 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001806 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001807 "specify a unique address");
1808
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001809 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810
Jan Engelhardt5429b412010-09-13 15:45:15 +02001811 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001812 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001813 "chain name `%s' too long (must be under %u chars)",
1814 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815
Harald Welte43ac1912002-03-03 09:43:07 +00001816 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001817 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001818 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001819
Rusty Russell8beb0492004-12-22 00:37:10 +00001820 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001821 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001822 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001823
Harald Welte43ac1912002-03-03 09:43:07 +00001824 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001825 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001826 "can't initialize ip6tables table `%s': %s",
1827 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001828
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001829 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 || command == CMD_DELETE
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001831 || command == CMD_CHECK
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 || command == CMD_INSERT
1833 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001834 if (strcmp(chain, "PREROUTING") == 0
1835 || strcmp(chain, "INPUT") == 0) {
1836 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001837 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001838 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001839 "Can't use -%c with %s\n",
1840 opt2char(OPT_VIANAMEOUT),
1841 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001842 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001843
Harald Welte43ac1912002-03-03 09:43:07 +00001844 if (strcmp(chain, "POSTROUTING") == 0
1845 || strcmp(chain, "OUTPUT") == 0) {
1846 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001847 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001848 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001849 "Can't use -%c with %s\n",
1850 opt2char(OPT_VIANAMEIN),
1851 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001852 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001853
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001854 if (cs.target && ip6tc_is_chain(cs.jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001855 fprintf(stderr,
1856 "Warning: using chain %s, not extension\n",
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001857 cs.jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001859 if (cs.target->t)
1860 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001861
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001862 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001863 }
1864
1865 /* If they didn't specify a target, or it's a chain
1866 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001867 if (!cs.target
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001868 && (strlen(cs.jumpto) == 0
1869 || ip6tc_is_chain(cs.jumpto, *handle))) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001870 size_t size;
1871
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001872 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001873 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874
1875 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001876 + cs.target->size;
1877 cs.target->t = xtables_calloc(1, size);
1878 cs.target->t->u.target_size = size;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001879 strcpy(cs.target->t->u.user.name, cs.jumpto);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001880 if (cs.target->init != NULL)
1881 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882 }
1883
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001884 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001885 /* it is no chain, and we can't load a plugin.
1886 * We cannot know if the plugin is corrupt, non
1887 * existant OR if the user just misspelled a
1888 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001889#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001890 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001891 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001892 "goto '%s' is not a chain\n",
1893 cs.jumpto);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001894#endif
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001895 xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001896 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001897 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001898 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 }
1900 }
1901
1902 switch (command) {
1903 case CMD_APPEND:
1904 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001905 nsaddrs, saddrs, smasks,
1906 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001907 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001908 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001910 case CMD_DELETE:
1911 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001912 nsaddrs, saddrs, smasks,
1913 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001914 cs.options&OPT_VERBOSE,
1915 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001916 break;
1917 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001918 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001919 break;
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001920 case CMD_CHECK:
1921 ret = check_entry(chain, e,
1922 nsaddrs, saddrs, smasks,
1923 ndaddrs, daddrs, dmasks,
1924 cs.options&OPT_VERBOSE,
1925 *handle, cs.matches, cs.target);
1926 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 case CMD_REPLACE:
1928 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001929 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001930 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 break;
1932 case CMD_INSERT:
1933 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001934 nsaddrs, saddrs, smasks,
1935 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001936 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001937 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001938 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001939 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001940 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 break;
1942 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001943 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001944 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001945 case CMD_ZERO_NUM:
1946 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1947 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001948 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001949 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001950 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001952 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001953 cs.options&OPT_VERBOSE,
1954 cs.options&OPT_NUMERIC,
1955 cs.options&OPT_EXPANDED,
1956 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001957 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001958 if (ret && (command & CMD_ZERO))
1959 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001960 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001961 if (ret && (command & CMD_ZERO_NUM))
1962 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001963 break;
1964 case CMD_LIST_RULES:
1965 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001966 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001967 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001968 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001969 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001970 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001971 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001972 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001973 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001974 if (ret && (command & CMD_ZERO_NUM))
1975 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001976 break;
1977 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001978 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001979 break;
1980 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001981 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 break;
1983 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001984 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985 break;
1986 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001987 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001988 break;
1989 default:
1990 /* We should never reach this... */
1991 exit_tryhelp(2);
1992 }
1993
1994 if (verbose > 1)
1995 dump_entries6(*handle);
1996
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001997 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001998
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001999 if (e != NULL) {
2000 free(e);
2001 e = NULL;
2002 }
2003
Michael Granzow332e4ac2009-04-09 18:24:36 +01002004 free(saddrs);
2005 free(smasks);
2006 free(daddrs);
2007 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002008 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002009
Rusty Russell5eed48a2000-06-02 20:12:24 +00002010 return ret;
2011}