xtables.c 29.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Code to take an iptables-style command line and do it. */

/*
 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
 *
 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
 *		    Paul 'Rusty' Russell <rusty@rustcorp.com.au>
 *		    Marc Boucher <marc+nf@mbsi.ca>
 *		    James Morris <jmorris@intercode.com.au>
 *		    Harald Welte <laforge@gnumonks.org>
 *		    Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
27
#include "config.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <getopt.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>
#include <unistd.h>
#include <iptables.h>
#include <xtables.h>
#include <fcntl.h>
#include "xshared.h"
#include "nft-shared.h"
#include "nft.h"

static struct option original_opts[] = {
	{.name = "append",	  .has_arg = 1, .val = 'A'},
	{.name = "delete",	  .has_arg = 1, .val = 'D'},
	{.name = "check",	  .has_arg = 1, .val = 'C'},
	{.name = "insert",	  .has_arg = 1, .val = 'I'},
	{.name = "replace",	  .has_arg = 1, .val = 'R'},
	{.name = "list",	  .has_arg = 2, .val = 'L'},
	{.name = "list-rules",	  .has_arg = 2, .val = 'S'},
	{.name = "flush",	  .has_arg = 2, .val = 'F'},
	{.name = "zero",	  .has_arg = 2, .val = 'Z'},
	{.name = "new-chain",	  .has_arg = 1, .val = 'N'},
	{.name = "delete-chain",  .has_arg = 2, .val = 'X'},
	{.name = "rename-chain",  .has_arg = 1, .val = 'E'},
	{.name = "policy",	  .has_arg = 1, .val = 'P'},
	{.name = "source",	  .has_arg = 1, .val = 's'},
	{.name = "destination",   .has_arg = 1, .val = 'd'},
	{.name = "src",		  .has_arg = 1, .val = 's'}, /* synonym */
	{.name = "dst",		  .has_arg = 1, .val = 'd'}, /* synonym */
	{.name = "protocol",	  .has_arg = 1, .val = 'p'},
	{.name = "in-interface",  .has_arg = 1, .val = 'i'},
	{.name = "jump",	  .has_arg = 1, .val = 'j'},
	{.name = "table",	  .has_arg = 1, .val = 't'},
	{.name = "match",	  .has_arg = 1, .val = 'm'},
	{.name = "numeric",	  .has_arg = 0, .val = 'n'},
	{.name = "out-interface", .has_arg = 1, .val = 'o'},
	{.name = "verbose",	  .has_arg = 0, .val = 'v'},
	{.name = "wait",	  .has_arg = 2, .val = 'w'},
73
	{.name = "wait-interval", .has_arg = 2, .val = 'W'},
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	{.name = "exact",	  .has_arg = 0, .val = 'x'},
	{.name = "fragments",	  .has_arg = 0, .val = 'f'},
	{.name = "version",	  .has_arg = 0, .val = 'V'},
	{.name = "help",	  .has_arg = 2, .val = 'h'},
	{.name = "line-numbers",  .has_arg = 0, .val = '0'},
	{.name = "modprobe",	  .has_arg = 1, .val = 'M'},
	{.name = "set-counters",  .has_arg = 1, .val = 'c'},
	{.name = "goto",	  .has_arg = 1, .val = 'g'},
	{.name = "ipv4",	  .has_arg = 0, .val = '4'},
	{.name = "ipv6",	  .has_arg = 0, .val = '6'},
	{NULL},
};

void xtables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));

struct xtables_globals xtables_globals = {
	.option_offset = 0,
91
	.program_version = PACKAGE_VERSION,
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	.orig_opts = original_opts,
	.exit_err = xtables_exit_error,
	.compat_rev = nft_compatible_revision,
};

static const int inverse_for_options[NUMBER_OF_OPT] =
{
/* -n */ 0,
/* -s */ IPT_INV_SRCIP,
/* -d */ IPT_INV_DSTIP,
/* -p */ XT_INV_PROTO,
/* -j */ 0,
/* -v */ 0,
/* -x */ 0,
/* -i */ IPT_INV_VIA_IN,
/* -o */ IPT_INV_VIA_OUT,
/*--line*/ 0,
/* -c */ 0,
/* -f */ IPT_INV_FRAG,
};

113
114
115
#define opts xt_params->opts
#define prog_name xt_params->program_name
#define prog_vers xt_params->program_version
116
117
118
119
120
121
122
123
124
125
126
127
128

static void __attribute__((noreturn))
exit_tryhelp(int status)
{
	if (line != -1)
		fprintf(stderr, "Error occurred at line: %d\n", line);
	fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
			prog_name, prog_name);
	xtables_free_opts(1);
	exit(status);
}

