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