1/* Code to take an ip6tables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
6 * (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 *
13 *	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>
34#include <ctype.h>
35#include <stdarg.h>
36#include <stdbool.h>
37#include <limits.h>
38#include <ip6tables.h>
39#include <xtables.h>
40#include <arpa/inet.h>
41#include <unistd.h>
42#include <fcntl.h>
43#include <sys/types.h>
44#include <sys/socket.h>
45#include "ip6tables-multi.h"
46#include "xshared.h"
47
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
55#define CMD_NONE		0x0000U
56#define CMD_INSERT		0x0001U
57#define CMD_DELETE		0x0002U
58#define CMD_DELETE_NUM		0x0004U
59#define CMD_REPLACE		0x0008U
60#define CMD_APPEND		0x0010U
61#define CMD_LIST		0x0020U
62#define CMD_FLUSH		0x0040U
63#define CMD_ZERO		0x0080U
64#define CMD_NEW_CHAIN		0x0100U
65#define CMD_DELETE_CHAIN	0x0200U
66#define CMD_SET_POLICY		0x0400U
67#define CMD_RENAME_CHAIN	0x0800U
68#define CMD_LIST_RULES		0x1000U
69#define CMD_ZERO_NUM		0x2000U
70#define CMD_CHECK		0x4000U
71#define NUMBER_OF_CMD	16
72static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
73				 'N', 'X', 'P', 'E', 'S', 'Z', 'C' };
74
75#define NUMBER_OF_OPT	ARRAY_SIZE(optflags)
76static const char optflags[]
77= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
78
79static const char unsupported_rev[] = " [unsupported revision]";
80
81static struct option original_opts[] = {
82	{.name = "append",        .has_arg = 1, .val = 'A'},
83	{.name = "delete",        .has_arg = 1, .val = 'D'},
84	{.name = "check" ,        .has_arg = 1, .val = 'C'},
85	{.name = "insert",        .has_arg = 1, .val = 'I'},
86	{.name = "replace",       .has_arg = 1, .val = 'R'},
87	{.name = "list",          .has_arg = 2, .val = 'L'},
88	{.name = "list-rules",    .has_arg = 2, .val = 'S'},
89	{.name = "flush",         .has_arg = 2, .val = 'F'},
90	{.name = "zero",          .has_arg = 2, .val = 'Z'},
91	{.name = "new-chain",     .has_arg = 1, .val = 'N'},
92	{.name = "delete-chain",  .has_arg = 2, .val = 'X'},
93	{.name = "rename-chain",  .has_arg = 1, .val = 'E'},
94	{.name = "policy",        .has_arg = 1, .val = 'P'},
95	{.name = "source",        .has_arg = 1, .val = 's'},
96	{.name = "destination",   .has_arg = 1, .val = 'd'},
97	{.name = "src",           .has_arg = 1, .val = 's'}, /* synonym */
98	{.name = "dst",           .has_arg = 1, .val = 'd'}, /* synonym */
99	{.name = "protocol",      .has_arg = 1, .val = 'p'},
100	{.name = "in-interface",  .has_arg = 1, .val = 'i'},
101	{.name = "jump",          .has_arg = 1, .val = 'j'},
102	{.name = "table",         .has_arg = 1, .val = 't'},
103	{.name = "match",         .has_arg = 1, .val = 'm'},
104	{.name = "numeric",       .has_arg = 0, .val = 'n'},
105	{.name = "out-interface", .has_arg = 1, .val = 'o'},
106	{.name = "verbose",       .has_arg = 0, .val = 'v'},
107	{.name = "wait",          .has_arg = 2, .val = 'w'},
108	{.name = "wait-interval", .has_arg = 2, .val = 'W'},
109	{.name = "exact",         .has_arg = 0, .val = 'x'},
110	{.name = "version",       .has_arg = 0, .val = 'V'},
111	{.name = "help",          .has_arg = 2, .val = 'h'},
112	{.name = "line-numbers",  .has_arg = 0, .val = '0'},
113	{.name = "modprobe",      .has_arg = 1, .val = 'M'},
114	{.name = "set-counters",  .has_arg = 1, .val = 'c'},
115	{.name = "goto",          .has_arg = 1, .val = 'g'},
116	{.name = "ipv4",          .has_arg = 0, .val = '4'},
117	{.name = "ipv6",          .has_arg = 0, .val = '6'},
118	{NULL},
119};
120
121void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
122struct xtables_globals ip6tables_globals = {
123	.option_offset = 0,
124	.program_version = IPTABLES_VERSION,
125	.orig_opts = original_opts,
126	.exit_err = ip6tables_exit_error,
127	.compat_rev = xtables_compatible_revision,
128};
129
130/* Table of legal combinations of commands and options.  If any of the
131 * given commands make an option legal, that option is legal (applies to
132 * CMD_LIST and CMD_ZERO only).
133 * Key:
134 *  +  compulsory
135 *  x  illegal
136 *     optional
137 */
138
139static const char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
140/* Well, it's better than "Re: Linux vs FreeBSD" */
141{
142	/*     -n  -s  -d  -p  -j  -v  -x  -i  -o --line -c */
143/*INSERT*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
144/*DELETE*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
145/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
146/*REPLACE*/   {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
147/*APPEND*/    {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
148/*LIST*/      {' ','x','x','x','x',' ',' ','x','x',' ','x'},
149/*FLUSH*/     {'x','x','x','x','x',' ','x','x','x','x','x'},
150/*ZERO*/      {'x','x','x','x','x',' ','x','x','x','x','x'},
151/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
152/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
153/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
154/*RENAME*/    {'x','x','x','x','x',' ','x','x','x','x','x'},
155/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'},
156/*ZERO_NUM*/  {'x','x','x','x','x',' ','x','x','x','x','x'},
157/*CHECK*/     {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
158};
159
160static const unsigned int inverse_for_options[NUMBER_OF_OPT] =
161{
162/* -n */ 0,
163/* -s */ IP6T_INV_SRCIP,
164/* -d */ IP6T_INV_DSTIP,
165/* -p */ XT_INV_PROTO,
166/* -j */ 0,
167/* -v */ 0,
168/* -x */ 0,
169/* -i */ IP6T_INV_VIA_IN,
170/* -o */ IP6T_INV_VIA_OUT,
171/*--line*/ 0,
172/* -c */ 0,
173};
174
175#define opts ip6tables_globals.opts
176#define prog_name ip6tables_globals.program_name
177#define prog_vers ip6tables_globals.program_version
178/* A few hardcoded protocols for 'all' and in case the user has no
179   /etc/protocols */
180struct pprot {
181	const char *name;
182	uint8_t num;
183};
184
185static void __attribute__((noreturn))
186exit_tryhelp(int status)
187{
188	if (line != -1)
189		fprintf(stderr, "Error occurred at line: %d\n", line);
190	fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
191			prog_name, prog_name);
192	xtables_free_opts(1);
193	exit(status);
194}
195
196static void
197exit_printhelp(const struct xtables_rule_match *matches)
198{
199	printf("%s v%s\n\n"
200"Usage: %s -[ACD] chain rule-specification [options]\n"
201"       %s -I chain [rulenum] rule-specification [options]\n"
202"       %s -R chain rulenum rule-specification [options]\n"
203"       %s -D chain rulenum [options]\n"
204"       %s -[LS] [chain [rulenum]] [options]\n"
205"       %s -[FZ] [chain] [options]\n"
206"       %s -[NX] chain\n"
207"       %s -E old-chain-name new-chain-name\n"
208"       %s -P chain target [options]\n"
209"       %s -h (print this help information)\n\n",
210	       prog_name, prog_vers, prog_name, prog_name,
211	       prog_name, prog_name, prog_name, prog_name,
212	       prog_name, prog_name, prog_name, prog_name);
213
214	printf(
215"Commands:\n"
216"Either long or short options are allowed.\n"
217"  --append  -A chain		Append to chain\n"
218"  --check   -C chain		Check for the existence of a rule\n"
219"  --delete  -D chain		Delete matching rule from chain\n"
220"  --delete  -D chain rulenum\n"
221"				Delete rule rulenum (1 = first) from chain\n"
222"  --insert  -I chain [rulenum]\n"
223"				Insert in chain as rulenum (default 1=first)\n"
224"  --replace -R chain rulenum\n"
225"				Replace rule rulenum (1 = first) in chain\n"
226"  --list    -L [chain [rulenum]]\n"
227"				List the rules in a chain or all chains\n"
228"  --list-rules -S [chain [rulenum]]\n"
229"				Print the rules in a chain or all chains\n"
230"  --flush   -F [chain]		Delete all rules in  chain or all chains\n"
231"  --zero    -Z [chain [rulenum]]\n"
232"				Zero counters in chain or all chains\n"
233"  --new     -N chain		Create a new user-defined chain\n"
234"  --delete-chain\n"
235"            -X [chain]		Delete a user-defined chain\n"
236"  --policy  -P chain target\n"
237"				Change policy on chain to target\n"
238"  --rename-chain\n"
239"            -E old-chain new-chain\n"
240"				Change chain name, (moving any references)\n"
241
242"Options:\n"
243"    --ipv4	-4		Error (line is ignored by ip6tables-restore)\n"
244"    --ipv6	-6		Nothing (line is ignored by iptables-restore)\n"
245"[!] --protocol	-p proto	protocol: by number or name, eg. `tcp'\n"
246"[!] --source	-s address[/mask][,...]\n"
247"				source specification\n"
248"[!] --destination -d address[/mask][,...]\n"
249"				destination specification\n"
250"[!] --in-interface -i input name[+]\n"
251"				network interface name ([+] for wildcard)\n"
252"  --jump	-j target\n"
253"				target for rule (may load target extension)\n"
254#ifdef IP6T_F_GOTO
255"  --goto	-g chain\n"
256"				jump to chain with no return\n"
257#endif
258"  --match	-m match\n"
259"				extended match (may load extension)\n"
260"  --numeric	-n		numeric output of addresses and ports\n"
261"[!] --out-interface -o output name[+]\n"
262"				network interface name ([+] for wildcard)\n"
263"  --table	-t table	table to manipulate (default: `filter')\n"
264"  --verbose	-v		verbose mode\n"
265"  --wait	-w [seconds]	maximum wait to acquire xtables lock before give up\n"
266"  --wait-interval -W [usecs]	wait time to try to acquire xtables lock\n"
267"				interval to wait for xtables lock\n"
268"				default is 1 second\n"
269"  --line-numbers		print line numbers when listing\n"
270"  --exact	-x		expand numbers (display exact values)\n"
271/*"[!] --fragment	-f		match second or further fragments only\n"*/
272"  --modprobe=<command>		try to insert modules using this command\n"
273"  --set-counters PKTS BYTES	set the counter during insert/append\n"
274"[!] --version	-V		print package version.\n");
275
276	print_extension_helps(xtables_targets, matches);
277	exit(0);
278}
279
280void
281ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
282{
283	va_list args;
284
285	va_start(args, msg);
286	fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
287	vfprintf(stderr, msg, args);
288	va_end(args);
289	fprintf(stderr, "\n");
290	if (status == PARAMETER_PROBLEM)
291		exit_tryhelp(status);
292	if (status == VERSION_PROBLEM)
293		fprintf(stderr,
294			"Perhaps ip6tables or your kernel needs to be upgraded.\n");
295	/* On error paths, make sure that we don't leak memory */
296	xtables_free_opts(1);
297	exit(status);
298}
299
300static void
301generic_opt_check(int command, int options)
302{
303	int i, j, legal = 0;
304
305	/* Check that commands are valid with options.  Complicated by the
306	 * fact that if an option is legal with *any* command given, it is
307	 * legal overall (ie. -z and -l).
308	 */
309	for (i = 0; i < NUMBER_OF_OPT; i++) {
310		legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
311
312		for (j = 0; j < NUMBER_OF_CMD; j++) {
313			if (!(command & (1<<j)))
314				continue;
315
316			if (!(options & (1<<i))) {
317				if (commands_v_options[j][i] == '+')
318					xtables_error(PARAMETER_PROBLEM,
319						   "You need to supply the `-%c' "
320						   "option for this command\n",
321						   optflags[i]);
322			} else {
323				if (commands_v_options[j][i] != 'x')
324					legal = 1;
325				else if (legal == 0)
326					legal = -1;
327			}
328		}
329		if (legal == -1)
330			xtables_error(PARAMETER_PROBLEM,
331				   "Illegal option `-%c' with this command\n",
332				   optflags[i]);
333	}
334}
335
336static char
337opt2char(int option)
338{
339	const char *ptr;
340	for (ptr = optflags; option > 1; option >>= 1, ptr++);
341
342	return *ptr;
343}
344
345static char
346cmd2char(int option)
347{
348	const char *ptr;
349	for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
350
351	return *ptr;
352}
353
354static void
355add_command(unsigned int *cmd, const int newcmd, const int othercmds,
356	    int invert)
357{
358	if (invert)
359		xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
360	if (*cmd & (~othercmds))
361		xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
362			   cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
363	*cmd |= newcmd;
364}
365
366/*
367 *	All functions starting with "parse" should succeed, otherwise
368 *	the program fails.
369 *	Most routines return pointers to static data that may change
370 *	between calls to the same or other routines with a few exceptions:
371 *	"host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
372 *	return global static data.
373*/
374
375/* These are invalid numbers as upper layer protocol */
376static int is_exthdr(uint16_t proto)
377{
378	return (proto == IPPROTO_ROUTING ||
379		proto == IPPROTO_FRAGMENT ||
380		proto == IPPROTO_AH ||
381		proto == IPPROTO_DSTOPTS);
382}
383
384/* Can't be zero. */
385static int
386parse_rulenumber(const char *rule)
387{
388	unsigned int rulenum;
389
390	if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
391		xtables_error(PARAMETER_PROBLEM,
392			   "Invalid rule number `%s'", rule);
393
394	return rulenum;
395}
396
397static void
398parse_chain(const char *chainname)
399{
400	const char *ptr;
401
402	if (strlen(chainname) >= XT_EXTENSION_MAXNAMELEN)
403		xtables_error(PARAMETER_PROBLEM,
404			   "chain name `%s' too long (must be under %u chars)",
405			   chainname, XT_EXTENSION_MAXNAMELEN);
406
407	if (*chainname == '-' || *chainname == '!')
408		xtables_error(PARAMETER_PROBLEM,
409			   "chain name not allowed to start "
410			   "with `%c'\n", *chainname);
411
412	if (xtables_find_target(chainname, XTF_TRY_LOAD))
413		xtables_error(PARAMETER_PROBLEM,
414			   "chain name may not clash "
415			   "with target name\n");
416
417	for (ptr = chainname; *ptr; ptr++)
418		if (isspace(*ptr))
419			xtables_error(PARAMETER_PROBLEM,
420				   "Invalid chain name `%s'", chainname);
421}
422
423static const char *
424parse_target(const char *targetname)
425{
426	const char *ptr;
427
428	if (strlen(targetname) < 1)
429		xtables_error(PARAMETER_PROBLEM,
430			   "Invalid target name (too short)");
431
432	if (strlen(targetname) >= XT_EXTENSION_MAXNAMELEN)
433		xtables_error(PARAMETER_PROBLEM,
434			   "Invalid target name `%s' (%u chars max)",
435			   targetname, XT_EXTENSION_MAXNAMELEN - 1);
436
437	for (ptr = targetname; *ptr; ptr++)
438		if (isspace(*ptr))
439			xtables_error(PARAMETER_PROBLEM,
440				   "Invalid target name `%s'", targetname);
441	return targetname;
442}
443
444static void
445set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
446	   int invert)
447{
448	if (*options & option)
449		xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
450			   opt2char(option));
451	*options |= option;
452
453	if (invert) {
454		unsigned int i;
455		for (i = 0; 1 << i != option; i++);
456
457		if (!inverse_for_options[i])
458			xtables_error(PARAMETER_PROBLEM,
459				   "cannot have ! before -%c",
460				   opt2char(option));
461		*invflg |= inverse_for_options[i];
462	}
463}
464
465
466static void
467print_header(unsigned int format, const char *chain, struct xtc_handle *handle)
468{
469	struct xt_counters counters;
470	const char *pol = ip6tc_get_policy(chain, &counters, handle);
471	printf("Chain %s", chain);
472	if (pol) {
473		printf(" (policy %s", pol);
474		if (!(format & FMT_NOCOUNTS)) {
475			fputc(' ', stdout);
476			xtables_print_num(counters.pcnt, (format|FMT_NOTABLE));
477			fputs("packets, ", stdout);
478			xtables_print_num(counters.bcnt, (format|FMT_NOTABLE));
479			fputs("bytes", stdout);
480		}
481		printf(")\n");
482	} else {
483		unsigned int refs;
484		if (!ip6tc_get_references(&refs, chain, handle))
485			printf(" (ERROR obtaining refs)\n");
486		else
487			printf(" (%u references)\n", refs);
488	}
489
490	if (format & FMT_LINENUMBERS)
491		printf(FMT("%-4s ", "%s "), "num");
492	if (!(format & FMT_NOCOUNTS)) {
493		if (format & FMT_KILOMEGAGIGA) {
494			printf(FMT("%5s ","%s "), "pkts");
495			printf(FMT("%5s ","%s "), "bytes");
496		} else {
497			printf(FMT("%8s ","%s "), "pkts");
498			printf(FMT("%10s ","%s "), "bytes");
499		}
500	}
501	if (!(format & FMT_NOTARGET))
502		printf(FMT("%-9s ","%s "), "target");
503	fputs(" prot ", stdout);
504	if (format & FMT_OPTIONS)
505		fputs("opt", stdout);
506	if (format & FMT_VIA) {
507		printf(FMT(" %-6s ","%s "), "in");
508		printf(FMT("%-6s ","%s "), "out");
509	}
510	printf(FMT(" %-19s ","%s "), "source");
511	printf(FMT(" %-19s "," %s "), "destination");
512	printf("\n");
513}
514
515
516static int
517print_match(const struct xt_entry_match *m,
518	    const struct ip6t_ip6 *ip,
519	    int numeric)
520{
521	const struct xtables_match *match =
522		xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
523
524	if (match) {
525		if (match->print && m->u.user.revision == match->revision)
526			match->print(ip, m, numeric);
527		else if (match->print)
528			printf("%s%s ", match->name, unsupported_rev);
529		else
530			printf("%s ", match->name);
531	} else {
532		if (m->u.user.name[0])
533			printf("UNKNOWN match `%s' ", m->u.user.name);
534	}
535	/* Don't stop iterating. */
536	return 0;
537}
538
539/* e is called `fw' here for historical reasons */
540static void
541print_firewall(const struct ip6t_entry *fw,
542	       const char *targname,
543	       unsigned int num,
544	       unsigned int format,
545	       struct xtc_handle *const handle)
546{
547	const struct xtables_target *target = NULL;
548	const struct xt_entry_target *t;
549	char buf[BUFSIZ];
550
551	if (!ip6tc_is_chain(targname, handle))
552		target = xtables_find_target(targname, XTF_TRY_LOAD);
553	else
554		target = xtables_find_target(XT_STANDARD_TARGET,
555		         XTF_LOAD_MUST_SUCCEED);
556
557	t = ip6t_get_target((struct ip6t_entry *)fw);
558
559	if (format & FMT_LINENUMBERS)
560		printf(FMT("%-4u ", "%u "), num);
561
562	if (!(format & FMT_NOCOUNTS)) {
563		xtables_print_num(fw->counters.pcnt, format);
564		xtables_print_num(fw->counters.bcnt, format);
565	}
566
567	if (!(format & FMT_NOTARGET))
568		printf(FMT("%-9s ", "%s "), targname);
569
570	fputc(fw->ipv6.invflags & XT_INV_PROTO ? '!' : ' ', stdout);
571	{
572		const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
573		if (pname)
574			printf(FMT("%-5s", "%s "), pname);
575		else
576			printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
577	}
578
579	if (format & FMT_OPTIONS) {
580		if (format & FMT_NOTABLE)
581			fputs("opt ", stdout);
582		fputc(' ', stdout); /* Invert flag of FRAG */
583		fputc(' ', stdout); /* -f */
584		fputc(' ', stdout);
585	}
586
587	if (format & FMT_VIA) {
588		char iface[IFNAMSIZ+2];
589
590		if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
591			iface[0] = '!';
592			iface[1] = '\0';
593		}
594		else iface[0] = '\0';
595
596		if (fw->ipv6.iniface[0] != '\0') {
597			strcat(iface, fw->ipv6.iniface);
598		}
599		else if (format & FMT_NUMERIC) strcat(iface, "*");
600		else strcat(iface, "any");
601		printf(FMT(" %-6s ","in %s "), iface);
602
603		if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
604			iface[0] = '!';
605			iface[1] = '\0';
606		}
607		else iface[0] = '\0';
608
609		if (fw->ipv6.outiface[0] != '\0') {
610			strcat(iface, fw->ipv6.outiface);
611		}
612		else if (format & FMT_NUMERIC) strcat(iface, "*");
613		else strcat(iface, "any");
614		printf(FMT("%-6s ","out %s "), iface);
615	}
616
617	fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
618	if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
619	    && !(format & FMT_NUMERIC))
620		printf(FMT("%-19s ","%s "), "anywhere");
621	else {
622		if (format & FMT_NUMERIC)
623			strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
624		else
625			strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
626		strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
627		printf(FMT("%-19s ","%s "), buf);
628	}
629
630	fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
631	if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
632	    && !(format & FMT_NUMERIC))
633		printf(FMT("%-19s ","-> %s"), "anywhere");
634	else {
635		if (format & FMT_NUMERIC)
636			strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
637		else
638			strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
639		strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
640		printf(FMT("%-19s ","-> %s"), buf);
641	}
642
643	if (format & FMT_NOTABLE)
644		fputs("  ", stdout);
645
646#ifdef IP6T_F_GOTO
647	if(fw->ipv6.flags & IP6T_F_GOTO)
648		printf("[goto] ");
649#endif
650
651	IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
652
653	if (target) {
654		if (target->print && t->u.user.revision == target->revision)
655			/* Print the target information. */
656			target->print(&fw->ipv6, t, format & FMT_NUMERIC);
657		else if (target->print)
658			printf(" %s%s", target->name, unsupported_rev);
659	} else if (t->u.target_size != sizeof(*t))
660		printf("[%u bytes of unknown target data] ",
661		       (unsigned int)(t->u.target_size - sizeof(*t)));
662
663	if (!(format & FMT_NONEWLINE))
664		fputc('\n', stdout);
665}
666
667static void
668print_firewall_line(const struct ip6t_entry *fw,
669		    struct xtc_handle *const h)
670{
671	struct xt_entry_target *t;
672
673	t = ip6t_get_target((struct ip6t_entry *)fw);
674	print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
675}
676
677static int
678append_entry(const xt_chainlabel chain,
679	     struct ip6t_entry *fw,
680	     unsigned int nsaddrs,
681	     const struct in6_addr saddrs[],
682	     const struct in6_addr smasks[],
683	     unsigned int ndaddrs,
684	     const struct in6_addr daddrs[],
685	     const struct in6_addr dmasks[],
686	     int verbose,
687	     struct xtc_handle *handle)
688{
689	unsigned int i, j;
690	int ret = 1;
691
692	for (i = 0; i < nsaddrs; i++) {
693		fw->ipv6.src = saddrs[i];
694		fw->ipv6.smsk = smasks[i];
695		for (j = 0; j < ndaddrs; j++) {
696			fw->ipv6.dst = daddrs[j];
697			fw->ipv6.dmsk = dmasks[j];
698			if (verbose)
699				print_firewall_line(fw, handle);
700			ret &= ip6tc_append_entry(chain, fw, handle);
701		}
702	}
703
704	return ret;
705}
706
707static int
708replace_entry(const xt_chainlabel chain,
709	      struct ip6t_entry *fw,
710	      unsigned int rulenum,
711	      const struct in6_addr *saddr, const struct in6_addr *smask,
712	      const struct in6_addr *daddr, const struct in6_addr *dmask,
713	      int verbose,
714	      struct xtc_handle *handle)
715{
716	fw->ipv6.src = *saddr;
717	fw->ipv6.dst = *daddr;
718	fw->ipv6.smsk = *smask;
719	fw->ipv6.dmsk = *dmask;
720
721	if (verbose)
722		print_firewall_line(fw, handle);
723	return ip6tc_replace_entry(chain, fw, rulenum, handle);
724}
725
726static int
727insert_entry(const xt_chainlabel chain,
728	     struct ip6t_entry *fw,
729	     unsigned int rulenum,
730	     unsigned int nsaddrs,
731	     const struct in6_addr saddrs[],
732	     const struct in6_addr smasks[],
733	     unsigned int ndaddrs,
734	     const struct in6_addr daddrs[],
735	     const struct in6_addr dmasks[],
736	     int verbose,
737	     struct xtc_handle *handle)
738{
739	unsigned int i, j;
740	int ret = 1;
741
742	for (i = 0; i < nsaddrs; i++) {
743		fw->ipv6.src = saddrs[i];
744		fw->ipv6.smsk = smasks[i];
745		for (j = 0; j < ndaddrs; j++) {
746			fw->ipv6.dst = daddrs[j];
747			fw->ipv6.dmsk = dmasks[j];
748			if (verbose)
749				print_firewall_line(fw, handle);
750			ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
751		}
752	}
753
754	return ret;
755}
756
757static unsigned char *
758make_delete_mask(const struct xtables_rule_match *matches,
759		 const struct xtables_target *target)
760{
761	/* Establish mask for comparison */
762	unsigned int size;
763	const struct xtables_rule_match *matchp;
764	unsigned char *mask, *mptr;
765
766	size = sizeof(struct ip6t_entry);
767	for (matchp = matches; matchp; matchp = matchp->next)
768		size += XT_ALIGN(sizeof(struct xt_entry_match)) + matchp->match->size;
769
770	mask = xtables_calloc(1, size
771			 + XT_ALIGN(sizeof(struct xt_entry_target))
772			 + target->size);
773
774	memset(mask, 0xFF, sizeof(struct ip6t_entry));
775	mptr = mask + sizeof(struct ip6t_entry);
776
777	for (matchp = matches; matchp; matchp = matchp->next) {
778		memset(mptr, 0xFF,
779		       XT_ALIGN(sizeof(struct xt_entry_match))
780		       + matchp->match->userspacesize);
781		mptr += XT_ALIGN(sizeof(struct xt_entry_match)) + matchp->match->size;
782	}
783
784	memset(mptr, 0xFF,
785	       XT_ALIGN(sizeof(struct xt_entry_target))
786	       + target->userspacesize);
787
788	return mask;
789}
790
791static int
792delete_entry(const xt_chainlabel chain,
793	     struct ip6t_entry *fw,
794	     unsigned int nsaddrs,
795	     const struct in6_addr saddrs[],
796	     const struct in6_addr smasks[],
797	     unsigned int ndaddrs,
798	     const struct in6_addr daddrs[],
799	     const struct in6_addr dmasks[],
800	     int verbose,
801	     struct xtc_handle *handle,
802	     struct xtables_rule_match *matches,
803	     const struct xtables_target *target)
804{
805	unsigned int i, j;
806	int ret = 1;
807	unsigned char *mask;
808
809	mask = make_delete_mask(matches, target);
810	for (i = 0; i < nsaddrs; i++) {
811		fw->ipv6.src = saddrs[i];
812		fw->ipv6.smsk = smasks[i];
813		for (j = 0; j < ndaddrs; j++) {
814			fw->ipv6.dst = daddrs[j];
815			fw->ipv6.dmsk = dmasks[j];
816			if (verbose)
817				print_firewall_line(fw, handle);
818			ret &= ip6tc_delete_entry(chain, fw, mask, handle);
819		}
820	}
821	free(mask);
822
823	return ret;
824}
825
826static int
827check_entry(const xt_chainlabel chain, struct ip6t_entry *fw,
828	    unsigned int nsaddrs, const struct in6_addr *saddrs,
829	    const struct in6_addr *smasks, unsigned int ndaddrs,
830	    const struct in6_addr *daddrs, const struct in6_addr *dmasks,
831	    bool verbose, struct xtc_handle *handle,
832	    struct xtables_rule_match *matches,
833	    const struct xtables_target *target)
834{
835	unsigned int i, j;
836	int ret = 1;
837	unsigned char *mask;
838
839	mask = make_delete_mask(matches, target);
840	for (i = 0; i < nsaddrs; i++) {
841		fw->ipv6.src = saddrs[i];
842		fw->ipv6.smsk = smasks[i];
843		for (j = 0; j < ndaddrs; j++) {
844			fw->ipv6.dst = daddrs[j];
845			fw->ipv6.dmsk = dmasks[j];
846			if (verbose)
847				print_firewall_line(fw, handle);
848			ret &= ip6tc_check_entry(chain, fw, mask, handle);
849		}
850	}
851
852	free(mask);
853	return ret;
854}
855
856int
857for_each_chain6(int (*fn)(const xt_chainlabel, int, struct xtc_handle *),
858	       int verbose, int builtinstoo, struct xtc_handle *handle)
859{
860	int ret = 1;
861	const char *chain;
862	char *chains;
863	unsigned int i, chaincount = 0;
864
865	chain = ip6tc_first_chain(handle);
866	while (chain) {
867		chaincount++;
868		chain = ip6tc_next_chain(handle);
869	}
870
871	chains = xtables_malloc(sizeof(xt_chainlabel) * chaincount);
872	i = 0;
873	chain = ip6tc_first_chain(handle);
874	while (chain) {
875		strcpy(chains + i*sizeof(xt_chainlabel), chain);
876		i++;
877		chain = ip6tc_next_chain(handle);
878	}
879
880	for (i = 0; i < chaincount; i++) {
881		if (!builtinstoo
882		    && ip6tc_builtin(chains + i*sizeof(xt_chainlabel),
883				    handle) == 1)
884			continue;
885		ret &= fn(chains + i*sizeof(xt_chainlabel), verbose, handle);
886	}
887
888	free(chains);
889	return ret;
890}
891
892int
893flush_entries6(const xt_chainlabel chain, int verbose,
894	      struct xtc_handle *handle)
895{
896	if (!chain)
897		return for_each_chain6(flush_entries6, verbose, 1, handle);
898
899	if (verbose)
900		fprintf(stdout, "Flushing chain `%s'\n", chain);
901	return ip6tc_flush_entries(chain, handle);
902}
903
904static int
905zero_entries(const xt_chainlabel chain, int verbose,
906	     struct xtc_handle *handle)
907{
908	if (!chain)
909		return for_each_chain6(zero_entries, verbose, 1, handle);
910
911	if (verbose)
912		fprintf(stdout, "Zeroing chain `%s'\n", chain);
913	return ip6tc_zero_entries(chain, handle);
914}
915
916int
917delete_chain6(const xt_chainlabel chain, int verbose,
918	     struct xtc_handle *handle)
919{
920	if (!chain)
921		return for_each_chain6(delete_chain6, verbose, 0, handle);
922
923	if (verbose)
924		fprintf(stdout, "Deleting chain `%s'\n", chain);
925	return ip6tc_delete_chain(chain, handle);
926}
927
928static int
929list_entries(const xt_chainlabel chain, int rulenum, int verbose, int numeric,
930	     int expanded, int linenumbers, struct xtc_handle *handle)
931{
932	int found = 0;
933	unsigned int format;
934	const char *this;
935
936	format = FMT_OPTIONS;
937	if (!verbose)
938		format |= FMT_NOCOUNTS;
939	else
940		format |= FMT_VIA;
941
942	if (numeric)
943		format |= FMT_NUMERIC;
944
945	if (!expanded)
946		format |= FMT_KILOMEGAGIGA;
947
948	if (linenumbers)
949		format |= FMT_LINENUMBERS;
950
951	for (this = ip6tc_first_chain(handle);
952	     this;
953	     this = ip6tc_next_chain(handle)) {
954		const struct ip6t_entry *i;
955		unsigned int num;
956
957		if (chain && strcmp(chain, this) != 0)
958			continue;
959
960		if (found) printf("\n");
961
962		if (!rulenum)
963		    print_header(format, this, handle);
964		i = ip6tc_first_rule(this, handle);
965
966		num = 0;
967		while (i) {
968			num++;
969			if (!rulenum || num == rulenum)
970				print_firewall(i,
971					       ip6tc_get_target(i, handle),
972					       num,
973					       format,
974					       handle);
975			i = ip6tc_next_rule(i, handle);
976		}
977		found = 1;
978	}
979
980	errno = ENOENT;
981	return found;
982}
983
984/* This assumes that mask is contiguous, and byte-bounded. */
985static void
986print_iface(char letter, const char *iface, const unsigned char *mask,
987	    int invert)
988{
989	unsigned int i;
990
991	if (mask[0] == 0)
992		return;
993
994	printf("%s -%c ", invert ? " !" : "", letter);
995
996	for (i = 0; i < IFNAMSIZ; i++) {
997		if (mask[i] != 0) {
998			if (iface[i] != '\0')
999				printf("%c", iface[i]);
1000		} else {
1001			/* we can access iface[i-1] here, because
1002			 * a few lines above we make sure that mask[0] != 0 */
1003			if (iface[i-1] != '\0')
1004				printf("+");
1005			break;
1006		}
1007	}
1008}
1009
1010/* The ip6tables looks up the /etc/protocols. */
1011static void print_proto(uint16_t proto, int invert)
1012{
1013	if (proto) {
1014		unsigned int i;
1015		const char *invertstr = invert ? " !" : "";
1016
1017		const struct protoent *pent = getprotobynumber(proto);
1018		if (pent) {
1019			printf("%s -p %s",
1020			       invertstr, pent->p_name);
1021			return;
1022		}
1023
1024		for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1025			if (xtables_chain_protos[i].num == proto) {
1026				printf("%s -p %s",
1027				       invertstr, xtables_chain_protos[i].name);
1028				return;
1029			}
1030
1031		printf("%s -p %u", invertstr, proto);
1032	}
1033}
1034
1035static int print_match_save(const struct xt_entry_match *e,
1036			const struct ip6t_ip6 *ip)
1037{
1038	const struct xtables_match *match =
1039		xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
1040
1041	if (match) {
1042		printf(" -m %s",
1043			match->alias ? match->alias(e) : e->u.user.name);
1044
1045		/* some matches don't provide a save function */
1046		if (match->save && e->u.user.revision == match->revision)
1047			match->save(ip, e);
1048		else if (match->save)
1049			printf(unsupported_rev);
1050	} else {
1051		if (e->u.match_size) {
1052			fprintf(stderr,
1053				"Can't find library for match `%s'\n",
1054				e->u.user.name);
1055			exit(1);
1056		}
1057	}
1058	return 0;
1059}
1060
1061/* Print a given ip including mask if necessary. */
1062static void print_ip(const char *prefix, const struct in6_addr *ip,
1063		     const struct in6_addr *mask, int invert)
1064{
1065	char buf[51];
1066	int l = xtables_ip6mask_to_cidr(mask);
1067
1068	if (l == 0 && !invert)
1069		return;
1070
1071	printf("%s %s %s",
1072		invert ? " !" : "",
1073		prefix,
1074		inet_ntop(AF_INET6, ip, buf, sizeof buf));
1075
1076	if (l == -1)
1077		printf("/%s", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1078	else
1079		printf("/%d", l);
1080}
1081
1082/* We want this to be readable, so only print out necessary fields.
1083 * Because that's the kind of world I want to live in.
1084 */
1085void print_rule6(const struct ip6t_entry *e,
1086		       struct xtc_handle *h, const char *chain, int counters)
1087{
1088	const struct xt_entry_target *t;
1089	const char *target_name;
1090
1091	/* print counters for iptables-save */
1092	if (counters > 0)
1093		printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1094
1095	/* print chain name */
1096	printf("-A %s", chain);
1097
1098	/* Print IP part. */
1099	print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1100			e->ipv6.invflags & IP6T_INV_SRCIP);
1101
1102	print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1103			e->ipv6.invflags & IP6T_INV_DSTIP);
1104
1105	print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1106		    e->ipv6.invflags & IP6T_INV_VIA_IN);
1107
1108	print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1109		    e->ipv6.invflags & IP6T_INV_VIA_OUT);
1110
1111	print_proto(e->ipv6.proto, e->ipv6.invflags & XT_INV_PROTO);
1112
1113#if 0
1114	/* not definied in ipv6
1115	 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1116	if (e->ipv6.flags & IPT_F_FRAG)
1117		printf("%s -f",
1118		       e->ipv6.invflags & IP6T_INV_FRAG ? " !" : "");
1119#endif
1120
1121	if (e->ipv6.flags & IP6T_F_TOS)
1122		printf("%s -? %d",
1123		       e->ipv6.invflags & IP6T_INV_TOS ? " !" : "",
1124		       e->ipv6.tos);
1125
1126	/* Print matchinfo part */
1127	if (e->target_offset) {
1128		IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1129	}
1130
1131	/* print counters for iptables -R */
1132	if (counters < 0)
1133		printf(" -c %llu %llu", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1134
1135	/* Print target name and targinfo part */
1136	target_name = ip6tc_get_target(e, h);
1137	t = ip6t_get_target((struct ip6t_entry *)e);
1138	if (t->u.user.name[0]) {
1139		struct xtables_target *target =
1140			xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
1141
1142		if (!target) {
1143			fprintf(stderr, "Can't find library for target `%s'\n",
1144				t->u.user.name);
1145			exit(1);
1146		}
1147
1148		printf(" -j %s", target->alias ? target->alias(t) : target_name);
1149		if (target->save && t->u.user.revision == target->revision)
1150			target->save(&e->ipv6, t);
1151		else if (target->save)
1152			printf(unsupported_rev);
1153		else {
1154			/* If the target size is greater than xt_entry_target
1155			 * there is something to be saved, we just don't know
1156			 * how to print it */
1157			if (t->u.target_size !=
1158			    sizeof(struct xt_entry_target)) {
1159				fprintf(stderr, "Target `%s' is missing "
1160						"save function\n",
1161					t->u.user.name);
1162				exit(1);
1163			}
1164		}
1165	} else if (target_name && (*target_name != '\0'))
1166#ifdef IP6T_F_GOTO
1167		printf(" -%c %s", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1168#else
1169		printf(" -j %s", target_name);
1170#endif
1171
1172	printf("\n");
1173}
1174
1175static int
1176list_rules(const xt_chainlabel chain, int rulenum, int counters,
1177	     struct xtc_handle *handle)
1178{
1179	const char *this = NULL;
1180	int found = 0;
1181
1182	if (counters)
1183	    counters = -1;		/* iptables -c format */
1184
1185	/* Dump out chain names first,
1186	 * thereby preventing dependency conflicts */
1187	if (!rulenum) for (this = ip6tc_first_chain(handle);
1188	     this;
1189	     this = ip6tc_next_chain(handle)) {
1190		if (chain && strcmp(this, chain) != 0)
1191			continue;
1192
1193		if (ip6tc_builtin(this, handle)) {
1194			struct xt_counters count;
1195			printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1196			if (counters)
1197			    printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1198			printf("\n");
1199		} else {
1200			printf("-N %s\n", this);
1201		}
1202	}
1203
1204	for (this = ip6tc_first_chain(handle);
1205	     this;
1206	     this = ip6tc_next_chain(handle)) {
1207		const struct ip6t_entry *e;
1208		int num = 0;
1209
1210		if (chain && strcmp(this, chain) != 0)
1211			continue;
1212
1213		/* Dump out rules */
1214		e = ip6tc_first_rule(this, handle);
1215		while(e) {
1216			num++;
1217			if (!rulenum || num == rulenum)
1218			    print_rule6(e, handle, this, counters);
1219			e = ip6tc_next_rule(e, handle);
1220		}
1221		found = 1;
1222	}
1223
1224	errno = ENOENT;
1225	return found;
1226}
1227
1228static struct ip6t_entry *
1229generate_entry(const struct ip6t_entry *fw,
1230	       struct xtables_rule_match *matches,
1231	       struct xt_entry_target *target)
1232{
1233	unsigned int size;
1234	struct xtables_rule_match *matchp;
1235	struct ip6t_entry *e;
1236
1237	size = sizeof(struct ip6t_entry);
1238	for (matchp = matches; matchp; matchp = matchp->next)
1239		size += matchp->match->m->u.match_size;
1240
1241	e = xtables_malloc(size + target->u.target_size);
1242	*e = *fw;
1243	e->target_offset = size;
1244	e->next_offset = size + target->u.target_size;
1245
1246	size = 0;
1247	for (matchp = matches; matchp; matchp = matchp->next) {
1248		memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1249		size += matchp->match->m->u.match_size;
1250	}
1251	memcpy(e->elems + size, target, target->u.target_size);
1252
1253	return e;
1254}
1255
1256static void command_jump(struct iptables_command_state *cs)
1257{
1258	size_t size;
1259
1260	set_option(&cs->options, OPT_JUMP, &cs->fw6.ipv6.invflags, cs->invert);
1261	cs->jumpto = parse_target(optarg);
1262	/* TRY_LOAD (may be chain name) */
1263	cs->target = xtables_find_target(cs->jumpto, XTF_TRY_LOAD);
1264
1265	if (cs->target == NULL)
1266		return;
1267
1268	size = XT_ALIGN(sizeof(struct xt_entry_target)) + cs->target->size;
1269
1270	cs->target->t = xtables_calloc(1, size);
1271	cs->target->t->u.target_size = size;
1272	if (cs->target->real_name == NULL) {
1273		strcpy(cs->target->t->u.user.name, cs->jumpto);
1274	} else {
1275		strcpy(cs->target->t->u.user.name, cs->target->real_name);
1276		if (!(cs->target->ext_flags & XTABLES_EXT_ALIAS))
1277			fprintf(stderr, "Notice: The %s target is converted into %s target "
1278			        "in rule listing and saving.\n",
1279			        cs->jumpto, cs->target->real_name);
1280	}
1281	cs->target->t->u.user.revision = cs->target->revision;
1282
1283	xs_init_target(cs->target);
1284	if (cs->target->x6_options != NULL)
1285		opts = xtables_options_xfrm(ip6tables_globals.orig_opts, opts,
1286					    cs->target->x6_options,
1287					    &cs->target->option_offset);
1288	else
1289		opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1290					     cs->target->extra_opts,
1291					     &cs->target->option_offset);
1292	if (opts == NULL)
1293		xtables_error(OTHER_PROBLEM, "can't alloc memory!");
1294}
1295
1296static void command_match(struct iptables_command_state *cs)
1297{
1298	struct xtables_match *m;
1299	size_t size;
1300
1301	if (cs->invert)
1302		xtables_error(PARAMETER_PROBLEM,
1303			   "unexpected ! flag before --match");
1304
1305	m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED, &cs->matches);
1306	size = XT_ALIGN(sizeof(struct xt_entry_match)) + m->size;
1307	m->m = xtables_calloc(1, size);
1308	m->m->u.match_size = size;
1309	if (m->real_name == NULL) {
1310		strcpy(m->m->u.user.name, m->name);
1311	} else {
1312		strcpy(m->m->u.user.name, m->real_name);
1313		if (!(m->ext_flags & XTABLES_EXT_ALIAS))
1314			fprintf(stderr, "Notice: The %s match is converted into %s match "
1315			        "in rule listing and saving.\n", m->name, m->real_name);
1316	}
1317	m->m->u.user.revision = m->revision;
1318
1319	xs_init_match(m);
1320	if (m == m->next)
1321		return;
1322	/* Merge options for non-cloned matches */
1323	if (m->x6_options != NULL)
1324		opts = xtables_options_xfrm(ip6tables_globals.orig_opts, opts,
1325					    m->x6_options, &m->option_offset);
1326	else if (m->extra_opts != NULL)
1327		opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
1328					     m->extra_opts, &m->option_offset);
1329}
1330
1331int do_command6(int argc, char *argv[], char **table,
1332		struct xtc_handle **handle, bool restore)
1333{
1334	struct iptables_command_state cs;
1335	struct ip6t_entry *e = NULL;
1336	unsigned int nsaddrs = 0, ndaddrs = 0;
1337	struct in6_addr *saddrs = NULL, *daddrs = NULL;
1338	struct in6_addr *smasks = NULL, *dmasks = NULL;
1339
1340	int verbose = 0;
1341	int wait = 0;
1342	struct timeval wait_interval = {
1343		.tv_sec	= 1,
1344	};
1345	bool wait_interval_set = false;
1346	const char *chain = NULL;
1347	const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1348	const char *policy = NULL, *newname = NULL;
1349	unsigned int rulenum = 0, command = 0;
1350	const char *pcnt = NULL, *bcnt = NULL;
1351	int ret = 1;
1352	struct xtables_match *m;
1353	struct xtables_rule_match *matchp;
1354	struct xtables_target *t;
1355	unsigned long long cnt;
1356
1357	memset(&cs, 0, sizeof(cs));
1358	cs.jumpto = "";
1359	cs.argv = argv;
1360
1361	/* re-set optind to 0 in case do_command6 gets called
1362	 * a second time */
1363	optind = 0;
1364
1365	/* clear mflags in case do_command6 gets called a second time
1366	 * (we clear the global list of all matches for security)*/
1367	for (m = xtables_matches; m; m = m->next)
1368		m->mflags = 0;
1369
1370	for (t = xtables_targets; t; t = t->next) {
1371		t->tflags = 0;
1372		t->used = 0;
1373	}
1374
1375	/* Suppress error messages: we may add new options if we
1376           demand-load a protocol. */
1377	opterr = 0;
1378
1379	opts = xt_params->orig_opts;
1380	while ((cs.c = getopt_long(argc, argv,
1381	   "-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvw::W::nt:m:xc:g:46",
1382					   opts, NULL)) != -1) {
1383		switch (cs.c) {
1384			/*
1385			 * Command selection
1386			 */
1387		case 'A':
1388			add_command(&command, CMD_APPEND, CMD_NONE,
1389				    cs.invert);
1390			chain = optarg;
1391			break;
1392
1393		case 'C':
1394			add_command(&command, CMD_CHECK, CMD_NONE,
1395			            cs.invert);
1396			chain = optarg;
1397			break;
1398
1399		case 'D':
1400			add_command(&command, CMD_DELETE, CMD_NONE,
1401				    cs.invert);
1402			chain = optarg;
1403			if (xs_has_arg(argc, argv)) {
1404				rulenum = parse_rulenumber(argv[optind++]);
1405				command = CMD_DELETE_NUM;
1406			}
1407			break;
1408
1409		case 'R':
1410			add_command(&command, CMD_REPLACE, CMD_NONE,
1411				    cs.invert);
1412			chain = optarg;
1413			if (xs_has_arg(argc, argv))
1414				rulenum = parse_rulenumber(argv[optind++]);
1415			else
1416				xtables_error(PARAMETER_PROBLEM,
1417					   "-%c requires a rule number",
1418					   cmd2char(CMD_REPLACE));
1419			break;
1420
1421		case 'I':
1422			add_command(&command, CMD_INSERT, CMD_NONE,
1423				    cs.invert);
1424			chain = optarg;
1425			if (xs_has_arg(argc, argv))
1426				rulenum = parse_rulenumber(argv[optind++]);
1427			else rulenum = 1;
1428			break;
1429
1430		case 'L':
1431			add_command(&command, CMD_LIST,
1432				    CMD_ZERO | CMD_ZERO_NUM, cs.invert);
1433			if (optarg) chain = optarg;
1434			else if (xs_has_arg(argc, argv))
1435				chain = argv[optind++];
1436			if (xs_has_arg(argc, argv))
1437				rulenum = parse_rulenumber(argv[optind++]);
1438			break;
1439
1440		case 'S':
1441			add_command(&command, CMD_LIST_RULES,
1442				    CMD_ZERO | CMD_ZERO_NUM, cs.invert);
1443			if (optarg) chain = optarg;
1444			else if (xs_has_arg(argc, argv))
1445				chain = argv[optind++];
1446			if (xs_has_arg(argc, argv))
1447				rulenum = parse_rulenumber(argv[optind++]);
1448			break;
1449
1450		case 'F':
1451			add_command(&command, CMD_FLUSH, CMD_NONE,
1452				    cs.invert);
1453			if (optarg) chain = optarg;
1454			else if (xs_has_arg(argc, argv))
1455				chain = argv[optind++];
1456			break;
1457
1458		case 'Z':
1459			add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
1460				    cs.invert);
1461			if (optarg) chain = optarg;
1462			else if (xs_has_arg(argc, argv))
1463				chain = argv[optind++];
1464			if (xs_has_arg(argc, argv)) {
1465				rulenum = parse_rulenumber(argv[optind++]);
1466				command = CMD_ZERO_NUM;
1467			}
1468			break;
1469
1470		case 'N':
1471			parse_chain(optarg);
1472			add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1473				    cs.invert);
1474			chain = optarg;
1475			break;
1476
1477		case 'X':
1478			add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1479				    cs.invert);
1480			if (optarg) chain = optarg;
1481			else if (xs_has_arg(argc, argv))
1482				chain = argv[optind++];
1483			break;
1484
1485		case 'E':
1486			add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1487				    cs.invert);
1488			chain = optarg;
1489			if (xs_has_arg(argc, argv))
1490				newname = argv[optind++];
1491			else
1492				xtables_error(PARAMETER_PROBLEM,
1493					   "-%c requires old-chain-name and "
1494					   "new-chain-name",
1495					    cmd2char(CMD_RENAME_CHAIN));
1496			break;
1497
1498		case 'P':
1499			add_command(&command, CMD_SET_POLICY, CMD_NONE,
1500				    cs.invert);
1501			chain = optarg;
1502			if (xs_has_arg(argc, argv))
1503				policy = argv[optind++];
1504			else
1505				xtables_error(PARAMETER_PROBLEM,
1506					   "-%c requires a chain and a policy",
1507					   cmd2char(CMD_SET_POLICY));
1508			break;
1509
1510		case 'h':
1511			if (!optarg)
1512				optarg = argv[optind];
1513
1514			/* ip6tables -p icmp -h */
1515			if (!cs.matches && cs.protocol)
1516				xtables_find_match(cs.protocol, XTF_TRY_LOAD,
1517					&cs.matches);
1518
1519			exit_printhelp(cs.matches);
1520
1521			/*
1522			 * Option selection
1523			 */
1524		case 'p':
1525			set_option(&cs.options, OPT_PROTOCOL, &cs.fw6.ipv6.invflags,
1526				   cs.invert);
1527
1528			/* Canonicalize into lower case */
1529			for (cs.protocol = optarg; *cs.protocol; cs.protocol++)
1530				*cs.protocol = tolower(*cs.protocol);
1531
1532			cs.protocol = optarg;
1533			cs.fw6.ipv6.proto = xtables_parse_protocol(cs.protocol);
1534			cs.fw6.ipv6.flags |= IP6T_F_PROTO;
1535
1536			if (cs.fw6.ipv6.proto == 0
1537			    && (cs.fw6.ipv6.invflags & XT_INV_PROTO))
1538				xtables_error(PARAMETER_PROBLEM,
1539					   "rule would never match protocol");
1540
1541			if (is_exthdr(cs.fw6.ipv6.proto)
1542			    && (cs.fw6.ipv6.invflags & XT_INV_PROTO) == 0)
1543				fprintf(stderr,
1544					"Warning: never matched protocol: %s. "
1545					"use extension match instead.\n",
1546					cs.protocol);
1547			break;
1548
1549		case 's':
1550			set_option(&cs.options, OPT_SOURCE, &cs.fw6.ipv6.invflags,
1551				   cs.invert);
1552			shostnetworkmask = optarg;
1553			break;
1554
1555		case 'd':
1556			set_option(&cs.options, OPT_DESTINATION, &cs.fw6.ipv6.invflags,
1557				   cs.invert);
1558			dhostnetworkmask = optarg;
1559			break;
1560
1561#ifdef IP6T_F_GOTO
1562		case 'g':
1563			set_option(&cs.options, OPT_JUMP, &cs.fw6.ipv6.invflags,
1564					cs.invert);
1565			cs.fw6.ipv6.flags |= IP6T_F_GOTO;
1566			cs.jumpto = parse_target(optarg);
1567			break;
1568#endif
1569
1570		case 'j':
1571			command_jump(&cs);
1572			break;
1573
1574
1575		case 'i':
1576			if (*optarg == '\0')
1577				xtables_error(PARAMETER_PROBLEM,
1578					"Empty interface is likely to be "
1579					"undesired");
1580			set_option(&cs.options, OPT_VIANAMEIN, &cs.fw6.ipv6.invflags,
1581				   cs.invert);
1582			xtables_parse_interface(optarg,
1583					cs.fw6.ipv6.iniface,
1584					cs.fw6.ipv6.iniface_mask);
1585			break;
1586
1587		case 'o':
1588			if (*optarg == '\0')
1589				xtables_error(PARAMETER_PROBLEM,
1590					"Empty interface is likely to be "
1591					"undesired");
1592			set_option(&cs.options, OPT_VIANAMEOUT, &cs.fw6.ipv6.invflags,
1593				   cs.invert);
1594			xtables_parse_interface(optarg,
1595					cs.fw6.ipv6.outiface,
1596					cs.fw6.ipv6.outiface_mask);
1597			break;
1598
1599		case 'v':
1600			if (!verbose)
1601				set_option(&cs.options, OPT_VERBOSE,
1602					   &cs.fw6.ipv6.invflags, cs.invert);
1603			verbose++;
1604			break;
1605
1606		case 'w':
1607			if (restore) {
1608				xtables_error(PARAMETER_PROBLEM,
1609					      "You cannot use `-w' from "
1610					      "ip6tables-restore");
1611			}
1612			wait = parse_wait_time(argc, argv);
1613			break;
1614
1615		case 'W':
1616			if (restore) {
1617				xtables_error(PARAMETER_PROBLEM,
1618					      "You cannot use `-W' from "
1619					      "ip6tables-restore");
1620			}
1621			parse_wait_interval(argc, argv, &wait_interval);
1622			wait_interval_set = true;
1623			break;
1624
1625		case 'm':
1626			command_match(&cs);
1627			break;
1628
1629		case 'n':
1630			set_option(&cs.options, OPT_NUMERIC, &cs.fw6.ipv6.invflags,
1631				   cs.invert);
1632			break;
1633
1634		case 't':
1635			if (cs.invert)
1636				xtables_error(PARAMETER_PROBLEM,
1637					   "unexpected ! flag before --table");
1638			*table = optarg;
1639			break;
1640
1641		case 'x':
1642			set_option(&cs.options, OPT_EXPANDED, &cs.fw6.ipv6.invflags,
1643				   cs.invert);
1644			break;
1645
1646		case 'V':
1647			if (cs.invert)
1648				printf("Not %s ;-)\n", prog_vers);
1649			else
1650				printf("%s v%s\n",
1651				       prog_name, prog_vers);
1652			exit(0);
1653
1654		case '0':
1655			set_option(&cs.options, OPT_LINENUMBERS, &cs.fw6.ipv6.invflags,
1656				   cs.invert);
1657			break;
1658
1659		case 'M':
1660			xtables_modprobe_program = optarg;
1661			break;
1662
1663		case 'c':
1664
1665			set_option(&cs.options, OPT_COUNTERS, &cs.fw6.ipv6.invflags,
1666				   cs.invert);
1667			pcnt = optarg;
1668			bcnt = strchr(pcnt + 1, ',');
1669			if (bcnt)
1670			    bcnt++;
1671			if (!bcnt && xs_has_arg(argc, argv))
1672				bcnt = argv[optind++];
1673			if (!bcnt)
1674				xtables_error(PARAMETER_PROBLEM,
1675					"-%c requires packet and byte counter",
1676					opt2char(OPT_COUNTERS));
1677
1678			if (sscanf(pcnt, "%llu", &cnt) != 1)
1679				xtables_error(PARAMETER_PROBLEM,
1680					"-%c packet counter not numeric",
1681					opt2char(OPT_COUNTERS));
1682			cs.fw6.counters.pcnt = cnt;
1683
1684			if (sscanf(bcnt, "%llu", &cnt) != 1)
1685				xtables_error(PARAMETER_PROBLEM,
1686					"-%c byte counter not numeric",
1687					opt2char(OPT_COUNTERS));
1688			cs.fw6.counters.bcnt = cnt;
1689			break;
1690
1691		case '4':
1692			/* This is not the IPv4 iptables */
1693			if (line != -1)
1694				return 1; /* success: line ignored */
1695			fprintf(stderr, "This is the IPv6 version of ip6tables.\n");
1696			exit_tryhelp(2);
1697
1698		case '6':
1699			/* This is indeed the IPv6 ip6tables */
1700			break;
1701
1702		case 1: /* non option */
1703			if (optarg[0] == '!' && optarg[1] == '\0') {
1704				if (cs.invert)
1705					xtables_error(PARAMETER_PROBLEM,
1706						   "multiple consecutive ! not"
1707						   " allowed");
1708				cs.invert = TRUE;
1709				optarg[0] = '\0';
1710				continue;
1711			}
1712			fprintf(stderr, "Bad argument `%s'\n", optarg);
1713			exit_tryhelp(2);
1714
1715		default:
1716			if (command_default(&cs, &ip6tables_globals) == 1)
1717				/*
1718				 * If new options were loaded, we must retry
1719				 * getopt immediately and not allow
1720				 * cs.invert=FALSE to be executed.
1721				 */
1722				continue;
1723			break;
1724		}
1725		cs.invert = FALSE;
1726	}
1727
1728	if (!wait && wait_interval_set)
1729		xtables_error(PARAMETER_PROBLEM,
1730			      "--wait-interval only makes sense with --wait\n");
1731
1732	if (strcmp(*table, "nat") == 0 &&
1733	    ((policy != NULL && strcmp(policy, "DROP") == 0) ||
1734	    (cs.jumpto != NULL && strcmp(cs.jumpto, "DROP") == 0)))
1735		xtables_error(PARAMETER_PROBLEM,
1736			"\nThe \"nat\" table is not intended for filtering, "
1737		        "the use of DROP is therefore inhibited.\n\n");
1738
1739	for (matchp = cs.matches; matchp; matchp = matchp->next)
1740		xtables_option_mfcall(matchp->match);
1741	if (cs.target != NULL)
1742		xtables_option_tfcall(cs.target);
1743
1744	/* Fix me: must put inverse options checking here --MN */
1745
1746	if (optind < argc)
1747		xtables_error(PARAMETER_PROBLEM,
1748			   "unknown arguments found on commandline");
1749	if (!command)
1750		xtables_error(PARAMETER_PROBLEM, "no command specified");
1751	if (cs.invert)
1752		xtables_error(PARAMETER_PROBLEM,
1753			   "nothing appropriate following !");
1754
1755	if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND | CMD_CHECK)) {
1756		if (!(cs.options & OPT_DESTINATION))
1757			dhostnetworkmask = "::0/0";
1758		if (!(cs.options & OPT_SOURCE))
1759			shostnetworkmask = "::0/0";
1760	}
1761
1762	if (shostnetworkmask)
1763		xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1764					  &smasks, &nsaddrs);
1765
1766	if (dhostnetworkmask)
1767		xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1768					  &dmasks, &ndaddrs);
1769
1770	if ((nsaddrs > 1 || ndaddrs > 1) &&
1771	    (cs.fw6.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1772		xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1773			   " source or destination IP addresses");
1774
1775	if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1776		xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
1777			   "specify a unique address");
1778
1779	generic_opt_check(command, cs.options);
1780
1781	/* Attempt to acquire the xtables lock */
1782	if (!restore && xtables_lock(wait, &wait_interval) == XT_LOCK_BUSY) {
1783		fprintf(stderr, "Another app is currently holding the xtables lock. ");
1784		if (wait == 0)
1785			fprintf(stderr, "Perhaps you want to use the -w option?\n");
1786		else
1787			fprintf(stderr, "Stopped waiting after %ds.\n", wait);
1788		xtables_free_opts(1);
1789		exit(RESOURCE_PROBLEM);
1790	}
1791
1792	/* only allocate handle if we weren't called with a handle */
1793	if (!*handle)
1794		*handle = ip6tc_init(*table);
1795
1796	/* try to insmod the module if iptc_init failed */
1797	if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
1798		*handle = ip6tc_init(*table);
1799
1800	if (!*handle)
1801		xtables_error(VERSION_PROBLEM,
1802			"can't initialize ip6tables table `%s': %s",
1803			*table, ip6tc_strerror(errno));
1804
1805	if (command == CMD_APPEND
1806	    || command == CMD_DELETE
1807	    || command == CMD_CHECK
1808	    || command == CMD_INSERT
1809	    || command == CMD_REPLACE) {
1810		if (strcmp(chain, "PREROUTING") == 0
1811		    || strcmp(chain, "INPUT") == 0) {
1812			/* -o not valid with incoming packets. */
1813			if (cs.options & OPT_VIANAMEOUT)
1814				xtables_error(PARAMETER_PROBLEM,
1815					   "Can't use -%c with %s\n",
1816					   opt2char(OPT_VIANAMEOUT),
1817					   chain);
1818		}
1819
1820		if (strcmp(chain, "POSTROUTING") == 0
1821		    || strcmp(chain, "OUTPUT") == 0) {
1822			/* -i not valid with outgoing packets */
1823			if (cs.options & OPT_VIANAMEIN)
1824				xtables_error(PARAMETER_PROBLEM,
1825					   "Can't use -%c with %s\n",
1826					   opt2char(OPT_VIANAMEIN),
1827					   chain);
1828		}
1829
1830		if (cs.target && ip6tc_is_chain(cs.jumpto, *handle)) {
1831			fprintf(stderr,
1832				"Warning: using chain %s, not extension\n",
1833				cs.jumpto);
1834
1835			if (cs.target->t)
1836				free(cs.target->t);
1837
1838			cs.target = NULL;
1839		}
1840
1841		/* If they didn't specify a target, or it's a chain
1842		   name, use standard. */
1843		if (!cs.target
1844		    && (strlen(cs.jumpto) == 0
1845			|| ip6tc_is_chain(cs.jumpto, *handle))) {
1846			size_t size;
1847
1848			cs.target = xtables_find_target(XT_STANDARD_TARGET,
1849					XTF_LOAD_MUST_SUCCEED);
1850
1851			size = sizeof(struct xt_entry_target)
1852				+ cs.target->size;
1853			cs.target->t = xtables_calloc(1, size);
1854			cs.target->t->u.target_size = size;
1855			strcpy(cs.target->t->u.user.name, cs.jumpto);
1856			xs_init_target(cs.target);
1857		}
1858
1859		if (!cs.target) {
1860			/* It is no chain, and we can't load a plugin.
1861			 * We cannot know if the plugin is corrupt, non
1862			 * existent OR if the user just misspelled a
1863			 * chain.
1864			 */
1865#ifdef IP6T_F_GOTO
1866			if (cs.fw6.ipv6.flags & IP6T_F_GOTO)
1867				xtables_error(PARAMETER_PROBLEM,
1868						"goto '%s' is not a chain\n",
1869						cs.jumpto);
1870#endif
1871			xtables_find_target(cs.jumpto, XTF_LOAD_MUST_SUCCEED);
1872		} else {
1873			e = generate_entry(&cs.fw6, cs.matches, cs.target->t);
1874			free(cs.target->t);
1875		}
1876	}
1877
1878	switch (command) {
1879	case CMD_APPEND:
1880		ret = append_entry(chain, e,
1881				   nsaddrs, saddrs, smasks,
1882				   ndaddrs, daddrs, dmasks,
1883				   cs.options&OPT_VERBOSE,
1884				   *handle);
1885		break;
1886	case CMD_DELETE:
1887		ret = delete_entry(chain, e,
1888				   nsaddrs, saddrs, smasks,
1889				   ndaddrs, daddrs, dmasks,
1890				   cs.options&OPT_VERBOSE,
1891				   *handle, cs.matches, cs.target);
1892		break;
1893	case CMD_DELETE_NUM:
1894		ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
1895		break;
1896	case CMD_CHECK:
1897		ret = check_entry(chain, e,
1898				   nsaddrs, saddrs, smasks,
1899				   ndaddrs, daddrs, dmasks,
1900				   cs.options&OPT_VERBOSE,
1901				   *handle, cs.matches, cs.target);
1902		break;
1903	case CMD_REPLACE:
1904		ret = replace_entry(chain, e, rulenum - 1,
1905				    saddrs, smasks, daddrs, dmasks,
1906				    cs.options&OPT_VERBOSE, *handle);
1907		break;
1908	case CMD_INSERT:
1909		ret = insert_entry(chain, e, rulenum - 1,
1910				   nsaddrs, saddrs, smasks,
1911				   ndaddrs, daddrs, dmasks,
1912				   cs.options&OPT_VERBOSE,
1913				   *handle);
1914		break;
1915	case CMD_FLUSH:
1916		ret = flush_entries6(chain, cs.options&OPT_VERBOSE, *handle);
1917		break;
1918	case CMD_ZERO:
1919		ret = zero_entries(chain, cs.options&OPT_VERBOSE, *handle);
1920		break;
1921	case CMD_ZERO_NUM:
1922		ret = ip6tc_zero_counter(chain, rulenum, *handle);
1923		break;
1924	case CMD_LIST:
1925	case CMD_LIST|CMD_ZERO:
1926	case CMD_LIST|CMD_ZERO_NUM:
1927		ret = list_entries(chain,
1928				   rulenum,
1929				   cs.options&OPT_VERBOSE,
1930				   cs.options&OPT_NUMERIC,
1931				   cs.options&OPT_EXPANDED,
1932				   cs.options&OPT_LINENUMBERS,
1933				   *handle);
1934		if (ret && (command & CMD_ZERO))
1935			ret = zero_entries(chain,
1936					   cs.options&OPT_VERBOSE, *handle);
1937		if (ret && (command & CMD_ZERO_NUM))
1938			ret = ip6tc_zero_counter(chain, rulenum, *handle);
1939		break;
1940	case CMD_LIST_RULES:
1941	case CMD_LIST_RULES|CMD_ZERO:
1942	case CMD_LIST_RULES|CMD_ZERO_NUM:
1943		ret = list_rules(chain,
1944				   rulenum,
1945				   cs.options&OPT_VERBOSE,
1946				   *handle);
1947		if (ret && (command & CMD_ZERO))
1948			ret = zero_entries(chain,
1949					   cs.options&OPT_VERBOSE, *handle);
1950		if (ret && (command & CMD_ZERO_NUM))
1951			ret = ip6tc_zero_counter(chain, rulenum, *handle);
1952		break;
1953	case CMD_NEW_CHAIN:
1954		ret = ip6tc_create_chain(chain, *handle);
1955		break;
1956	case CMD_DELETE_CHAIN:
1957		ret = delete_chain6(chain, cs.options&OPT_VERBOSE, *handle);
1958		break;
1959	case CMD_RENAME_CHAIN:
1960		ret = ip6tc_rename_chain(chain, newname,	*handle);
1961		break;
1962	case CMD_SET_POLICY:
1963		ret = ip6tc_set_policy(chain, policy, cs.options&OPT_COUNTERS ? &cs.fw6.counters : NULL, *handle);
1964		break;
1965	default:
1966		/* We should never reach this... */
1967		exit_tryhelp(2);
1968	}
1969
1970	if (verbose > 1)
1971		dump_entries6(*handle);
1972
1973	xtables_rule_matches_free(&cs.matches);
1974
1975	if (e != NULL) {
1976		free(e);
1977		e = NULL;
1978	}
1979
1980	free(saddrs);
1981	free(smasks);
1982	free(daddrs);
1983	free(dmasks);
1984	xtables_free_opts(1);
1985
1986	return ret;
1987}
1988