static void
129
printhelp(const struct xtables_rule_match *matches)
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{
	printf("%s v%s\n\n"
"Usage: %s -[ACD] chain rule-specification [options]\n"
"	%s -I chain [rulenum] rule-specification [options]\n"
"	%s -R chain rulenum rule-specification [options]\n"
"	%s -D chain rulenum [options]\n"
"	%s -[LS] [chain [rulenum]] [options]\n"
"	%s -[FZ] [chain] [options]\n"
"	%s -[NX] chain\n"
"	%s -E old-chain-name new-chain-name\n"
"	%s -P chain target [options]\n"
"	%s -h (print this help information)\n\n",
	       prog_name, prog_vers, prog_name, prog_name,
	       prog_name, prog_name, prog_name, prog_name,
	       prog_name, prog_name, prog_name, prog_name);

	printf(
"Commands:\n"
"Either long or short options are allowed.\n"
"  --append  -A chain		Append to chain\n"
"  --check   -C chain		Check for the existence of a rule\n"
"  --delete  -D chain		Delete matching rule from chain\n"
"  --delete  -D chain rulenum\n"
"				Delete rule rulenum (1 = first) from chain\n"
"  --insert  -I chain [rulenum]\n"
"				Insert in chain as rulenum (default 1=first)\n"
"  --replace -R chain rulenum\n"
"				Replace rule rulenum (1 = first) in chain\n"
"  --list    -L [chain [rulenum]]\n"
"				List the rules in a chain or all chains\n"
"  --list-rules -S [chain [rulenum]]\n"
"				Print the rules in a chain or all chains\n"
"  --flush   -F [chain]		Delete all rules in  chain or all chains\n"
"  --zero    -Z [chain [rulenum]]\n"
"				Zero counters in chain or all chains\n"
"  --new     -N chain		Create a new user-defined chain\n"
"  --delete-chain\n"
"	     -X [chain]		Delete a user-defined chain\n"
"  --policy  -P chain target\n"
"				Change policy on chain to target\n"
"  --rename-chain\n"
"	     -E old-chain new-chain\n"
"				Change chain name, (moving any references)\n"

"Options:\n"
"    --ipv4	-4		Nothing (line is ignored by ip6tables-restore)\n"
"    --ipv6	-6		Error (line is ignored by iptables-restore)\n"
"[!] --proto	-p proto	protocol: by number or name, eg. `tcp'\n"
"[!] --source	-s address[/mask][...]\n"
"				source specification\n"
"[!] --destination -d address[/mask][...]\n"
"				destination specification\n"
"[!] --in-interface -i input name[+]\n"
"				network interface name ([+] for wildcard)\n"
" --jump	-j target\n"
"				target for rule (may load target extension)\n"
#ifdef IPT_F_GOTO
"  --goto      -g chain\n"
"			       jump to chain with no return\n"
#endif
"  --match	-m match\n"
"				extended match (may load extension)\n"
"  --numeric	-n		numeric output of addresses and ports\n"
"[!] --out-interface -o output name[+]\n"
"				network interface name ([+] for wildcard)\n"
"  --table	-t table	table to manipulate (default: `filter')\n"
"  --verbose	-v		verbose mode\n"
197
198
199
"  --wait	-w [seconds]	maximum wait to acquire xtables lock before give up\n"
"  --wait-interval -W [usecs]	wait time to try to acquire xtables lock\n"
"				default is 1 second\n"
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"  --line-numbers		print line numbers when listing\n"
"  --exact	-x		expand numbers (display exact values)\n"
"[!] --fragment	-f		match second or further fragments only\n"
"  --modprobe=<command>		try to insert modules using this command\n"
"  --set-counters PKTS BYTES	set the counter during insert/append\n"
"[!] --version	-V		print package version.\n");

	print_extension_helps(xtables_targets, matches);
}

void
xtables_exit_error(enum xtables_exittype status, const char *msg, ...)
{
	va_list args;

	va_start(args, msg);
216
	fprintf(stderr, "%s v%s (nf_tables): ", prog_name, prog_vers);
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	vfprintf(stderr, msg, args);
	va_end(args);
	fprintf(stderr, "\n");
	if (status == PARAMETER_PROBLEM)
		exit_tryhelp(status);
	if (status == VERSION_PROBLEM)
		fprintf(stderr,
			"Perhaps iptables or your kernel needs to be upgraded.\n");
	/* On error paths, make sure that we don't leak memory */
	xtables_free_opts(1);
	exit(status);
}

/*
 *	All functions starting with "parse" should succeed, otherwise
 *	the program fails.
 *	Most routines return pointers to static data that may change
 *	between calls to the same or other routines with a few exceptions:
 *	"host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
 *	return global static data.
*/

/* Christophe Burki wants `-p 6' to imply `-m tcp'.  */

static void
set_option(unsigned int *options, unsigned int option, uint8_t *invflg,
	   int invert)
{
	if (*options & option)
		xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
			   opt2char(option));
	*options |= option;

	if (invert) {
		unsigned int i;
		for (i = 0; 1 << i != option; i++);

		if (!inverse_for_options[i])
			xtables_error(PARAMETER_PROBLEM,
				   "cannot have ! before -%c",
				   opt2char(option));
		*invflg |= inverse_for_options[i];
	}
}

