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