blob: 967a5f80db8a79eb5a46e70e0ff8d3b6525f4d96 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010085#define CMD_CHECK 0x4000U
86#define NUMBER_OF_CMD 16
Rusty Russell5eed48a2000-06-02 20:12:24 +000087static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010088 'Z', 'N', 'X', 'P', 'E', 'S', 'C' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000089
Jan Engelhardtf4b6e522011-02-07 03:16:14 +010090#define NUMBER_OF_OPT ARRAY_SIZE(optflags)
91static const char optflags[]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000092= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010095 {.name = "append", .has_arg = 1, .val = 'A'},
96 {.name = "delete", .has_arg = 1, .val = 'D'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +010097 {.name = "check" , .has_arg = 1, .val = 'C'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010098 {.name = "insert", .has_arg = 1, .val = 'I'},
99 {.name = "replace", .has_arg = 1, .val = 'R'},
100 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200101 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100102 {.name = "flush", .has_arg = 2, .val = 'F'},
103 {.name = "zero", .has_arg = 2, .val = 'Z'},
104 {.name = "new-chain", .has_arg = 1, .val = 'N'},
105 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
106 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
107 {.name = "policy", .has_arg = 1, .val = 'P'},
108 {.name = "source", .has_arg = 1, .val = 's'},
109 {.name = "destination", .has_arg = 1, .val = 'd'},
110 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
111 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
112 {.name = "protocol", .has_arg = 1, .val = 'p'},
113 {.name = "in-interface", .has_arg = 1, .val = 'i'},
114 {.name = "jump", .has_arg = 1, .val = 'j'},
115 {.name = "table", .has_arg = 1, .val = 't'},
116 {.name = "match", .has_arg = 1, .val = 'm'},
117 {.name = "numeric", .has_arg = 0, .val = 'n'},
118 {.name = "out-interface", .has_arg = 1, .val = 'o'},
119 {.name = "verbose", .has_arg = 0, .val = 'v'},
120 {.name = "exact", .has_arg = 0, .val = 'x'},
121 {.name = "version", .has_arg = 0, .val = 'V'},
122 {.name = "help", .has_arg = 2, .val = 'h'},
123 {.name = "line-numbers", .has_arg = 0, .val = '0'},
124 {.name = "modprobe", .has_arg = 1, .val = 'M'},
125 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200126 {.name = "goto", .has_arg = 1, .val = 'g'},
Maciej Żenczykowski57664122011-04-14 02:22:14 -0700127 {.name = "ipv4", .has_arg = 0, .val = '4'},
128 {.name = "ipv6", .has_arg = 0, .val = '6'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100129 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000130};
131
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100132void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100133struct xtables_globals ip6tables_globals = {
134 .option_offset = 0,
135 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500136 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100137 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100138};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000139
140/* Table of legal combinations of commands and options. If any of the
141 * given commands make an option legal, that option is legal (applies to
142 * CMD_LIST and CMD_ZERO only).
143 * Key:
144 * + compulsory
145 * x illegal
146 * optional
147 */
148
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100149static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000150/* Well, it's better than "Re: Linux vs FreeBSD" */
151{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200152 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000153/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
154/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
155/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
156/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
157/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
158/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
159/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
160/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700161/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
163/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200164/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200165/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100166/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'},
167/*CHECK*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000168};
169
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100170static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000171{
172/* -n */ 0,
173/* -s */ IP6T_INV_SRCIP,
174/* -d */ IP6T_INV_DSTIP,
175/* -p */ IP6T_INV_PROTO,
176/* -j */ 0,
177/* -v */ 0,
178/* -x */ 0,
179/* -i */ IP6T_INV_VIA_IN,
180/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000181/*--line*/ 0,
182/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000183};
184
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100185#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500186#define prog_name ip6tables_globals.program_name
187#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000188/* A few hardcoded protocols for 'all' and in case the user has no
189 /etc/protocols */
190struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100191 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100192 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000193};
194
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200195static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000196exit_tryhelp(int status)
197{
Harald Weltea8658ca2003-03-05 07:46:15 +0000198 if (line != -1)
199 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000200 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500201 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500202 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000203 exit(status);
204}
205
Patrick McHardy0b639362007-09-08 16:00:01 +0000206static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100207exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000208{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000209 printf("%s v%s\n\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100210"Usage: %s -[ACD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100211" %s -I chain [rulenum] rule-specification [options]\n"
212" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000213" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200214" %s -[LS] [chain [rulenum]] [options]\n"
215" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000216" %s -[NX] chain\n"
217" %s -E old-chain-name new-chain-name\n"
218" %s -P chain target [options]\n"
219" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500220 prog_name, prog_vers, prog_name, prog_name,
221 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100222 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000223
224 printf(
225"Commands:\n"
226"Either long or short options are allowed.\n"
227" --append -A chain Append to chain\n"
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100228" --check -C chain Check for the existence of a rule\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000229" --delete -D chain Delete matching rule from chain\n"
230" --delete -D chain rulenum\n"
231" Delete rule rulenum (1 = first) from chain\n"
232" --insert -I chain [rulenum]\n"
233" Insert in chain as rulenum (default 1=first)\n"
234" --replace -R chain rulenum\n"
235" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200236" --list -L [chain [rulenum]]\n"
237" List the rules in a chain or all chains\n"
238" --list-rules -S [chain [rulenum]]\n"
239" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700241" --zero -Z [chain [rulenum]]\n"
242" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000243" --new -N chain Create a new user-defined chain\n"
244" --delete-chain\n"
245" -X [chain] Delete a user-defined chain\n"
246" --policy -P chain target\n"
247" Change policy on chain to target\n"
248" --rename-chain\n"
249" -E old-chain new-chain\n"
250" Change chain name, (moving any references)\n"
251
252"Options:\n"
Maciej Żenczykowski57664122011-04-14 02:22:14 -0700253" --ipv4 -4 Error (line is ignored by ip6tables-restore)\n"
254" --ipv6 -6 Nothing (line is ignored by iptables-restore)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200255"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100256"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000257" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100258"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000259" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200260"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000261" network interface name ([+] for wildcard)\n"
262" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000263" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200264#ifdef IP6T_F_GOTO
265" --goto -g chain\n"
266" jump to chain with no return\n"
267#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000268" --match -m match\n"
269" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200271"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272" network interface name ([+] for wildcard)\n"
273" --table -t table table to manipulate (default: `filter')\n"
274" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000275" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000276" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000277/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000278" --modprobe=<command> try to insert modules using this command\n"
279" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280"[!] --version -V print package version.\n");
281
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200282 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283 exit(0);
284}
285
Patrick McHardy0b639362007-09-08 16:00:01 +0000286void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100287ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000288{
289 va_list args;
290
291 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500292 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000293 vfprintf(stderr, msg, args);
294 va_end(args);
295 fprintf(stderr, "\n");
296 if (status == PARAMETER_PROBLEM)
297 exit_tryhelp(status);
298 if (status == VERSION_PROBLEM)
299 fprintf(stderr,
300 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
301 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500302 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000303 exit(status);
304}
305
Rusty Russell5eed48a2000-06-02 20:12:24 +0000306static void
307generic_opt_check(int command, int options)
308{
309 int i, j, legal = 0;
310
311 /* Check that commands are valid with options. Complicated by the
312 * fact that if an option is legal with *any* command given, it is
313 * legal overall (ie. -z and -l).
314 */
315 for (i = 0; i < NUMBER_OF_OPT; i++) {
316 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
317
318 for (j = 0; j < NUMBER_OF_CMD; j++) {
319 if (!(command & (1<<j)))
320 continue;
321
322 if (!(options & (1<<i))) {
323 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100324 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000325 "You need to supply the `-%c' "
326 "option for this command\n",
327 optflags[i]);
328 } else {
329 if (commands_v_options[j][i] != 'x')
330 legal = 1;
331 else if (legal == 0)
332 legal = -1;
333 }
334 }
335 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100336 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000337 "Illegal option `-%c' with this command\n",
338 optflags[i]);
339 }
340}
341
342static char
343opt2char(int option)
344{
345 const char *ptr;
346 for (ptr = optflags; option > 1; option >>= 1, ptr++);
347
348 return *ptr;
349}
350
351static char
352cmd2char(int option)
353{
354 const char *ptr;
355 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
356
357 return *ptr;
358}
359
360static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000361add_command(unsigned int *cmd, const int newcmd, const int othercmds,
362 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000363{
364 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100365 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000366 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100367 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000368 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
369 *cmd |= newcmd;
370}
371
Rusty Russell5eed48a2000-06-02 20:12:24 +0000372/*
373 * All functions starting with "parse" should succeed, otherwise
374 * the program fails.
375 * Most routines return pointers to static data that may change
376 * between calls to the same or other routines with a few exceptions:
377 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
378 * return global static data.
379*/
380
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000381/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100382static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000383{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000384 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000385 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000386 proto == IPPROTO_AH ||
387 proto == IPPROTO_DSTOPTS);
388}
389
Rusty Russell5eed48a2000-06-02 20:12:24 +0000390/* Can't be zero. */
391static int
392parse_rulenumber(const char *rule)
393{
Harald Welte43ac1912002-03-03 09:43:07 +0000394 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000395
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100396 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100397 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000398 "Invalid rule number `%s'", rule);
399
400 return rulenum;
401}
402
403static const char *
404parse_target(const char *targetname)
405{
406 const char *ptr;
407
408 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100409 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000410 "Invalid target name (too short)");
411
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200412 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100413 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000414 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200415 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000416
417 for (ptr = targetname; *ptr; ptr++)
418 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100419 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000420 "Invalid target name `%s'", targetname);
421 return targetname;
422}
423
Rusty Russell5eed48a2000-06-02 20:12:24 +0000424static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100425set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426 int invert)
427{
428 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100429 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000430 opt2char(option));
431 *options |= option;
432
433 if (invert) {
434 unsigned int i;
435 for (i = 0; 1 << i != option; i++);
436
437 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100438 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000439 "cannot have ! before -%c",
440 opt2char(option));
441 *invflg |= inverse_for_options[i];
442 }
443}
444
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000445static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100446print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000447{
Harald Welte43ac1912002-03-03 09:43:07 +0000448 if (format & FMT_KILOMEGAGIGA) {
449 if (number > 99999) {
450 number = (number + 500) / 1000;
451 if (number > 9999) {
452 number = (number + 500) / 1000;
453 if (number > 9999) {
454 number = (number + 500) / 1000;
455 if (number > 9999) {
456 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000457 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000458 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000459 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000460 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000461 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000462 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000463 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000464 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000465 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000466 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000467 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000468}
469
Harald Welte43ac1912002-03-03 09:43:07 +0000470
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100472print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000473{
474 struct ip6t_counters counters;
475 const char *pol = ip6tc_get_policy(chain, &counters, handle);
476 printf("Chain %s", chain);
477 if (pol) {
478 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000479 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000480 fputc(' ', stdout);
481 print_num(counters.pcnt, (format|FMT_NOTABLE));
482 fputs("packets, ", stdout);
483 print_num(counters.bcnt, (format|FMT_NOTABLE));
484 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000485 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486 printf(")\n");
487 } else {
488 unsigned int refs;
489 if (!ip6tc_get_references(&refs, chain, handle))
490 printf(" (ERROR obtaining refs)\n");
491 else
492 printf(" (%u references)\n", refs);
493 }
494
495 if (format & FMT_LINENUMBERS)
496 printf(FMT("%-4s ", "%s "), "num");
497 if (!(format & FMT_NOCOUNTS)) {
498 if (format & FMT_KILOMEGAGIGA) {
499 printf(FMT("%5s ","%s "), "pkts");
500 printf(FMT("%5s ","%s "), "bytes");
501 } else {
502 printf(FMT("%8s ","%s "), "pkts");
503 printf(FMT("%10s ","%s "), "bytes");
504 }
505 }
506 if (!(format & FMT_NOTARGET))
507 printf(FMT("%-9s ","%s "), "target");
508 fputs(" prot ", stdout);
509 if (format & FMT_OPTIONS)
510 fputs("opt", stdout);
511 if (format & FMT_VIA) {
512 printf(FMT(" %-6s ","%s "), "in");
513 printf(FMT("%-6s ","%s "), "out");
514 }
515 printf(FMT(" %-19s ","%s "), "source");
516 printf(FMT(" %-19s "," %s "), "destination");
517 printf("\n");
518}
519
Harald Welte43ac1912002-03-03 09:43:07 +0000520
Rusty Russell5eed48a2000-06-02 20:12:24 +0000521static int
522print_match(const struct ip6t_entry_match *m,
523 const struct ip6t_ip6 *ip,
524 int numeric)
525{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100526 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100527 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000528
529 if (match) {
530 if (match->print)
531 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000532 else
533 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000534 } else {
535 if (m->u.user.name[0])
536 printf("UNKNOWN match `%s' ", m->u.user.name);
537 }
538 /* Don't stop iterating. */
539 return 0;
540}
541
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100542/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000543static void
544print_firewall(const struct ip6t_entry *fw,
545 const char *targname,
546 unsigned int num,
547 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100548 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000549{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100550 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100552 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553 char buf[BUFSIZ];
554
Rusty Russell5eed48a2000-06-02 20:12:24 +0000555 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100556 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000557 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100558 target = xtables_find_target(IP6T_STANDARD_TARGET,
559 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000560
561 t = ip6t_get_target((struct ip6t_entry *)fw);
562 flags = fw->ipv6.flags;
563
564 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200565 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000566
567 if (!(format & FMT_NOCOUNTS)) {
568 print_num(fw->counters.pcnt, format);
569 print_num(fw->counters.bcnt, format);
570 }
571
572 if (!(format & FMT_NOTARGET))
573 printf(FMT("%-9s ", "%s "), targname);
574
575 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
576 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100577 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578 if (pname)
579 printf(FMT("%-5s", "%s "), pname);
580 else
581 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
582 }
583
584 if (format & FMT_OPTIONS) {
585 if (format & FMT_NOTABLE)
586 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000587 fputc(' ', stdout); /* Invert flag of FRAG */
588 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000589 fputc(' ', stdout);
590 }
591
592 if (format & FMT_VIA) {
593 char iface[IFNAMSIZ+2];
594
595 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
596 iface[0] = '!';
597 iface[1] = '\0';
598 }
599 else iface[0] = '\0';
600
601 if (fw->ipv6.iniface[0] != '\0') {
602 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000603 }
604 else if (format & FMT_NUMERIC) strcat(iface, "*");
605 else strcat(iface, "any");
606 printf(FMT(" %-6s ","in %s "), iface);
607
608 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
609 iface[0] = '!';
610 iface[1] = '\0';
611 }
612 else iface[0] = '\0';
613
614 if (fw->ipv6.outiface[0] != '\0') {
615 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000616 }
617 else if (format & FMT_NUMERIC) strcat(iface, "*");
618 else strcat(iface, "any");
619 printf(FMT("%-6s ","out %s "), iface);
620 }
621
622 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000623 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000624 && !(format & FMT_NUMERIC))
625 printf(FMT("%-19s ","%s "), "anywhere");
626 else {
627 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100628 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000629 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100630 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
631 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000632 printf(FMT("%-19s ","%s "), buf);
633 }
634
635 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
636 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
637 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200638 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000639 else {
640 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100641 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000642 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100643 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
644 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200645 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000646 }
647
648 if (format & FMT_NOTABLE)
649 fputs(" ", stdout);
650
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200651#ifdef IP6T_F_GOTO
652 if(fw->ipv6.flags & IP6T_F_GOTO)
653 printf("[goto] ");
654#endif
655
Rusty Russell5eed48a2000-06-02 20:12:24 +0000656 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
657
658 if (target) {
659 if (target->print)
660 /* Print the target information. */
661 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
662 } else if (t->u.target_size != sizeof(*t))
663 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000664 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000665
666 if (!(format & FMT_NONEWLINE))
667 fputc('\n', stdout);
668}
669
670static void
671print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100672 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000673{
674 struct ip6t_entry_target *t;
675
676 t = ip6t_get_target((struct ip6t_entry *)fw);
677 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
678}
679
680static int
681append_entry(const ip6t_chainlabel chain,
682 struct ip6t_entry *fw,
683 unsigned int nsaddrs,
684 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100685 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000686 unsigned int ndaddrs,
687 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100688 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100690 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000691{
692 unsigned int i, j;
693 int ret = 1;
694
695 for (i = 0; i < nsaddrs; i++) {
696 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100697 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000698 for (j = 0; j < ndaddrs; j++) {
699 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100700 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100702 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 ret &= ip6tc_append_entry(chain, fw, handle);
704 }
705 }
706
707 return ret;
708}
709
710static int
711replace_entry(const ip6t_chainlabel chain,
712 struct ip6t_entry *fw,
713 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100714 const struct in6_addr *saddr, const struct in6_addr *smask,
715 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100717 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000718{
719 fw->ipv6.src = *saddr;
720 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100721 fw->ipv6.smsk = *smask;
722 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000723
724 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100725 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000726 return ip6tc_replace_entry(chain, fw, rulenum, handle);
727}
728
729static int
730insert_entry(const ip6t_chainlabel chain,
731 struct ip6t_entry *fw,
732 unsigned int rulenum,
733 unsigned int nsaddrs,
734 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100735 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 unsigned int ndaddrs,
737 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100738 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100740 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000741{
742 unsigned int i, j;
743 int ret = 1;
744
745 for (i = 0; i < nsaddrs; i++) {
746 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100747 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748 for (j = 0; j < ndaddrs; j++) {
749 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100750 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100752 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000753 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
754 }
755 }
756
757 return ret;
758}
759
760static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100761make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100762 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000763{
764 /* Establish mask for comparison */
765 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100766 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000767 unsigned char *mask, *mptr;
768
769 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000770 for (matchp = matches; matchp; matchp = matchp->next)
771 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000772
Jan Engelhardt630ef482009-01-27 14:58:41 +0100773 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000774 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100775 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000776
777 memset(mask, 0xFF, sizeof(struct ip6t_entry));
778 mptr = mask + sizeof(struct ip6t_entry);
779
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000780 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000782 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000783 + matchp->match->userspacesize);
784 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000785 }
786
Max Kellermann5b76f682008-01-29 13:42:48 +0000787 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000788 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100789 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790
791 return mask;
792}
793
794static int
795delete_entry(const ip6t_chainlabel chain,
796 struct ip6t_entry *fw,
797 unsigned int nsaddrs,
798 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100799 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 unsigned int ndaddrs,
801 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100802 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100804 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100805 struct xtables_rule_match *matches,
806 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000807{
808 unsigned int i, j;
809 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000810 unsigned char *mask;
811
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100812 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000814 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100815 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000816 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000817 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100818 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000819 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100820 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000821 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 }
823 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000824 free(mask);
825
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826 return ret;
827}
828
Stefan Tomanekd59b9db2011-03-08 22:42:51 +0100829static int
830check_entry(const ip6t_chainlabel chain, struct ip6t_entry *fw,
831 unsigned int nsaddrs, const struct in6_addr *saddrs,
832 const struct in6_addr *smasks, unsigned int ndaddrs,
833 const struct in6_addr *daddrs, const struct in6_addr *dmasks,
834 bool verbose, struct ip6tc_handle *handle,
835 struct xtables_rule_match *matches,
836 const struct xtables_target *target)
837{
838 unsigned int i, j;
839 int ret = 1;
840 unsigned char *mask;
841
842 mask = make_delete_mask(matches, target);
843 for (i = 0; i < nsaddrs; i++) {
844 fw->ipv6.src = saddrs[i];
845 fw->ipv6.smsk = smasks[i];
846 for (j = 0; j < ndaddrs; j++) {
847 fw->ipv6.dst = daddrs[j];
848 fw->ipv6.dmsk = dmasks[j];
849 if (verbose)
850 print_firewall_line(fw, handle);
851 ret &= ip6tc_check_entry(chain, fw, mask, handle);
852 }
853 }
854
855 free(mask);
856 return ret;
857}
858
András Kis-Szabó764316a2001-02-26 17:31:20 +0000859int
Maciej Zenczykowski241e7352011-04-04 15:34:54 +0200860for_each_chain6(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100861 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862{
Max Kellermann5b76f682008-01-29 13:42:48 +0000863 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864 const char *chain;
865 char *chains;
866 unsigned int i, chaincount = 0;
867
868 chain = ip6tc_first_chain(handle);
869 while (chain) {
870 chaincount++;
871 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000872 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873
Jan Engelhardt630ef482009-01-27 14:58:41 +0100874 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875 i = 0;
876 chain = ip6tc_first_chain(handle);
877 while (chain) {
878 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
879 i++;
880 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000881 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000882
883 for (i = 0; i < chaincount; i++) {
884 if (!builtinstoo
885 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100886 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000887 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000888 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889 }
890
891 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000892 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000893}
894
András Kis-Szabó764316a2001-02-26 17:31:20 +0000895int
Maciej Zenczykowski74ace0a2011-04-04 15:35:47 +0200896flush_entries6(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100897 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898{
899 if (!chain)
Maciej Zenczykowski74ace0a2011-04-04 15:35:47 +0200900 return for_each_chain6(flush_entries6, verbose, 1, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000901
902 if (verbose)
903 fprintf(stdout, "Flushing chain `%s'\n", chain);
904 return ip6tc_flush_entries(chain, handle);
905}
906
907static int
908zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100909 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000910{
911 if (!chain)
Maciej Zenczykowski241e7352011-04-04 15:34:54 +0200912 return for_each_chain6(zero_entries, verbose, 1, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000913
914 if (verbose)
915 fprintf(stdout, "Zeroing chain `%s'\n", chain);
916 return ip6tc_zero_entries(chain, handle);
917}
918
András Kis-Szabó764316a2001-02-26 17:31:20 +0000919int
Maciej Zenczykowski85aae152011-04-04 15:36:45 +0200920delete_chain6(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100921 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000922{
923 if (!chain)
Maciej Zenczykowski85aae152011-04-04 15:36:45 +0200924 return for_each_chain6(delete_chain6, verbose, 0, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000925
926 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000927 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000928 return ip6tc_delete_chain(chain, handle);
929}
930
931static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200932list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100933 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000934{
935 int found = 0;
936 unsigned int format;
937 const char *this;
938
939 format = FMT_OPTIONS;
940 if (!verbose)
941 format |= FMT_NOCOUNTS;
942 else
943 format |= FMT_VIA;
944
945 if (numeric)
946 format |= FMT_NUMERIC;
947
948 if (!expanded)
949 format |= FMT_KILOMEGAGIGA;
950
951 if (linenumbers)
952 format |= FMT_LINENUMBERS;
953
954 for (this = ip6tc_first_chain(handle);
955 this;
956 this = ip6tc_next_chain(handle)) {
957 const struct ip6t_entry *i;
958 unsigned int num;
959
960 if (chain && strcmp(chain, this) != 0)
961 continue;
962
963 if (found) printf("\n");
964
Henrik Nordstrombb340822008-05-13 13:09:23 +0200965 if (!rulenum)
966 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000967 i = ip6tc_first_rule(this, handle);
968
969 num = 0;
970 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200971 num++;
972 if (!rulenum || num == rulenum)
973 print_firewall(i,
974 ip6tc_get_target(i, handle),
975 num,
976 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100977 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 i = ip6tc_next_rule(i, handle);
979 }
980 found = 1;
981 }
982
983 errno = ENOENT;
984 return found;
985}
986
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200987/* This assumes that mask is contiguous, and byte-bounded. */
988static void
989print_iface(char letter, const char *iface, const unsigned char *mask,
990 int invert)
991{
992 unsigned int i;
993
994 if (mask[0] == 0)
995 return;
996
Jan Engelhardtaa66aed2011-02-16 02:41:22 +0100997 printf("%s -%c ", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200998
999 for (i = 0; i < IFNAMSIZ; i++) {
1000 if (mask[i] != 0) {
1001 if (iface[i] != '\0')
1002 printf("%c", iface[i]);
1003 } else {
1004 /* we can access iface[i-1] here, because
1005 * a few lines above we make sure that mask[0] != 0 */
1006 if (iface[i-1] != '\0')
1007 printf("+");
1008 break;
1009 }
1010 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011}
1012
1013/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001014static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001015{
1016 if (proto) {
1017 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +01001018 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001019
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001020 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001021 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001022 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001023 invertstr, pent->p_name);
1024 return;
1025 }
1026
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001027 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1028 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001029 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001030 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001031 return;
1032 }
1033
Jan Engelhardt73866352010-12-18 02:04:59 +01001034 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001035 }
1036}
1037
1038static int print_match_save(const struct ip6t_entry_match *e,
1039 const struct ip6t_ip6 *ip)
1040{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001041 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001042 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001043
1044 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001045 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001046
1047 /* some matches don't provide a save function */
1048 if (match->save)
1049 match->save(ip, e);
1050 } else {
1051 if (e->u.match_size) {
1052 fprintf(stderr,
1053 "Can't find library for match `%s'\n",
1054 e->u.user.name);
1055 exit(1);
1056 }
1057 }
1058 return 0;
1059}
1060
1061/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001062static void print_ip(const char *prefix, const struct in6_addr *ip,
1063 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001064{
1065 char buf[51];
1066 int l = ipv6_prefix_length(mask);
1067
1068 if (l == 0 && !invert)
1069 return;
1070
Jan Engelhardt73866352010-12-18 02:04:59 +01001071 printf("%s %s %s",
1072 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001073 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001074 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1075
1076 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001077 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001078 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001079 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001080}
1081
1082/* We want this to be readable, so only print out neccessary fields.
1083 * Because that's the kind of world I want to live in. */
Maciej Zenczykowski9680f2e2011-04-04 15:37:43 +02001084void print_rule6(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001085 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001086{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001087 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001088 const char *target_name;
1089
1090 /* print counters for iptables-save */
1091 if (counters > 0)
1092 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1093
1094 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001095 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001096
1097 /* Print IP part. */
1098 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1099 e->ipv6.invflags & IP6T_INV_SRCIP);
1100
1101 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1102 e->ipv6.invflags & IP6T_INV_DSTIP);
1103
1104 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1105 e->ipv6.invflags & IP6T_INV_VIA_IN);
1106
1107 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1108 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1109
1110 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1111
1112#if 0
1113 /* not definied in ipv6
1114 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1115 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001116 printf("%s -f",
1117 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001118#endif
1119
1120 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001121 printf("%s -? %d",
1122 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001123 e->ipv6.tos);
1124
1125 /* Print matchinfo part */
1126 if (e->target_offset) {
1127 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1128 }
1129
1130 /* print counters for iptables -R */
1131 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001132 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001133
1134 /* Print target name */
1135 target_name = ip6tc_get_target(e, h);
1136 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001137#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001138 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001139#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001140 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001141#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001142
1143 /* Print targinfo part */
1144 t = ip6t_get_target((struct ip6t_entry *)e);
1145 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001146 struct xtables_target *target =
1147 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001148
1149 if (!target) {
1150 fprintf(stderr, "Can't find library for target `%s'\n",
1151 t->u.user.name);
1152 exit(1);
1153 }
1154
1155 if (target->save)
1156 target->save(&e->ipv6, t);
1157 else {
1158 /* If the target size is greater than ip6t_entry_target
1159 * there is something to be saved, we just don't know
1160 * how to print it */
1161 if (t->u.target_size !=
1162 sizeof(struct ip6t_entry_target)) {
1163 fprintf(stderr, "Target `%s' is missing "
1164 "save function\n",
1165 t->u.user.name);
1166 exit(1);
1167 }
1168 }
1169 }
1170 printf("\n");
1171}
1172
1173static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001174list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001175 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001176{
1177 const char *this = NULL;
1178 int found = 0;
1179
1180 if (counters)
1181 counters = -1; /* iptables -c format */
1182
1183 /* Dump out chain names first,
1184 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001185 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001186 this;
1187 this = ip6tc_next_chain(handle)) {
1188 if (chain && strcmp(this, chain) != 0)
1189 continue;
1190
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001191 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001192 struct ip6t_counters count;
1193 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1194 if (counters)
1195 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1196 printf("\n");
1197 } else {
1198 printf("-N %s\n", this);
1199 }
1200 }
1201
1202 for (this = ip6tc_first_chain(handle);
1203 this;
1204 this = ip6tc_next_chain(handle)) {
1205 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001206 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001207
1208 if (chain && strcmp(this, chain) != 0)
1209 continue;
1210
1211 /* Dump out rules */
1212 e = ip6tc_first_rule(this, handle);
1213 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001214 num++;
1215 if (!rulenum || num == rulenum)
Maciej Zenczykowski9680f2e2011-04-04 15:37:43 +02001216 print_rule6(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001217 e = ip6tc_next_rule(e, handle);
1218 }
1219 found = 1;
1220 }
1221
1222 errno = ENOENT;
1223 return found;
1224}
1225
Rusty Russell5eed48a2000-06-02 20:12:24 +00001226static struct ip6t_entry *
1227generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001228 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001229 struct ip6t_entry_target *target)
1230{
1231 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001232 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001233 struct ip6t_entry *e;
1234
1235 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001236 for (matchp = matches; matchp; matchp = matchp->next)
1237 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001238
Jan Engelhardt630ef482009-01-27 14:58:41 +01001239 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 *e = *fw;
1241 e->target_offset = size;
1242 e->next_offset = size + target->u.target_size;
1243
1244 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001245 for (matchp = matches; matchp; matchp = matchp->next) {
1246 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1247 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001248 }
1249 memcpy(e->elems + size, target, target->u.target_size);
1250
1251 return e;
1252}
1253
Jan Engelhardt395e4412009-02-10 10:43:08 +01001254static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001255{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001256 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001257
1258 for (matchp = *matches; matchp;) {
1259 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001260 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001261 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001262 matchp->match->m = NULL;
1263 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001264 if (matchp->match == matchp->match->next) {
1265 free(matchp->match);
1266 matchp->match = NULL;
1267 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001268 free(matchp);
1269 matchp = tmp;
1270 }
1271
1272 *matches = NULL;
1273}
1274
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001275static void command_default(struct iptables_command_state *cs)
1276{
1277 struct xtables_rule_match *matchp;
1278 struct xtables_match *m;
1279
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001280 if (cs->target != NULL &&
1281 (cs->target->parse != NULL || cs->target->x6_parse != NULL) &&
Jan Engelhardtaf3d73e2011-02-11 01:45:26 +01001282 cs->c >= cs->target->option_offset &&
1283 cs->c < cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001284 xtables_option_tpcall(cs->c, cs->argv, cs->invert,
1285 cs->target, &cs->fw);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001286 return;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001287 }
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001288
1289 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1290 m = matchp->match;
1291
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001292 if (matchp->completed ||
1293 (m->x6_parse == NULL && m->parse == NULL))
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001294 continue;
1295 if (cs->c < matchp->match->option_offset ||
1296 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1297 continue;
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001298 xtables_option_mpcall(cs->c, cs->argv, cs->invert, m, &cs->fw);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001299 return;
1300 }
1301
1302 /* Try loading protocol */
1303 m = load_proto(cs);
1304 if (m != NULL) {
1305 size_t size;
1306
1307 cs->proto_used = 1;
1308
1309 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1310
1311 m->m = xtables_calloc(1, size);
1312 m->m->u.match_size = size;
1313 strcpy(m->m->u.user.name, m->name);
1314 m->m->u.user.revision = m->revision;
1315 if (m->init != NULL)
1316 m->init(m->m);
1317
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001318 if (m->x6_options != NULL)
1319 opts = xtables_options_xfrm(ip6tables_globals.orig_opts,
1320 opts, m->x6_options,
1321 &m->option_offset);
1322 else
1323 opts = xtables_merge_options(ip6tables_globals.orig_opts,
1324 opts,
1325 m->extra_opts,
1326 &m->option_offset);
1327 if (opts == NULL)
1328 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001329 optind--;
1330 return;
1331 }
1332
Jan Engelhardt58b491f2011-02-07 03:45:26 +01001333 if (cs->c == ':')
1334 xtables_error(PARAMETER_PROBLEM, "option \"%s\" "
1335 "requires an argument", cs->argv[optind-1]);
1336 if (cs->c == '?')
1337 xtables_error(PARAMETER_PROBLEM, "unknown option "
1338 "\"%s\"", cs->argv[optind-1]);
Jan Engelhardt94e247b2011-02-07 03:20:02 +01001339 xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001340}
1341
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001342static void command_jump(struct iptables_command_state *cs)
1343{
1344 size_t size;
1345
1346 set_option(&cs->options, OPT_JUMP, &cs->fw6.ipv6.invflags, cs->invert);
1347 cs->jumpto = parse_target(optarg);
1348 /* TRY_LOAD (may be chain name) */
1349 cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1350
1351 if (cs->target == NULL)
1352 return;
1353
1354 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)) + cs->target->size;
1355
1356 cs->target->t = xtables_calloc(1, size);
1357 cs->target->t->u.target_size = size;
1358 strcpy(cs->target->t->u.user.name, cs->jumpto);
1359 cs->target->t->u.user.revision = cs->target->revision;
1360 if (cs->target->init != NULL)
1361 cs->target->init(cs->target->t);
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001362 if (cs->target->x6_options != NULL)
1363 opts = xtables_options_xfrm(ip6tables_globals.orig_opts, opts,
1364 cs->target->x6_options,
1365 &cs->target->option_offset);
1366 else
1367 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1368 cs->target->extra_opts,
1369 &cs->target->option_offset);
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001370 if (opts == NULL)
1371 xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1372}
1373
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001374static void command_match(struct iptables_command_state *cs)
1375{
1376 struct xtables_match *m;
1377 size_t size;
1378
1379 if (cs->invert)
1380 xtables_error(PARAMETER_PROBLEM,
1381 "unexpected ! flag before --match");
1382
1383 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1384 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1385 m->m = xtables_calloc(1, size);
1386 m->m->u.match_size = size;
1387 strcpy(m->m->u.user.name, m->name);
1388 m->m->u.user.revision = m->revision;
1389 if (m->init != NULL)
1390 m->init(m->m);
Jan Engelhardtaa37acc2011-02-07 04:00:50 +01001391 if (m == m->next)
1392 return;
1393 /* Merge options for non-cloned matches */
1394 if (m->x6_options != NULL)
1395 opts = xtables_options_xfrm(ip6tables_globals.orig_opts, opts,
1396 m->x6_options, &m->option_offset);
1397 else if (m->extra_opts != NULL)
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001398 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1399 m->extra_opts, &m->option_offset);
1400}
1401
Jan Engelhardtfd187312008-11-10 16:59:27 +01001402int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001403{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001404 struct iptables_command_state cs;
1405 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001406 unsigned int nsaddrs = 0, ndaddrs = 0;
1407 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001408 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001409
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001410 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001411 const char *chain = NULL;
1412 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1413 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001414 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001415 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001417 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001418 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001419 struct xtables_target *t;
Patrick McHardy875441e2007-10-17 08:48:58 +00001420 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001421
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001422 memset(&cs, 0, sizeof(cs));
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001423 cs.jumpto = "";
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001424 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001425
Maciej Zenczykowski742e9a42011-04-04 15:38:44 +02001426 /* re-set optind to 0 in case do_command6 gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001427 * a second time */
1428 optind = 0;
1429
Maciej Zenczykowski742e9a42011-04-04 15:38:44 +02001430 /* clear mflags in case do_command6 gets called a second time
Harald Welte43ac1912002-03-03 09:43:07 +00001431 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001432 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001433 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001434
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001435 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001436 t->tflags = 0;
1437 t->used = 0;
1438 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001439
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440 /* Suppress error messages: we may add new options if we
1441 demand-load a protocol. */
1442 opterr = 0;
1443
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001444 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001445 while ((cs.c = getopt_long(argc, argv,
Maciej Żenczykowski57664122011-04-14 02:22:14 -07001446 "-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:46",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001447 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001448 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001449 /*
1450 * Command selection
1451 */
1452 case 'A':
1453 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001454 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001455 chain = optarg;
1456 break;
1457
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001458 case 'C':
1459 add_command(&command, CMD_CHECK, CMD_NONE,
1460 cs.invert);
1461 chain = optarg;
1462 break;
1463
Rusty Russell5eed48a2000-06-02 20:12:24 +00001464 case 'D':
1465 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001466 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001467 chain = optarg;
1468 if (optind < argc && argv[optind][0] != '-'
1469 && argv[optind][0] != '!') {
1470 rulenum = parse_rulenumber(argv[optind++]);
1471 command = CMD_DELETE_NUM;
1472 }
1473 break;
1474
Rusty Russell5eed48a2000-06-02 20:12:24 +00001475 case 'R':
1476 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001477 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001478 chain = optarg;
1479 if (optind < argc && argv[optind][0] != '-'
1480 && argv[optind][0] != '!')
1481 rulenum = parse_rulenumber(argv[optind++]);
1482 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001483 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001484 "-%c requires a rule number",
1485 cmd2char(CMD_REPLACE));
1486 break;
1487
1488 case 'I':
1489 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001490 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001491 chain = optarg;
1492 if (optind < argc && argv[optind][0] != '-'
1493 && argv[optind][0] != '!')
1494 rulenum = parse_rulenumber(argv[optind++]);
1495 else rulenum = 1;
1496 break;
1497
1498 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001499 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001500 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001501 if (optarg) chain = optarg;
1502 else if (optind < argc && argv[optind][0] != '-'
1503 && argv[optind][0] != '!')
1504 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001505 if (optind < argc && argv[optind][0] != '-'
1506 && argv[optind][0] != '!')
1507 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001508 break;
1509
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001510 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001511 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001512 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001513 if (optarg) chain = optarg;
1514 else if (optind < argc && argv[optind][0] != '-'
1515 && argv[optind][0] != '!')
1516 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001517 if (optind < argc && argv[optind][0] != '-'
1518 && argv[optind][0] != '!')
1519 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001520 break;
1521
Rusty Russell5eed48a2000-06-02 20:12:24 +00001522 case 'F':
1523 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001524 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001525 if (optarg) chain = optarg;
1526 else if (optind < argc && argv[optind][0] != '-'
1527 && argv[optind][0] != '!')
1528 chain = argv[optind++];
1529 break;
1530
1531 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001532 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001533 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 if (optarg) chain = optarg;
1535 else if (optind < argc && argv[optind][0] != '-'
1536 && argv[optind][0] != '!')
1537 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001538 if (optind < argc && argv[optind][0] != '-'
1539 && argv[optind][0] != '!') {
1540 rulenum = parse_rulenumber(argv[optind++]);
1541 command = CMD_ZERO_NUM;
1542 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001543 break;
1544
1545 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001546 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001547 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001548 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001549 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001550 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001551 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001552 "chain name may not clash "
1553 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001554 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001555 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556 chain = optarg;
1557 break;
1558
1559 case 'X':
1560 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001561 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001562 if (optarg) chain = optarg;
1563 else if (optind < argc && argv[optind][0] != '-'
1564 && argv[optind][0] != '!')
1565 chain = argv[optind++];
1566 break;
1567
1568 case 'E':
1569 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001570 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001571 chain = optarg;
1572 if (optind < argc && argv[optind][0] != '-'
1573 && argv[optind][0] != '!')
1574 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001575 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001576 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001577 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001578 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001579 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001580 break;
1581
1582 case 'P':
1583 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001584 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001585 chain = optarg;
1586 if (optind < argc && argv[optind][0] != '-'
1587 && argv[optind][0] != '!')
1588 policy = argv[optind++];
1589 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001590 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001591 "-%c requires a chain and a policy",
1592 cmd2char(CMD_SET_POLICY));
1593 break;
1594
1595 case 'h':
1596 if (!optarg)
1597 optarg = argv[optind];
1598
Jonas Berlin1b91e592005-04-01 06:58:38 +00001599 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001600 if (!cs.matches && cs.protocol)
1601 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1602 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001603
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001604 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605
1606 /*
1607 * Option selection
1608 */
1609 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001610 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001611 set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001612 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001613
1614 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001615 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1616 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001618 cs.protocol = optarg;
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001619 cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1620 cs.fw6.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001621
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001622 if (cs.fw6.ipv6.proto == 0
1623 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001624 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001626
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001627 if (is_exthdr(cs.fw6.ipv6.proto)
1628 && (cs.fw6.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001629 fprintf(stderr,
1630 "Warning: never matched protocol: %s. "
1631 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001632 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001633 break;
1634
1635 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001636 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001637 set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001638 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001639 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001640 break;
1641
1642 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001643 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001644 set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001645 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001646 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001647 break;
1648
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001649#ifdef IP6T_F_GOTO
1650 case 'g':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001651 set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001652 cs.invert);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001653 cs.fw6.ipv6.flags |= IP6T_F_GOTO;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001654 cs.jumpto = parse_target(optarg);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001655 break;
1656#endif
1657
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 case 'j':
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001659 command_jump(&cs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 break;
1661
1662
1663 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001664 if (*optarg == '\0')
1665 xtables_error(PARAMETER_PROBLEM,
1666 "Empty interface is likely to be "
1667 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001668 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001669 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001670 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001671 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001672 cs.fw6.ipv6.iniface,
1673 cs.fw6.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001674 break;
1675
1676 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001677 if (*optarg == '\0')
1678 xtables_error(PARAMETER_PROBLEM,
1679 "Empty interface is likely to be "
1680 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001681 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001682 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001683 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001684 xtables_parse_interface(optarg,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001685 cs.fw6.ipv6.outiface,
1686 cs.fw6.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001687 break;
1688
1689 case 'v':
1690 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001691 set_option(&cs.options, OPT_VERBOSE,
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001692 &cs.fw6.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693 verbose++;
1694 break;
1695
Jan Engelhardt17e310b2011-02-07 03:42:47 +01001696 case 'm':
1697 command_match(&cs);
1698 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699
1700 case 'n':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001701 set_option(&cs.options, OPT_NUMERIC, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001702 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001703 break;
1704
1705 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001706 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001707 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001708 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001709 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 break;
1711
1712 case 'x':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001713 set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001714 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 break;
1716
1717 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001718 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001719 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001720 else
1721 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001722 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001723 exit(0);
1724
1725 case '0':
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001726 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001727 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001728 break;
1729
Harald Welte43ac1912002-03-03 09:43:07 +00001730 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001731 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001732 break;
1733
1734 case 'c':
1735
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001736 set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001737 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001738 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001739 bcnt = strchr(pcnt + 1, ',');
1740 if (bcnt)
1741 bcnt++;
1742 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001743 && argv[optind][0] != '!')
1744 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001745 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001746 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001747 "-%c requires packet and byte counter",
1748 opt2char(OPT_COUNTERS));
1749
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001750 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001751 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001752 "-%c packet counter not numeric",
1753 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001754 cs.fw6.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001755
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001756 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001757 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001758 "-%c byte counter not numeric",
1759 opt2char(OPT_COUNTERS));
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001760 cs.fw6.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001761 break;
1762
Maciej Żenczykowski57664122011-04-14 02:22:14 -07001763 case '4':
1764 /* This is not the IPv4 iptables */
1765 if (line != -1)
1766 return 1; /* success: line ignored */
1767 fprintf(stderr, "This is the IPv6 version of ip6tables.\n");
1768 exit_tryhelp(2);
1769
1770 case '6':
1771 /* This is indeed the IPv6 ip6tables */
1772 break;
1773
Rusty Russell5eed48a2000-06-02 20:12:24 +00001774 case 1: /* non option */
1775 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001776 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001777 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001778 "multiple consecutive ! not"
1779 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001780 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001781 optarg[0] = '\0';
1782 continue;
1783 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001784 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001785 exit_tryhelp(2);
1786
1787 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001788 command_default(&cs);
1789 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001790 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001791 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001792 }
1793
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001794 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt3af739b2011-02-10 16:57:37 +01001795 xtables_option_mfcall(matchp->match);
1796 if (cs.target != NULL)
1797 xtables_option_tfcall(cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001798
1799 /* Fix me: must put inverse options checking here --MN */
1800
1801 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001802 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001803 "unknown arguments found on commandline");
1804 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001805 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001806 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001807 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001808 "nothing appropriate following !");
1809
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001810 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND | CMD_CHECK)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001811 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001812 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001813 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001814 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815 }
1816
1817 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001818 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1819 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001820
1821 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001822 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1823 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001824
1825 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001826 (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001827 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001828 " source or destination IP addresses");
1829
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001831 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 "specify a unique address");
1833
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001834 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835
Jan Engelhardt5429b412010-09-13 15:45:15 +02001836 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001837 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001838 "chain name `%s' too long (must be under %u chars)",
1839 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001840
Harald Welte43ac1912002-03-03 09:43:07 +00001841 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001842 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001843 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001844
Rusty Russell8beb0492004-12-22 00:37:10 +00001845 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001846 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001847 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001848
Harald Welte43ac1912002-03-03 09:43:07 +00001849 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001850 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001851 "can't initialize ip6tables table `%s': %s",
1852 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001853
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001854 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001855 || command == CMD_DELETE
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001856 || command == CMD_CHECK
Rusty Russell5eed48a2000-06-02 20:12:24 +00001857 || command == CMD_INSERT
1858 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001859 if (strcmp(chain, "PREROUTING") == 0
1860 || strcmp(chain, "INPUT") == 0) {
1861 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001862 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001863 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001864 "Can't use -%c with %s\n",
1865 opt2char(OPT_VIANAMEOUT),
1866 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001867 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001868
Harald Welte43ac1912002-03-03 09:43:07 +00001869 if (strcmp(chain, "POSTROUTING") == 0
1870 || strcmp(chain, "OUTPUT") == 0) {
1871 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001872 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001873 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001874 "Can't use -%c with %s\n",
1875 opt2char(OPT_VIANAMEIN),
1876 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001877 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001879 if (cs.target && ip6tc_is_chain(cs.jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001880 fprintf(stderr,
1881 "Warning: using chain %s, not extension\n",
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001882 cs.jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001883
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001884 if (cs.target->t)
1885 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001886
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001887 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001888 }
1889
1890 /* If they didn't specify a target, or it's a chain
1891 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001892 if (!cs.target
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001893 && (strlen(cs.jumpto) == 0
1894 || ip6tc_is_chain(cs.jumpto, *handle))) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001895 size_t size;
1896
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001897 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001898 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899
1900 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001901 + cs.target->size;
1902 cs.target->t = xtables_calloc(1, size);
1903 cs.target->t->u.target_size = size;
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001904 strcpy(cs.target->t->u.user.name, cs.jumpto);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001905 if (cs.target->init != NULL)
1906 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001907 }
1908
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001909 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001910 /* it is no chain, and we can't load a plugin.
1911 * We cannot know if the plugin is corrupt, non
1912 * existant OR if the user just misspelled a
1913 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001914#ifdef IP6T_F_GOTO
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001915 if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001916 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001917 "goto '%s' is not a chain\n",
1918 cs.jumpto);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001919#endif
Jan Engelhardt9bb76092011-02-07 03:39:36 +01001920 xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 } else {
Jan Engelhardt7a548b32011-02-07 00:00:42 +01001922 e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001923 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001924 }
1925 }
1926
1927 switch (command) {
1928 case CMD_APPEND:
1929 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001930 nsaddrs, saddrs, smasks,
1931 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001932 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001933 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001934 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935 case CMD_DELETE:
1936 ret = delete_entry(chain, e,
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,
1940 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 break;
1942 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001943 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001944 break;
Stefan Tomanekd59b9db2011-03-08 22:42:51 +01001945 case CMD_CHECK:
1946 ret = check_entry(chain, e,
1947 nsaddrs, saddrs, smasks,
1948 ndaddrs, daddrs, dmasks,
1949 cs.options&OPT_VERBOSE,
1950 *handle, cs.matches, cs.target);
1951 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 case CMD_REPLACE:
1953 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001954 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001955 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001956 break;
1957 case CMD_INSERT:
1958 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001959 nsaddrs, saddrs, smasks,
1960 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001961 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001962 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001964 case CMD_FLUSH:
Maciej Zenczykowski74ace0a2011-04-04 15:35:47 +02001965 ret = flush_entries6(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001966 break;
1967 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001968 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001969 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001970 case CMD_ZERO_NUM:
1971 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1972 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001973 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001974 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001975 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001976 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001977 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001978 cs.options&OPT_VERBOSE,
1979 cs.options&OPT_NUMERIC,
1980 cs.options&OPT_EXPANDED,
1981 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001982 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001983 if (ret && (command & CMD_ZERO))
1984 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001985 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001986 if (ret && (command & CMD_ZERO_NUM))
1987 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001988 break;
1989 case CMD_LIST_RULES:
1990 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001991 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001992 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001993 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001994 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001995 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001996 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001997 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001998 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001999 if (ret && (command & CMD_ZERO_NUM))
2000 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 break;
2002 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002003 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002004 break;
2005 case CMD_DELETE_CHAIN:
Maciej Zenczykowski85aae152011-04-04 15:36:45 +02002006 ret = delete_chain6(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007 break;
2008 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002009 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002010 break;
2011 case CMD_SET_POLICY:
Jan Engelhardt7a548b32011-02-07 00:00:42 +01002012 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002013 break;
2014 default:
2015 /* We should never reach this... */
2016 exit_tryhelp(2);
2017 }
2018
2019 if (verbose > 1)
2020 dump_entries6(*handle);
2021
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002022 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002023
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002024 if (e != NULL) {
2025 free(e);
2026 e = NULL;
2027 }
2028
Michael Granzow332e4ac2009-04-09 18:24:36 +01002029 free(saddrs);
2030 free(smasks);
2031 free(daddrs);
2032 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002033 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002034
Rusty Russell5eed48a2000-06-02 20:12:24 +00002035 return ret;
2036}