static int
add_entry(const char *chain,
	  const char *table,
	  struct iptables_command_state *cs,
	  int rulenum, int family,
	  const struct addr_mask s,
	  const struct addr_mask d,
	  bool verbose, struct nft_handle *h, bool append)
{
	unsigned int i, j;
	int ret = 1;

	for (i = 0; i < s.naddrs; i++) {
		if (family == AF_INET) {
			cs->fw.ip.src.s_addr = s.addr.v4[i].s_addr;
			cs->fw.ip.smsk.s_addr = s.mask.v4[i].s_addr;
			for (j = 0; j < d.naddrs; j++) {
				cs->fw.ip.dst.s_addr = d.addr.v4[j].s_addr;
				cs->fw.ip.dmsk.s_addr = d.mask.v4[j].s_addr;

				if (append) {
283
					ret = nft_cmd_rule_append(h, chain, table,
284
							      cs, NULL,
285
286
							      verbose);
				} else {
287
					ret = nft_cmd_rule_insert(h, chain, table,
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
							      cs, rulenum,
							      verbose);
				}
			}
		} else if (family == AF_INET6) {
			memcpy(&cs->fw6.ipv6.src,
			       &s.addr.v6[i], sizeof(struct in6_addr));
			memcpy(&cs->fw6.ipv6.smsk,
			       &s.mask.v6[i], sizeof(struct in6_addr));
			for (j = 0; j < d.naddrs; j++) {
				memcpy(&cs->fw6.ipv6.dst,
				       &d.addr.v6[j], sizeof(struct in6_addr));
				memcpy(&cs->fw6.ipv6.dmsk,
				       &d.mask.v6[j], sizeof(struct in6_addr));
				if (append) {
303
					ret = nft_cmd_rule_append(h, chain, table,
304
							      cs, NULL,
305
306
							      verbose);
				} else {
307
					ret = nft_cmd_rule_insert(h, chain, table,
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
							      cs, rulenum,
							      verbose);
				}
			}
		}
	}

	return ret;
}

static int
replace_entry(const char *chain, const char *table,
	      struct iptables_command_state *cs,
	      unsigned int rulenum,
	      int family,
	      const struct addr_mask s,
	      const struct addr_mask d,
	      bool verbose, struct nft_handle *h)
{
	if (family == AF_INET) {
		cs->fw.ip.src.s_addr = s.addr.v4->s_addr;
		cs->fw.ip.dst.s_addr = d.addr.v4->s_addr;
		cs->fw.ip.smsk.s_addr = s.mask.v4->s_addr;
		cs->fw.ip.dmsk.s_addr = d.mask.v4->s_addr;
	} else if (family == AF_INET6) {
		memcpy(&cs->fw6.ipv6.src, s.addr.v6, sizeof(struct in6_addr));
		memcpy(&cs->fw6.ipv6.dst, d.addr.v6, sizeof(struct in6_addr));
		memcpy(&cs->fw6.ipv6.smsk, s.mask.v6, sizeof(struct in6_addr));
		memcpy(&cs->fw6.ipv6.dmsk, d.mask.v6, sizeof(struct in6_addr));
	} else
		return 1;

340
	return nft_cmd_rule_replace(h, chain, table, cs, rulenum, verbose);
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
}

static int
delete_entry(const char *chain, const char *table,
	     struct iptables_command_state *cs,
	     int family,
	     const struct addr_mask s,
	     const struct addr_mask d,
	     bool verbose,
	     struct nft_handle *h)
{
	unsigned int i, j;
	int ret = 1;

	for (i = 0; i < s.naddrs; i++) {
		if (family == AF_INET) {
			cs->fw.ip.src.s_addr = s.addr.v4[i].s_addr;
			cs->fw.ip.smsk.s_addr = s.mask.v4[i].s_addr;
			for (j = 0; j < d.naddrs; j++) {
				cs->fw.ip.dst.s_addr = d.addr.v4[j].s_addr;
				cs->fw.ip.dmsk.s_addr = d.mask.v4[j].s_addr;
362
				ret = nft_cmd_rule_delete(h, chain,
363
364
365
366
367
368
369
370
371
372
373
374
						      table, cs, verbose);
			}
		} else if (family == AF_INET6) {
			memcpy(&cs->fw6.ipv6.src,
			       &s.addr.v6[i], sizeof(struct in6_addr));
			memcpy(&cs->fw6.ipv6.smsk,
			       &s.mask.v6[i], sizeof(struct in6_addr));
			for (j = 0; j < d.naddrs; j++) {
				memcpy(&cs->fw6.ipv6.dst,
				       &d.addr.v6[j], sizeof(struct in6_addr));
				memcpy(&cs->fw6.ipv6.dmsk,
				       &d.mask.v6[j], sizeof(struct in6_addr));
375
				ret = nft_cmd_rule_delete(h, chain,
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
						      table, cs, verbose);
			}
		}
	}

	return ret;
}

