blob: 947f3c9b1062147ef762769a5643f982a49a388f [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
85#define NUMBER_OF_CMD 15
Rusty Russell5eed48a2000-06-02 20:12:24 +000086static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070087 'Z', 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
Rusty Russell5eed48a2000-06-02 20:12:24 +000089#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100106 {.name = "append", .has_arg = 1, .val = 'A'},
107 {.name = "delete", .has_arg = 1, .val = 'D'},
108 {.name = "insert", .has_arg = 1, .val = 'I'},
109 {.name = "replace", .has_arg = 1, .val = 'R'},
110 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200111 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100112 {.name = "flush", .has_arg = 2, .val = 'F'},
113 {.name = "zero", .has_arg = 2, .val = 'Z'},
114 {.name = "new-chain", .has_arg = 1, .val = 'N'},
115 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
116 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
117 {.name = "policy", .has_arg = 1, .val = 'P'},
118 {.name = "source", .has_arg = 1, .val = 's'},
119 {.name = "destination", .has_arg = 1, .val = 'd'},
120 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
121 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
122 {.name = "protocol", .has_arg = 1, .val = 'p'},
123 {.name = "in-interface", .has_arg = 1, .val = 'i'},
124 {.name = "jump", .has_arg = 1, .val = 'j'},
125 {.name = "table", .has_arg = 1, .val = 't'},
126 {.name = "match", .has_arg = 1, .val = 'm'},
127 {.name = "numeric", .has_arg = 0, .val = 'n'},
128 {.name = "out-interface", .has_arg = 1, .val = 'o'},
129 {.name = "verbose", .has_arg = 0, .val = 'v'},
130 {.name = "exact", .has_arg = 0, .val = 'x'},
131 {.name = "version", .has_arg = 0, .val = 'V'},
132 {.name = "help", .has_arg = 2, .val = 'h'},
133 {.name = "line-numbers", .has_arg = 0, .val = '0'},
134 {.name = "modprobe", .has_arg = 1, .val = 'M'},
135 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200136 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100137 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100146void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100147struct xtables_globals ip6tables_globals = {
148 .option_offset = 0,
149 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500150 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100151 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100152};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000153
154/* Table of legal combinations of commands and options. If any of the
155 * given commands make an option legal, that option is legal (applies to
156 * CMD_LIST and CMD_ZERO only).
157 * Key:
158 * + compulsory
159 * x illegal
160 * optional
161 */
162
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100163static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000164/* Well, it's better than "Re: Linux vs FreeBSD" */
165{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200166 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000167/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
168/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
169/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
170/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
171/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
172/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
173/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700175/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000176/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
177/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200178/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200179/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
180/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000181};
182
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100183static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184{
185/* -n */ 0,
186/* -s */ IP6T_INV_SRCIP,
187/* -d */ IP6T_INV_DSTIP,
188/* -p */ IP6T_INV_PROTO,
189/* -j */ 0,
190/* -v */ 0,
191/* -x */ 0,
192/* -i */ IP6T_INV_VIA_IN,
193/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000194/*--line*/ 0,
195/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000196};
197
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100198#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500199#define prog_name ip6tables_globals.program_name
200#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201/* A few hardcoded protocols for 'all' and in case the user has no
202 /etc/protocols */
203struct pprot {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100204 const char *name;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100205 uint8_t num;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000206};
207
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100208static const char *
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100209proto_to_name(uint8_t proto, int nolookup)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000210{
211 unsigned int i;
212
213 if (proto && !nolookup) {
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100214 const struct protoent *pent = getprotobynumber(proto);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000215 if (pent)
216 return pent->p_name;
217 }
218
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100219 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
220 if (xtables_chain_protos[i].num == proto)
221 return xtables_chain_protos[i].name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000222
223 return NULL;
224}
225
Dmitry V. Levin24bb0782010-05-14 13:26:22 +0200226static void __attribute__((noreturn))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227exit_tryhelp(int status)
228{
Harald Weltea8658ca2003-03-05 07:46:15 +0000229 if (line != -1)
230 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000231 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500232 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500233 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000234 exit(status);
235}
236
Patrick McHardy0b639362007-09-08 16:00:01 +0000237static void
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100238exit_printhelp(const struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000239{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000241"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100242" %s -I chain [rulenum] rule-specification [options]\n"
243" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000244" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200245" %s -[LS] [chain [rulenum]] [options]\n"
246" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000247" %s -[NX] chain\n"
248" %s -E old-chain-name new-chain-name\n"
249" %s -P chain target [options]\n"
250" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500251 prog_name, prog_vers, prog_name, prog_name,
252 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100253 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000254
255 printf(
256"Commands:\n"
257"Either long or short options are allowed.\n"
258" --append -A chain Append to chain\n"
259" --delete -D chain Delete matching rule from chain\n"
260" --delete -D chain rulenum\n"
261" Delete rule rulenum (1 = first) from chain\n"
262" --insert -I chain [rulenum]\n"
263" Insert in chain as rulenum (default 1=first)\n"
264" --replace -R chain rulenum\n"
265" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200266" --list -L [chain [rulenum]]\n"
267" List the rules in a chain or all chains\n"
268" --list-rules -S [chain [rulenum]]\n"
269" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700271" --zero -Z [chain [rulenum]]\n"
272" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000273" --new -N chain Create a new user-defined chain\n"
274" --delete-chain\n"
275" -X [chain] Delete a user-defined chain\n"
276" --policy -P chain target\n"
277" Change policy on chain to target\n"
278" --rename-chain\n"
279" -E old-chain new-chain\n"
280" Change chain name, (moving any references)\n"
281
282"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200283"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100284"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100286"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200288"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289" network interface name ([+] for wildcard)\n"
290" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000291" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200292#ifdef IP6T_F_GOTO
293" --goto -g chain\n"
294" jump to chain with no return\n"
295#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000296" --match -m match\n"
297" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000298" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200299"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000300" network interface name ([+] for wildcard)\n"
301" --table -t table table to manipulate (default: `filter')\n"
302" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000303" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000304" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000305/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000306" --modprobe=<command> try to insert modules using this command\n"
307" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000308"[!] --version -V print package version.\n");
309
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200310 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000311 exit(0);
312}
313
Patrick McHardy0b639362007-09-08 16:00:01 +0000314void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100315ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000316{
317 va_list args;
318
319 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500320 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000321 vfprintf(stderr, msg, args);
322 va_end(args);
323 fprintf(stderr, "\n");
324 if (status == PARAMETER_PROBLEM)
325 exit_tryhelp(status);
326 if (status == VERSION_PROBLEM)
327 fprintf(stderr,
328 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
329 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500330 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000331 exit(status);
332}
333
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334static void
335generic_opt_check(int command, int options)
336{
337 int i, j, legal = 0;
338
339 /* Check that commands are valid with options. Complicated by the
340 * fact that if an option is legal with *any* command given, it is
341 * legal overall (ie. -z and -l).
342 */
343 for (i = 0; i < NUMBER_OF_OPT; i++) {
344 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
345
346 for (j = 0; j < NUMBER_OF_CMD; j++) {
347 if (!(command & (1<<j)))
348 continue;
349
350 if (!(options & (1<<i))) {
351 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100352 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000353 "You need to supply the `-%c' "
354 "option for this command\n",
355 optflags[i]);
356 } else {
357 if (commands_v_options[j][i] != 'x')
358 legal = 1;
359 else if (legal == 0)
360 legal = -1;
361 }
362 }
363 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100364 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000365 "Illegal option `-%c' with this command\n",
366 optflags[i]);
367 }
368}
369
370static char
371opt2char(int option)
372{
373 const char *ptr;
374 for (ptr = optflags; option > 1; option >>= 1, ptr++);
375
376 return *ptr;
377}
378
379static char
380cmd2char(int option)
381{
382 const char *ptr;
383 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
384
385 return *ptr;
386}
387
388static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000389add_command(unsigned int *cmd, const int newcmd, const int othercmds,
390 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000391{
392 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100393 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000394 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100395 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
397 *cmd |= newcmd;
398}
399
Rusty Russell5eed48a2000-06-02 20:12:24 +0000400/*
401 * All functions starting with "parse" should succeed, otherwise
402 * the program fails.
403 * Most routines return pointers to static data that may change
404 * between calls to the same or other routines with a few exceptions:
405 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
406 * return global static data.
407*/
408
Rusty Russell5eed48a2000-06-02 20:12:24 +0000409/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200410static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100411find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100412 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000413{
Harald Welteed498492001-07-23 01:24:22 +0000414 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000415
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100416 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100417 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000418
Harald Welte43ac1912002-03-03 09:43:07 +0000419 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100420 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000421 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100422 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000423
424 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000425}
426
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000427/* These are invalid numbers as upper layer protocol */
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100428static int is_exthdr(uint16_t proto)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000429{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000430 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000431 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000432 proto == IPPROTO_AH ||
433 proto == IPPROTO_DSTOPTS);
434}
435
Rusty Russell5eed48a2000-06-02 20:12:24 +0000436/* Can't be zero. */
437static int
438parse_rulenumber(const char *rule)
439{
Harald Welte43ac1912002-03-03 09:43:07 +0000440 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100442 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100443 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000444 "Invalid rule number `%s'", rule);
445
446 return rulenum;
447}
448
449static const char *
450parse_target(const char *targetname)
451{
452 const char *ptr;
453
454 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100455 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000456 "Invalid target name (too short)");
457
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200458 if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100459 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000460 "Invalid target name `%s' (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200461 targetname, XT_EXTENSION_MAXNAMELEN - 1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462
463 for (ptr = targetname; *ptr; ptr++)
464 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100465 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000466 "Invalid target name `%s'", targetname);
467 return targetname;
468}
469
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100471set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472 int invert)
473{
474 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100475 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000476 opt2char(option));
477 *options |= option;
478
479 if (invert) {
480 unsigned int i;
481 for (i = 0; 1 << i != option; i++);
482
483 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100484 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000485 "cannot have ! before -%c",
486 opt2char(option));
487 *invflg |= inverse_for_options[i];
488 }
489}
490
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000491static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100492print_num(uint64_t number, unsigned int format)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000493{
Harald Welte43ac1912002-03-03 09:43:07 +0000494 if (format & FMT_KILOMEGAGIGA) {
495 if (number > 99999) {
496 number = (number + 500) / 1000;
497 if (number > 9999) {
498 number = (number + 500) / 1000;
499 if (number > 9999) {
500 number = (number + 500) / 1000;
501 if (number > 9999) {
502 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000503 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000504 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000505 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000506 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000507 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000508 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000509 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000510 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000511 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000512 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000513 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000514}
515
Harald Welte43ac1912002-03-03 09:43:07 +0000516
Rusty Russell5eed48a2000-06-02 20:12:24 +0000517static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100518print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519{
520 struct ip6t_counters counters;
521 const char *pol = ip6tc_get_policy(chain, &counters, handle);
522 printf("Chain %s", chain);
523 if (pol) {
524 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000525 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000526 fputc(' ', stdout);
527 print_num(counters.pcnt, (format|FMT_NOTABLE));
528 fputs("packets, ", stdout);
529 print_num(counters.bcnt, (format|FMT_NOTABLE));
530 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000531 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000532 printf(")\n");
533 } else {
534 unsigned int refs;
535 if (!ip6tc_get_references(&refs, chain, handle))
536 printf(" (ERROR obtaining refs)\n");
537 else
538 printf(" (%u references)\n", refs);
539 }
540
541 if (format & FMT_LINENUMBERS)
542 printf(FMT("%-4s ", "%s "), "num");
543 if (!(format & FMT_NOCOUNTS)) {
544 if (format & FMT_KILOMEGAGIGA) {
545 printf(FMT("%5s ","%s "), "pkts");
546 printf(FMT("%5s ","%s "), "bytes");
547 } else {
548 printf(FMT("%8s ","%s "), "pkts");
549 printf(FMT("%10s ","%s "), "bytes");
550 }
551 }
552 if (!(format & FMT_NOTARGET))
553 printf(FMT("%-9s ","%s "), "target");
554 fputs(" prot ", stdout);
555 if (format & FMT_OPTIONS)
556 fputs("opt", stdout);
557 if (format & FMT_VIA) {
558 printf(FMT(" %-6s ","%s "), "in");
559 printf(FMT("%-6s ","%s "), "out");
560 }
561 printf(FMT(" %-19s ","%s "), "source");
562 printf(FMT(" %-19s "," %s "), "destination");
563 printf("\n");
564}
565
Harald Welte43ac1912002-03-03 09:43:07 +0000566
Rusty Russell5eed48a2000-06-02 20:12:24 +0000567static int
568print_match(const struct ip6t_entry_match *m,
569 const struct ip6t_ip6 *ip,
570 int numeric)
571{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100572 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000574
575 if (match) {
576 if (match->print)
577 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000578 else
579 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000580 } else {
581 if (m->u.user.name[0])
582 printf("UNKNOWN match `%s' ", m->u.user.name);
583 }
584 /* Don't stop iterating. */
585 return 0;
586}
587
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100588/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000589static void
590print_firewall(const struct ip6t_entry *fw,
591 const char *targname,
592 unsigned int num,
593 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100594 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000595{
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100596 const struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000597 const struct ip6t_entry_target *t;
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100598 uint8_t flags;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599 char buf[BUFSIZ];
600
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100602 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000603 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100604 target = xtables_find_target(IP6T_STANDARD_TARGET,
605 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000606
607 t = ip6t_get_target((struct ip6t_entry *)fw);
608 flags = fw->ipv6.flags;
609
610 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200611 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000612
613 if (!(format & FMT_NOCOUNTS)) {
614 print_num(fw->counters.pcnt, format);
615 print_num(fw->counters.bcnt, format);
616 }
617
618 if (!(format & FMT_NOTARGET))
619 printf(FMT("%-9s ", "%s "), targname);
620
621 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
622 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100623 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000624 if (pname)
625 printf(FMT("%-5s", "%s "), pname);
626 else
627 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
628 }
629
630 if (format & FMT_OPTIONS) {
631 if (format & FMT_NOTABLE)
632 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000633 fputc(' ', stdout); /* Invert flag of FRAG */
634 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000635 fputc(' ', stdout);
636 }
637
638 if (format & FMT_VIA) {
639 char iface[IFNAMSIZ+2];
640
641 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
642 iface[0] = '!';
643 iface[1] = '\0';
644 }
645 else iface[0] = '\0';
646
647 if (fw->ipv6.iniface[0] != '\0') {
648 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000649 }
650 else if (format & FMT_NUMERIC) strcat(iface, "*");
651 else strcat(iface, "any");
652 printf(FMT(" %-6s ","in %s "), iface);
653
654 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
655 iface[0] = '!';
656 iface[1] = '\0';
657 }
658 else iface[0] = '\0';
659
660 if (fw->ipv6.outiface[0] != '\0') {
661 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000662 }
663 else if (format & FMT_NUMERIC) strcat(iface, "*");
664 else strcat(iface, "any");
665 printf(FMT("%-6s ","out %s "), iface);
666 }
667
668 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000669 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000670 && !(format & FMT_NUMERIC))
671 printf(FMT("%-19s ","%s "), "anywhere");
672 else {
673 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100674 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000675 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100676 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
677 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000678 printf(FMT("%-19s ","%s "), buf);
679 }
680
681 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
682 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
683 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200684 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000685 else {
686 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100687 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000688 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100689 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
690 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200691 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000692 }
693
694 if (format & FMT_NOTABLE)
695 fputs(" ", stdout);
696
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200697#ifdef IP6T_F_GOTO
698 if(fw->ipv6.flags & IP6T_F_GOTO)
699 printf("[goto] ");
700#endif
701
Rusty Russell5eed48a2000-06-02 20:12:24 +0000702 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
703
704 if (target) {
705 if (target->print)
706 /* Print the target information. */
707 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
708 } else if (t->u.target_size != sizeof(*t))
709 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000710 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711
712 if (!(format & FMT_NONEWLINE))
713 fputc('\n', stdout);
714}
715
716static void
717print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100718 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000719{
720 struct ip6t_entry_target *t;
721
722 t = ip6t_get_target((struct ip6t_entry *)fw);
723 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
724}
725
726static int
727append_entry(const ip6t_chainlabel chain,
728 struct ip6t_entry *fw,
729 unsigned int nsaddrs,
730 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100731 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000732 unsigned int ndaddrs,
733 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100734 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100736 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000737{
738 unsigned int i, j;
739 int ret = 1;
740
741 for (i = 0; i < nsaddrs; i++) {
742 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100743 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000744 for (j = 0; j < ndaddrs; j++) {
745 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100746 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000747 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100748 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 ret &= ip6tc_append_entry(chain, fw, handle);
750 }
751 }
752
753 return ret;
754}
755
756static int
757replace_entry(const ip6t_chainlabel chain,
758 struct ip6t_entry *fw,
759 unsigned int rulenum,
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100760 const struct in6_addr *saddr, const struct in6_addr *smask,
761 const struct in6_addr *daddr, const struct in6_addr *dmask,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000762 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100763 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000764{
765 fw->ipv6.src = *saddr;
766 fw->ipv6.dst = *daddr;
Jan Engelhardt75cb7632009-11-15 15:51:27 +0100767 fw->ipv6.smsk = *smask;
768 fw->ipv6.dmsk = *dmask;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769
770 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100771 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000772 return ip6tc_replace_entry(chain, fw, rulenum, handle);
773}
774
775static int
776insert_entry(const ip6t_chainlabel chain,
777 struct ip6t_entry *fw,
778 unsigned int rulenum,
779 unsigned int nsaddrs,
780 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100781 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782 unsigned int ndaddrs,
783 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100784 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000785 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100786 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000787{
788 unsigned int i, j;
789 int ret = 1;
790
791 for (i = 0; i < nsaddrs; i++) {
792 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100793 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000794 for (j = 0; j < ndaddrs; j++) {
795 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100796 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100798 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000799 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
800 }
801 }
802
803 return ret;
804}
805
806static unsigned char *
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100807make_delete_mask(const struct xtables_rule_match *matches,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100808 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809{
810 /* Establish mask for comparison */
811 unsigned int size;
Jan Engelhardtd1435e02010-12-18 01:40:04 +0100812 const struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 unsigned char *mask, *mptr;
814
815 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000816 for (matchp = matches; matchp; matchp = matchp->next)
817 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818
Jan Engelhardt630ef482009-01-27 14:58:41 +0100819 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000820 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100821 + target->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822
823 memset(mask, 0xFF, sizeof(struct ip6t_entry));
824 mptr = mask + sizeof(struct ip6t_entry);
825
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000826 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000828 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000829 + matchp->match->userspacesize);
830 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831 }
832
Max Kellermann5b76f682008-01-29 13:42:48 +0000833 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000834 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100835 + target->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836
837 return mask;
838}
839
840static int
841delete_entry(const ip6t_chainlabel chain,
842 struct ip6t_entry *fw,
843 unsigned int nsaddrs,
844 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100845 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846 unsigned int ndaddrs,
847 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100848 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100850 struct ip6tc_handle *handle,
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100851 struct xtables_rule_match *matches,
852 const struct xtables_target *target)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853{
854 unsigned int i, j;
855 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856 unsigned char *mask;
857
Jan Engelhardt4f0d7b62009-10-27 02:59:33 +0100858 mask = make_delete_mask(matches, target);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000859 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000860 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100861 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000863 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100864 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000865 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100866 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000867 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000868 }
869 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000870 free(mask);
871
Rusty Russell5eed48a2000-06-02 20:12:24 +0000872 return ret;
873}
874
András Kis-Szabó764316a2001-02-26 17:31:20 +0000875int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100876for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
877 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878{
Max Kellermann5b76f682008-01-29 13:42:48 +0000879 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000880 const char *chain;
881 char *chains;
882 unsigned int i, chaincount = 0;
883
884 chain = ip6tc_first_chain(handle);
885 while (chain) {
886 chaincount++;
887 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000888 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889
Jan Engelhardt630ef482009-01-27 14:58:41 +0100890 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000891 i = 0;
892 chain = ip6tc_first_chain(handle);
893 while (chain) {
894 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
895 i++;
896 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000897 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898
899 for (i = 0; i < chaincount; i++) {
900 if (!builtinstoo
901 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100902 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000904 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000905 }
906
907 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000908 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000909}
910
András Kis-Szabó764316a2001-02-26 17:31:20 +0000911int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000912flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100913 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000914{
915 if (!chain)
916 return for_each_chain(flush_entries, verbose, 1, handle);
917
918 if (verbose)
919 fprintf(stdout, "Flushing chain `%s'\n", chain);
920 return ip6tc_flush_entries(chain, handle);
921}
922
923static int
924zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100925 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000926{
927 if (!chain)
928 return for_each_chain(zero_entries, verbose, 1, handle);
929
930 if (verbose)
931 fprintf(stdout, "Zeroing chain `%s'\n", chain);
932 return ip6tc_zero_entries(chain, handle);
933}
934
András Kis-Szabó764316a2001-02-26 17:31:20 +0000935int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100937 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938{
939 if (!chain)
940 return for_each_chain(delete_chain, verbose, 0, handle);
941
942 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000943 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000944 return ip6tc_delete_chain(chain, handle);
945}
946
947static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200948list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100949 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950{
951 int found = 0;
952 unsigned int format;
953 const char *this;
954
955 format = FMT_OPTIONS;
956 if (!verbose)
957 format |= FMT_NOCOUNTS;
958 else
959 format |= FMT_VIA;
960
961 if (numeric)
962 format |= FMT_NUMERIC;
963
964 if (!expanded)
965 format |= FMT_KILOMEGAGIGA;
966
967 if (linenumbers)
968 format |= FMT_LINENUMBERS;
969
970 for (this = ip6tc_first_chain(handle);
971 this;
972 this = ip6tc_next_chain(handle)) {
973 const struct ip6t_entry *i;
974 unsigned int num;
975
976 if (chain && strcmp(chain, this) != 0)
977 continue;
978
979 if (found) printf("\n");
980
Henrik Nordstrombb340822008-05-13 13:09:23 +0200981 if (!rulenum)
982 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 i = ip6tc_first_rule(this, handle);
984
985 num = 0;
986 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200987 num++;
988 if (!rulenum || num == rulenum)
989 print_firewall(i,
990 ip6tc_get_target(i, handle),
991 num,
992 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100993 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000994 i = ip6tc_next_rule(i, handle);
995 }
996 found = 1;
997 }
998
999 errno = ENOENT;
1000 return found;
1001}
1002
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001003/* This assumes that mask is contiguous, and byte-bounded. */
1004static void
1005print_iface(char letter, const char *iface, const unsigned char *mask,
1006 int invert)
1007{
1008 unsigned int i;
1009
1010 if (mask[0] == 0)
1011 return;
1012
Jan Engelhardt73866352010-12-18 02:04:59 +01001013 printf("%s -%c", invert ? " !" : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001014
1015 for (i = 0; i < IFNAMSIZ; i++) {
1016 if (mask[i] != 0) {
1017 if (iface[i] != '\0')
1018 printf("%c", iface[i]);
1019 } else {
1020 /* we can access iface[i-1] here, because
1021 * a few lines above we make sure that mask[0] != 0 */
1022 if (iface[i-1] != '\0')
1023 printf("+");
1024 break;
1025 }
1026 }
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001027}
1028
1029/* The ip6tables looks up the /etc/protocols. */
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001030static void print_proto(uint16_t proto, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001031{
1032 if (proto) {
1033 unsigned int i;
Jan Engelhardt73866352010-12-18 02:04:59 +01001034 const char *invertstr = invert ? " !" : "";
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001035
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001036 const struct protoent *pent = getprotobynumber(proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001037 if (pent) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001038 printf("%s -p %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001039 invertstr, pent->p_name);
1040 return;
1041 }
1042
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001043 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1044 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001045 printf("%s -p %s",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001046 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001047 return;
1048 }
1049
Jan Engelhardt73866352010-12-18 02:04:59 +01001050 printf("%s -p %u", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001051 }
1052}
1053
1054static int print_match_save(const struct ip6t_entry_match *e,
1055 const struct ip6t_ip6 *ip)
1056{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001057 const struct xtables_match *match =
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001058 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001059
1060 if (match) {
Jan Engelhardt73866352010-12-18 02:04:59 +01001061 printf(" -m %s", e->u.user.name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001062
1063 /* some matches don't provide a save function */
1064 if (match->save)
1065 match->save(ip, e);
1066 } else {
1067 if (e->u.match_size) {
1068 fprintf(stderr,
1069 "Can't find library for match `%s'\n",
1070 e->u.user.name);
1071 exit(1);
1072 }
1073 }
1074 return 0;
1075}
1076
1077/* print a given ip including mask if neccessary */
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001078static void print_ip(const char *prefix, const struct in6_addr *ip,
1079 const struct in6_addr *mask, int invert)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001080{
1081 char buf[51];
1082 int l = ipv6_prefix_length(mask);
1083
1084 if (l == 0 && !invert)
1085 return;
1086
Jan Engelhardt73866352010-12-18 02:04:59 +01001087 printf("%s %s %s",
1088 invert ? " !" : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001089 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001090 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1091
1092 if (l == -1)
Jan Engelhardt73866352010-12-18 02:04:59 +01001093 printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001094 else
Jan Engelhardt73866352010-12-18 02:04:59 +01001095 printf("/%d", l);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001096}
1097
1098/* We want this to be readable, so only print out neccessary fields.
1099 * Because that's the kind of world I want to live in. */
1100void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001101 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001102{
Jan Engelhardtd1435e02010-12-18 01:40:04 +01001103 const struct ip6t_entry_target *t;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001104 const char *target_name;
1105
1106 /* print counters for iptables-save */
1107 if (counters > 0)
1108 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1109
1110 /* print chain name */
Jan Engelhardt73866352010-12-18 02:04:59 +01001111 printf("-A %s", chain);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001112
1113 /* Print IP part. */
1114 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1115 e->ipv6.invflags & IP6T_INV_SRCIP);
1116
1117 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1118 e->ipv6.invflags & IP6T_INV_DSTIP);
1119
1120 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1121 e->ipv6.invflags & IP6T_INV_VIA_IN);
1122
1123 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1124 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1125
1126 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1127
1128#if 0
1129 /* not definied in ipv6
1130 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1131 if (e->ipv6.flags & IPT_F_FRAG)
Jan Engelhardt73866352010-12-18 02:04:59 +01001132 printf("%s -f",
1133 e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001134#endif
1135
1136 if (e->ipv6.flags & IP6T_F_TOS)
Jan Engelhardt73866352010-12-18 02:04:59 +01001137 printf("%s -? %d",
1138 e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001139 e->ipv6.tos);
1140
1141 /* Print matchinfo part */
1142 if (e->target_offset) {
1143 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1144 }
1145
1146 /* print counters for iptables -R */
1147 if (counters < 0)
Jan Engelhardt73866352010-12-18 02:04:59 +01001148 printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001149
1150 /* Print target name */
1151 target_name = ip6tc_get_target(e, h);
1152 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001153#ifdef IP6T_F_GOTO
Jan Engelhardt73866352010-12-18 02:04:59 +01001154 printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001155#else
Jan Engelhardt73866352010-12-18 02:04:59 +01001156 printf(" -j %s", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001157#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001158
1159 /* Print targinfo part */
1160 t = ip6t_get_target((struct ip6t_entry *)e);
1161 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001162 struct xtables_target *target =
1163 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001164
1165 if (!target) {
1166 fprintf(stderr, "Can't find library for target `%s'\n",
1167 t->u.user.name);
1168 exit(1);
1169 }
1170
1171 if (target->save)
1172 target->save(&e->ipv6, t);
1173 else {
1174 /* If the target size is greater than ip6t_entry_target
1175 * there is something to be saved, we just don't know
1176 * how to print it */
1177 if (t->u.target_size !=
1178 sizeof(struct ip6t_entry_target)) {
1179 fprintf(stderr, "Target `%s' is missing "
1180 "save function\n",
1181 t->u.user.name);
1182 exit(1);
1183 }
1184 }
1185 }
1186 printf("\n");
1187}
1188
1189static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001190list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001191 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001192{
1193 const char *this = NULL;
1194 int found = 0;
1195
1196 if (counters)
1197 counters = -1; /* iptables -c format */
1198
1199 /* Dump out chain names first,
1200 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001201 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001202 this;
1203 this = ip6tc_next_chain(handle)) {
1204 if (chain && strcmp(this, chain) != 0)
1205 continue;
1206
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001207 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001208 struct ip6t_counters count;
1209 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1210 if (counters)
1211 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1212 printf("\n");
1213 } else {
1214 printf("-N %s\n", this);
1215 }
1216 }
1217
1218 for (this = ip6tc_first_chain(handle);
1219 this;
1220 this = ip6tc_next_chain(handle)) {
1221 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001222 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001223
1224 if (chain && strcmp(this, chain) != 0)
1225 continue;
1226
1227 /* Dump out rules */
1228 e = ip6tc_first_rule(this, handle);
1229 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001230 num++;
1231 if (!rulenum || num == rulenum)
1232 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001233 e = ip6tc_next_rule(e, handle);
1234 }
1235 found = 1;
1236 }
1237
1238 errno = ENOENT;
1239 return found;
1240}
1241
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242static struct ip6t_entry *
1243generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001244 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001245 struct ip6t_entry_target *target)
1246{
1247 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001248 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001249 struct ip6t_entry *e;
1250
1251 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001252 for (matchp = matches; matchp; matchp = matchp->next)
1253 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001254
Jan Engelhardt630ef482009-01-27 14:58:41 +01001255 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001256 *e = *fw;
1257 e->target_offset = size;
1258 e->next_offset = size + target->u.target_size;
1259
1260 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001261 for (matchp = matches; matchp; matchp = matchp->next) {
1262 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1263 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001264 }
1265 memcpy(e->elems + size, target, target->u.target_size);
1266
1267 return e;
1268}
1269
Jan Engelhardt395e4412009-02-10 10:43:08 +01001270static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001271{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001272 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001273
1274 for (matchp = *matches; matchp;) {
1275 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001276 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001277 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001278 matchp->match->m = NULL;
1279 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001280 if (matchp->match == matchp->match->next) {
1281 free(matchp->match);
1282 matchp->match = NULL;
1283 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001284 free(matchp);
1285 matchp = tmp;
1286 }
1287
1288 *matches = NULL;
1289}
1290
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001291struct iptables_command_state {
1292 struct ip6t_entry fw;
1293 int invert;
1294 int c;
1295 unsigned int options;
1296 struct xtables_rule_match *matches;
1297 struct xtables_target *target;
1298 char *protocol;
1299 int proto_used;
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001300 char **argv;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001301};
1302
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001303static void command_default(struct iptables_command_state *cs)
1304{
1305 struct xtables_rule_match *matchp;
1306 struct xtables_match *m;
1307
1308 if (cs->target == NULL || cs->target->parse == NULL ||
1309 cs->c < cs->target->option_offset ||
1310 cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE ||
1311 !cs->target->parse(cs->c - cs->target->option_offset,
1312 cs->argv, cs->invert,
1313 &cs->target->tflags,
1314 &cs->fw, &cs->target->t)) {
1315 for (matchp = cs->matches; matchp; matchp = matchp->next) {
1316 if (matchp->completed ||
1317 matchp->match->parse == NULL)
1318 continue;
1319 if (cs->c < matchp->match->option_offset ||
1320 cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
1321 continue;
1322 if (matchp->match->parse(cs->c - matchp->match->option_offset,
1323 cs->argv, cs->invert,
1324 &matchp->match->mflags,
1325 &cs->fw,
1326 &matchp->match->m))
1327 break;
1328 }
1329 m = matchp ? matchp->match : NULL;
1330
1331 /* If you listen carefully, you can
1332 actually hear this code suck. */
1333
1334 /* some explanations (after four different bugs
1335 * in 3 different releases): If we encounter a
1336 * parameter, that has not been parsed yet,
1337 * it's not an option of an explicitly loaded
1338 * match or a target. However, we support
1339 * implicit loading of the protocol match
1340 * extension. '-p tcp' means 'l4 proto 6' and
1341 * at the same time 'load tcp protocol match on
1342 * demand if we specify --dport'.
1343 *
1344 * To make this work, we need to make sure:
1345 * - the parameter has not been parsed by
1346 * a match (m above)
1347 * - a protocol has been specified
1348 * - the protocol extension has not been
1349 * loaded yet, or is loaded and unused
1350 * [think of ip6tables-restore!]
1351 * - the protocol extension can be successively
1352 * loaded
1353 */
1354 if (m == NULL
1355 && cs->protocol
1356 && (!find_proto(cs->protocol, XTF_DONT_LOAD,
1357 cs->options&OPT_NUMERIC, NULL)
1358 || (find_proto(cs->protocol, XTF_DONT_LOAD,
1359 cs->options&OPT_NUMERIC, NULL)
1360 && (cs->proto_used == 0))
1361 )
1362 && (m = find_proto(cs->protocol, XTF_TRY_LOAD,
1363 cs->options&OPT_NUMERIC, &cs->matches))) {
1364 /* Try loading protocol */
1365 size_t size;
1366
1367 cs->proto_used = 1;
1368
1369 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1370 + m->size;
1371
1372 m->m = xtables_calloc(1, size);
1373 m->m->u.match_size = size;
1374 strcpy(m->m->u.user.name, m->name);
1375 m->m->u.user.revision = m->revision;
1376 if (m->init != NULL)
1377 m->init(m->m);
1378
1379 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1380 m->extra_opts, &m->option_offset);
1381
1382 optind--;
1383 return;
1384 }
1385
1386 if (!m) {
1387 if (cs->c == '?') {
1388 if (optopt) {
1389 xtables_error(
1390 PARAMETER_PROBLEM,
1391 "option `%s' "
1392 "requires an "
1393 "argument",
1394 cs->argv[optind-1]);
1395 } else {
1396 xtables_error(
1397 PARAMETER_PROBLEM,
1398 "unknown option "
1399 "`%s'",
1400 cs->argv[optind-1]);
1401 }
1402 }
1403 xtables_error(PARAMETER_PROBLEM,
1404 "Unknown arg `%s'", optarg);
1405 }
1406 }
1407}
1408
Jan Engelhardtfd187312008-11-10 16:59:27 +01001409int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410{
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001411 struct iptables_command_state cs;
1412 struct ip6t_entry *e = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 unsigned int nsaddrs = 0, ndaddrs = 0;
1414 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001415 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001417 int verbose = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418 const char *chain = NULL;
1419 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1420 const char *policy = NULL, *newname = NULL;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001421 unsigned int rulenum = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001422 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001424 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001425 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001426 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 const char *jumpto = "";
Patrick McHardy875441e2007-10-17 08:48:58 +00001428 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001430 memset(&cs, 0, sizeof(cs));
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001431 cs.argv = argv;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432
Harald Welte43ac1912002-03-03 09:43:07 +00001433 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001434 * a second time */
1435 optind = 0;
1436
Harald Welte43ac1912002-03-03 09:43:07 +00001437 /* clear mflags in case do_command gets called a second time
1438 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001439 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001440 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001441
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001442 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001443 t->tflags = 0;
1444 t->used = 0;
1445 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001446
Rusty Russell5eed48a2000-06-02 20:12:24 +00001447 /* Suppress error messages: we may add new options if we
1448 demand-load a protocol. */
1449 opterr = 0;
1450
Jan Engelhardtd3b2e392010-11-28 15:35:06 +01001451 opts = xt_params->orig_opts;
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001452 while ((cs.c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001453 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454 opts, NULL)) != -1) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001455 switch (cs.c) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001456 /*
1457 * Command selection
1458 */
1459 case 'A':
1460 add_command(&command, CMD_APPEND, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001461 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001462 chain = optarg;
1463 break;
1464
1465 case 'D':
1466 add_command(&command, CMD_DELETE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001467 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001468 chain = optarg;
1469 if (optind < argc && argv[optind][0] != '-'
1470 && argv[optind][0] != '!') {
1471 rulenum = parse_rulenumber(argv[optind++]);
1472 command = CMD_DELETE_NUM;
1473 }
1474 break;
1475
Rusty Russell5eed48a2000-06-02 20:12:24 +00001476 case 'R':
1477 add_command(&command, CMD_REPLACE, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001478 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001479 chain = optarg;
1480 if (optind < argc && argv[optind][0] != '-'
1481 && argv[optind][0] != '!')
1482 rulenum = parse_rulenumber(argv[optind++]);
1483 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001484 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001485 "-%c requires a rule number",
1486 cmd2char(CMD_REPLACE));
1487 break;
1488
1489 case 'I':
1490 add_command(&command, CMD_INSERT, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001491 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001492 chain = optarg;
1493 if (optind < argc && argv[optind][0] != '-'
1494 && argv[optind][0] != '!')
1495 rulenum = parse_rulenumber(argv[optind++]);
1496 else rulenum = 1;
1497 break;
1498
1499 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001500 add_command(&command, CMD_LIST,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001501 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001502 if (optarg) chain = optarg;
1503 else if (optind < argc && argv[optind][0] != '-'
1504 && argv[optind][0] != '!')
1505 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001506 if (optind < argc && argv[optind][0] != '-'
1507 && argv[optind][0] != '!')
1508 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 break;
1510
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001511 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001512 add_command(&command, CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001513 CMD_ZERO | CMD_ZERO_NUM, cs.invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001514 if (optarg) chain = optarg;
1515 else if (optind < argc && argv[optind][0] != '-'
1516 && argv[optind][0] != '!')
1517 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001518 if (optind < argc && argv[optind][0] != '-'
1519 && argv[optind][0] != '!')
1520 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001521 break;
1522
Rusty Russell5eed48a2000-06-02 20:12:24 +00001523 case 'F':
1524 add_command(&command, CMD_FLUSH, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001525 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001526 if (optarg) chain = optarg;
1527 else if (optind < argc && argv[optind][0] != '-'
1528 && argv[optind][0] != '!')
1529 chain = argv[optind++];
1530 break;
1531
1532 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001533 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001534 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001535 if (optarg) chain = optarg;
1536 else if (optind < argc && argv[optind][0] != '-'
1537 && argv[optind][0] != '!')
1538 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001539 if (optind < argc && argv[optind][0] != '-'
1540 && argv[optind][0] != '!') {
1541 rulenum = parse_rulenumber(argv[optind++]);
1542 command = CMD_ZERO_NUM;
1543 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544 break;
1545
1546 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001547 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001548 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001549 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001550 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001551 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001552 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001553 "chain name may not clash "
1554 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001555 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001556 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001557 chain = optarg;
1558 break;
1559
1560 case 'X':
1561 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001562 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 if (optarg) chain = optarg;
1564 else if (optind < argc && argv[optind][0] != '-'
1565 && argv[optind][0] != '!')
1566 chain = argv[optind++];
1567 break;
1568
1569 case 'E':
1570 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001571 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001572 chain = optarg;
1573 if (optind < argc && argv[optind][0] != '-'
1574 && argv[optind][0] != '!')
1575 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001576 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001577 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001578 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001579 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001580 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581 break;
1582
1583 case 'P':
1584 add_command(&command, CMD_SET_POLICY, CMD_NONE,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001585 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001586 chain = optarg;
1587 if (optind < argc && argv[optind][0] != '-'
1588 && argv[optind][0] != '!')
1589 policy = argv[optind++];
1590 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001591 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001592 "-%c requires a chain and a policy",
1593 cmd2char(CMD_SET_POLICY));
1594 break;
1595
1596 case 'h':
1597 if (!optarg)
1598 optarg = argv[optind];
1599
Jonas Berlin1b91e592005-04-01 06:58:38 +00001600 /* ip6tables -p icmp -h */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001601 if (!cs.matches && cs.protocol)
1602 xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1603 &cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001604
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001605 exit_printhelp(cs.matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001606
1607 /*
1608 * Option selection
1609 */
1610 case 'p':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001611 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1612 set_option(&cs.options, OPT_PROTOCOL, &cs.fw.ipv6.invflags,
1613 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614
1615 /* Canonicalize into lower case */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001616 for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1617 *cs.protocol = tolower(*cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001618
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001619 cs.protocol = optarg;
1620 cs.fw.ipv6.proto = xtables_parse_protocol(cs.protocol);
1621 cs.fw.ipv6.flags |= IP6T_F_PROTO;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001623 if (cs.fw.ipv6.proto == 0
1624 && (cs.fw.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001625 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001626 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001627
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001628 if (is_exthdr(cs.fw.ipv6.proto)
1629 && (cs.fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001630 fprintf(stderr,
1631 "Warning: never matched protocol: %s. "
1632 "use extension match instead.\n",
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001633 cs.protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 break;
1635
1636 case 's':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001637 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1638 set_option(&cs.options, OPT_SOURCE, &cs.fw.ipv6.invflags,
1639 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001640 shostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001641 break;
1642
1643 case 'd':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001644 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1645 set_option(&cs.options, OPT_DESTINATION, &cs.fw.ipv6.invflags,
1646 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001647 dhostnetworkmask = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648 break;
1649
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001650#ifdef IP6T_F_GOTO
1651 case 'g':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001652 set_option(&cs.options, OPT_JUMP, &cs.fw.ipv6.invflags,
1653 cs.invert);
1654 cs.fw.ipv6.flags |= IP6T_F_GOTO;
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001655 jumpto = parse_target(optarg);
1656 break;
1657#endif
1658
Rusty Russell5eed48a2000-06-02 20:12:24 +00001659 case 'j':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001660 set_option(&cs.options, OPT_JUMP, &cs.fw.ipv6.invflags,
1661 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001662 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001663 /* TRY_LOAD (may be chain name) */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001664 cs.target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001665
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001666 if (cs.target) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001667 size_t size;
1668
Harald Welte43ac1912002-03-03 09:43:07 +00001669 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001670 + cs.target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001671
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001672 cs.target->t = xtables_calloc(1, size);
1673 cs.target->t->u.target_size = size;
1674 strcpy(cs.target->t->u.user.name, jumpto);
1675 cs.target->t->u.user.revision = cs.target->revision;
1676 if (cs.target->init != NULL)
1677 cs.target->init(cs.target->t);
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001678 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001679 cs.target->extra_opts,
1680 &cs.target->option_offset);
Patrick McHardy455559b2008-04-15 15:51:19 +02001681 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001682 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001683 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001684 }
1685 break;
1686
1687
1688 case 'i':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001689 if (*optarg == '\0')
1690 xtables_error(PARAMETER_PROBLEM,
1691 "Empty interface is likely to be "
1692 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001693 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1694 set_option(&cs.options, OPT_VIANAMEIN, &cs.fw.ipv6.invflags,
1695 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001696 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001697 cs.fw.ipv6.iniface,
1698 cs.fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699 break;
1700
1701 case 'o':
Jan Engelhardt5b1fecc2011-01-07 12:26:59 +01001702 if (*optarg == '\0')
1703 xtables_error(PARAMETER_PROBLEM,
1704 "Empty interface is likely to be "
1705 "undesired");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001706 xtables_check_inverse(optarg, &cs.invert, &optind, argc, argv);
1707 set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw.ipv6.invflags,
1708 cs.invert);
Jan Engelhardtbbe83862009-10-24 00:45:33 +02001709 xtables_parse_interface(optarg,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001710 cs.fw.ipv6.outiface,
1711 cs.fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712 break;
1713
1714 case 'v':
1715 if (!verbose)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001716 set_option(&cs.options, OPT_VERBOSE,
1717 &cs.fw.ipv6.invflags, cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001718 verbose++;
1719 break;
1720
1721 case 'm': {
1722 size_t size;
1723
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001724 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001725 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001726 "unexpected ! flag before --match");
1727
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001728 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001729 &cs.matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001730 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1731 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001732 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001733 m->m->u.match_size = size;
1734 strcpy(m->m->u.user.name, m->name);
Jan Engelhardt11c2dd52010-06-07 12:00:24 +02001735 m->m->u.user.revision = m->revision;
Jonas Berlin1b91e592005-04-01 06:58:38 +00001736 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001737 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001738 if (m != m->next)
1739 /* Merge options for non-cloned matches */
Jan Engelhardt600f38d2010-10-29 18:57:42 +02001740 opts = xtables_merge_options(ip6tables_globals.orig_opts, opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001741 }
1742 break;
1743
1744 case 'n':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001745 set_option(&cs.options, OPT_NUMERIC, &cs.fw.ipv6.invflags,
1746 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001747 break;
1748
1749 case 't':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001750 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001751 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001752 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001753 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001754 break;
1755
1756 case 'x':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001757 set_option(&cs.options, OPT_EXPANDED, &cs.fw.ipv6.invflags,
1758 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001759 break;
1760
1761 case 'V':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001762 if (cs.invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001763 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001764 else
1765 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001766 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001767 exit(0);
1768
1769 case '0':
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001770 set_option(&cs.options, OPT_LINENUMBERS, &cs.fw.ipv6.invflags,
1771 cs.invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001772 break;
1773
Harald Welte43ac1912002-03-03 09:43:07 +00001774 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001775 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001776 break;
1777
1778 case 'c':
1779
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001780 set_option(&cs.options, OPT_COUNTERS, &cs.fw.ipv6.invflags,
1781 cs.invert);
Harald Welte43ac1912002-03-03 09:43:07 +00001782 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001783 bcnt = strchr(pcnt + 1, ',');
1784 if (bcnt)
1785 bcnt++;
1786 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001787 && argv[optind][0] != '!')
1788 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001789 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001790 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001791 "-%c requires packet and byte counter",
1792 opt2char(OPT_COUNTERS));
1793
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001794 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001795 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001796 "-%c packet counter not numeric",
1797 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001798 cs.fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001799
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001800 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001801 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001802 "-%c byte counter not numeric",
1803 opt2char(OPT_COUNTERS));
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001804 cs.fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001805 break;
1806
Rusty Russell5eed48a2000-06-02 20:12:24 +00001807 case 1: /* non option */
1808 if (optarg[0] == '!' && optarg[1] == '\0') {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001809 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001810 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001811 "multiple consecutive ! not"
1812 " allowed");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001813 cs.invert = TRUE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001814 optarg[0] = '\0';
1815 continue;
1816 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001817 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001818 exit_tryhelp(2);
1819
1820 default:
Jan Engelhardtf935ae02011-02-06 17:14:48 +01001821 command_default(&cs);
1822 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001823 }
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001824 cs.invert = FALSE;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825 }
1826
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001827 for (matchp = cs.matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001828 if (matchp->match->final_check != NULL)
1829 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001830
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001831 if (cs.target != NULL && cs.target->final_check != NULL)
1832 cs.target->final_check(cs.target->tflags);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833
1834 /* Fix me: must put inverse options checking here --MN */
1835
1836 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001837 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001838 "unknown arguments found on commandline");
1839 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001840 xtables_error(PARAMETER_PROBLEM, "no command specified");
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001841 if (cs.invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001842 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001843 "nothing appropriate following !");
1844
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001845 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001846 if (!(cs.options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001847 dhostnetworkmask = "::0/0";
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001848 if (!(cs.options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001849 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001850 }
1851
1852 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001853 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1854 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001855
1856 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001857 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1858 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001859
1860 if ((nsaddrs > 1 || ndaddrs > 1) &&
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001861 (cs.fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001862 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001863 " source or destination IP addresses");
1864
Rusty Russell5eed48a2000-06-02 20:12:24 +00001865 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001866 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001867 "specify a unique address");
1868
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001869 generic_opt_check(command, cs.options);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001870
Jan Engelhardt5429b412010-09-13 15:45:15 +02001871 if (chain != NULL && strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001872 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt5429b412010-09-13 15:45:15 +02001873 "chain name `%s' too long (must be under %u chars)",
1874 chain, XT_EXTENSION_MAXNAMELEN);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001875
Harald Welte43ac1912002-03-03 09:43:07 +00001876 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001877 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001878 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001879
Rusty Russell8beb0492004-12-22 00:37:10 +00001880 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001881 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001882 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001883
Harald Welte43ac1912002-03-03 09:43:07 +00001884 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001885 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001886 "can't initialize ip6tables table `%s': %s",
1887 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001888
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001889 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001890 || command == CMD_DELETE
1891 || command == CMD_INSERT
1892 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001893 if (strcmp(chain, "PREROUTING") == 0
1894 || strcmp(chain, "INPUT") == 0) {
1895 /* -o not valid with incoming packets. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001896 if (cs.options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001897 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001898 "Can't use -%c with %s\n",
1899 opt2char(OPT_VIANAMEOUT),
1900 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001901 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001902
Harald Welte43ac1912002-03-03 09:43:07 +00001903 if (strcmp(chain, "POSTROUTING") == 0
1904 || strcmp(chain, "OUTPUT") == 0) {
1905 /* -i not valid with outgoing packets */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001906 if (cs.options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001907 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001908 "Can't use -%c with %s\n",
1909 opt2char(OPT_VIANAMEIN),
1910 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001911 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001913 if (cs.target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001914 fprintf(stderr,
1915 "Warning: using chain %s, not extension\n",
1916 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001917
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001918 if (cs.target->t)
1919 free(cs.target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001920
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001921 cs.target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922 }
1923
1924 /* If they didn't specify a target, or it's a chain
1925 name, use standard. */
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001926 if (!cs.target
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 && (strlen(jumpto) == 0
1928 || ip6tc_is_chain(jumpto, *handle))) {
1929 size_t size;
1930
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001931 cs.target = xtables_find_target(IP6T_STANDARD_TARGET,
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001932 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933
1934 size = sizeof(struct ip6t_entry_target)
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001935 + cs.target->size;
1936 cs.target->t = xtables_calloc(1, size);
1937 cs.target->t->u.target_size = size;
1938 strcpy(cs.target->t->u.user.name, jumpto);
1939 if (cs.target->init != NULL)
1940 cs.target->init(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 }
1942
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001943 if (!cs.target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001944 /* it is no chain, and we can't load a plugin.
1945 * We cannot know if the plugin is corrupt, non
1946 * existant OR if the user just misspelled a
1947 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001948#ifdef IP6T_F_GOTO
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001949 if (cs.fw.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001950 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001951 "goto '%s' is not a chain\n", jumpto);
1952#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001953 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001954 } else {
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001955 e = generate_entry(&cs.fw, cs.matches, cs.target->t);
1956 free(cs.target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 }
1958 }
1959
1960 switch (command) {
1961 case CMD_APPEND:
1962 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001963 nsaddrs, saddrs, smasks,
1964 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001965 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001966 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001967 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 case CMD_DELETE:
1969 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001970 nsaddrs, saddrs, smasks,
1971 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001972 cs.options&OPT_VERBOSE,
1973 *handle, cs.matches, cs.target);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001974 break;
1975 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001976 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001977 break;
1978 case CMD_REPLACE:
1979 ret = replace_entry(chain, e, rulenum - 1,
Jan Engelhardt75cb7632009-11-15 15:51:27 +01001980 saddrs, smasks, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001981 cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 break;
1983 case CMD_INSERT:
1984 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001985 nsaddrs, saddrs, smasks,
1986 ndaddrs, daddrs, dmasks,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001987 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001988 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001989 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 case CMD_FLUSH:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001991 ret = flush_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001992 break;
1993 case CMD_ZERO:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01001994 ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001995 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001996 case CMD_ZERO_NUM:
1997 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1998 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001999 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002000 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07002001 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002002 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002003 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002004 cs.options&OPT_VERBOSE,
2005 cs.options&OPT_NUMERIC,
2006 cs.options&OPT_EXPANDED,
2007 cs.options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002008 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002009 if (ret && (command & CMD_ZERO))
2010 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002011 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07002012 if (ret && (command & CMD_ZERO_NUM))
2013 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002014 break;
2015 case CMD_LIST_RULES:
2016 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07002017 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002018 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002019 rulenum,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002020 cs.options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002021 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002022 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002023 ret = zero_entries(chain,
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002024 cs.options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07002025 if (ret && (command & CMD_ZERO_NUM))
2026 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002027 break;
2028 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002029 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002030 break;
2031 case CMD_DELETE_CHAIN:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002032 ret = delete_chain(chain, cs.options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002033 break;
2034 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002035 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002036 break;
2037 case CMD_SET_POLICY:
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002038 ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002039 break;
2040 default:
2041 /* We should never reach this... */
2042 exit_tryhelp(2);
2043 }
2044
2045 if (verbose > 1)
2046 dump_entries6(*handle);
2047
Jan Engelhardt3a9d8b02011-02-06 15:52:11 +01002048 clear_rule_matches(&cs.matches);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002049
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002050 if (e != NULL) {
2051 free(e);
2052 e = NULL;
2053 }
2054
Michael Granzow332e4ac2009-04-09 18:24:36 +01002055 free(saddrs);
2056 free(smasks);
2057 free(daddrs);
2058 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002059 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002060
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 return ret;
2062}