blob: c4846fd9d6fc043e3393f973b321652d68da3bbd [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* 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 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
31#include <iptables.h>
32
33#ifndef TRUE
34#define TRUE 1
35#endif
36#ifndef FALSE
37#define FALSE 0
38#endif
39
40#ifndef IPT_LIB_DIR
41#define IPT_LIB_DIR "/usr/local/lib/iptables"
42#endif
43
44#define FMT_NUMERIC 0x0001
45#define FMT_NOCOUNTS 0x0002
46#define FMT_KILOMEGAGIGA 0x0004
47#define FMT_OPTIONS 0x0008
48#define FMT_NOTABLE 0x0010
49#define FMT_NOTARGET 0x0020
50#define FMT_VIA 0x0040
51#define FMT_NONEWLINE 0x0080
52#define FMT_LINENUMBERS 0x0100
53
54#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
55 | FMT_NUMERIC | FMT_NOTABLE)
56#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
57
58
59#define CMD_NONE 0x0000U
60#define CMD_INSERT 0x0001U
61#define CMD_DELETE 0x0002U
62#define CMD_DELETE_NUM 0x0004U
63#define CMD_REPLACE 0x0008U
64#define CMD_APPEND 0x0010U
65#define CMD_LIST 0x0020U
66#define CMD_FLUSH 0x0040U
67#define CMD_ZERO 0x0080U
68#define CMD_NEW_CHAIN 0x0100U
69#define CMD_DELETE_CHAIN 0x0200U
70#define CMD_SET_POLICY 0x0400U
71#define CMD_CHECK 0x0800U
72#define CMD_RENAME_CHAIN 0x1000U
73#define NUMBER_OF_CMD 13
74static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
75 'N', 'X', 'P', 'C', 'E' };
76
77#define OPTION_OFFSET 256
78
79#define OPT_NONE 0x00000U
80#define OPT_NUMERIC 0x00001U
81#define OPT_SOURCE 0x00002U
82#define OPT_DESTINATION 0x00004U
83#define OPT_PROTOCOL 0x00008U
84#define OPT_JUMP 0x00010U
85#define OPT_VERBOSE 0x00020U
86#define OPT_EXPANDED 0x00040U
87#define OPT_VIANAMEIN 0x00080U
88#define OPT_VIANAMEOUT 0x00100U
89#define OPT_FRAGMENT 0x00200U
90#define OPT_LINENUMBERS 0x00400U
91#define NUMBER_OF_OPT 11
92static const char optflags[NUMBER_OF_OPT]
93= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3'};
94
95static struct option original_opts[] = {
96 { "append", 1, 0, 'A' },
97 { "delete", 1, 0, 'D' },
98 { "insert", 1, 0, 'I' },
99 { "replace", 1, 0, 'R' },
100 { "list", 2, 0, 'L' },
101 { "flush", 2, 0, 'F' },
102 { "zero", 2, 0, 'Z' },
103 { "check", 1, 0, 'C' },
104 { "new-chain", 1, 0, 'N' },
105 { "delete-chain", 2, 0, 'X' },
106 { "rename-chain", 2, 0, 'E' },
107 { "policy", 1, 0, 'P' },
108 { "source", 1, 0, 's' },
109 { "destination", 1, 0, 'd' },
110 { "src", 1, 0, 's' }, /* synonym */
111 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000112 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000113 { "in-interface", 1, 0, 'i' },
114 { "jump", 1, 0, 'j' },
115 { "table", 1, 0, 't' },
116 { "match", 1, 0, 'm' },
117 { "numeric", 0, 0, 'n' },
118 { "out-interface", 1, 0, 'o' },
119 { "verbose", 0, 0, 'v' },
120 { "exact", 0, 0, 'x' },
121 { "fragments", 0, 0, 'f' },
122 { "version", 0, 0, 'V' },
123 { "help", 2, 0, 'h' },
124 { "line-numbers", 0, 0, '0' },
125 { 0 }
126};
127
Rusty Russell9e1d2142000-04-23 09:11:12 +0000128#if 0
129static struct ipt_entry_target *
130ipt_get_target(struct ipt_entry *e)
131{
132 return (void *)e + e->target_offset;
133}
134#endif
135
Marc Bouchere6869a82000-03-20 06:03:29 +0000136static struct option *opts = original_opts;
137static unsigned int global_option_offset = 0;
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 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 -f --line */
152/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
153/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
154/*DELETE_NUM*/{'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',' '},
158/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
159/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
160/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
161/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
162/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
163/*CHECK*/ {'x','+','+','+','x',' ','x','+','+',' ','x'},
164/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
165};
166
167static int inverse_for_options[NUMBER_OF_OPT] =
168{
169/* -n */ 0,
170/* -s */ IPT_INV_SRCIP,
171/* -d */ IPT_INV_DSTIP,
172/* -p */ IPT_INV_PROTO,
173/* -j */ 0,
174/* -v */ 0,
175/* -x */ 0,
176/* -i */ IPT_INV_VIA_IN,
177/* -o */ IPT_INV_VIA_OUT,
178/* -f */ IPT_INV_FRAG,
179/*--line*/ 0
180};
181
182const char *program_version;
183const char *program_name;
184
Rusty Russell2e0a3212000-04-19 11:23:18 +0000185/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000186struct iptables_match *iptables_matches = NULL;
187struct iptables_target *iptables_targets = NULL;
188
189/* Extra debugging from libiptc */
190extern void dump_entries(const iptc_handle_t handle);
191
192/* A few hardcoded protocols for 'all' and in case the user has no
193 /etc/protocols */
194struct pprot {
195 char *name;
196 u_int8_t num;
197};
198
199static const struct pprot chain_protos[] = {
200 { "tcp", IPPROTO_TCP },
201 { "udp", IPPROTO_UDP },
202 { "icmp", IPPROTO_ICMP },
203 { "all", 0 },
204};
205
206static char *
207proto_to_name(u_int8_t proto)
208{
209 unsigned int i;
210
211 if (proto) {
212 struct protoent *pent = getprotobynumber(proto);
213 if (pent)
214 return pent->p_name;
215 }
216
217 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
218 if (chain_protos[i].num == proto)
219 return chain_protos[i].name;
220
221 return NULL;
222}
223
224struct in_addr *
225dotted_to_addr(const char *dotted)
226{
227 static struct in_addr addr;
228 unsigned char *addrp;
229 char *p, *q;
230 int onebyte, i;
231 char buf[20];
232
233 /* copy dotted string, because we need to modify it */
234 strncpy(buf, dotted, sizeof(buf) - 1);
235 addrp = (unsigned char *) &(addr.s_addr);
236
237 p = buf;
238 for (i = 0; i < 3; i++) {
239 if ((q = strchr(p, '.')) == NULL)
240 return (struct in_addr *) NULL;
241
242 *q = '\0';
243 if ((onebyte = string_to_number(p, 0, 255)) == -1)
244 return (struct in_addr *) NULL;
245
246 addrp[i] = (unsigned char) onebyte;
247 p = q + 1;
248 }
249
250 /* we've checked 3 bytes, now we check the last one */
251 if ((onebyte = string_to_number(p, 0, 255)) == -1)
252 return (struct in_addr *) NULL;
253
254 addrp[3] = (unsigned char) onebyte;
255
256 return &addr;
257}
258
259static struct in_addr *
260network_to_addr(const char *name)
261{
262 struct netent *net;
263 static struct in_addr addr;
264
265 if ((net = getnetbyname(name)) != NULL) {
266 if (net->n_addrtype != AF_INET)
267 return (struct in_addr *) NULL;
268 addr.s_addr = htonl((unsigned long) net->n_net);
269 return &addr;
270 }
271
272 return (struct in_addr *) NULL;
273}
274
275static void
276inaddrcpy(struct in_addr *dst, struct in_addr *src)
277{
278 /* memcpy(dst, src, sizeof(struct in_addr)); */
279 dst->s_addr = src->s_addr;
280}
281
282void
283exit_error(enum exittype status, char *msg, ...)
284{
285 va_list args;
286
287 va_start(args, msg);
288 fprintf(stderr, "%s v%s: ", program_name, program_version);
289 vfprintf(stderr, msg, args);
290 va_end(args);
291 fprintf(stderr, "\n");
292 if (status == PARAMETER_PROBLEM)
293 exit_tryhelp(status);
294 if (status == VERSION_PROBLEM)
295 fprintf(stderr,
296 "Perhaps iptables or your kernel needs to be upgraded.\n");
297 exit(status);
298}
299
300void
301exit_tryhelp(int status)
302{
303 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
304 program_name, program_name );
305 exit(status);
306}
307
308void
309exit_printhelp(void)
310{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000311 struct iptables_match *m = NULL;
312 struct iptables_target *t = NULL;
313
Marc Bouchere6869a82000-03-20 06:03:29 +0000314 printf("%s v%s\n\n"
315"Usage: %s -[ADC] chain rule-specification [options]\n"
316" %s -[RI] chain rulenum rule-specification [options]\n"
317" %s -D chain rulenum [options]\n"
318" %s -[LFZ] [chain] [options]\n"
319" %s -[NX] chain\n"
320" %s -E old-chain-name new-chain-name\n"
321" %s -P chain target [options]\n"
322" %s -h (print this help information)\n\n",
323 program_name, program_version, program_name, program_name,
324 program_name, program_name, program_name, program_name,
325 program_name, program_name);
326
327 printf(
328"Commands:\n"
329"Either long or short options are allowed.\n"
330" --append -A chain Append to chain\n"
331" --delete -D chain Delete matching rule from chain\n"
332" --delete -D chain rulenum\n"
333" Delete rule rulenum (1 = first) from chain\n"
334" --insert -I chain [rulenum]\n"
335" Insert in chain as rulenum (default 1=first)\n"
336" --replace -R chain rulenum\n"
337" Replace rule rulenum (1 = first) in chain\n"
338" --list -L [chain] List the rules in a chain or all chains\n"
339" --flush -F [chain] Delete all rules in chain or all chains\n"
340" --zero -Z [chain] Zero counters in chain or all chains\n"
341" --check -C chain Test this packet on chain\n"
342" --new -N chain Create a new user-defined chain\n"
343" --delete-chain\n"
344" -X [chain] Delete a user-defined chain\n"
345" --policy -P chain target\n"
346" Change policy on chain to target\n"
347" --rename-chain\n"
348" -E old-chain new-chain\n"
349" Change chain name, (moving any references)\n"
350
351"Options:\n"
352" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
353" --source -s [!] address[/mask]\n"
354" source specification\n"
355" --destination -d [!] address[/mask]\n"
356" destination specification\n"
357" --in-interface -i [!] input name[+]\n"
358" network interface name ([+] for wildcard)\n"
359" --jump -j target\n"
360" target for rule\n"
361" --numeric -n numeric output of addresses and ports\n"
362" --out-interface -o [!] output name[+]\n"
363" network interface name ([+] for wildcard)\n"
364" --table -t table table to manipulate (default: `filter')\n"
365" --verbose -v verbose mode\n"
366" --exact -x expand numbers (display exact values)\n"
367"[!] --fragment -f match second or further fragments only\n"
368"[!] --version -V print package version.\n");
369
Rusty Russell2e0a3212000-04-19 11:23:18 +0000370 /* Print out any special helps. A user might like to be able to add a --help
371 to the commandline, and see expected results. So we call help for all
372 matches & targets */
373 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000374 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000375 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000376 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000377 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000378 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000379 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000380 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000381 exit(0);
382}
383
384static void
385generic_opt_check(int command, int options)
386{
387 int i, j, legal = 0;
388
389 /* Check that commands are valid with options. Complicated by the
390 * fact that if an option is legal with *any* command given, it is
391 * legal overall (ie. -z and -l).
392 */
393 for (i = 0; i < NUMBER_OF_OPT; i++) {
394 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
395
396 for (j = 0; j < NUMBER_OF_CMD; j++) {
397 if (!(command & (1<<j)))
398 continue;
399
400 if (!(options & (1<<i))) {
401 if (commands_v_options[j][i] == '+')
402 exit_error(PARAMETER_PROBLEM,
403 "You need to supply the `-%c' "
404 "option for this command\n",
405 optflags[i]);
406 } else {
407 if (commands_v_options[j][i] != 'x')
408 legal = 1;
409 else if (legal == 0)
410 legal = -1;
411 }
412 }
413 if (legal == -1)
414 exit_error(PARAMETER_PROBLEM,
415 "Illegal option `-%c' with this command\n",
416 optflags[i]);
417 }
418}
419
420static char
421opt2char(int option)
422{
423 const char *ptr;
424 for (ptr = optflags; option > 1; option >>= 1, ptr++);
425
426 return *ptr;
427}
428
429static char
430cmd2char(int option)
431{
432 const char *ptr;
433 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
434
435 return *ptr;
436}
437
438static void
439add_command(int *cmd, const int newcmd, const int othercmds, int invert)
440{
441 if (invert)
442 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
443 if (*cmd & (~othercmds))
444 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
445 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
446 *cmd |= newcmd;
447}
448
449int
450check_inverse(const char option[], int *invert)
451{
452 if (option && strcmp(option, "!") == 0) {
453 if (*invert)
454 exit_error(PARAMETER_PROBLEM,
455 "Multiple `!' flags not allowed");
456
457 *invert = TRUE;
458 return TRUE;
459 }
460 return FALSE;
461}
462
463static void *
464fw_calloc(size_t count, size_t size)
465{
466 void *p;
467
468 if ((p = calloc(count, size)) == NULL) {
469 perror("iptables: calloc failed");
470 exit(1);
471 }
472 return p;
473}
474
475static void *
476fw_malloc(size_t size)
477{
478 void *p;
479
480 if ((p = malloc(size)) == NULL) {
481 perror("iptables: malloc failed");
482 exit(1);
483 }
484 return p;
485}
486
487static struct in_addr *
488host_to_addr(const char *name, unsigned int *naddr)
489{
490 struct hostent *host;
491 struct in_addr *addr;
492 unsigned int i;
493
494 *naddr = 0;
495 if ((host = gethostbyname(name)) != NULL) {
496 if (host->h_addrtype != AF_INET ||
497 host->h_length != sizeof(struct in_addr))
498 return (struct in_addr *) NULL;
499
500 while (host->h_addr_list[*naddr] != (char *) NULL)
501 (*naddr)++;
502 addr = fw_calloc(*naddr, sizeof(struct in_addr));
503 for (i = 0; i < *naddr; i++)
504 inaddrcpy(&(addr[i]),
505 (struct in_addr *) host->h_addr_list[i]);
506 return addr;
507 }
508
509 return (struct in_addr *) NULL;
510}
511
512static char *
513addr_to_host(const struct in_addr *addr)
514{
515 struct hostent *host;
516
517 if ((host = gethostbyaddr((char *) addr,
518 sizeof(struct in_addr), AF_INET)) != NULL)
519 return (char *) host->h_name;
520
521 return (char *) NULL;
522}
523
524/*
525 * All functions starting with "parse" should succeed, otherwise
526 * the program fails.
527 * Most routines return pointers to static data that may change
528 * between calls to the same or other routines with a few exceptions:
529 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
530 * return global static data.
531*/
532
533static struct in_addr *
534parse_hostnetwork(const char *name, unsigned int *naddrs)
535{
536 struct in_addr *addrp, *addrptmp;
537
538 if ((addrptmp = dotted_to_addr(name)) != NULL ||
539 (addrptmp = network_to_addr(name)) != NULL) {
540 addrp = fw_malloc(sizeof(struct in_addr));
541 inaddrcpy(addrp, addrptmp);
542 *naddrs = 1;
543 return addrp;
544 }
545 if ((addrp = host_to_addr(name, naddrs)) != NULL)
546 return addrp;
547
548 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
549}
550
551static struct in_addr *
552parse_mask(char *mask)
553{
554 static struct in_addr maskaddr;
555 struct in_addr *addrp;
556 int bits;
557
558 if (mask == NULL) {
559 /* no mask at all defaults to 32 bits */
560 maskaddr.s_addr = 0xFFFFFFFF;
561 return &maskaddr;
562 }
563 if ((addrp = dotted_to_addr(mask)) != NULL)
564 /* dotted_to_addr already returns a network byte order addr */
565 return addrp;
566 if ((bits = string_to_number(mask, 0, 32)) == -1)
567 exit_error(PARAMETER_PROBLEM,
568 "invalid mask `%s' specified", mask);
569 if (bits != 0) {
570 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
571 return &maskaddr;
572 }
573
574 maskaddr.s_addr = 0L;
575 return &maskaddr;
576}
577
578static void
579parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
580 struct in_addr *maskp, unsigned int *naddrs)
581{
582 struct in_addr *addrp;
583 char buf[256];
584 char *p;
585 int i, j, k, n;
586
587 strncpy(buf, name, sizeof(buf) - 1);
588 if ((p = strrchr(buf, '/')) != NULL) {
589 *p = '\0';
590 addrp = parse_mask(p + 1);
591 } else
592 addrp = parse_mask(NULL);
593 inaddrcpy(maskp, addrp);
594
595 /* if a null mask is given, the name is ignored, like in "any/0" */
596 if (maskp->s_addr == 0L)
597 strcpy(buf, "0.0.0.0");
598
599 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
600 n = *naddrs;
601 for (i = 0, j = 0; i < n; i++) {
602 addrp[j++].s_addr &= maskp->s_addr;
603 for (k = 0; k < j - 1; k++) {
604 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
605 (*naddrs)--;
606 j--;
607 break;
608 }
609 }
610 }
611}
612
613struct iptables_match *
614find_match(const char *name, int tryload)
615{
616 struct iptables_match *ptr;
617
618 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
619 if (strcmp(name, ptr->name) == 0)
620 break;
621 }
622
623 if (!ptr && tryload) {
624 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
625 + strlen(name)];
626 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000627 if (dlopen(path, RTLD_NOW)) {
628 /* Found library. If it didn't register itself,
629 maybe they specified target as match. */
630 ptr = find_match(name, 0);
631 if (!ptr)
632 exit_error(PARAMETER_PROBLEM,
633 "Couldn't load match `%s'\n",
634 name);
635 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000636 }
637
638 return ptr;
639}
640
641static u_int16_t
642parse_protocol(const char *s)
643{
644 int proto = string_to_number(s, 0, 65535);
645
646 if (proto == -1) {
647 struct protoent *pent;
648
649 if ((pent = getprotobyname(s)))
650 proto = pent->p_proto;
651 else {
652 unsigned int i;
653 for (i = 0;
654 i < sizeof(chain_protos)/sizeof(struct pprot);
655 i++) {
656 if (strcmp(s, chain_protos[i].name) == 0) {
657 proto = chain_protos[i].num;
658 break;
659 }
660 }
661 if (i == sizeof(chain_protos)/sizeof(struct pprot))
662 exit_error(PARAMETER_PROBLEM,
663 "unknown protocol `%s' specified",
664 s);
665 }
666 }
667
668 return (u_int16_t)proto;
669}
670
671static void
672parse_interface(const char *arg, char *vianame, unsigned char *mask)
673{
674 int vialen = strlen(arg);
675 unsigned int i;
676
677 memset(mask, 0, IFNAMSIZ);
678 memset(vianame, 0, IFNAMSIZ);
679
680 if (vialen + 1 > IFNAMSIZ)
681 exit_error(PARAMETER_PROBLEM,
682 "interface name `%s' must be shorter than IFNAMSIZ"
683 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000684
Marc Bouchere6869a82000-03-20 06:03:29 +0000685 strcpy(vianame, arg);
686 if (vialen == 0)
687 memset(mask, 0, IFNAMSIZ);
688 else if (vianame[vialen - 1] == '+') {
689 memset(mask, 0xFF, vialen - 1);
690 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
691 /* Remove `+' */
692 vianame[vialen - 1] = '\0';
693 } else {
694 /* Include nul-terminator in match */
695 memset(mask, 0xFF, vialen + 1);
696 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
697 }
698 for (i = 0; vianame[i]; i++) {
699 if (!isalnum(vianame[i])) {
700 printf("Warning: wierd character in interface"
701 " `%s' (No aliases, :, ! or *).\n",
702 vianame);
703 break;
704 }
705 }
706}
707
708/* Can't be zero. */
709static int
710parse_rulenumber(const char *rule)
711{
712 int rulenum = string_to_number(rule, 1, INT_MAX);
713
714 if (rulenum == -1)
715 exit_error(PARAMETER_PROBLEM,
716 "Invalid rule number `%s'", rule);
717
718 return rulenum;
719}
720
721static const char *
722parse_target(const char *targetname)
723{
724 const char *ptr;
725
726 if (strlen(targetname) < 1)
727 exit_error(PARAMETER_PROBLEM,
728 "Invalid target name (too short)");
729
730 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
731 exit_error(PARAMETER_PROBLEM,
732 "Invalid target name `%s' (%i chars max)",
733 targetname, sizeof(ipt_chainlabel)-1);
734
735 for (ptr = targetname; *ptr; ptr++)
736 if (isspace(*ptr))
737 exit_error(PARAMETER_PROBLEM,
738 "Invalid target name `%s'", targetname);
739 return targetname;
740}
741
742static char *
743addr_to_network(const struct in_addr *addr)
744{
745 struct netent *net;
746
747 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
748 return (char *) net->n_name;
749
750 return (char *) NULL;
751}
752
753char *
754addr_to_dotted(const struct in_addr *addrp)
755{
756 static char buf[20];
757 const unsigned char *bytep;
758
759 bytep = (const unsigned char *) &(addrp->s_addr);
760 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
761 return buf;
762}
763static char *
764addr_to_anyname(const struct in_addr *addr)
765{
766 char *name;
767
768 if ((name = addr_to_host(addr)) != NULL ||
769 (name = addr_to_network(addr)) != NULL)
770 return name;
771
772 return addr_to_dotted(addr);
773}
774
775static char *
776mask_to_dotted(const struct in_addr *mask)
777{
778 int i;
779 static char buf[20];
780 u_int32_t maskaddr, bits;
781
782 maskaddr = ntohl(mask->s_addr);
783
784 if (maskaddr == 0xFFFFFFFFL)
785 /* we don't want to see "/32" */
786 return "";
787
788 i = 32;
789 bits = 0xFFFFFFFEL;
790 while (--i >= 0 && maskaddr != bits)
791 bits <<= 1;
792 if (i >= 0)
793 sprintf(buf, "/%d", i);
794 else
795 /* mask was not a decent combination of 1's and 0's */
796 sprintf(buf, "/%s", addr_to_dotted(mask));
797
798 return buf;
799}
800
801int
802string_to_number(const char *s, int min, int max)
803{
804 int number;
805 char *end;
806
807 /* Handle hex, octal, etc. */
808 number = (int)strtol(s, &end, 0);
809 if (*end == '\0' && end != s) {
810 /* we parsed a number, let's see if we want this */
811 if (min <= number && number <= max)
812 return number;
813 }
814 return -1;
815}
816
817static void
818set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
819 int invert)
820{
821 if (*options & option)
822 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
823 opt2char(option));
824 *options |= option;
825
826 if (invert) {
827 unsigned int i;
828 for (i = 0; 1 << i != option; i++);
829
830 if (!inverse_for_options[i])
831 exit_error(PARAMETER_PROBLEM,
832 "cannot have ! before -%c",
833 opt2char(option));
834 *invflg |= inverse_for_options[i];
835 }
836}
837
838struct iptables_target *
839find_target(const char *name, int tryload)
840{
841 struct iptables_target *ptr;
842
843 /* Standard target? */
844 if (strcmp(name, "") == 0
845 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
846 || strcmp(name, IPTC_LABEL_DROP) == 0
847 || strcmp(name, IPTC_LABEL_QUEUE) == 0
848 || strcmp(name, IPTC_LABEL_RETURN) == 0)
849 name = "standard";
850
851 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
852 if (strcmp(name, ptr->name) == 0)
853 break;
854 }
855
856 if (!ptr && tryload) {
857 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
858 + strlen(name)];
859 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000860 if (dlopen(path, RTLD_NOW)) {
861 /* Found library. If it didn't register itself,
862 maybe they specified match as a target. */
863 ptr = find_target(name, 0);
864 if (!ptr)
865 exit_error(PARAMETER_PROBLEM,
866 "Couldn't load target `%s'\n",
867 name);
868 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000869 }
870
871 return ptr;
872}
873
874static struct option *
875merge_options(struct option *oldopts, struct option *newopts,
876 unsigned int *option_offset)
877{
878 unsigned int num_old, num_new, i;
879 struct option *merge;
880
881 for (num_old = 0; oldopts[num_old].name; num_old++);
882 for (num_new = 0; newopts[num_new].name; num_new++);
883
884 global_option_offset += OPTION_OFFSET;
885 *option_offset = global_option_offset;
886
887 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
888 memcpy(merge, oldopts, num_old * sizeof(struct option));
889 for (i = 0; i < num_new; i++) {
890 merge[num_old + i] = newopts[i];
891 merge[num_old + i].val += *option_offset;
892 }
893 memset(merge + num_old + num_new, 0, sizeof(struct option));
894
895 return merge;
896}
897
898void
899register_match(struct iptables_match *me)
900{
901 if (strcmp(me->version, program_version) != 0) {
902 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
903 program_name, me->name, me->version, program_version);
904 exit(1);
905 }
906
907 if (find_match(me->name, 0)) {
908 fprintf(stderr, "%s: match `%s' already registered.\n",
909 program_name, me->name);
910 exit(1);
911 }
912
913 /* Prepend to list. */
914 me->next = iptables_matches;
915 iptables_matches = me;
916 me->m = NULL;
917 me->mflags = 0;
918
919 opts = merge_options(opts, me->extra_opts, &me->option_offset);
920}
921
922void
923register_target(struct iptables_target *me)
924{
925 if (strcmp(me->version, program_version) != 0) {
926 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
927 program_name, me->name, me->version, program_version);
928 exit(1);
929 }
930
931 if (find_target(me->name, 0)) {
932 fprintf(stderr, "%s: target `%s' already registered.\n",
933 program_name, me->name);
934 exit(1);
935 }
936
937 /* Prepend to list. */
938 me->next = iptables_targets;
939 iptables_targets = me;
940 me->t = NULL;
941 me->tflags = 0;
942
943 opts = merge_options(opts, me->extra_opts, &me->option_offset);
944}
945
946static void
947print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
948{
949 struct ipt_counters counters;
950 const char *pol = iptc_get_policy(chain, &counters, handle);
951 printf("Chain %s", chain);
952 if (pol) {
953 printf(" (policy %s", pol);
954 if (!(format & FMT_NOCOUNTS))
955 printf(" %llu packets, %llu bytes",
956 counters.pcnt, counters.bcnt);
957 printf(")\n");
958 } else {
959 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000960 if (!iptc_get_references(&refs, chain, handle))
961 printf(" (ERROR obtaining refs)\n");
962 else
963 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000964 }
965
966 if (format & FMT_LINENUMBERS)
967 printf(FMT("%-4s ", "%s "), "num");
968 if (!(format & FMT_NOCOUNTS)) {
969 if (format & FMT_KILOMEGAGIGA) {
970 printf(FMT("%5s ","%s "), "pkts");
971 printf(FMT("%5s ","%s "), "bytes");
972 } else {
973 printf(FMT("%8s ","%s "), "pkts");
974 printf(FMT("%10s ","%s "), "bytes");
975 }
976 }
977 if (!(format & FMT_NOTARGET))
978 printf(FMT("%-9s ","%s "), "target");
979 fputs(" prot ", stdout);
980 if (format & FMT_OPTIONS)
981 fputs("opt", stdout);
982 if (format & FMT_VIA) {
983 printf(FMT(" %-6s ","%s "), "in");
984 printf(FMT("%-6s ","%s "), "out");
985 }
986 printf(FMT(" %-19s ","%s "), "source");
987 printf(FMT(" %-19s "," %s "), "destination");
988 printf("\n");
989}
990
991static void
992print_num(u_int64_t number, unsigned int format)
993{
994 if (format & FMT_KILOMEGAGIGA) {
995 if (number > 99999) {
996 number = (number + 500) / 1000;
997 if (number > 9999) {
998 number = (number + 500) / 1000;
999 if (number > 9999) {
1000 number = (number + 500) / 1000;
1001 printf(FMT("%4lluG ","%lluG "),number);
1002 }
1003 else printf(FMT("%4lluM ","%lluM "), number);
1004 } else
1005 printf(FMT("%4lluK ","%lluK "), number);
1006 } else
1007 printf(FMT("%5llu ","%llu "), number);
1008 } else
1009 printf(FMT("%8llu ","%llu "), number);
1010}
1011
1012static int
1013print_match(const struct ipt_entry_match *m,
1014 const struct ipt_ip *ip,
1015 int numeric)
1016{
1017 struct iptables_match *match = find_match(m->u.name, 1);
1018
1019 if (match) {
1020 if (match->print)
1021 match->print(ip, m, numeric);
1022 } else {
1023 if (m->u.name[0])
1024 printf("UNKNOWN match `%s' ", m->u.name);
1025 }
1026 /* Don't stop iterating. */
1027 return 0;
1028}
1029
1030/* e is called `fw' here for hysterical raisins */
1031static void
1032print_firewall(const struct ipt_entry *fw,
1033 const char *targname,
1034 unsigned int num,
1035 unsigned int format,
1036 const iptc_handle_t handle)
1037{
1038 struct iptables_target *target = NULL;
1039 const struct ipt_entry_target *t;
1040 u_int8_t flags;
1041 char buf[BUFSIZ];
1042
1043 /* User creates a chain called "REJECT": this overrides the
1044 `REJECT' target module. Keep feeding them rope until the
1045 revolution... Bwahahahahah */
1046 if (!iptc_is_chain(targname, handle))
1047 target = find_target(targname, 1);
1048 else
1049 target = find_target(IPT_STANDARD_TARGET, 1);
1050
1051 t = ipt_get_target((struct ipt_entry *)fw);
1052 flags = fw->ip.flags;
1053
1054 if (format & FMT_LINENUMBERS)
1055 printf(FMT("%-4u ", "%u "), num+1);
1056
1057 if (!(format & FMT_NOCOUNTS)) {
1058 print_num(fw->counters.pcnt, format);
1059 print_num(fw->counters.bcnt, format);
1060 }
1061
1062 if (!(format & FMT_NOTARGET))
1063 printf(FMT("%-9s ", "%s "), targname);
1064
1065 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1066 {
1067 char *pname = proto_to_name(fw->ip.proto);
1068 if (pname)
1069 printf(FMT("%-5s", "%s "), pname);
1070 else
1071 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1072 }
1073
1074 if (format & FMT_OPTIONS) {
1075 if (format & FMT_NOTABLE)
1076 fputs("opt ", stdout);
1077 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1078 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1079 fputc(' ', stdout);
1080 }
1081
1082 if (format & FMT_VIA) {
1083 char iface[IFNAMSIZ+2];
1084
1085 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1086 iface[0] = '!';
1087 iface[1] = '\0';
1088 }
1089 else iface[0] = '\0';
1090
1091 if (fw->ip.iniface[0] != '\0') {
1092 strcat(iface, fw->ip.iniface);
1093 /* If it doesn't compare the nul-term, it's a
1094 wildcard. */
1095 if (fw->ip.iniface_mask[strlen(fw->ip.iniface)] == 0)
1096 strcat(iface, "+");
1097 }
1098 else if (format & FMT_NUMERIC) strcat(iface, "*");
1099 else strcat(iface, "any");
1100 printf(FMT(" %-6s ","in %s "), iface);
1101
1102 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1103 iface[0] = '!';
1104 iface[1] = '\0';
1105 }
1106 else iface[0] = '\0';
1107
1108 if (fw->ip.outiface[0] != '\0') {
1109 strcat(iface, fw->ip.outiface);
1110 /* If it doesn't compare the nul-term, it's a
1111 wildcard. */
1112 if (fw->ip.outiface_mask[strlen(fw->ip.outiface)] == 0)
1113 strcat(iface, "+");
1114 }
1115 else if (format & FMT_NUMERIC) strcat(iface, "*");
1116 else strcat(iface, "any");
1117 printf(FMT("%-6s ","out %s "), iface);
1118 }
1119
1120 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1121 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1122 printf(FMT("%-19s ","%s "), "anywhere");
1123 else {
1124 if (format & FMT_NUMERIC)
1125 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1126 else
1127 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1128 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1129 printf(FMT("%-19s ","%s "), buf);
1130 }
1131
1132 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1133 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1134 printf(FMT("%-19s","-> %s"), "anywhere");
1135 else {
1136 if (format & FMT_NUMERIC)
1137 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1138 else
1139 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1140 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1141 printf(FMT("%-19s","-> %s"), buf);
1142 }
1143
1144 if (format & FMT_NOTABLE)
1145 fputs(" ", stdout);
1146
1147 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1148
1149 if (target) {
1150 if (target->print)
1151 /* Print the target information. */
1152 target->print(&fw->ip, t, format & FMT_NUMERIC);
1153 } else if (t->target_size != sizeof(*t))
1154 printf("[%u bytes of unknown target data] ",
1155 t->target_size - sizeof(*t));
1156
1157 if (!(format & FMT_NONEWLINE))
1158 fputc('\n', stdout);
1159}
1160
1161static void
1162print_firewall_line(const struct ipt_entry *fw,
1163 const iptc_handle_t h)
1164{
1165 struct ipt_entry_target *t;
1166
1167 t = ipt_get_target((struct ipt_entry *)fw);
1168 print_firewall(fw, t->u.name, 0, FMT_PRINT_RULE, h);
1169}
1170
1171static int
1172append_entry(const ipt_chainlabel chain,
1173 struct ipt_entry *fw,
1174 unsigned int nsaddrs,
1175 const struct in_addr saddrs[],
1176 unsigned int ndaddrs,
1177 const struct in_addr daddrs[],
1178 int verbose,
1179 iptc_handle_t *handle)
1180{
1181 unsigned int i, j;
1182 int ret = 1;
1183
1184 for (i = 0; i < nsaddrs; i++) {
1185 fw->ip.src.s_addr = saddrs[i].s_addr;
1186 for (j = 0; j < ndaddrs; j++) {
1187 fw->ip.dst.s_addr = daddrs[j].s_addr;
1188 if (verbose)
1189 print_firewall_line(fw, *handle);
1190 ret &= iptc_append_entry(chain, fw, handle);
1191 }
1192 }
1193
1194 return ret;
1195}
1196
1197static int
1198replace_entry(const ipt_chainlabel chain,
1199 struct ipt_entry *fw,
1200 unsigned int rulenum,
1201 const struct in_addr *saddr,
1202 const struct in_addr *daddr,
1203 int verbose,
1204 iptc_handle_t *handle)
1205{
1206 fw->ip.src.s_addr = saddr->s_addr;
1207 fw->ip.dst.s_addr = daddr->s_addr;
1208
1209 if (verbose)
1210 print_firewall_line(fw, *handle);
1211 return iptc_replace_entry(chain, fw, rulenum, handle);
1212}
1213
1214static int
1215insert_entry(const ipt_chainlabel chain,
1216 struct ipt_entry *fw,
1217 unsigned int rulenum,
1218 unsigned int nsaddrs,
1219 const struct in_addr saddrs[],
1220 unsigned int ndaddrs,
1221 const struct in_addr daddrs[],
1222 int verbose,
1223 iptc_handle_t *handle)
1224{
1225 unsigned int i, j;
1226 int ret = 1;
1227
1228 for (i = 0; i < nsaddrs; i++) {
1229 fw->ip.src.s_addr = saddrs[i].s_addr;
1230 for (j = 0; j < ndaddrs; j++) {
1231 fw->ip.dst.s_addr = daddrs[j].s_addr;
1232 if (verbose)
1233 print_firewall_line(fw, *handle);
1234 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1235 }
1236 }
1237
1238 return ret;
1239}
1240
Rusty Russell2e0a3212000-04-19 11:23:18 +00001241static unsigned char *
1242make_delete_mask(struct ipt_entry *fw)
1243{
1244 /* Establish mask for comparison */
1245 unsigned int size;
1246 struct iptables_match *m;
1247 unsigned char *mask, *mptr;
1248
1249 size = sizeof(struct ipt_entry);
1250 for (m = iptables_matches; m; m = m->next)
1251 size += sizeof(struct ipt_entry_match) + m->size;
1252
Rusty Russell9e1d2142000-04-23 09:11:12 +00001253 mask = fw_calloc(1, size
1254 + sizeof(struct ipt_entry_target)
1255 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001256
Rusty Russell9e1d2142000-04-23 09:11:12 +00001257 memset(mask, 0xFF, sizeof(struct ipt_entry));
1258 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001259
1260 for (m = iptables_matches; m; m = m->next) {
1261 memset(mptr, 0xFF,
1262 sizeof(struct ipt_entry_match) + m->userspacesize);
1263 mptr += sizeof(struct ipt_entry_match) + m->size;
1264 }
1265
1266 memset(mptr, 0xFF, sizeof(struct ipt_entry_target));
1267 mptr += sizeof(struct ipt_entry_target);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001268 memset(mptr, 0xFF, iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001269
1270 return mask;
1271}
1272
Marc Bouchere6869a82000-03-20 06:03:29 +00001273static int
1274delete_entry(const ipt_chainlabel chain,
1275 struct ipt_entry *fw,
1276 unsigned int nsaddrs,
1277 const struct in_addr saddrs[],
1278 unsigned int ndaddrs,
1279 const struct in_addr daddrs[],
1280 int verbose,
1281 iptc_handle_t *handle)
1282{
1283 unsigned int i, j;
1284 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001285 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001286
Rusty Russell2e0a3212000-04-19 11:23:18 +00001287 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 for (i = 0; i < nsaddrs; i++) {
1289 fw->ip.src.s_addr = saddrs[i].s_addr;
1290 for (j = 0; j < ndaddrs; j++) {
1291 fw->ip.dst.s_addr = daddrs[j].s_addr;
1292 if (verbose)
1293 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001294 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001295 }
1296 }
1297 return ret;
1298}
1299
1300static int
1301check_packet(const ipt_chainlabel chain,
1302 struct ipt_entry *fw,
1303 unsigned int nsaddrs,
1304 const struct in_addr saddrs[],
1305 unsigned int ndaddrs,
1306 const struct in_addr daddrs[],
1307 int verbose,
1308 iptc_handle_t *handle)
1309{
1310 int ret = 1;
1311 unsigned int i, j;
1312 const char *msg;
1313
1314 for (i = 0; i < nsaddrs; i++) {
1315 fw->ip.src.s_addr = saddrs[i].s_addr;
1316 for (j = 0; j < ndaddrs; j++) {
1317 fw->ip.dst.s_addr = daddrs[j].s_addr;
1318 if (verbose)
1319 print_firewall_line(fw, *handle);
1320 msg = iptc_check_packet(chain, fw, handle);
1321 if (!msg) ret = 0;
1322 else printf("%s\n", msg);
1323 }
1324 }
1325
1326 return ret;
1327}
1328
1329static int
1330for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001331 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001332{
1333 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001334 const char *chain;
1335 char *chains;
1336 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001337
Rusty Russell9e1d2142000-04-23 09:11:12 +00001338 chain = iptc_first_chain(handle);
1339 while (chain) {
1340 chaincount++;
1341 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001342 }
1343
Rusty Russell9e1d2142000-04-23 09:11:12 +00001344 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1345 i = 0;
1346 chain = iptc_first_chain(handle);
1347 while (chain) {
1348 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1349 i++;
1350 chain = iptc_next_chain(handle);
1351 }
1352
1353 for (i = 0; i < chaincount; i++) {
1354 if (!builtinstoo
1355 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1356 *handle))
1357 continue;
1358 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1359 }
1360
1361 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001362 return ret;
1363}
1364
1365static int
1366flush_entries(const ipt_chainlabel chain, int verbose,
1367 iptc_handle_t *handle)
1368{
1369 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001370 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001371
1372 if (verbose)
1373 fprintf(stdout, "Flushing chain `%s'\n", chain);
1374 return iptc_flush_entries(chain, handle);
1375}
Marc Bouchere6869a82000-03-20 06:03:29 +00001376
1377static int
1378zero_entries(const ipt_chainlabel chain, int verbose,
1379 iptc_handle_t *handle)
1380{
1381 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001382 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001383
Marc Bouchere6869a82000-03-20 06:03:29 +00001384 if (verbose)
1385 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1386 return iptc_zero_entries(chain, handle);
1387}
1388
1389static int
1390delete_chain(const ipt_chainlabel chain, int verbose,
1391 iptc_handle_t *handle)
1392{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001393 if (!chain)
1394 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001395
1396 if (verbose)
1397 fprintf(stdout, "Deleting chain `%s'\n", chain);
1398 return iptc_delete_chain(chain, handle);
1399}
1400
1401static int
1402list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1403 int expanded, int linenumbers, iptc_handle_t *handle)
1404{
1405 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001406 unsigned int format;
1407 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001408
1409 format = FMT_OPTIONS;
1410 if (!verbose)
1411 format |= FMT_NOCOUNTS;
1412 else
1413 format |= FMT_VIA;
1414
1415 if (numeric)
1416 format |= FMT_NUMERIC;
1417
1418 if (!expanded)
1419 format |= FMT_KILOMEGAGIGA;
1420
1421 if (linenumbers)
1422 format |= FMT_LINENUMBERS;
1423
Rusty Russell9e1d2142000-04-23 09:11:12 +00001424 for (this = iptc_first_chain(handle);
1425 this;
1426 this = iptc_next_chain(handle)) {
1427 const struct ipt_entry *i;
1428 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001429
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 if (chain && strcmp(chain, this) != 0)
1431 continue;
1432
1433 if (found) printf("\n");
1434
1435 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001436 i = iptc_first_rule(this, handle);
1437
1438 num = 0;
1439 while (i) {
1440 print_firewall(i,
1441 iptc_get_target(i, handle),
1442 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001443 format,
1444 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001445 i = iptc_next_rule(i, handle);
1446 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001447 found = 1;
1448 }
1449
1450 errno = ENOENT;
1451 return found;
1452}
1453
1454static struct ipt_entry *
1455generate_entry(const struct ipt_entry *fw,
1456 struct iptables_match *matches,
1457 struct ipt_entry_target *target)
1458{
1459 unsigned int size;
1460 struct iptables_match *m;
1461 struct ipt_entry *e;
1462
1463 size = sizeof(struct ipt_entry);
1464 for (m = matches; m; m = m->next)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001465 size += m->m->match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001466
1467 e = fw_malloc(size + target->target_size);
1468 *e = *fw;
1469 e->target_offset = size;
1470 e->next_offset = size + target->target_size;
1471
1472 size = 0;
1473 for (m = matches; m; m = m->next) {
Rusty Russell9e1d2142000-04-23 09:11:12 +00001474 memcpy(e->elems + size, m->m, m->m->match_size);
1475 size += m->m->match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001476 }
1477 memcpy(e->elems + size, target, target->target_size);
1478
1479 return e;
1480}
1481
1482int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1483{
1484 struct ipt_entry fw, *e = NULL;
1485 int invert = 0;
1486 unsigned int nsaddrs = 0, ndaddrs = 0;
1487 struct in_addr *saddrs = NULL, *daddrs = NULL;
1488
1489 int c, verbose = 0;
1490 const char *chain = NULL;
1491 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1492 const char *policy = NULL, *newname = NULL;
1493 unsigned int rulenum = 0, options = 0, command = 0;
1494 int ret = 1;
1495 struct iptables_match *m;
1496 struct iptables_target *target = NULL;
1497 const char *jumpto = "";
1498 char *protocol = NULL;
1499
1500 memset(&fw, 0, sizeof(fw));
1501
1502 /* Suppress error messages: we may add new options if we
1503 demand-load a protocol. */
1504 opterr = 0;
1505
1506 while ((c = getopt_long(argc, argv,
1507 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1508 opts, NULL)) != -1) {
1509 switch (c) {
1510 /*
1511 * Command selection
1512 */
1513 case 'A':
1514 add_command(&command, CMD_APPEND, CMD_NONE,
1515 invert);
1516 chain = optarg;
1517 break;
1518
1519 case 'D':
1520 add_command(&command, CMD_DELETE, CMD_NONE,
1521 invert);
1522 chain = optarg;
1523 if (optind < argc && argv[optind][0] != '-'
1524 && argv[optind][0] != '!') {
1525 rulenum = parse_rulenumber(argv[optind++]);
1526 command = CMD_DELETE_NUM;
1527 }
1528 break;
1529
1530 case 'C':
1531 add_command(&command, CMD_CHECK, CMD_NONE,
1532 invert);
1533 chain = optarg;
1534 break;
1535
1536 case 'R':
1537 add_command(&command, CMD_REPLACE, CMD_NONE,
1538 invert);
1539 chain = optarg;
1540 if (optind < argc && argv[optind][0] != '-'
1541 && argv[optind][0] != '!')
1542 rulenum = parse_rulenumber(argv[optind++]);
1543 else
1544 exit_error(PARAMETER_PROBLEM,
1545 "-%c requires a rule number",
1546 cmd2char(CMD_REPLACE));
1547 break;
1548
1549 case 'I':
1550 add_command(&command, CMD_INSERT, CMD_NONE,
1551 invert);
1552 chain = optarg;
1553 if (optind < argc && argv[optind][0] != '-'
1554 && argv[optind][0] != '!')
1555 rulenum = parse_rulenumber(argv[optind++]);
1556 else rulenum = 1;
1557 break;
1558
1559 case 'L':
1560 add_command(&command, CMD_LIST, CMD_ZERO,
1561 invert);
1562 if (optarg) chain = optarg;
1563 else if (optind < argc && argv[optind][0] != '-'
1564 && argv[optind][0] != '!')
1565 chain = argv[optind++];
1566 break;
1567
1568 case 'F':
1569 add_command(&command, CMD_FLUSH, CMD_NONE,
1570 invert);
1571 if (optarg) chain = optarg;
1572 else if (optind < argc && argv[optind][0] != '-'
1573 && argv[optind][0] != '!')
1574 chain = argv[optind++];
1575 break;
1576
1577 case 'Z':
1578 add_command(&command, CMD_ZERO, CMD_LIST,
1579 invert);
1580 if (optarg) chain = optarg;
1581 else if (optind < argc && argv[optind][0] != '-'
1582 && argv[optind][0] != '!')
1583 chain = argv[optind++];
1584 break;
1585
1586 case 'N':
1587 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1588 invert);
1589 chain = optarg;
1590 break;
1591
1592 case 'X':
1593 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1594 invert);
1595 if (optarg) chain = optarg;
1596 else if (optind < argc && argv[optind][0] != '-'
1597 && argv[optind][0] != '!')
1598 chain = argv[optind++];
1599 break;
1600
1601 case 'E':
1602 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1603 invert);
1604 chain = optarg;
1605 if (optind < argc && argv[optind][0] != '-'
1606 && argv[optind][0] != '!')
1607 newname = argv[optind++];
1608 break;
1609
1610 case 'P':
1611 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1612 invert);
1613 chain = optarg;
1614 if (optind < argc && argv[optind][0] != '-'
1615 && argv[optind][0] != '!')
1616 policy = argv[optind++];
1617 else
1618 exit_error(PARAMETER_PROBLEM,
1619 "-%c requires a chain and a policy",
1620 cmd2char(CMD_SET_POLICY));
1621 break;
1622
1623 case 'h':
1624 if (!optarg)
1625 optarg = argv[optind];
1626
Rusty Russell2e0a3212000-04-19 11:23:18 +00001627 /* iptables -p icmp -h */
1628 if (!iptables_matches && protocol)
1629 find_match(protocol, 1);
1630
Marc Bouchere6869a82000-03-20 06:03:29 +00001631 exit_printhelp();
1632
1633 /*
1634 * Option selection
1635 */
1636 case 'p':
1637 if (check_inverse(optarg, &invert))
1638 optind++;
1639 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1640 invert);
1641
1642 /* Canonicalize into lower case */
1643 for (protocol = argv[optind-1]; *protocol; protocol++)
1644 *protocol = tolower(*protocol);
1645
1646 protocol = argv[optind-1];
1647 fw.ip.proto = parse_protocol(protocol);
1648
1649 if (fw.ip.proto == 0
1650 && (fw.ip.invflags & IPT_INV_PROTO))
1651 exit_error(PARAMETER_PROBLEM,
1652 "rule would never match protocol");
1653 fw.nfcache |= NFC_IP_PROTO;
1654 break;
1655
1656 case 's':
1657 if (check_inverse(optarg, &invert))
1658 optind++;
1659 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1660 invert);
1661 shostnetworkmask = argv[optind-1];
1662 fw.nfcache |= NFC_IP_SRC;
1663 break;
1664
1665 case 'd':
1666 if (check_inverse(optarg, &invert))
1667 optind++;
1668 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1669 invert);
1670 dhostnetworkmask = argv[optind-1];
1671 fw.nfcache |= NFC_IP_DST;
1672 break;
1673
1674 case 'j':
1675 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1676 invert);
1677 jumpto = parse_target(optarg);
1678 target = find_target(jumpto, 1);
1679
1680 if (target) {
1681 size_t size = sizeof(struct ipt_entry_target)
1682 + IPT_ALIGN(target->size);
1683
Rusty Russell2e0a3212000-04-19 11:23:18 +00001684 target->t = fw_calloc(1, size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 target->t->target_size = size;
1686 strcpy(target->t->u.name, jumpto);
1687 target->init(target->t, &fw.nfcache);
1688 }
1689 break;
1690
1691
1692 case 'i':
1693 if (check_inverse(optarg, &invert))
1694 optind++;
1695 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1696 invert);
1697 parse_interface(argv[optind-1],
1698 fw.ip.iniface,
1699 fw.ip.iniface_mask);
1700 fw.nfcache |= NFC_IP_IF_IN;
1701 break;
1702
1703 case 'o':
1704 if (check_inverse(optarg, &invert))
1705 optind++;
1706 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1707 invert);
1708 parse_interface(argv[optind-1],
1709 fw.ip.outiface,
1710 fw.ip.outiface_mask);
1711 fw.nfcache |= NFC_IP_IF_OUT;
1712 break;
1713
1714 case 'f':
1715 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1716 invert);
1717 fw.ip.flags |= IPT_F_FRAG;
1718 fw.nfcache |= NFC_IP_FRAG;
1719 break;
1720
1721 case 'v':
1722 if (!verbose)
1723 set_option(&options, OPT_VERBOSE,
1724 &fw.ip.invflags, invert);
1725 verbose++;
1726 break;
1727
1728 case 'm':
1729 if (invert)
1730 exit_error(PARAMETER_PROBLEM,
1731 "unexpected ! flag before --match");
1732
1733 m = find_match(optarg, 1);
1734 if (!m)
1735 exit_error(PARAMETER_PROBLEM,
1736 "Couldn't load match `%s'", optarg);
1737 else {
1738 size_t size = sizeof(struct ipt_entry_match)
1739 + IPT_ALIGN(m->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001740 m->m = fw_calloc(1, size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001741 m->m->match_size = size;
1742 strcpy(m->m->u.name, optarg);
1743 m->init(m->m, &fw.nfcache);
1744 }
1745 break;
1746
1747 case 'n':
1748 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1749 invert);
1750 break;
1751
1752 case 't':
1753 if (invert)
1754 exit_error(PARAMETER_PROBLEM,
1755 "unexpected ! flag before --table");
1756 *table = argv[optind-1];
1757 break;
1758
1759 case 'x':
1760 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1761 invert);
1762 break;
1763
1764 case 'V':
1765 if (invert)
1766 printf("Not %s ;-)\n", program_version);
1767 else
1768 printf("%s v%s\n",
1769 program_name, program_version);
1770 exit(0);
1771
1772 case '0':
1773 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1774 invert);
1775 break;
1776
1777 case 1: /* non option */
1778 if (optarg[0] == '!' && optarg[1] == '\0') {
1779 if (invert)
1780 exit_error(PARAMETER_PROBLEM,
1781 "multiple consecutive ! not"
1782 " allowed");
1783 invert = TRUE;
1784 optarg[0] = '\0';
1785 continue;
1786 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001787 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001788 exit_tryhelp(2);
1789
1790 default:
1791 /* FIXME: This scheme doesn't allow two of the same
1792 matches --RR */
1793 if (!target
1794 || !(target->parse(c - target->option_offset,
1795 argv, invert,
1796 &target->tflags,
1797 &fw, &target->t))) {
1798 for (m = iptables_matches; m; m = m->next) {
1799 if (m->parse(c - m->option_offset,
1800 argv, invert,
1801 &m->mflags,
1802 &fw,
1803 &fw.nfcache,
1804 &m->m))
1805 break;
1806 }
1807
1808 /* If you listen carefully, you can
1809 acually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00001810 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00001811 && protocol
Rusty Russell9e1d2142000-04-23 09:11:12 +00001812 && !find_match(protocol, 0)
Marc Bouchere6869a82000-03-20 06:03:29 +00001813 && (m = find_match(protocol, 1))) {
1814 /* Try loading protocol */
1815 size_t size = sizeof(struct ipt_entry_match)
1816 + IPT_ALIGN(m->size);
1817
Rusty Russell2e0a3212000-04-19 11:23:18 +00001818 m->m = fw_calloc(1, size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001819 m->m->match_size = size;
1820 strcpy(m->m->u.name, protocol);
1821 m->init(m->m, &fw.nfcache);
1822
1823 optind--;
1824 continue;
1825 }
1826 if (!m)
1827 exit_error(PARAMETER_PROBLEM,
1828 "Unknown arg `%s'",
1829 argv[optind-1]);
1830 }
1831 }
1832 invert = FALSE;
1833 }
1834
1835 for (m = iptables_matches; m; m = m->next)
1836 m->final_check(m->mflags);
1837 if (target)
1838 target->final_check(target->tflags);
1839
1840 /* Fix me: must put inverse options checking here --MN */
1841
1842 if (optind < argc)
1843 exit_error(PARAMETER_PROBLEM,
1844 "unknown arguments found on commandline");
1845 if (!command)
1846 exit_error(PARAMETER_PROBLEM, "no command specified");
1847 if (invert)
1848 exit_error(PARAMETER_PROBLEM,
1849 "nothing appropriate following !");
1850
Marc Boucher744bd022000-04-22 22:36:10 +00001851 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1852 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001853 if (!(options & OPT_DESTINATION))
1854 dhostnetworkmask = "0.0.0.0/0";
1855 if (!(options & OPT_SOURCE))
1856 shostnetworkmask = "0.0.0.0/0";
1857 }
1858
1859 if (shostnetworkmask)
1860 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1861 &(fw.ip.smsk), &nsaddrs);
1862
1863 if (dhostnetworkmask)
1864 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1865 &(fw.ip.dmsk), &ndaddrs);
1866
1867 if ((nsaddrs > 1 || ndaddrs > 1) &&
1868 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1869 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1870 " source or destination IP addresses");
1871
1872 if (command == CMD_CHECK && fw.ip.invflags != 0)
1873 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1874 cmd2char(CMD_CHECK));
1875
1876 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1877 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1878 "specify a unique address");
1879
1880 generic_opt_check(command, options);
1881
1882 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1883 exit_error(PARAMETER_PROBLEM,
1884 "chain name `%s' too long (must be under %i chars)",
1885 chain, IPT_FUNCTION_MAXNAMELEN);
1886
1887 *handle = iptc_init(*table);
1888 if (!*handle)
1889 exit_error(VERSION_PROBLEM,
1890 "can't initialize iptables table `%s': %s",
1891 *table, iptc_strerror(errno));
1892
Marc Boucher744bd022000-04-22 22:36:10 +00001893 if (command == CMD_CHECK
1894 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001895 || command == CMD_DELETE
1896 || command == CMD_INSERT
1897 || command == CMD_REPLACE) {
1898 /* -o not valid with incoming packets. */
1899 if (options & OPT_VIANAMEOUT)
1900 if (strcmp(chain, "PREROUTING") == 0
1901 || strcmp(chain, "INPUT") == 0) {
1902 exit_error(PARAMETER_PROBLEM,
1903 "Can't use -%c with %s\n",
1904 opt2char(OPT_VIANAMEOUT),
1905 chain);
1906 }
1907
1908 /* -i not valid with outgoing packets */
1909 if (options & OPT_VIANAMEIN)
1910 if (strcmp(chain, "POSTROUTING") == 0
1911 || strcmp(chain, "OUTPUT") == 0) {
1912 exit_error(PARAMETER_PROBLEM,
1913 "Can't use -%c with %s\n",
1914 opt2char(OPT_VIANAMEIN),
1915 chain);
1916 }
1917
1918 if (target && iptc_is_chain(jumpto, *handle)) {
1919 printf("Warning: using chain %s, not extension\n",
1920 jumpto);
1921
1922 target = NULL;
1923 }
1924
1925 /* If they didn't specify a target, or it's a chain
1926 name, use standard. */
1927 if (!target
1928 && (strlen(jumpto) == 0
1929 || iptc_is_chain(jumpto, *handle))) {
1930 size_t size;
1931 target = find_target(IPT_STANDARD_TARGET, 1);
1932
1933 if (!target)
1934 exit_error(OTHER_PROBLEM,
1935 "Can't find standard target\n");
1936
1937 size = sizeof(struct ipt_entry_target)
1938 + IPT_ALIGN(target->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001939 target->t = fw_calloc(1, size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001940 target->t->target_size = size;
1941 strcpy(target->t->u.name, jumpto);
1942 target->init(target->t, &fw.nfcache);
1943 }
1944
Rusty Russell7e53bf92000-03-20 07:03:28 +00001945 if (!target) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001946 struct ipt_entry_target unknown_target;
Rusty Russell7e53bf92000-03-20 07:03:28 +00001947
Marc Bouchere6869a82000-03-20 06:03:29 +00001948 /* Don't know it. Must be extension with no
1949 options? */
1950 unknown_target.target_size = sizeof(unknown_target);
1951 strcpy(unknown_target.u.name, jumpto);
1952
1953 e = generate_entry(&fw, iptables_matches,
1954 &unknown_target);
1955 } else {
1956 e = generate_entry(&fw, iptables_matches, target->t);
1957 }
1958 }
1959
1960 switch (command) {
1961 case CMD_APPEND:
1962 ret = append_entry(chain, e,
1963 nsaddrs, saddrs, ndaddrs, daddrs,
1964 options&OPT_VERBOSE,
1965 handle);
1966 break;
1967 case CMD_CHECK:
1968 ret = check_packet(chain, e,
1969 nsaddrs, saddrs, ndaddrs, daddrs,
1970 options&OPT_VERBOSE, handle);
1971 break;
1972 case CMD_DELETE:
1973 ret = delete_entry(chain, e,
1974 nsaddrs, saddrs, ndaddrs, daddrs,
1975 options&OPT_VERBOSE,
1976 handle);
1977 break;
1978 case CMD_DELETE_NUM:
1979 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
1980 break;
1981 case CMD_REPLACE:
1982 ret = replace_entry(chain, e, rulenum - 1,
1983 saddrs, daddrs, options&OPT_VERBOSE,
1984 handle);
1985 break;
1986 case CMD_INSERT:
1987 ret = insert_entry(chain, e, rulenum - 1,
1988 nsaddrs, saddrs, ndaddrs, daddrs,
1989 options&OPT_VERBOSE,
1990 handle);
1991 break;
1992 case CMD_LIST:
1993 ret = list_entries(chain,
1994 options&OPT_VERBOSE,
1995 options&OPT_NUMERIC,
1996 options&OPT_EXPANDED,
1997 options&OPT_LINENUMBERS,
1998 handle);
1999 break;
2000 case CMD_FLUSH:
2001 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2002 break;
2003 case CMD_ZERO:
2004 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2005 break;
2006 case CMD_LIST|CMD_ZERO:
2007 ret = list_entries(chain,
2008 options&OPT_VERBOSE,
2009 options&OPT_NUMERIC,
2010 options&OPT_EXPANDED,
2011 options&OPT_LINENUMBERS,
2012 handle);
2013 if (ret)
2014 ret = zero_entries(chain,
2015 options&OPT_VERBOSE, handle);
2016 break;
2017 case CMD_NEW_CHAIN:
2018 ret = iptc_create_chain(chain, handle);
2019 break;
2020 case CMD_DELETE_CHAIN:
2021 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2022 break;
2023 case CMD_RENAME_CHAIN:
2024 ret = iptc_rename_chain(chain, newname, handle);
2025 break;
2026 case CMD_SET_POLICY:
2027 ret = iptc_set_policy(chain, policy, handle);
2028 break;
2029 default:
2030 /* We should never reach this... */
2031 exit_tryhelp(2);
2032 }
2033
2034 if (verbose > 1)
2035 dump_entries(*handle);
2036
2037 return ret;
2038}