static int
check_entry(const char *chain, const char *table,
	    struct iptables_command_state *cs,
	    int family,
	    const struct addr_mask s,
	    const struct addr_mask d,
	    bool verbose, struct nft_handle *h)
{
	unsigned int i, j;
	int ret = 1;

	for (i = 0; i < s.naddrs; i++) {
		if (family == AF_INET) {
			cs->fw.ip.src.s_addr = s.addr.v4[i].s_addr;
			cs->fw.ip.smsk.s_addr = s.mask.v4[i].s_addr;
			for (j = 0; j < d.naddrs; j++) {
				cs->fw.ip.dst.s_addr = d.addr.v4[j].s_addr;
				cs->fw.ip.dmsk.s_addr = d.mask.v4[j].s_addr;
402
				ret = nft_cmd_rule_check(h, chain,
403
404
405
406
407
408
409
410
411
412
413
414
						     table, cs, verbose);
			}
		} else if (family == AF_INET6) {
			memcpy(&cs->fw6.ipv6.src,
			       &s.addr.v6[i], sizeof(struct in6_addr));
			memcpy(&cs->fw6.ipv6.smsk,
			       &s.mask.v6[i], sizeof(struct in6_addr));
			for (j = 0; j < d.naddrs; j++) {
				memcpy(&cs->fw6.ipv6.dst,
				       &d.addr.v6[j], sizeof(struct in6_addr));
				memcpy(&cs->fw6.ipv6.dmsk,
				       &d.mask.v6[j], sizeof(struct in6_addr));
415
				ret = nft_cmd_rule_check(h, chain,
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
						     table, cs, verbose);
			}
		}
	}

	return ret;
}

static int
list_entries(struct nft_handle *h, const char *chain, const char *table,
	     int rulenum, int verbose, int numeric, int expanded,
	     int linenumbers)
{
	unsigned int format;

	format = FMT_OPTIONS;
	if (!verbose)
		format |= FMT_NOCOUNTS;
	else
		format |= FMT_VIA;

	if (numeric)
		format |= FMT_NUMERIC;

	if (!expanded)
		format |= FMT_KILOMEGAGIGA;

	if (linenumbers)
		format |= FMT_LINENUMBERS;

446
	return nft_cmd_rule_list(h, chain, table, rulenum, format);
447
448
449
450
451
452
453
454
455
}

static int
list_rules(struct nft_handle *h, const char *chain, const char *table,
	   int rulenum, int counters)
{
	if (counters)
	    counters = -1;		/* iptables -c format */

456
	return nft_cmd_rule_list_save(h, chain, table, rulenum, counters);
457
458
}

459
460
461
void do_parse(struct nft_handle *h, int argc, char *argv[],
	      struct nft_xt_cmd_parse *p, struct iptables_command_state *cs,
	      struct xtables_args *args)
