blob: ac376e239a2f208d00ab468c0371215e74cf38e8 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
85#define NUMBER_OF_CMD 15
Rusty Russell5eed48a2000-06-02 20:12:24 +000086static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070087 'Z', 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
Rusty Russell5eed48a2000-06-02 20:12:24 +000089#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100106 {.name = "append", .has_arg = 1, .val = 'A'},
107 {.name = "delete", .has_arg = 1, .val = 'D'},
108 {.name = "insert", .has_arg = 1, .val = 'I'},
109 {.name = "replace", .has_arg = 1, .val = 'R'},
110 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200111 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100112 {.name = "flush", .has_arg = 2, .val = 'F'},
113 {.name = "zero", .has_arg = 2, .val = 'Z'},
114 {.name = "new-chain", .has_arg = 1, .val = 'N'},
115 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
116 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
117 {.name = "policy", .has_arg = 1, .val = 'P'},
118 {.name = "source", .has_arg = 1, .val = 's'},
119 {.name = "destination", .has_arg = 1, .val = 'd'},
120 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
121 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
122 {.name = "protocol", .has_arg = 1, .val = 'p'},
123 {.name = "in-interface", .has_arg = 1, .val = 'i'},
124 {.name = "jump", .has_arg = 1, .val = 'j'},
125 {.name = "table", .has_arg = 1, .val = 't'},
126 {.name = "match", .has_arg = 1, .val = 'm'},
127 {.name = "numeric", .has_arg = 0, .val = 'n'},
128 {.name = "out-interface", .has_arg = 1, .val = 'o'},
129 {.name = "verbose", .has_arg = 0, .val = 'v'},
130 {.name = "exact", .has_arg = 0, .val = 'x'},
131 {.name = "version", .has_arg = 0, .val = 'V'},
132 {.name = "help", .has_arg = 2, .val = 'h'},
133 {.name = "line-numbers", .has_arg = 0, .val = '0'},
134 {.name = "modprobe", .has_arg = 1, .val = 'M'},
135 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200136 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100137 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100146void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100147struct xtables_globals ip6tables_globals = {
148 .option_offset = 0,
149 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500150 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100151 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100152};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000153
154/* Table of legal combinations of commands and options. If any of the
155 * given commands make an option legal, that option is legal (applies to
156 * CMD_LIST and CMD_ZERO only).
157 * Key:
158 * + compulsory
159 * x illegal
160 * optional
161 */
162
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100163static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000164/* Well, it's better than "Re: Linux vs FreeBSD" */
165{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200166 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000167/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
168/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
169/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
170/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
171/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
172/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
173/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700175/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000176/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
177/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200178/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200179/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
180/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000181};
182
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100183static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184{
185/* -n */ 0,
186/* -s */ IP6T_INV_SRCIP,
187/* -d */ IP6T_INV_DSTIP,
188/* -p */ IP6T_INV_PROTO,
189/* -j */ 0,
190/* -v */ 0,
191/* -x */ 0,
192/* -i */ IP6T_INV_VIA_IN,
193/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000194/*--line*/ 0,
195/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000196};
197
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100198#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500199#define prog_name ip6tables_globals.program_name
200#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201/* A few hardcoded protocols for 'all' and in case the user has no
202 /etc/protocols */
203struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100204 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100205 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000206};
207
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200208static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000209exit_tryhelp(int status)
210{
Harald Weltea8658ca2003-03-05 07:46:15 +0000211 if (line != -1)
212 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000213 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500214 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500215 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000216 exit(status);
217}
218
Patrick McHardy0b639362007-09-08 16:00:01 +0000219static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100220exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000221{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000222 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000223"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100224" %s -I chain [rulenum] rule-specification [options]\n"
225" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200227" %s -[LS] [chain [rulenum]] [options]\n"
228" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000229" %s -[NX] chain\n"
230" %s -E old-chain-name new-chain-name\n"
231" %s -P chain target [options]\n"
232" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500233 prog_name, prog_vers, prog_name, prog_name,
234 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100235 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000236
237 printf(
238"Commands:\n"
239"Either long or short options are allowed.\n"
240" --append -A chain Append to chain\n"
241" --delete -D chain Delete matching rule from chain\n"
242" --delete -D chain rulenum\n"
243" Delete rule rulenum (1 = first) from chain\n"
244" --insert -I chain [rulenum]\n"
245" Insert in chain as rulenum (default 1=first)\n"
246" --replace -R chain rulenum\n"
247" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200248" --list -L [chain [rulenum]]\n"
249" List the rules in a chain or all chains\n"
250" --list-rules -S [chain [rulenum]]\n"
251" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000252" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700253" --zero -Z [chain [rulenum]]\n"
254" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255" --new -N chain Create a new user-defined chain\n"
256" --delete-chain\n"
257" -X [chain] Delete a user-defined chain\n"
258" --policy -P chain target\n"
259" Change policy on chain to target\n"
260" --rename-chain\n"
261" -E old-chain new-chain\n"
262" Change chain name, (moving any references)\n"
263
264"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200265"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100266"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000267" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100268"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000269" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200270"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271" network interface name ([+] for wildcard)\n"
272" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000273" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200274#ifdef IP6T_F_GOTO
275" --goto -g chain\n"
276" jump to chain with no return\n"
277#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000278" --match -m match\n"
279" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200281"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282" network interface name ([+] for wildcard)\n"
283" --table -t table table to manipulate (default: `filter')\n"
284" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000285" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000286" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000287/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000288" --modprobe=<command> try to insert modules using this command\n"
289" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000290"[!] --version -V print package version.\n");
291
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200292 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000293 exit(0);
294}
295
Patrick McHardy0b639362007-09-08 16:00:01 +0000296void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100297ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000298{
299 va_list args;
300
301 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500302 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000303 vfprintf(stderr, msg, args);
304 va_end(args);
305 fprintf(stderr, "\n");
306 if (status == PARAMETER_PROBLEM)
307 exit_tryhelp(status);
308 if (status == VERSION_PROBLEM)
309 fprintf(stderr,
310 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
311 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500312 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000313 exit(status);
314}
315
Rusty Russell5eed48a2000-06-02 20:12:24 +0000316static void
317generic_opt_check(int command, int options)
318{
319 int i, j, legal = 0;
320
321 /* Check that commands are valid with options. Complicated by the
322 * fact that if an option is legal with *any* command given, it is
323 * legal overall (ie. -z and -l).
324 */
325 for (i = 0; i < NUMBER_OF_OPT; i++) {
326 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
327
328 for (j = 0; j < NUMBER_OF_CMD; j++) {
329 if (!(command & (1<<j)))
330 continue;
331
332 if (!(options & (1<<i))) {
333 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100334 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335 "You need to supply the `-%c' "
336 "option for this command\n",
337 optflags[i]);
338 } else {
339 if (commands_v_options[j][i] != 'x')
340 legal = 1;
341 else if (legal == 0)
342 legal = -1;
343 }
344 }
345 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100346 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000347 "Illegal option `-%c' with this command\n",
348 optflags[i]);
349 }
350}
351
352static char
353opt2char(int option)
354{
355 const char *ptr;
356 for (ptr = optflags; option > 1; option >>= 1, ptr++);
357
358 return *ptr;
359}
360
361static char
362cmd2char(int option)
363{
364 const char *ptr;
365 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
366
367 return *ptr;
368}
369
370static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000371add_command(unsigned int *cmd, const int newcmd, const int othercmds,
372 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000373{
374 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100375 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000376 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100377 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000378 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
379 *cmd |= newcmd;
380}
381
Rusty Russell5eed48a2000-06-02 20:12:24 +0000382/*
383 * All functions starting with "parse" should succeed, otherwise
384 * the program fails.
385 * Most routines return pointers to static data that may change
386 * between calls to the same or other routines with a few exceptions:
387 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
388 * return global static data.
389*/
390
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000391/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100392static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000393{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000394 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000395 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000396 proto == IPPROTO_AH ||
397 proto == IPPROTO_DSTOPTS);
398}
399
Rusty Russell5eed48a2000-06-02 20:12:24 +0000400/* Can't be zero. */
401static int
402parse_rulenumber(const char *rule)
403{
Harald Welte43ac1912002-03-03 09:43:07 +0000404 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000405
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100406 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100407 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000408 "Invalid rule number `%s'", rule);
409
410 return rulenum;
411}
412
413static const char *
414parse_target(const char *targetname)
415{
416 const char *ptr;
417
418 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100419 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000420 "Invalid target name (too short)");
421
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200422 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100423 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000424 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200425 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426
427 for (ptr = targetname; *ptr; ptr++)
428 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100429 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000430 "Invalid target name `%s'", targetname);
431 return targetname;
432}
433
Rusty Russell5eed48a2000-06-02 20:12:24 +0000434static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100435set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000436 int invert)
437{
438 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100439 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000440 opt2char(option));
441 *options |= option;
442
443 if (invert) {
444 unsigned int i;
445 for (i = 0; 1 << i != option; i++);
446
447 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100448 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000449 "cannot have ! before -%c",
450 opt2char(option));
451 *invflg |= inverse_for_options[i];
452 }
453}
454
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000455static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100456print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000457{
Harald Welte43ac1912002-03-03 09:43:07 +0000458 if (format & FMT_KILOMEGAGIGA) {
459 if (number > 99999) {
460 number = (number + 500) / 1000;
461 if (number > 9999) {
462 number = (number + 500) / 1000;
463 if (number > 9999) {
464 number = (number + 500) / 1000;
465 if (number > 9999) {
466 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000467 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000468 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000469 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000470 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000471 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000472 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000473 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000474 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000475 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000476 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000477 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478}
479
Harald Welte43ac1912002-03-03 09:43:07 +0000480
Rusty Russell5eed48a2000-06-02 20:12:24 +0000481static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100482print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483{
484 struct ip6t_counters counters;
485 const char *pol = ip6tc_get_policy(chain, &counters, handle);
486 printf("Chain %s", chain);
487 if (pol) {
488 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000489 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000490 fputc(' ', stdout);
491 print_num(counters.pcnt, (format|FMT_NOTABLE));
492 fputs("packets, ", stdout);
493 print_num(counters.bcnt, (format|FMT_NOTABLE));
494 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000495 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000496 printf(")\n");
497 } else {
498 unsigned int refs;
499 if (!ip6tc_get_references(&refs, chain, handle))
500 printf(" (ERROR obtaining refs)\n");
501 else
502 printf(" (%u references)\n", refs);
503 }
504
505 if (format & FMT_LINENUMBERS)
506 printf(FMT("%-4s ", "%s "), "num");
507 if (!(format & FMT_NOCOUNTS)) {
508 if (format & FMT_KILOMEGAGIGA) {
509 printf(FMT("%5s ","%s "), "pkts");
510 printf(FMT("%5s ","%s "), "bytes");
511 } else {
512 printf(FMT("%8s ","%s "), "pkts");
513 printf(FMT("%10s ","%s "), "bytes");
514 }
515 }
516 if (!(format & FMT_NOTARGET))
517 printf(FMT("%-9s ","%s "), "target");
518 fputs(" prot ", stdout);
519 if (format & FMT_OPTIONS)
520 fputs("opt", stdout);
521 if (format & FMT_VIA) {
522 printf(FMT(" %-6s ","%s "), "in");
523 printf(FMT("%-6s ","%s "), "out");
524 }
525 printf(FMT(" %-19s ","%s "), "source");
526 printf(FMT(" %-19s "," %s "), "destination");
527 printf("\n");
528}
529
Harald Welte43ac1912002-03-03 09:43:07 +0000530
Rusty Russell5eed48a2000-06-02 20:12:24 +0000531static int
532print_match(const struct ip6t_entry_match *m,
533 const struct ip6t_ip6 *ip,
534 int numeric)
535{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100536 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100537 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000538
539 if (match) {
540 if (match->print)
541 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000542 else
543 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000544 } else {
545 if (m->u.user.name[0])
546 printf("UNKNOWN match `%s' ", m->u.user.name);
547 }
548 /* Don't stop iterating. */
549 return 0;
550}
551
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100552/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553static void
554print_firewall(const struct ip6t_entry *fw,
555 const char *targname,
556 unsigned int num,
557 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100558 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000559{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100560 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000561 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100562 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000563 char buf[BUFSIZ];
564
Rusty Russell5eed48a2000-06-02 20:12:24 +0000565 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100566 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000567 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100568 target = xtables_find_target(IP6T_STANDARD_TARGET,
569 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000570
571 t = ip6t_get_target((struct ip6t_entry *)fw);
572 flags = fw->ipv6.flags;
573
574 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200575 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000576
577 if (!(format & FMT_NOCOUNTS)) {
578 print_num(fw->counters.pcnt, format);
579 print_num(fw->counters.bcnt, format);
580 }
581
582 if (!(format & FMT_NOTARGET))
583 printf(FMT("%-9s ", "%s "), targname);
584
585 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
586 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100587 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000588 if (pname)
589 printf(FMT("%-5s", "%s "), pname);
590 else
591 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
592 }
593
594 if (format & FMT_OPTIONS) {
595 if (format & FMT_NOTABLE)
596 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000597 fputc(' ', stdout); /* Invert flag of FRAG */
598 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599 fputc(' ', stdout);
600 }
601
602 if (format & FMT_VIA) {
603 char iface[IFNAMSIZ+2];
604
605 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
606 iface[0] = '!';
607 iface[1] = '\0';
608 }
609 else iface[0] = '\0';
610
611 if (fw->ipv6.iniface[0] != '\0') {
612 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000613 }
614 else if (format & FMT_NUMERIC) strcat(iface, "*");
615 else strcat(iface, "any");
616 printf(FMT(" %-6s ","in %s "), iface);
617
618 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
619 iface[0] = '!';
620 iface[1] = '\0';
621 }
622 else iface[0] = '\0';
623
624 if (fw->ipv6.outiface[0] != '\0') {
625 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000626 }
627 else if (format & FMT_NUMERIC) strcat(iface, "*");
628 else strcat(iface, "any");
629 printf(FMT("%-6s ","out %s "), iface);
630 }
631
632 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000633 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000634 && !(format & FMT_NUMERIC))
635 printf(FMT("%-19s ","%s "), "anywhere");
636 else {
637 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100638 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000639 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100640 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
641 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000642 printf(FMT("%-19s ","%s "), buf);
643 }
644
645 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
646 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
647 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200648 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000649 else {
650 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100651 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000652 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100653 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
654 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200655 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000656 }
657
658 if (format & FMT_NOTABLE)
659 fputs(" ", stdout);
660
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200661#ifdef IP6T_F_GOTO
662 if(fw->ipv6.flags & IP6T_F_GOTO)
663 printf("[goto] ");
664#endif
665
Rusty Russell5eed48a2000-06-02 20:12:24 +0000666 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
667
668 if (target) {
669 if (target->print)
670 /* Print the target information. */
671 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
672 } else if (t->u.target_size != sizeof(*t))
673 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000674 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000675
676 if (!(format & FMT_NONEWLINE))
677 fputc('\n', stdout);
678}
679
680static void
681print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100682 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000683{
684 struct ip6t_entry_target *t;
685
686 t = ip6t_get_target((struct ip6t_entry *)fw);
687 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
688}
689
690static int
691append_entry(const ip6t_chainlabel chain,
692 struct ip6t_entry *fw,
693 unsigned int nsaddrs,
694 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100695 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696 unsigned int ndaddrs,
697 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100698 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000699 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100700 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701{
702 unsigned int i, j;
703 int ret = 1;
704
705 for (i = 0; i < nsaddrs; i++) {
706 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100707 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708 for (j = 0; j < ndaddrs; j++) {
709 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100710 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100712 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000713 ret &= ip6tc_append_entry(chain, fw, handle);
714 }
715 }
716
717 return ret;
718}
719
720static int
721replace_entry(const ip6t_chainlabel chain,
722 struct ip6t_entry *fw,
723 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100724 const struct in6_addr *saddr, const struct in6_addr *smask,
725 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000726 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100727 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728{
729 fw->ipv6.src = *saddr;
730 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100731 fw->ipv6.smsk = *smask;
732 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000733
734 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100735 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 return ip6tc_replace_entry(chain, fw, rulenum, handle);
737}
738
739static int
740insert_entry(const ip6t_chainlabel chain,
741 struct ip6t_entry *fw,
742 unsigned int rulenum,
743 unsigned int nsaddrs,
744 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100745 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746 unsigned int ndaddrs,
747 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100748 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100750 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751{
752 unsigned int i, j;
753 int ret = 1;
754
755 for (i = 0; i < nsaddrs; i++) {
756 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100757 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000758 for (j = 0; j < ndaddrs; j++) {
759 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100760 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100762 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000763 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
764 }
765 }
766
767 return ret;
768}
769
770static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100771make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100772 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000773{
774 /* Establish mask for comparison */
775 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100776 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777 unsigned char *mask, *mptr;
778
779 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000780 for (matchp = matches; matchp; matchp = matchp->next)
781 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782
Jan Engelhardt630ef482009-01-27 14:58:41 +0100783 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000784 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100785 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000786
787 memset(mask, 0xFF, sizeof(struct ip6t_entry));
788 mptr = mask + sizeof(struct ip6t_entry);
789
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000790 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000791 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000792 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000793 + matchp->match->userspacesize);
794 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 }
796
Max Kellermann5b76f682008-01-29 13:42:48 +0000797 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000798 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100799 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800
801 return mask;
802}
803
804static int
805delete_entry(const ip6t_chainlabel chain,
806 struct ip6t_entry *fw,
807 unsigned int nsaddrs,
808 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100809 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000810 unsigned int ndaddrs,
811 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100812 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100814 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100815 struct xtables_rule_match *matches,
816 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000817{
818 unsigned int i, j;
819 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 unsigned char *mask;
821
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100822 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000823 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000824 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100825 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000827 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100828 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000829 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100830 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000831 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000832 }
833 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000834 free(mask);
835
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836 return ret;
837}
838
András Kis-Szabó764316a2001-02-26 17:31:20 +0000839int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100840for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
841 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000842{
Max Kellermann5b76f682008-01-29 13:42:48 +0000843 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 const char *chain;
845 char *chains;
846 unsigned int i, chaincount = 0;
847
848 chain = ip6tc_first_chain(handle);
849 while (chain) {
850 chaincount++;
851 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000852 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853
Jan Engelhardt630ef482009-01-27 14:58:41 +0100854 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 i = 0;
856 chain = ip6tc_first_chain(handle);
857 while (chain) {
858 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
859 i++;
860 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000861 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862
863 for (i = 0; i < chaincount; i++) {
864 if (!builtinstoo
865 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100866 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000867 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000868 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000869 }
870
871 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000872 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873}
874
András Kis-Szabó764316a2001-02-26 17:31:20 +0000875int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000876flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100877 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878{
879 if (!chain)
880 return for_each_chain(flush_entries, verbose, 1, handle);
881
882 if (verbose)
883 fprintf(stdout, "Flushing chain `%s'\n", chain);
884 return ip6tc_flush_entries(chain, handle);
885}
886
887static int
888zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100889 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000890{
891 if (!chain)
892 return for_each_chain(zero_entries, verbose, 1, handle);
893
894 if (verbose)
895 fprintf(stdout, "Zeroing chain `%s'\n", chain);
896 return ip6tc_zero_entries(chain, handle);
897}
898
András Kis-Szabó764316a2001-02-26 17:31:20 +0000899int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000900delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100901 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000902{
903 if (!chain)
904 return for_each_chain(delete_chain, verbose, 0, handle);
905
906 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000907 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 return ip6tc_delete_chain(chain, handle);
909}
910
911static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200912list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100913 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000914{
915 int found = 0;
916 unsigned int format;
917 const char *this;
918
919 format = FMT_OPTIONS;
920 if (!verbose)
921 format |= FMT_NOCOUNTS;
922 else
923 format |= FMT_VIA;
924
925 if (numeric)
926 format |= FMT_NUMERIC;
927
928 if (!expanded)
929 format |= FMT_KILOMEGAGIGA;
930
931 if (linenumbers)
932 format |= FMT_LINENUMBERS;
933
934 for (this = ip6tc_first_chain(handle);
935 this;
936 this = ip6tc_next_chain(handle)) {
937 const struct ip6t_entry *i;
938 unsigned int num;
939
940 if (chain && strcmp(chain, this) != 0)
941 continue;
942
943 if (found) printf("\n");
944
Henrik Nordstrombb340822008-05-13 13:09:23 +0200945 if (!rulenum)
946 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947 i = ip6tc_first_rule(this, handle);
948
949 num = 0;
950 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200951 num++;
952 if (!rulenum || num == rulenum)
953 print_firewall(i,
954 ip6tc_get_target(i, handle),
955 num,
956 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100957 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000958 i = ip6tc_next_rule(i, handle);
959 }
960 found = 1;
961 }
962
963 errno = ENOENT;
964 return found;
965}
966
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200967/* This assumes that mask is contiguous, and byte-bounded. */
968static void
969print_iface(char letter, const char *iface, const unsigned char *mask,
970 int invert)
971{
972 unsigned int i;
973
974 if (mask[0] == 0)
975 return;
976
Jan Engelhardt73866352010-12-18 02:04:59 +0100977 printf("%s -%c", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200978
979 for (i = 0; i < IFNAMSIZ; i++) {
980 if (mask[i] != 0) {
981 if (iface[i] != '\0')
982 printf("%c", iface[i]);
983 } else {
984 /* we can access iface[i-1] here, because
985 * a few lines above we make sure that mask[0] != 0 */
986 if (iface[i-1] != '\0')
987 printf("+");
988 break;
989 }
990 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200991}
992
993/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100994static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200995{
996 if (proto) {
997 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +0100998 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200999
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001000 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001001 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001002 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001003 invertstr, pent->p_name);
1004 return;
1005 }
1006
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001007 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1008 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001009 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001010 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011 return;
1012 }
1013
Jan Engelhardt73866352010-12-18 02:04:59 +01001014 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001015 }
1016}
1017
1018static int print_match_save(const struct ip6t_entry_match *e,
1019 const struct ip6t_ip6 *ip)
1020{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001021 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001022 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001023
1024 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001025 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001026
1027 /* some matches don't provide a save function */
1028 if (match->save)
1029 match->save(ip, e);
1030 } else {
1031 if (e->u.match_size) {
1032 fprintf(stderr,
1033 "Can't find library for match `%s'\n",
1034 e->u.user.name);
1035 exit(1);
1036 }
1037 }
1038 return 0;
1039}
1040
1041/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001042static void print_ip(const char *prefix, const struct in6_addr *ip,
1043 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001044{
1045 char buf[51];
1046 int l = ipv6_prefix_length(mask);
1047
1048 if (l == 0 && !invert)
1049 return;
1050
Jan Engelhardt73866352010-12-18 02:04:59 +01001051 printf("%s %s %s",
1052 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001053 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001054 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1055
1056 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001057 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001058 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001059 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001060}
1061
1062/* We want this to be readable, so only print out neccessary fields.
1063 * Because that's the kind of world I want to live in. */
1064void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001065 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001066{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001067 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001068 const char *target_name;
1069
1070 /* print counters for iptables-save */
1071 if (counters > 0)
1072 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1073
1074 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001075 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001076
1077 /* Print IP part. */
1078 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1079 e->ipv6.invflags & IP6T_INV_SRCIP);
1080
1081 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1082 e->ipv6.invflags & IP6T_INV_DSTIP);
1083
1084 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1085 e->ipv6.invflags & IP6T_INV_VIA_IN);
1086
1087 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1088 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1089
1090 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1091
1092#if 0
1093 /* not definied in ipv6
1094 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1095 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001096 printf("%s -f",
1097 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001098#endif
1099
1100 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001101 printf("%s -? %d",
1102 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001103 e->ipv6.tos);
1104
1105 /* Print matchinfo part */
1106 if (e->target_offset) {
1107 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1108 }
1109
1110 /* print counters for iptables -R */
1111 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001112 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001113
1114 /* Print target name */
1115 target_name = ip6tc_get_target(e, h);
1116 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001117#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001118 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001119#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001120 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001121#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001122
1123 /* Print targinfo part */
1124 t = ip6t_get_target((struct ip6t_entry *)e);
1125 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001126 struct xtables_target *target =
1127 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001128
1129 if (!target) {
1130 fprintf(stderr, "Can't find library for target `%s'\n",
1131 t->u.user.name);
1132 exit(1);
1133 }
1134
1135 if (target->save)
1136 target->save(&e->ipv6, t);
1137 else {
1138 /* If the target size is greater than ip6t_entry_target
1139 * there is something to be saved, we just don't know
1140 * how to print it */
1141 if (t->u.target_size !=
1142 sizeof(struct ip6t_entry_target)) {
1143 fprintf(stderr, "Target `%s' is missing "
1144 "save function\n",
1145 t->u.user.name);
1146 exit(1);
1147 }
1148 }
1149 }
1150 printf("\n");
1151}
1152
1153static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001154list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001155 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001156{
1157 const char *this = NULL;
1158 int found = 0;
1159
1160 if (counters)
1161 counters = -1; /* iptables -c format */
1162
1163 /* Dump out chain names first,
1164 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001165 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001166 this;
1167 this = ip6tc_next_chain(handle)) {
1168 if (chain && strcmp(this, chain) != 0)
1169 continue;
1170
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001171 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001172 struct ip6t_counters count;
1173 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1174 if (counters)
1175 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1176 printf("\n");
1177 } else {
1178 printf("-N %s\n", this);
1179 }
1180 }
1181
1182 for (this = ip6tc_first_chain(handle);
1183 this;
1184 this = ip6tc_next_chain(handle)) {
1185 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001186 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001187
1188 if (chain && strcmp(this, chain) != 0)
1189 continue;
1190
1191 /* Dump out rules */
1192 e = ip6tc_first_rule(this, handle);
1193 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001194 num++;
1195 if (!rulenum || num == rulenum)
1196 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001197 e = ip6tc_next_rule(e, handle);
1198 }
1199 found = 1;
1200 }
1201
1202 errno = ENOENT;
1203 return found;
1204}
1205
Rusty Russell5eed48a2000-06-02 20:12:24 +00001206static struct ip6t_entry *
1207generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001208 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001209 struct ip6t_entry_target *target)
1210{
1211 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001212 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001213 struct ip6t_entry *e;
1214
1215 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001216 for (matchp = matches; matchp; matchp = matchp->next)
1217 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001218
Jan Engelhardt630ef482009-01-27 14:58:41 +01001219 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001220 *e = *fw;
1221 e->target_offset = size;
1222 e->next_offset = size + target->u.target_size;
1223
1224 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001225 for (matchp = matches; matchp; matchp = matchp->next) {
1226 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1227 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001228 }
1229 memcpy(e->elems + size, target, target->u.target_size);
1230
1231 return e;
1232}
1233
Jan Engelhardt395e4412009-02-10 10:43:08 +01001234static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001235{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001236 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001237
1238 for (matchp = *matches; matchp;) {
1239 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001240 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001241 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001242 matchp->match->m = NULL;
1243 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001244 if (matchp->match == matchp->match->next) {
1245 free(matchp->match);
1246 matchp->match = NULL;
1247 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001248 free(matchp);
1249 matchp = tmp;
1250 }
1251
1252 *matches = NULL;
1253}
1254
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001255static void command_default(struct iptables_command_state *cs)
1256{
1257 struct xtables_rule_match *matchp;
1258 struct xtables_match *m;
1259
1260 if (cs->target == NULL || cs->target->parse == NULL ||
1261 cs->c < cs->target->option_offset ||
1262 cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE ||
1263 !cs->target->parse(cs->c - cs->target->option_offset,
1264 cs->argv, cs->invert,
1265 &cs->target->tflags,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001266 &cs->fw6, &cs->target->t)) {
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001267 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1268 if (matchp->completed ||
1269 matchp->match->parse == NULL)
1270 continue;
1271 if (cs->c < matchp->match->option_offset ||
1272 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1273 continue;
1274 if (matchp->match->parse(cs->c - matchp->match->option_offset,
1275 cs->argv, cs->invert,
1276 &matchp->match->mflags,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001277 &cs->fw6,
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001278 &matchp->match->m))
1279 break;
1280 }
1281 m = matchp ? matchp->match : NULL;
1282
1283 /* If you listen carefully, you can
1284 actually hear this code suck. */
1285
1286 /* some explanations (after four different bugs
1287 * in 3 different releases): If we encounter a
1288 * parameter, that has not been parsed yet,
1289 * it's not an option of an explicitly loaded
1290 * match or a target. However, we support
1291 * implicit loading of the protocol match
1292 * extension. '-p tcp' means 'l4 proto 6' and
1293 * at the same time 'load tcp protocol match on
1294 * demand if we specify --dport'.
1295 *
1296 * To make this work, we need to make sure:
1297 * - the parameter has not been parsed by
1298 * a match (m above)
1299 * - a protocol has been specified
1300 * - the protocol extension has not been
1301 * loaded yet, or is loaded and unused
1302 * [think of ip6tables-restore!]
1303 * - the protocol extension can be successively
1304 * loaded
1305 */
1306 if (m == NULL
1307 && cs->protocol
1308 && (!find_proto(cs->protocol, XTF_DONT_LOAD,
1309 cs->options&OPT_NUMERIC, NULL)
1310 || (find_proto(cs->protocol, XTF_DONT_LOAD,
1311 cs->options&OPT_NUMERIC, NULL)
1312 && (cs->proto_used == 0))
1313 )
1314 && (m = find_proto(cs->protocol, XTF_TRY_LOAD,
1315 cs->options&OPT_NUMERIC, &cs->matches))) {
1316 /* Try loading protocol */
1317 size_t size;
1318
1319 cs->proto_used = 1;
1320
1321 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1322 + m->size;
1323
1324 m->m = xtables_calloc(1, size);
1325 m->m->u.match_size = size;
1326 strcpy(m->m->u.user.name, m->name);
1327 m->m->u.user.revision = m->revision;
1328 if (m->init != NULL)
1329 m->init(m->m);
1330
1331 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1332 m->extra_opts, &m->option_offset);
1333
1334 optind--;
1335 return;
1336 }
1337
1338 if (!m) {
1339 if (cs->c == '?') {
1340 if (optopt) {
1341 xtables_error(
1342 PARAMETER_PROBLEM,
1343 "option `%s' "
1344 "requires an "
1345 "argument",
1346 cs->argv[optind-1]);
1347 } else {
1348 xtables_error(
1349 PARAMETER_PROBLEM,
1350 "unknown option "
1351 "`%s'",
1352 cs->argv[optind-1]);
1353 }
1354 }
1355 xtables_error(PARAMETER_PROBLEM,
1356 "Unknown arg `%s'", optarg);
1357 }
1358 }
1359}
1360
Jan Engelhardtfd187312008-11-10 16:59:27 +01001361int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001363 struct iptables_command_state cs;
1364 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001365 unsigned int nsaddrs = 0, ndaddrs = 0;
1366 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001367 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001368
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001369 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001370 const char *chain = NULL;
1371 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1372 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001373 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001374 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001376 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001377 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001378 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001379 const char *jumpto = "";
Patrick McHardy875441e2007-10-17 08:48:58 +00001380 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001381
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001382 memset(&cs, 0, sizeof(cs));
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001383 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384
Harald Welte43ac1912002-03-03 09:43:07 +00001385 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001386 * a second time */
1387 optind = 0;
1388
Harald Welte43ac1912002-03-03 09:43:07 +00001389 /* clear mflags in case do_command gets called a second time
1390 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001391 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001392 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001393
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001394 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001395 t->tflags = 0;
1396 t->used = 0;
1397 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001398
Rusty Russell5eed48a2000-06-02 20:12:24 +00001399 /* Suppress error messages: we may add new options if we
1400 demand-load a protocol. */
1401 opterr = 0;
1402
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001403 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001404 while ((cs.c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001405 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001406 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001407 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001408 /*
1409 * Command selection
1410 */
1411 case 'A':
1412 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001413 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 chain = optarg;
1415 break;
1416
1417 case 'D':
1418 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001419 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001420 chain = optarg;
1421 if (optind < argc && argv[optind][0] != '-'
1422 && argv[optind][0] != '!') {
1423 rulenum = parse_rulenumber(argv[optind++]);
1424 command = CMD_DELETE_NUM;
1425 }
1426 break;
1427
Rusty Russell5eed48a2000-06-02 20:12:24 +00001428 case 'R':
1429 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001430 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 chain = optarg;
1432 if (optind < argc && argv[optind][0] != '-'
1433 && argv[optind][0] != '!')
1434 rulenum = parse_rulenumber(argv[optind++]);
1435 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001436 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001437 "-%c requires a rule number",
1438 cmd2char(CMD_REPLACE));
1439 break;
1440
1441 case 'I':
1442 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001443 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001444 chain = optarg;
1445 if (optind < argc && argv[optind][0] != '-'
1446 && argv[optind][0] != '!')
1447 rulenum = parse_rulenumber(argv[optind++]);
1448 else rulenum = 1;
1449 break;
1450
1451 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001452 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001453 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454 if (optarg) chain = optarg;
1455 else if (optind < argc && argv[optind][0] != '-'
1456 && argv[optind][0] != '!')
1457 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001458 if (optind < argc && argv[optind][0] != '-'
1459 && argv[optind][0] != '!')
1460 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001461 break;
1462
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001463 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001464 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001465 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001466 if (optarg) chain = optarg;
1467 else if (optind < argc && argv[optind][0] != '-'
1468 && argv[optind][0] != '!')
1469 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001470 if (optind < argc && argv[optind][0] != '-'
1471 && argv[optind][0] != '!')
1472 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001473 break;
1474
Rusty Russell5eed48a2000-06-02 20:12:24 +00001475 case 'F':
1476 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001477 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001478 if (optarg) chain = optarg;
1479 else if (optind < argc && argv[optind][0] != '-'
1480 && argv[optind][0] != '!')
1481 chain = argv[optind++];
1482 break;
1483
1484 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001485 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001486 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001487 if (optarg) chain = optarg;
1488 else if (optind < argc && argv[optind][0] != '-'
1489 && argv[optind][0] != '!')
1490 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001491 if (optind < argc && argv[optind][0] != '-'
1492 && argv[optind][0] != '!') {
1493 rulenum = parse_rulenumber(argv[optind++]);
1494 command = CMD_ZERO_NUM;
1495 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001496 break;
1497
1498 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001499 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001500 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001501 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001502 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001503 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001504 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001505 "chain name may not clash "
1506 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001507 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001508 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 chain = optarg;
1510 break;
1511
1512 case 'X':
1513 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001514 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001515 if (optarg) chain = optarg;
1516 else if (optind < argc && argv[optind][0] != '-'
1517 && argv[optind][0] != '!')
1518 chain = argv[optind++];
1519 break;
1520
1521 case 'E':
1522 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001523 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001524 chain = optarg;
1525 if (optind < argc && argv[optind][0] != '-'
1526 && argv[optind][0] != '!')
1527 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001528 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001529 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001530 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001531 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001532 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533 break;
1534
1535 case 'P':
1536 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001537 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 chain = optarg;
1539 if (optind < argc && argv[optind][0] != '-'
1540 && argv[optind][0] != '!')
1541 policy = argv[optind++];
1542 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001543 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544 "-%c requires a chain and a policy",
1545 cmd2char(CMD_SET_POLICY));
1546 break;
1547
1548 case 'h':
1549 if (!optarg)
1550 optarg = argv[optind];
1551
Jonas Berlin1b91e592005-04-01 06:58:38 +00001552 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001553 if (!cs.matches && cs.protocol)
1554 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1555 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001557 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001558
1559 /*
1560 * Option selection
1561 */
1562 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001563 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001564 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001565 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001566
1567 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001568 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1569 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001571 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001572 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1573 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001575 if (cs.fw6.ipv6.proto == 0
1576 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001577 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001579
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001580 if (is_exthdr(cs.fw6.ipv6.proto)
1581 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001582 fprintf(stderr,
1583 "Warning: never matched protocol: %s. "
1584 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001585 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001586 break;
1587
1588 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001589 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001590 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001591 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001592 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001593 break;
1594
1595 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001596 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001597 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001598 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001599 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001600 break;
1601
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001602#ifdef IP6T_F_GOTO
1603 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001604 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001605 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001606 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001607 jumpto = parse_target(optarg);
1608 break;
1609#endif
1610
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 case 'j':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001612 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001613 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001615 /* TRY_LOAD (may be chain name) */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001616 cs.target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001618 if (cs.target) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001619 size_t size;
1620
Harald Welte43ac1912002-03-03 09:43:07 +00001621 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001622 + cs.target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001624 cs.target->t = xtables_calloc(1, size);
1625 cs.target->t->u.target_size = size;
1626 strcpy(cs.target->t->u.user.name, jumpto);
1627 cs.target->t->u.user.revision = cs.target->revision;
1628 if (cs.target->init != NULL)
1629 cs.target->init(cs.target->t);
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001630 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001631 cs.target->extra_opts,
1632 &cs.target->option_offset);
Patrick McHardy455559b2008-04-15 15:51:19 +02001633 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001634 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001635 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001636 }
1637 break;
1638
1639
1640 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001641 if (*optarg == '\0')
1642 xtables_error(PARAMETER_PROBLEM,
1643 "Empty interface is likely to be "
1644 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001645 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001646 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001647 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001648 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001649 cs.fw6.ipv6.iniface,
1650 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001651 break;
1652
1653 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001654 if (*optarg == '\0')
1655 xtables_error(PARAMETER_PROBLEM,
1656 "Empty interface is likely to be "
1657 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001658 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001659 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001660 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001661 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001662 cs.fw6.ipv6.outiface,
1663 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 break;
1665
1666 case 'v':
1667 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001668 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001669 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001670 verbose++;
1671 break;
1672
1673 case 'm': {
1674 size_t size;
1675
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001676 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001677 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001678 "unexpected ! flag before --match");
1679
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001680 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001681 &cs.matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001682 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1683 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001684 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001685 m->m->u.match_size = size;
1686 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001687 m->m->u.user.revision = m->revision;
Jonas Berlin1b91e592005-04-01 06:58:38 +00001688 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001689 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001690 if (m != m->next)
1691 /* Merge options for non-cloned matches */
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001692 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693 }
1694 break;
1695
1696 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001697 set_option(&cs.options, OPT_NUMERIC, &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 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001702 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001703 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001705 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706 break;
1707
1708 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001709 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001710 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001711 break;
1712
1713 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001714 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001715 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001716 else
1717 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001718 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001719 exit(0);
1720
1721 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001722 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001723 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001724 break;
1725
Harald Welte43ac1912002-03-03 09:43:07 +00001726 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001727 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001728 break;
1729
1730 case 'c':
1731
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001732 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001733 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001734 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001735 bcnt = strchr(pcnt + 1, ',');
1736 if (bcnt)
1737 bcnt++;
1738 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001739 && argv[optind][0] != '!')
1740 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001741 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001742 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001743 "-%c requires packet and byte counter",
1744 opt2char(OPT_COUNTERS));
1745
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001746 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001747 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001748 "-%c packet counter not numeric",
1749 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001750 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001751
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001752 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001753 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001754 "-%c byte counter not numeric",
1755 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001756 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001757 break;
1758
Rusty Russell5eed48a2000-06-02 20:12:24 +00001759 case 1: /* non option */
1760 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001761 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001762 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763 "multiple consecutive ! not"
1764 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001765 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001766 optarg[0] = '\0';
1767 continue;
1768 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001769 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001770 exit_tryhelp(2);
1771
1772 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001773 command_default(&cs);
1774 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001775 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001776 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001777 }
1778
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001779 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001780 if (matchp->match->final_check != NULL)
1781 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001782
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001783 if (cs.target != NULL && cs.target->final_check != NULL)
1784 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001785
1786 /* Fix me: must put inverse options checking here --MN */
1787
1788 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001789 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001790 "unknown arguments found on commandline");
1791 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001792 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001793 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001794 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001795 "nothing appropriate following !");
1796
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001797 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001798 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001799 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001800 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001801 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001802 }
1803
1804 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001805 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1806 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001807
1808 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001809 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1810 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001811
1812 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001813 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001814 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815 " source or destination IP addresses");
1816
Rusty Russell5eed48a2000-06-02 20:12:24 +00001817 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001818 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001819 "specify a unique address");
1820
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001821 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001822
Jan Engelhardt5429b412010-09-13 15:45:15 +02001823 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001824 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001825 "chain name `%s' too long (must be under %u chars)",
1826 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001827
Harald Welte43ac1912002-03-03 09:43:07 +00001828 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001829 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001830 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001831
Rusty Russell8beb0492004-12-22 00:37:10 +00001832 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001833 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001834 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001835
Harald Welte43ac1912002-03-03 09:43:07 +00001836 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001837 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001838 "can't initialize ip6tables table `%s': %s",
1839 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001840
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001841 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001842 || command == CMD_DELETE
1843 || command == CMD_INSERT
1844 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001845 if (strcmp(chain, "PREROUTING") == 0
1846 || strcmp(chain, "INPUT") == 0) {
1847 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001848 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001849 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001850 "Can't use -%c with %s\n",
1851 opt2char(OPT_VIANAMEOUT),
1852 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001853 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001854
Harald Welte43ac1912002-03-03 09:43:07 +00001855 if (strcmp(chain, "POSTROUTING") == 0
1856 || strcmp(chain, "OUTPUT") == 0) {
1857 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001858 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001859 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001860 "Can't use -%c with %s\n",
1861 opt2char(OPT_VIANAMEIN),
1862 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001863 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001864
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001865 if (cs.target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001866 fprintf(stderr,
1867 "Warning: using chain %s, not extension\n",
1868 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001869
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001870 if (cs.target->t)
1871 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001872
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001873 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874 }
1875
1876 /* If they didn't specify a target, or it's a chain
1877 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001878 if (!cs.target
Rusty Russell5eed48a2000-06-02 20:12:24 +00001879 && (strlen(jumpto) == 0
1880 || ip6tc_is_chain(jumpto, *handle))) {
1881 size_t size;
1882
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001883 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001884 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001885
1886 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001887 + cs.target->size;
1888 cs.target->t = xtables_calloc(1, size);
1889 cs.target->t->u.target_size = size;
1890 strcpy(cs.target->t->u.user.name, jumpto);
1891 if (cs.target->init != NULL)
1892 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893 }
1894
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001895 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001896 /* it is no chain, and we can't load a plugin.
1897 * We cannot know if the plugin is corrupt, non
1898 * existant OR if the user just misspelled a
1899 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001900#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001901 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001902 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001903 "goto '%s' is not a chain\n", jumpto);
1904#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001905 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001906 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001907 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001908 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 }
1910 }
1911
1912 switch (command) {
1913 case CMD_APPEND:
1914 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001915 nsaddrs, saddrs, smasks,
1916 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001917 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001918 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001919 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001920 case CMD_DELETE:
1921 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001922 nsaddrs, saddrs, smasks,
1923 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001924 cs.options&OPT_VERBOSE,
1925 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001926 break;
1927 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001928 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001929 break;
1930 case CMD_REPLACE:
1931 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001932 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001933 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001934 break;
1935 case CMD_INSERT:
1936 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001937 nsaddrs, saddrs, smasks,
1938 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001939 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001940 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001943 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001944 break;
1945 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001946 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001948 case CMD_ZERO_NUM:
1949 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1950 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001951 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001953 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001954 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001955 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001956 cs.options&OPT_VERBOSE,
1957 cs.options&OPT_NUMERIC,
1958 cs.options&OPT_EXPANDED,
1959 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001960 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001961 if (ret && (command & CMD_ZERO))
1962 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001963 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001964 if (ret && (command & CMD_ZERO_NUM))
1965 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001966 break;
1967 case CMD_LIST_RULES:
1968 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001969 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001970 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001971 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001972 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001973 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001974 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001976 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001977 if (ret && (command & CMD_ZERO_NUM))
1978 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001979 break;
1980 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001981 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 break;
1983 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001984 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985 break;
1986 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001987 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001988 break;
1989 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001990 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001991 break;
1992 default:
1993 /* We should never reach this... */
1994 exit_tryhelp(2);
1995 }
1996
1997 if (verbose > 1)
1998 dump_entries6(*handle);
1999
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002000 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002001
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002002 if (e != NULL) {
2003 free(e);
2004 e = NULL;
2005 }
2006
Michael Granzow332e4ac2009-04-09 18:24:36 +01002007 free(saddrs);
2008 free(smasks);
2009 free(daddrs);
2010 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002011 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002012
Rusty Russell5eed48a2000-06-02 20:12:24 +00002013 return ret;
2014}