462
463
464
{
	struct xtables_match *m;
	struct xtables_rule_match *matchp;
465
466
	bool wait_interval_set = false;
	struct timeval wait_interval;
467
	struct xtables_target *t;
468
	bool table_set = false;
469
	int wait = 0;
470

471
472
473
	memset(cs, 0, sizeof(*cs));
	cs->jumpto = "";
	cs->argv = argv;
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493

	/* re-set optind to 0 in case do_command4 gets called
	 * a second time */
	optind = 0;

	/* clear mflags in case do_command4 gets called a second time
	 * (we clear the global list of all matches for security)*/
	for (m = xtables_matches; m; m = m->next)
		m->mflags = 0;

	for (t = xtables_targets; t; t = t->next) {
		t->tflags = 0;
		t->used = 0;
	}

	/* Suppress error messages: we may add new options if we
	   demand-load a protocol. */
	opterr = 0;

	opts = xt_params->orig_opts;
494
495
	while ((cs->c = getopt_long(argc, argv,
	   "-:A:C:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvw::W::nt:m:xc:g:46",
496
					   opts, NULL)) != -1) {
497
		switch (cs->c) {
498
499
500
501
			/*
			 * Command selection
			 */
		case 'A':
502
503
504
			add_command(&p->command, CMD_APPEND, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
505
506
507
			break;

		case 'C':
508
509
510
			add_command(&p->command, CMD_CHECK, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
511
512
513
			break;

		case 'D':
514
515
516
			add_command(&p->command, CMD_DELETE, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
517
			if (xs_has_arg(argc, argv)) {
518
519
				p->rulenum = parse_rulenumber(argv[optind++]);
				p->command = CMD_DELETE_NUM;
520
521
522
523
			}
			break;

		case 'R':
524
525
526
			add_command(&p->command, CMD_REPLACE, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
527
			if (xs_has_arg(argc, argv))
528
				p->rulenum = parse_rulenumber(argv[optind++]);
529
530
531
532
533
534
535
			else
				xtables_error(PARAMETER_PROBLEM,
					   "-%c requires a rule number",
					   cmd2char(CMD_REPLACE));
			break;

		case 'I':
536
537
538
			add_command(&p->command, CMD_INSERT, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
539
			if (xs_has_arg(argc, argv))
540
541
542
				p->rulenum = parse_rulenumber(argv[optind++]);
			else
				p->rulenum = 1;
543
544
545
			break;

		case 'L':
546
547
548
549
			add_command(&p->command, CMD_LIST,
				    CMD_ZERO | CMD_ZERO_NUM, cs->invert);
			if (optarg)
				p->chain = optarg;
550
			else if (xs_has_arg(argc, argv))
551
				p->chain = argv[optind++];
552
			if (xs_has_arg(argc, argv))
553
				p->rulenum = parse_rulenumber(argv[optind++]);
554
555
556
			break;

		case 'S':
557
558
559
560
			add_command(&p->command, CMD_LIST_RULES,
				    CMD_ZERO|CMD_ZERO_NUM, cs->invert);
			if (optarg)
				p->chain = optarg;
561
			else if (xs_has_arg(argc, argv))
562
				p->chain = argv[optind++];
563
			if (xs_has_arg(argc, argv))
564
				p->rulenum = parse_rulenumber(argv[optind++]);
565
566
567
			break;

		case 'F':
568
569
570
571
			add_command(&p->command, CMD_FLUSH, CMD_NONE,
				    cs->invert);
			if (optarg)
				p->chain = optarg;
572
			else if (xs_has_arg(argc, argv))
573
				p->chain = argv[optind++];
574
575
576
			break;

		case 'Z':
577
578
579
580
			add_command(&p->command, CMD_ZERO,
				    CMD_LIST|CMD_LIST_RULES, cs->invert);
			if (optarg)
				p->chain = optarg;
581
			else if (xs_has_arg(argc, argv))
582
				p->chain = argv[optind++];
583
			if (xs_has_arg(argc, argv)) {
584
585
				p->rulenum = parse_rulenumber(argv[optind++]);
				p->command = CMD_ZERO_NUM;
586
587
588
589
590
591
592
593
594
595
596
597
			}
			break;

		case 'N':
			if (optarg && (*optarg == '-' || *optarg == '!'))
				xtables_error(PARAMETER_PROBLEM,
					   "chain name not allowed to start "
					   "with `%c'\n", *optarg);
			if (xtables_find_target(optarg, XTF_TRY_LOAD))
				xtables_error(PARAMETER_PROBLEM,
					   "chain name may not clash "
					   "with target name\n");
598
599
600
			add_command(&p->command, CMD_NEW_CHAIN, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
601
602
603
			break;

		case 'X':
604
605
606
607
			add_command(&p->command, CMD_DELETE_CHAIN, CMD_NONE,
				    cs->invert);
			if (optarg)
				p->chain = optarg;
608
			else if (xs_has_arg(argc, argv))
609
				p->chain = argv[optind++];
610
611
612
			break;

		case 'E':
613
614
615
			add_command(&p->command, CMD_RENAME_CHAIN, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
616
			if (xs_has_arg(argc, argv))
617
				p->newname = argv[optind++];
618
619
620
621
622
623
624
625
			else
				xtables_error(PARAMETER_PROBLEM,
					   "-%c requires old-chain-name and "
					   "new-chain-name",
					    cmd2char(CMD_RENAME_CHAIN));
			break;

		case 'P':
626
627
628
			add_command(&p->command, CMD_SET_POLICY, CMD_NONE,
				    cs->invert);
			p->chain = optarg;
629
			if (xs_has_arg(argc, argv))
630
				p->policy = argv[optind++];
631
632
633
634
635
636
637
638
639
640
641
			else
				xtables_error(PARAMETER_PROBLEM,
					   "-%c requires a chain and a policy",
					   cmd2char(CMD_SET_POLICY));
			break;

		case 'h':
			if (!optarg)
				optarg = argv[optind];

			/* iptables -p icmp -h */
642
643
644
			if (!cs->matches && cs->protocol)
				xtables_find_match(cs->protocol,
					XTF_TRY_LOAD, &cs->matches);
645

646
647
648
			printhelp(cs->matches);
			p->command = CMD_NONE;
			return;
649
650
651
652
653

			/*
			 * Option selection
			 */
		case 'p':
654
655
			set_option(&cs->options, OPT_PROTOCOL,
				   &args->invflags, cs->invert);
656
657

			/* Canonicalize into lower case */
658
659
			for (cs->protocol = optarg; *cs->protocol; cs->protocol++)
				*cs->protocol = tolower(*cs->protocol);
660

661
662
			cs->protocol = optarg;
			args->proto = xtables_parse_protocol(cs->protocol);
663

664
665
			if (args->proto == 0 &&
			    (args->invflags & XT_INV_PROTO))
666
667
668
669
				xtables_error(PARAMETER_PROBLEM,
					   "rule would never match protocol");

			/* This needs to happen here to parse extensions */
670
			h->ops->proto_parse(cs, args);
671
672
673
			break;

		case 's':
674
675
676
			set_option(&cs->options, OPT_SOURCE,
				   &args->invflags, cs->invert);
			args->shostnetworkmask = optarg;
677
678
679
			break;

		case 'd':
680
681
682
			set_option(&cs->options, OPT_DESTINATION,
				   &args->invflags, cs->invert);
			args->dhostnetworkmask = optarg;
683
684
685
686
			break;

#ifdef IPT_F_GOTO
		case 'g':
687
688
689
			set_option(&cs->options, OPT_JUMP, &args->invflags,
				   cs->invert);
			args->goto_set = true;
690
			cs->jumpto = xt_parse_target(optarg);
691
692
693
694
			break;
#endif

		case 'j':
695
696
			set_option(&cs->options, OPT_JUMP, &cs->fw.ip.invflags,
				   cs->invert);
697
			command_jump(cs, optarg);
698
699
700
701
702
703
704
705
			break;


		case 'i':
			if (*optarg == '\0')
				xtables_error(PARAMETER_PROBLEM,
					"Empty interface is likely to be "
					"undesired");
706
707
			set_option(&cs->options, OPT_VIANAMEIN,
				   &args->invflags, cs->invert);
708
			xtables_parse_interface(optarg,
709
710
						args->iniface,
						args->iniface_mask);
711
712
713
714
715
716
717
			break;

		case 'o':
			if (*optarg == '\0')
				xtables_error(PARAMETER_PROBLEM,
					"Empty interface is likely to be "
					"undesired");
718
719
			set_option(&cs->options, OPT_VIANAMEOUT,
				   &args->invflags, cs->invert);
720
			xtables_parse_interface(optarg,
721
722
						args->outiface,
						args->outiface_mask);
723
724
725
			break;

		case 'f':
726
			if (args->family == AF_INET6) {
727
728
729
730
				xtables_error(PARAMETER_PROBLEM,
					"`-f' is not supported in IPv6, "
					"use -m frag instead");
			}
731
732
733
			set_option(&cs->options, OPT_FRAGMENT, &args->invflags,
				   cs->invert);
			args->flags |= IPT_F_FRAG;
734
735
736
			break;

		case 'v':
737
738
739
740
			if (!p->verbose)
				set_option(&cs->options, OPT_VERBOSE,
					   &args->invflags, cs->invert);
			p->verbose++;
741
742
743
			break;

		case 'm':
744
			command_match(cs);
745
746
747
			break;

		case 'n':
748
749
			set_option(&cs->options, OPT_NUMERIC, &args->invflags,
				   cs->invert);
750
751
752
			break;

		case 't':
753
			if (cs->invert)
754
755
				xtables_error(PARAMETER_PROBLEM,
					   "unexpected ! flag before --table");
756
757
758
759
			if (p->restore && table_set)
				xtables_error(PARAMETER_PROBLEM,
					      "The -t option (seen in line %u) cannot be used in %s.\n",
					      line, xt_params->program_name);
760
761
762
763
			if (!nft_table_builtin_find(h, optarg))
				xtables_error(VERSION_PROBLEM,
					      "table '%s' does not exist",
					      optarg);
764
			p->table = optarg;
765
			table_set = true;
766
767
768
			break;

		case 'x':
769
770
			set_option(&cs->options, OPT_EXPANDED, &args->invflags,
				   cs->invert);
771
772
773
			break;

		case 'V':
774
			if (cs->invert)
775
776
				printf("Not %s ;-)\n", prog_vers);
			else
777
				printf("%s v%s (nf_tables)\n",
778
779
780
781
				       prog_name, prog_vers);
			exit(0);

		case 'w':
782
			if (p->restore) {
783
784
785
786
				xtables_error(PARAMETER_PROBLEM,
					      "You cannot use `-w' from "
					      "iptables-restore");
			}
787
788

			wait = parse_wait_time(argc, argv);
789
790
			break;

791
792
793
794
795
796
797
		case 'W':
			if (p->restore) {
				xtables_error(PARAMETER_PROBLEM,
					      "You cannot use `-W' from "
					      "iptables-restore");
			}

798
			parse_wait_interval(argc, argv, &wait_interval);
799
800
801
			wait_interval_set = true;
			break;

802
		case '0':
803
804
			set_option(&cs->options, OPT_LINENUMBERS,
				   &args->invflags, cs->invert);
805
806
807
808
809
810
811
			break;

		case 'M':
			xtables_modprobe_program = optarg;
			break;

		case 'c':
812
813
814
815
816
817
			set_option(&cs->options, OPT_COUNTERS, &args->invflags,
				   cs->invert);
			args->pcnt = optarg;
			args->bcnt = strchr(args->pcnt + 1, ',');
			if (args->bcnt)
			    args->bcnt++;
818
			if (!args->bcnt && xs_has_arg(argc, argv))
819
820
				args->bcnt = argv[optind++];
			if (!args->bcnt)
821
822
823
824
				xtables_error(PARAMETER_PROBLEM,
					"-%c requires packet and byte counter",
					opt2char(OPT_COUNTERS));

825
			if (sscanf(args->pcnt, "%llu", &args->pcnt_cnt) != 1)
826
827
828
829
				xtables_error(PARAMETER_PROBLEM,
					"-%c packet counter not numeric",
					opt2char(OPT_COUNTERS));

830
			if (sscanf(args->bcnt, "%llu", &args->bcnt_cnt) != 1)
831
832
833
834
835
836
				xtables_error(PARAMETER_PROBLEM,
					"-%c byte counter not numeric",
					opt2char(OPT_COUNTERS));
			break;

		case '4':
837
838
839
			if (args->family == AF_INET)
				break;

840
841
842
			if (p->restore && args->family == AF_INET6)
				return;

843
			exit_tryhelp(2);
844
845

		case '6':
846
847
848
			if (args->family == AF_INET6)
				break;

849
850
851
			if (p->restore && args->family == AF_INET)
				return;

852
			exit_tryhelp(2);
853
854
855

		case 1: /* non option */
			if (optarg[0] == '!' && optarg[1] == '\0') {
856
				if (cs->invert)
857
858
859
					xtables_error(PARAMETER_PROBLEM,
						   "multiple consecutive ! not"
						   " allowed");
860
				cs->invert = true;
861
862
863
864
865
866
867
				optarg[0] = '\0';
				continue;
			}
			fprintf(stderr, "Bad argument `%s'\n", optarg);
			exit_tryhelp(2);

		default:
868
			if (command_default(cs, &xtables_globals) == 1)
869
870
871
872
				/* cf. ip6tables.c */
				continue;
			break;
		}
873
		cs->invert = false;
874
875
	}

876
877
878
	if (strcmp(p->table, "nat") == 0 &&
	    ((p->policy != NULL && strcmp(p->policy, "DROP") == 0) ||
	    (cs->jumpto != NULL && strcmp(cs->jumpto, "DROP") == 0)))
879
880
881
882
		xtables_error(PARAMETER_PROBLEM,
			"\nThe \"nat\" table is not intended for filtering, "
			"the use of DROP is therefore inhibited.\n\n");

883
884
885
886
887
	if (!wait && wait_interval_set)
		xtables_error(PARAMETER_PROBLEM,
			      "--wait-interval only makes sense with --wait\n");

	for (matchp = cs->matches; matchp; matchp = matchp->next)
888
		xtables_option_mfcall(matchp->match);
889
890
	if (cs->target != NULL)
		xtables_option_tfcall(cs->target);
891
892
893
894
895
896

	/* Fix me: must put inverse options checking here --MN */

	if (optind < argc)
		xtables_error(PARAMETER_PROBLEM,
			   "unknown arguments found on commandline");
897
	if (!p->command)
898
		xtables_error(PARAMETER_PROBLEM, "no command specified");
899
	if (cs->invert)
900
901
902
903
904
		xtables_error(PARAMETER_PROBLEM,
			   "nothing appropriate following !");

	/* Set only if required, needed by xtables-restore */
	if (h->family == AF_UNSPEC)
905
		h->family = args->family;
906

907
	h->ops->post_parse(p->command, cs, args);
908

909
910
	if (p->command == CMD_REPLACE &&
	    (args->s.naddrs != 1 || args->d.naddrs != 1))
911
912
913
		xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
			   "specify a unique address");

914
	generic_opt_check(p->command, cs->options);
915

916
	if (p->chain != NULL && strlen(p->chain) >= XT_EXTENSION_MAXNAMELEN)
917
918
		xtables_error(PARAMETER_PROBLEM,
			   "chain name `%s' too long (must be under %u chars)",
919
920
921
922
			   p->chain, XT_EXTENSION_MAXNAMELEN);

	if (p->command == CMD_APPEND ||
	    p->command == CMD_DELETE ||
923
	    p->command == CMD_DELETE_NUM ||
924
925
926
927
928
	    p->command == CMD_CHECK ||
	    p->command == CMD_INSERT ||
	    p->command == CMD_REPLACE) {
		if (strcmp(p->chain, "PREROUTING") == 0
		    || strcmp(p->chain, "INPUT") == 0) {
929
			/* -o not valid with incoming packets. */
930
			if (cs->options & OPT_VIANAMEOUT)
931
932
933
				xtables_error(PARAMETER_PROBLEM,
					   "Can't use -%c with %s\n",
					   opt2char(OPT_VIANAMEOUT),
934
					   p->chain);
935
936
		}

937
938
		if (strcmp(p->chain, "POSTROUTING") == 0
		    || strcmp(p->chain, "OUTPUT") == 0) {
939
			/* -i not valid with outgoing packets */
940
			if (cs->options & OPT_VIANAMEIN)
941
942
943
				xtables_error(PARAMETER_PROBLEM,
					   "Can't use -%c with %s\n",
					   opt2char(OPT_VIANAMEIN),
944
					   p->chain);
945
946
		}
	}
947
}
948

949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
int do_commandx(struct nft_handle *h, int argc, char *argv[], char **table,
		bool restore)
{
	int ret = 1;
	struct nft_xt_cmd_parse p = {
		.table		= *table,
		.restore	= restore,
	};
	struct iptables_command_state cs;
	struct xtables_args args = {
		.family = h->family,
	};

	do_parse(h, argc, argv, &p, &cs, &args);

	switch (p.command) {
965
	case CMD_APPEND:
966
967
968
		ret = add_entry(p.chain, p.table, &cs, 0, h->family,
				args.s, args.d,
				cs.options & OPT_VERBOSE, h, true);
969
970
		break;
	case CMD_DELETE:
971
972
973
		ret = delete_entry(p.chain, p.table, &cs, h->family,
				   args.s, args.d,
				   cs.options & OPT_VERBOSE, h);
974
975
		break;
	case CMD_DELETE_NUM:
976
977
		ret = nft_cmd_rule_delete_num(h, p.chain, p.table,
					      p.rulenum - 1, p.verbose);
978
979
		break;
	case CMD_CHECK:
980
981
982
		ret = check_entry(p.chain, p.table, &cs, h->family,
				  args.s, args.d,
				  cs.options & OPT_VERBOSE, h);
983
984
		break;
	case CMD_REPLACE:
985
		ret = replace_entry(p.chain, p.table, &cs, p.rulenum - 1,
986
				    h->family, args.s, args.d,
987
				    cs.options & OPT_VERBOSE, h);
988
989
		break;
	case CMD_INSERT:
990
991
992
		ret = add_entry(p.chain, p.table, &cs, p.rulenum - 1,
				h->family, args.s, args.d,
				cs.options&OPT_VERBOSE, h, false);
993
994
		break;
	case CMD_FLUSH:
995
996
		ret = nft_cmd_rule_flush(h, p.chain, p.table,
					 cs.options & OPT_VERBOSE);
997
998
		break;
	case CMD_ZERO:
999
1000
		ret = nft_cmd_chain_zero_counters(h, p.chain, p.table,
						  cs.options & OPT_VERBOSE);
1001
1002
		break;
	case CMD_ZERO_NUM:
1003
		ret = nft_cmd_rule_zero_counters(h, p.chain, p.table,
1004
					     p.rulenum - 1);
1005
1006
1007
1008
		break;
	case CMD_LIST:
	case CMD_LIST|CMD_ZERO:
	case CMD_LIST|CMD_ZERO_NUM:
1009
1010
1011
1012
1013
1014
		ret = list_entries(h, p.chain, p.table, p.rulenum,
				   cs.options & OPT_VERBOSE,
				   cs.options & OPT_NUMERIC,
				   cs.options & OPT_EXPANDED,
				   cs.options & OPT_LINENUMBERS);
		if (ret && (p.command & CMD_ZERO)) {
1015
			ret = nft_cmd_chain_zero_counters(h, p.chain, p.table,
1016
						      cs.options & OPT_VERBOSE);
1017
1018
		}
		if (ret && (p.command & CMD_ZERO_NUM)) {
1019
			ret = nft_cmd_rule_zero_counters(h, p.chain, p.table,
1020
1021
						     p.rulenum - 1);
		}
1022
		nft_check_xt_legacy(h->family, false);
1023
1024
1025
1026
		break;
	case CMD_LIST_RULES:
	case CMD_LIST_RULES|CMD_ZERO:
	case CMD_LIST_RULES|CMD_ZERO_NUM:
1027
1028
1029
		ret = list_rules(h, p.chain, p.table, p.rulenum,
				 cs.options & OPT_VERBOSE);
		if (ret && (p.command & CMD_ZERO)) {
1030
			ret = nft_cmd_chain_zero_counters(h, p.chain, p.table,
1031
						      cs.options & OPT_VERBOSE);
1032
1033
		}
		if (ret && (p.command & CMD_ZERO_NUM)) {
1034
			ret = nft_cmd_rule_zero_counters(h, p.chain, p.table,
1035
1036
						     p.rulenum - 1);
		}
1037
		nft_check_xt_legacy(h->family, false);
1038
1039
		break;
	case CMD_NEW_CHAIN:
1040
		ret = nft_cmd_chain_user_add(h, p.chain, p.table);
1041
1042
		break;
	case CMD_DELETE_CHAIN:
1043
		ret = nft_cmd_chain_user_del(h, p.chain, p.table,
1044
					 cs.options & OPT_VERBOSE);
1045
1046
		break;
	case CMD_RENAME_CHAIN:
1047
		ret = nft_cmd_chain_user_rename(h, p.chain, p.table, p.newname);
1048
1049
		break;
	case CMD_SET_POLICY:
1050
		ret = nft_cmd_chain_set(h, p.table, p.chain, p.policy, NULL);
1051
		break;
1052
1053
1054
	case CMD_NONE:
	/* do_parse ignored the line (eg: -4 with ip6tables-restore) */
		break;
1055
1056
1057
1058
1059
	default:
		/* We should never reach this... */
		exit_tryhelp(2);
	}

1060
	*table = p.table;
1061

1062
	nft_clear_iptables_command_state(&cs);
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078

	if (h->family == AF_INET) {
		free(args.s.addr.v4);
		free(args.s.mask.v4);
		free(args.d.addr.v4);
		free(args.d.mask.v4);
	} else if (h->family == AF_INET6) {
		free(args.s.addr.v6);
		free(args.s.mask.v6);
		free(args.d.addr.v6);
		free(args.d.mask.v6);
	}
	xtables_free_opts(1);

	return ret;
}