blob: cd3c1fe20587c01a5a639905dea5786fd8294abb [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 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
41#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
50#ifndef IPT_LIB_DIR
51#define IPT_LIB_DIR "/usr/local/lib/iptables"
52#endif
53
Harald Welte82dd2ec2000-12-19 05:18:15 +000054#ifndef PROC_SYS_MODPROBE
55#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
56#endif
57
Marc Bouchere6869a82000-03-20 06:03:29 +000058#define FMT_NUMERIC 0x0001
59#define FMT_NOCOUNTS 0x0002
60#define FMT_KILOMEGAGIGA 0x0004
61#define FMT_OPTIONS 0x0008
62#define FMT_NOTABLE 0x0010
63#define FMT_NOTARGET 0x0020
64#define FMT_VIA 0x0040
65#define FMT_NONEWLINE 0x0080
66#define FMT_LINENUMBERS 0x0100
67
68#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
69 | FMT_NUMERIC | FMT_NOTABLE)
70#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
71
72
73#define CMD_NONE 0x0000U
74#define CMD_INSERT 0x0001U
75#define CMD_DELETE 0x0002U
76#define CMD_DELETE_NUM 0x0004U
77#define CMD_REPLACE 0x0008U
78#define CMD_APPEND 0x0010U
79#define CMD_LIST 0x0020U
80#define CMD_FLUSH 0x0040U
81#define CMD_ZERO 0x0080U
82#define CMD_NEW_CHAIN 0x0100U
83#define CMD_DELETE_CHAIN 0x0200U
84#define CMD_SET_POLICY 0x0400U
85#define CMD_CHECK 0x0800U
86#define CMD_RENAME_CHAIN 0x1000U
87#define NUMBER_OF_CMD 13
88static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000089 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000090
91#define OPTION_OFFSET 256
92
93#define OPT_NONE 0x00000U
94#define OPT_NUMERIC 0x00001U
95#define OPT_SOURCE 0x00002U
96#define OPT_DESTINATION 0x00004U
97#define OPT_PROTOCOL 0x00008U
98#define OPT_JUMP 0x00010U
99#define OPT_VERBOSE 0x00020U
100#define OPT_EXPANDED 0x00040U
101#define OPT_VIANAMEIN 0x00080U
102#define OPT_VIANAMEOUT 0x00100U
103#define OPT_FRAGMENT 0x00200U
104#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +0000105#define OPT_COUNTERS 0x00800U
106#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000107static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000108= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000109
110static struct option original_opts[] = {
111 { "append", 1, 0, 'A' },
112 { "delete", 1, 0, 'D' },
113 { "insert", 1, 0, 'I' },
114 { "replace", 1, 0, 'R' },
115 { "list", 2, 0, 'L' },
116 { "flush", 2, 0, 'F' },
117 { "zero", 2, 0, 'Z' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000118 { "new-chain", 1, 0, 'N' },
119 { "delete-chain", 2, 0, 'X' },
120 { "rename-chain", 2, 0, 'E' },
121 { "policy", 1, 0, 'P' },
122 { "source", 1, 0, 's' },
123 { "destination", 1, 0, 'd' },
124 { "src", 1, 0, 's' }, /* synonym */
125 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000126 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000127 { "in-interface", 1, 0, 'i' },
128 { "jump", 1, 0, 'j' },
129 { "table", 1, 0, 't' },
130 { "match", 1, 0, 'm' },
131 { "numeric", 0, 0, 'n' },
132 { "out-interface", 1, 0, 'o' },
133 { "verbose", 0, 0, 'v' },
134 { "exact", 0, 0, 'x' },
135 { "fragments", 0, 0, 'f' },
136 { "version", 0, 0, 'V' },
137 { "help", 2, 0, 'h' },
138 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000139 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000140 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000141 { 0 }
142};
143
Rusty Russell4e242f82000-05-31 06:33:50 +0000144#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000145struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000146ipt_get_target(struct ipt_entry *e)
147{
148 return (void *)e + e->target_offset;
149}
150#endif
151
Marc Bouchere6869a82000-03-20 06:03:29 +0000152static struct option *opts = original_opts;
153static unsigned int global_option_offset = 0;
154
155/* Table of legal combinations of commands and options. If any of the
156 * given commands make an option legal, that option is legal (applies to
157 * CMD_LIST and CMD_ZERO only).
158 * Key:
159 * + compulsory
160 * x illegal
161 * optional
162 */
163
164static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
165/* Well, it's better than "Re: Linux vs FreeBSD" */
166{
167 /* -n -s -d -p -j -v -x -i -o -f --line */
168/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
169/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
170/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
171/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
172/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
173/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
174/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
175/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
176/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
177/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
178/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000179/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000180/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
181};
182
183static int inverse_for_options[NUMBER_OF_OPT] =
184{
185/* -n */ 0,
186/* -s */ IPT_INV_SRCIP,
187/* -d */ IPT_INV_DSTIP,
188/* -p */ IPT_INV_PROTO,
189/* -j */ 0,
190/* -v */ 0,
191/* -x */ 0,
192/* -i */ IPT_INV_VIA_IN,
193/* -o */ IPT_INV_VIA_OUT,
194/* -f */ IPT_INV_FRAG,
195/*--line*/ 0
196};
197
198const char *program_version;
199const char *program_name;
200
Rusty Russell2e0a3212000-04-19 11:23:18 +0000201/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000202struct iptables_match *iptables_matches = NULL;
203struct iptables_target *iptables_targets = NULL;
204
205/* Extra debugging from libiptc */
206extern void dump_entries(const iptc_handle_t handle);
207
208/* A few hardcoded protocols for 'all' and in case the user has no
209 /etc/protocols */
210struct pprot {
211 char *name;
212 u_int8_t num;
213};
214
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000215/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000216/* defined in netinet/in.h */
217#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000218#ifndef IPPROTO_ESP
219#define IPPROTO_ESP 50
220#endif
221#ifndef IPPROTO_AH
222#define IPPROTO_AH 51
223#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000224#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000225
Marc Bouchere6869a82000-03-20 06:03:29 +0000226static const struct pprot chain_protos[] = {
227 { "tcp", IPPROTO_TCP },
228 { "udp", IPPROTO_UDP },
229 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000230 { "esp", IPPROTO_ESP },
231 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000232 { "all", 0 },
233};
234
235static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000236proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000237{
238 unsigned int i;
239
Rusty Russell28381a42000-05-10 00:19:50 +0000240 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
253struct in_addr *
254dotted_to_addr(const char *dotted)
255{
256 static struct in_addr addr;
257 unsigned char *addrp;
258 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000259 unsigned int onebyte;
260 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000261 char buf[20];
262
263 /* copy dotted string, because we need to modify it */
264 strncpy(buf, dotted, sizeof(buf) - 1);
265 addrp = (unsigned char *) &(addr.s_addr);
266
267 p = buf;
268 for (i = 0; i < 3; i++) {
269 if ((q = strchr(p, '.')) == NULL)
270 return (struct in_addr *) NULL;
271
272 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000273 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 return (struct in_addr *) NULL;
275
276 addrp[i] = (unsigned char) onebyte;
277 p = q + 1;
278 }
279
280 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000281 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000282 return (struct in_addr *) NULL;
283
284 addrp[3] = (unsigned char) onebyte;
285
286 return &addr;
287}
288
289static struct in_addr *
290network_to_addr(const char *name)
291{
292 struct netent *net;
293 static struct in_addr addr;
294
295 if ((net = getnetbyname(name)) != NULL) {
296 if (net->n_addrtype != AF_INET)
297 return (struct in_addr *) NULL;
298 addr.s_addr = htonl((unsigned long) net->n_net);
299 return &addr;
300 }
301
302 return (struct in_addr *) NULL;
303}
304
305static void
306inaddrcpy(struct in_addr *dst, struct in_addr *src)
307{
308 /* memcpy(dst, src, sizeof(struct in_addr)); */
309 dst->s_addr = src->s_addr;
310}
311
312void
313exit_error(enum exittype status, char *msg, ...)
314{
315 va_list args;
316
317 va_start(args, msg);
318 fprintf(stderr, "%s v%s: ", program_name, program_version);
319 vfprintf(stderr, msg, args);
320 va_end(args);
321 fprintf(stderr, "\n");
322 if (status == PARAMETER_PROBLEM)
323 exit_tryhelp(status);
324 if (status == VERSION_PROBLEM)
325 fprintf(stderr,
326 "Perhaps iptables or your kernel needs to be upgraded.\n");
327 exit(status);
328}
329
330void
331exit_tryhelp(int status)
332{
333 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
334 program_name, program_name );
335 exit(status);
336}
337
338void
339exit_printhelp(void)
340{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000341 struct iptables_match *m = NULL;
342 struct iptables_target *t = NULL;
343
Marc Bouchere6869a82000-03-20 06:03:29 +0000344 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000345"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000346" %s -[RI] chain rulenum rule-specification [options]\n"
347" %s -D chain rulenum [options]\n"
348" %s -[LFZ] [chain] [options]\n"
349" %s -[NX] chain\n"
350" %s -E old-chain-name new-chain-name\n"
351" %s -P chain target [options]\n"
352" %s -h (print this help information)\n\n",
353 program_name, program_version, program_name, program_name,
354 program_name, program_name, program_name, program_name,
355 program_name, program_name);
356
357 printf(
358"Commands:\n"
359"Either long or short options are allowed.\n"
360" --append -A chain Append to chain\n"
361" --delete -D chain Delete matching rule from chain\n"
362" --delete -D chain rulenum\n"
363" Delete rule rulenum (1 = first) from chain\n"
364" --insert -I chain [rulenum]\n"
365" Insert in chain as rulenum (default 1=first)\n"
366" --replace -R chain rulenum\n"
367" Replace rule rulenum (1 = first) in chain\n"
368" --list -L [chain] List the rules in a chain or all chains\n"
369" --flush -F [chain] Delete all rules in chain or all chains\n"
370" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000371" --new -N chain Create a new user-defined chain\n"
372" --delete-chain\n"
373" -X [chain] Delete a user-defined chain\n"
374" --policy -P chain target\n"
375" Change policy on chain to target\n"
376" --rename-chain\n"
377" -E old-chain new-chain\n"
378" Change chain name, (moving any references)\n"
379
380"Options:\n"
381" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
382" --source -s [!] address[/mask]\n"
383" source specification\n"
384" --destination -d [!] address[/mask]\n"
385" destination specification\n"
386" --in-interface -i [!] input name[+]\n"
387" network interface name ([+] for wildcard)\n"
388" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000389" target for rule (may load target extension)\n"
390" --match -m match\n"
391" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000392" --numeric -n numeric output of addresses and ports\n"
393" --out-interface -o [!] output name[+]\n"
394" network interface name ([+] for wildcard)\n"
395" --table -t table table to manipulate (default: `filter')\n"
396" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000397" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000398" --exact -x expand numbers (display exact values)\n"
399"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000400" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000401" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000402"[!] --version -V print package version.\n");
403
Rusty Russell363112d2000-08-11 13:49:26 +0000404 /* Print out any special helps. A user might like to be able
405 to add a --help to the commandline, and see expected
406 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000407 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000408 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000409 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000410 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000411 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000412 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000413 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000414 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000415 exit(0);
416}
417
418static void
419generic_opt_check(int command, int options)
420{
421 int i, j, legal = 0;
422
423 /* Check that commands are valid with options. Complicated by the
424 * fact that if an option is legal with *any* command given, it is
425 * legal overall (ie. -z and -l).
426 */
427 for (i = 0; i < NUMBER_OF_OPT; i++) {
428 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
429
430 for (j = 0; j < NUMBER_OF_CMD; j++) {
431 if (!(command & (1<<j)))
432 continue;
433
434 if (!(options & (1<<i))) {
435 if (commands_v_options[j][i] == '+')
436 exit_error(PARAMETER_PROBLEM,
437 "You need to supply the `-%c' "
438 "option for this command\n",
439 optflags[i]);
440 } else {
441 if (commands_v_options[j][i] != 'x')
442 legal = 1;
443 else if (legal == 0)
444 legal = -1;
445 }
446 }
447 if (legal == -1)
448 exit_error(PARAMETER_PROBLEM,
449 "Illegal option `-%c' with this command\n",
450 optflags[i]);
451 }
452}
453
454static char
455opt2char(int option)
456{
457 const char *ptr;
458 for (ptr = optflags; option > 1; option >>= 1, ptr++);
459
460 return *ptr;
461}
462
463static char
464cmd2char(int option)
465{
466 const char *ptr;
467 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
468
469 return *ptr;
470}
471
472static void
473add_command(int *cmd, const int newcmd, const int othercmds, int invert)
474{
475 if (invert)
476 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
477 if (*cmd & (~othercmds))
478 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
479 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
480 *cmd |= newcmd;
481}
482
483int
Harald Welteb77f1da2002-03-14 11:35:58 +0000484check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000485{
486 if (option && strcmp(option, "!") == 0) {
487 if (*invert)
488 exit_error(PARAMETER_PROBLEM,
489 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000490 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000491 if (optind) {
492 *optind = *optind+1;
493 if (argc && *optind > argc)
494 exit_error(PARAMETER_PROBLEM,
495 "no argument following `!'");
496 }
497
Marc Bouchere6869a82000-03-20 06:03:29 +0000498 return TRUE;
499 }
500 return FALSE;
501}
502
503static void *
504fw_calloc(size_t count, size_t size)
505{
506 void *p;
507
508 if ((p = calloc(count, size)) == NULL) {
509 perror("iptables: calloc failed");
510 exit(1);
511 }
512 return p;
513}
514
515static void *
516fw_malloc(size_t size)
517{
518 void *p;
519
520 if ((p = malloc(size)) == NULL) {
521 perror("iptables: malloc failed");
522 exit(1);
523 }
524 return p;
525}
526
527static struct in_addr *
528host_to_addr(const char *name, unsigned int *naddr)
529{
530 struct hostent *host;
531 struct in_addr *addr;
532 unsigned int i;
533
534 *naddr = 0;
535 if ((host = gethostbyname(name)) != NULL) {
536 if (host->h_addrtype != AF_INET ||
537 host->h_length != sizeof(struct in_addr))
538 return (struct in_addr *) NULL;
539
540 while (host->h_addr_list[*naddr] != (char *) NULL)
541 (*naddr)++;
542 addr = fw_calloc(*naddr, sizeof(struct in_addr));
543 for (i = 0; i < *naddr; i++)
544 inaddrcpy(&(addr[i]),
545 (struct in_addr *) host->h_addr_list[i]);
546 return addr;
547 }
548
549 return (struct in_addr *) NULL;
550}
551
552static char *
553addr_to_host(const struct in_addr *addr)
554{
555 struct hostent *host;
556
557 if ((host = gethostbyaddr((char *) addr,
558 sizeof(struct in_addr), AF_INET)) != NULL)
559 return (char *) host->h_name;
560
561 return (char *) NULL;
562}
563
564/*
565 * All functions starting with "parse" should succeed, otherwise
566 * the program fails.
567 * Most routines return pointers to static data that may change
568 * between calls to the same or other routines with a few exceptions:
569 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
570 * return global static data.
571*/
572
573static struct in_addr *
574parse_hostnetwork(const char *name, unsigned int *naddrs)
575{
576 struct in_addr *addrp, *addrptmp;
577
578 if ((addrptmp = dotted_to_addr(name)) != NULL ||
579 (addrptmp = network_to_addr(name)) != NULL) {
580 addrp = fw_malloc(sizeof(struct in_addr));
581 inaddrcpy(addrp, addrptmp);
582 *naddrs = 1;
583 return addrp;
584 }
585 if ((addrp = host_to_addr(name, naddrs)) != NULL)
586 return addrp;
587
588 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
589}
590
591static struct in_addr *
592parse_mask(char *mask)
593{
594 static struct in_addr maskaddr;
595 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000596 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000597
598 if (mask == NULL) {
599 /* no mask at all defaults to 32 bits */
600 maskaddr.s_addr = 0xFFFFFFFF;
601 return &maskaddr;
602 }
603 if ((addrp = dotted_to_addr(mask)) != NULL)
604 /* dotted_to_addr already returns a network byte order addr */
605 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000606 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000607 exit_error(PARAMETER_PROBLEM,
608 "invalid mask `%s' specified", mask);
609 if (bits != 0) {
610 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
611 return &maskaddr;
612 }
613
614 maskaddr.s_addr = 0L;
615 return &maskaddr;
616}
617
Marc Boucherb93c7982001-12-06 14:50:19 +0000618void
Marc Bouchere6869a82000-03-20 06:03:29 +0000619parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
620 struct in_addr *maskp, unsigned int *naddrs)
621{
622 struct in_addr *addrp;
623 char buf[256];
624 char *p;
625 int i, j, k, n;
626
627 strncpy(buf, name, sizeof(buf) - 1);
628 if ((p = strrchr(buf, '/')) != NULL) {
629 *p = '\0';
630 addrp = parse_mask(p + 1);
631 } else
632 addrp = parse_mask(NULL);
633 inaddrcpy(maskp, addrp);
634
635 /* if a null mask is given, the name is ignored, like in "any/0" */
636 if (maskp->s_addr == 0L)
637 strcpy(buf, "0.0.0.0");
638
639 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
640 n = *naddrs;
641 for (i = 0, j = 0; i < n; i++) {
642 addrp[j++].s_addr &= maskp->s_addr;
643 for (k = 0; k < j - 1; k++) {
644 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
645 (*naddrs)--;
646 j--;
647 break;
648 }
649 }
650 }
651}
652
653struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000654find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000655{
656 struct iptables_match *ptr;
657
658 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
659 if (strcmp(name, ptr->name) == 0)
660 break;
661 }
662
Harald Welte3efb6ea2001-08-06 18:50:21 +0000663#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000664 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000665 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
666 + strlen(name)];
667 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000668 if (dlopen(path, RTLD_NOW)) {
669 /* Found library. If it didn't register itself,
670 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000671 ptr = find_match(name, DONT_LOAD);
672
Rusty Russell9e1d2142000-04-23 09:11:12 +0000673 if (!ptr)
674 exit_error(PARAMETER_PROBLEM,
675 "Couldn't load match `%s'\n",
676 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000677 } else if (tryload == LOAD_MUST_SUCCEED)
678 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000679 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000680 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000681 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000682#else
683 if (ptr && !ptr->loaded) {
684 if (tryload != DONT_LOAD)
685 ptr->loaded = 1;
686 else
687 ptr = NULL;
688 }
Marc Boucher067477b2002-03-24 15:09:31 +0000689 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
690 exit_error(PARAMETER_PROBLEM,
691 "Couldn't find match `%s'\n", name);
692 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000693#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000694
Harald Welteae1ff9f2000-12-01 14:26:20 +0000695 if (ptr)
696 ptr->used = 1;
697
Marc Bouchere6869a82000-03-20 06:03:29 +0000698 return ptr;
699}
700
Rusty Russell28381a42000-05-10 00:19:50 +0000701/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
702static struct iptables_match *
703find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
704{
Harald Welteed498492001-07-23 01:24:22 +0000705 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000706
Harald Welte0b0013a2002-02-18 16:15:31 +0000707 if (string_to_number(pname, 0, 255, &proto) != -1) {
708 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000709
Harald Welte0b0013a2002-02-18 16:15:31 +0000710 if (protoname)
711 return find_match(protoname, tryload);
712 } else
713 return find_match(pname, tryload);
714
715 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000716}
717
Marc Boucherb93c7982001-12-06 14:50:19 +0000718u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000719parse_protocol(const char *s)
720{
Harald Welteed498492001-07-23 01:24:22 +0000721 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000722
Harald Welteed498492001-07-23 01:24:22 +0000723 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000724 struct protoent *pent;
725
726 if ((pent = getprotobyname(s)))
727 proto = pent->p_proto;
728 else {
729 unsigned int i;
730 for (i = 0;
731 i < sizeof(chain_protos)/sizeof(struct pprot);
732 i++) {
733 if (strcmp(s, chain_protos[i].name) == 0) {
734 proto = chain_protos[i].num;
735 break;
736 }
737 }
738 if (i == sizeof(chain_protos)/sizeof(struct pprot))
739 exit_error(PARAMETER_PROBLEM,
740 "unknown protocol `%s' specified",
741 s);
742 }
743 }
744
745 return (u_int16_t)proto;
746}
747
748static void
749parse_interface(const char *arg, char *vianame, unsigned char *mask)
750{
751 int vialen = strlen(arg);
752 unsigned int i;
753
754 memset(mask, 0, IFNAMSIZ);
755 memset(vianame, 0, IFNAMSIZ);
756
757 if (vialen + 1 > IFNAMSIZ)
758 exit_error(PARAMETER_PROBLEM,
759 "interface name `%s' must be shorter than IFNAMSIZ"
760 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000761
Marc Bouchere6869a82000-03-20 06:03:29 +0000762 strcpy(vianame, arg);
763 if (vialen == 0)
764 memset(mask, 0, IFNAMSIZ);
765 else if (vianame[vialen - 1] == '+') {
766 memset(mask, 0xFF, vialen - 1);
767 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000768 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000769 } else {
770 /* Include nul-terminator in match */
771 memset(mask, 0xFF, vialen + 1);
772 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000773 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000774 if (!isalnum(vianame[i])
775 && vianame[i] != '_'
776 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000777 printf("Warning: wierd character in interface"
778 " `%s' (No aliases, :, ! or *).\n",
779 vianame);
780 break;
781 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000782 }
783 }
784}
785
786/* Can't be zero. */
787static int
788parse_rulenumber(const char *rule)
789{
Harald Welteed498492001-07-23 01:24:22 +0000790 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000791
Harald Welteed498492001-07-23 01:24:22 +0000792 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000793 exit_error(PARAMETER_PROBLEM,
794 "Invalid rule number `%s'", rule);
795
796 return rulenum;
797}
798
799static const char *
800parse_target(const char *targetname)
801{
802 const char *ptr;
803
804 if (strlen(targetname) < 1)
805 exit_error(PARAMETER_PROBLEM,
806 "Invalid target name (too short)");
807
808 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
809 exit_error(PARAMETER_PROBLEM,
810 "Invalid target name `%s' (%i chars max)",
811 targetname, sizeof(ipt_chainlabel)-1);
812
813 for (ptr = targetname; *ptr; ptr++)
814 if (isspace(*ptr))
815 exit_error(PARAMETER_PROBLEM,
816 "Invalid target name `%s'", targetname);
817 return targetname;
818}
819
820static char *
821addr_to_network(const struct in_addr *addr)
822{
823 struct netent *net;
824
825 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
826 return (char *) net->n_name;
827
828 return (char *) NULL;
829}
830
831char *
832addr_to_dotted(const struct in_addr *addrp)
833{
834 static char buf[20];
835 const unsigned char *bytep;
836
837 bytep = (const unsigned char *) &(addrp->s_addr);
838 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
839 return buf;
840}
Marc Boucherb93c7982001-12-06 14:50:19 +0000841
842char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000843addr_to_anyname(const struct in_addr *addr)
844{
845 char *name;
846
847 if ((name = addr_to_host(addr)) != NULL ||
848 (name = addr_to_network(addr)) != NULL)
849 return name;
850
851 return addr_to_dotted(addr);
852}
853
Marc Boucherb93c7982001-12-06 14:50:19 +0000854char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000855mask_to_dotted(const struct in_addr *mask)
856{
857 int i;
858 static char buf[20];
859 u_int32_t maskaddr, bits;
860
861 maskaddr = ntohl(mask->s_addr);
862
863 if (maskaddr == 0xFFFFFFFFL)
864 /* we don't want to see "/32" */
865 return "";
866
867 i = 32;
868 bits = 0xFFFFFFFEL;
869 while (--i >= 0 && maskaddr != bits)
870 bits <<= 1;
871 if (i >= 0)
872 sprintf(buf, "/%d", i);
873 else
874 /* mask was not a decent combination of 1's and 0's */
875 sprintf(buf, "/%s", addr_to_dotted(mask));
876
877 return buf;
878}
879
880int
Harald Welteed498492001-07-23 01:24:22 +0000881string_to_number(const char *s, unsigned int min, unsigned int max,
882 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000883{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000884 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000885 char *end;
886
887 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000888 errno = 0;
889 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000890 if (*end == '\0' && end != s) {
891 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000892 if (errno != ERANGE && min <= number && number <= max) {
893 *ret = number;
894 return 0;
895 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000896 }
897 return -1;
898}
899
900static void
901set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
902 int invert)
903{
904 if (*options & option)
905 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
906 opt2char(option));
907 *options |= option;
908
909 if (invert) {
910 unsigned int i;
911 for (i = 0; 1 << i != option; i++);
912
913 if (!inverse_for_options[i])
914 exit_error(PARAMETER_PROBLEM,
915 "cannot have ! before -%c",
916 opt2char(option));
917 *invflg |= inverse_for_options[i];
918 }
919}
920
921struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000922find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000923{
924 struct iptables_target *ptr;
925
926 /* Standard target? */
927 if (strcmp(name, "") == 0
928 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
929 || strcmp(name, IPTC_LABEL_DROP) == 0
930 || strcmp(name, IPTC_LABEL_QUEUE) == 0
931 || strcmp(name, IPTC_LABEL_RETURN) == 0)
932 name = "standard";
933
934 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
935 if (strcmp(name, ptr->name) == 0)
936 break;
937 }
938
Harald Welte3efb6ea2001-08-06 18:50:21 +0000939#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000940 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000941 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
942 + strlen(name)];
943 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000944 if (dlopen(path, RTLD_NOW)) {
945 /* Found library. If it didn't register itself,
946 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000947 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000948 if (!ptr)
949 exit_error(PARAMETER_PROBLEM,
950 "Couldn't load target `%s'\n",
951 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000952 } else if (tryload == LOAD_MUST_SUCCEED)
953 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000954 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000955 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000956 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000957#else
958 if (ptr && !ptr->loaded) {
959 if (tryload != DONT_LOAD)
960 ptr->loaded = 1;
961 else
962 ptr = NULL;
963 }
Marc Boucher067477b2002-03-24 15:09:31 +0000964 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
965 exit_error(PARAMETER_PROBLEM,
966 "Couldn't find target `%s'\n", name);
967 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000968#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000969
Harald Welteae1ff9f2000-12-01 14:26:20 +0000970 if (ptr)
971 ptr->used = 1;
972
Marc Bouchere6869a82000-03-20 06:03:29 +0000973 return ptr;
974}
975
976static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000977merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 unsigned int *option_offset)
979{
980 unsigned int num_old, num_new, i;
981 struct option *merge;
982
983 for (num_old = 0; oldopts[num_old].name; num_old++);
984 for (num_new = 0; newopts[num_new].name; num_new++);
985
986 global_option_offset += OPTION_OFFSET;
987 *option_offset = global_option_offset;
988
989 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
990 memcpy(merge, oldopts, num_old * sizeof(struct option));
991 for (i = 0; i < num_new; i++) {
992 merge[num_old + i] = newopts[i];
993 merge[num_old + i].val += *option_offset;
994 }
995 memset(merge + num_old + num_new, 0, sizeof(struct option));
996
997 return merge;
998}
999
1000void
1001register_match(struct iptables_match *me)
1002{
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001003 struct iptables_match **i;
1004
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 if (strcmp(me->version, program_version) != 0) {
1006 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1007 program_name, me->name, me->version, program_version);
1008 exit(1);
1009 }
1010
Rusty Russell52a51492000-05-02 16:44:29 +00001011 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001012 fprintf(stderr, "%s: match `%s' already registered.\n",
1013 program_name, me->name);
1014 exit(1);
1015 }
1016
Rusty Russell73f72f52000-07-03 10:17:57 +00001017 if (me->size != IPT_ALIGN(me->size)) {
1018 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1019 program_name, me->name, me->size);
1020 exit(1);
1021 }
1022
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001023 /* Append to list. */
1024 for (i = &iptables_matches; *i; i = &(*i)->next);
1025 me->next = NULL;
1026 *i = me;
1027
Marc Bouchere6869a82000-03-20 06:03:29 +00001028 me->m = NULL;
1029 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001030}
1031
1032void
1033register_target(struct iptables_target *me)
1034{
1035 if (strcmp(me->version, program_version) != 0) {
1036 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1037 program_name, me->name, me->version, program_version);
1038 exit(1);
1039 }
1040
Rusty Russell52a51492000-05-02 16:44:29 +00001041 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001042 fprintf(stderr, "%s: target `%s' already registered.\n",
1043 program_name, me->name);
1044 exit(1);
1045 }
1046
Rusty Russell73f72f52000-07-03 10:17:57 +00001047 if (me->size != IPT_ALIGN(me->size)) {
1048 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1049 program_name, me->name, me->size);
1050 exit(1);
1051 }
1052
Marc Bouchere6869a82000-03-20 06:03:29 +00001053 /* Prepend to list. */
1054 me->next = iptables_targets;
1055 iptables_targets = me;
1056 me->t = NULL;
1057 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001058}
1059
1060static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001061print_num(u_int64_t number, unsigned int format)
1062{
1063 if (format & FMT_KILOMEGAGIGA) {
1064 if (number > 99999) {
1065 number = (number + 500) / 1000;
1066 if (number > 9999) {
1067 number = (number + 500) / 1000;
1068 if (number > 9999) {
1069 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001070 if (number > 9999) {
1071 number = (number + 500) / 1000;
1072 printf(FMT("%4lluT ","%lluT "), number);
1073 }
1074 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001075 }
1076 else printf(FMT("%4lluM ","%lluM "), number);
1077 } else
1078 printf(FMT("%4lluK ","%lluK "), number);
1079 } else
1080 printf(FMT("%5llu ","%llu "), number);
1081 } else
1082 printf(FMT("%8llu ","%llu "), number);
1083}
1084
1085
1086static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001087print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1088{
1089 struct ipt_counters counters;
1090 const char *pol = iptc_get_policy(chain, &counters, handle);
1091 printf("Chain %s", chain);
1092 if (pol) {
1093 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001094 if (!(format & FMT_NOCOUNTS)) {
1095 fputc(' ', stdout);
1096 print_num(counters.pcnt, (format|FMT_NOTABLE));
1097 fputs("packets, ", stdout);
1098 print_num(counters.bcnt, (format|FMT_NOTABLE));
1099 fputs("bytes", stdout);
1100 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001101 printf(")\n");
1102 } else {
1103 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001104 if (!iptc_get_references(&refs, chain, handle))
1105 printf(" (ERROR obtaining refs)\n");
1106 else
1107 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001108 }
1109
1110 if (format & FMT_LINENUMBERS)
1111 printf(FMT("%-4s ", "%s "), "num");
1112 if (!(format & FMT_NOCOUNTS)) {
1113 if (format & FMT_KILOMEGAGIGA) {
1114 printf(FMT("%5s ","%s "), "pkts");
1115 printf(FMT("%5s ","%s "), "bytes");
1116 } else {
1117 printf(FMT("%8s ","%s "), "pkts");
1118 printf(FMT("%10s ","%s "), "bytes");
1119 }
1120 }
1121 if (!(format & FMT_NOTARGET))
1122 printf(FMT("%-9s ","%s "), "target");
1123 fputs(" prot ", stdout);
1124 if (format & FMT_OPTIONS)
1125 fputs("opt", stdout);
1126 if (format & FMT_VIA) {
1127 printf(FMT(" %-6s ","%s "), "in");
1128 printf(FMT("%-6s ","%s "), "out");
1129 }
1130 printf(FMT(" %-19s ","%s "), "source");
1131 printf(FMT(" %-19s "," %s "), "destination");
1132 printf("\n");
1133}
1134
Marc Bouchere6869a82000-03-20 06:03:29 +00001135
1136static int
1137print_match(const struct ipt_entry_match *m,
1138 const struct ipt_ip *ip,
1139 int numeric)
1140{
Rusty Russell52a51492000-05-02 16:44:29 +00001141 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001142
1143 if (match) {
1144 if (match->print)
1145 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001146 else
Rusty Russellb039b022000-09-01 06:04:05 +00001147 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001148 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001149 if (m->u.user.name[0])
1150 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001151 }
1152 /* Don't stop iterating. */
1153 return 0;
1154}
1155
1156/* e is called `fw' here for hysterical raisins */
1157static void
1158print_firewall(const struct ipt_entry *fw,
1159 const char *targname,
1160 unsigned int num,
1161 unsigned int format,
1162 const iptc_handle_t handle)
1163{
1164 struct iptables_target *target = NULL;
1165 const struct ipt_entry_target *t;
1166 u_int8_t flags;
1167 char buf[BUFSIZ];
1168
Marc Bouchere6869a82000-03-20 06:03:29 +00001169 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001170 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001171 else
Rusty Russell52a51492000-05-02 16:44:29 +00001172 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001173
1174 t = ipt_get_target((struct ipt_entry *)fw);
1175 flags = fw->ip.flags;
1176
1177 if (format & FMT_LINENUMBERS)
1178 printf(FMT("%-4u ", "%u "), num+1);
1179
1180 if (!(format & FMT_NOCOUNTS)) {
1181 print_num(fw->counters.pcnt, format);
1182 print_num(fw->counters.bcnt, format);
1183 }
1184
1185 if (!(format & FMT_NOTARGET))
1186 printf(FMT("%-9s ", "%s "), targname);
1187
1188 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1189 {
Rusty Russell28381a42000-05-10 00:19:50 +00001190 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001191 if (pname)
1192 printf(FMT("%-5s", "%s "), pname);
1193 else
1194 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1195 }
1196
1197 if (format & FMT_OPTIONS) {
1198 if (format & FMT_NOTABLE)
1199 fputs("opt ", stdout);
1200 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1201 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1202 fputc(' ', stdout);
1203 }
1204
1205 if (format & FMT_VIA) {
1206 char iface[IFNAMSIZ+2];
1207
1208 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1209 iface[0] = '!';
1210 iface[1] = '\0';
1211 }
1212 else iface[0] = '\0';
1213
1214 if (fw->ip.iniface[0] != '\0') {
1215 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001216 }
1217 else if (format & FMT_NUMERIC) strcat(iface, "*");
1218 else strcat(iface, "any");
1219 printf(FMT(" %-6s ","in %s "), iface);
1220
1221 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1222 iface[0] = '!';
1223 iface[1] = '\0';
1224 }
1225 else iface[0] = '\0';
1226
1227 if (fw->ip.outiface[0] != '\0') {
1228 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001229 }
1230 else if (format & FMT_NUMERIC) strcat(iface, "*");
1231 else strcat(iface, "any");
1232 printf(FMT("%-6s ","out %s "), iface);
1233 }
1234
1235 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1236 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1237 printf(FMT("%-19s ","%s "), "anywhere");
1238 else {
1239 if (format & FMT_NUMERIC)
1240 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1241 else
1242 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1243 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1244 printf(FMT("%-19s ","%s "), buf);
1245 }
1246
1247 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1248 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1249 printf(FMT("%-19s","-> %s"), "anywhere");
1250 else {
1251 if (format & FMT_NUMERIC)
1252 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1253 else
1254 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1255 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1256 printf(FMT("%-19s","-> %s"), buf);
1257 }
1258
1259 if (format & FMT_NOTABLE)
1260 fputs(" ", stdout);
1261
1262 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1263
1264 if (target) {
1265 if (target->print)
1266 /* Print the target information. */
1267 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001268 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001269 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001270 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001271
1272 if (!(format & FMT_NONEWLINE))
1273 fputc('\n', stdout);
1274}
1275
1276static void
1277print_firewall_line(const struct ipt_entry *fw,
1278 const iptc_handle_t h)
1279{
1280 struct ipt_entry_target *t;
1281
1282 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001283 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001284}
1285
1286static int
1287append_entry(const ipt_chainlabel chain,
1288 struct ipt_entry *fw,
1289 unsigned int nsaddrs,
1290 const struct in_addr saddrs[],
1291 unsigned int ndaddrs,
1292 const struct in_addr daddrs[],
1293 int verbose,
1294 iptc_handle_t *handle)
1295{
1296 unsigned int i, j;
1297 int ret = 1;
1298
1299 for (i = 0; i < nsaddrs; i++) {
1300 fw->ip.src.s_addr = saddrs[i].s_addr;
1301 for (j = 0; j < ndaddrs; j++) {
1302 fw->ip.dst.s_addr = daddrs[j].s_addr;
1303 if (verbose)
1304 print_firewall_line(fw, *handle);
1305 ret &= iptc_append_entry(chain, fw, handle);
1306 }
1307 }
1308
1309 return ret;
1310}
1311
1312static int
1313replace_entry(const ipt_chainlabel chain,
1314 struct ipt_entry *fw,
1315 unsigned int rulenum,
1316 const struct in_addr *saddr,
1317 const struct in_addr *daddr,
1318 int verbose,
1319 iptc_handle_t *handle)
1320{
1321 fw->ip.src.s_addr = saddr->s_addr;
1322 fw->ip.dst.s_addr = daddr->s_addr;
1323
1324 if (verbose)
1325 print_firewall_line(fw, *handle);
1326 return iptc_replace_entry(chain, fw, rulenum, handle);
1327}
1328
1329static int
1330insert_entry(const ipt_chainlabel chain,
1331 struct ipt_entry *fw,
1332 unsigned int rulenum,
1333 unsigned int nsaddrs,
1334 const struct in_addr saddrs[],
1335 unsigned int ndaddrs,
1336 const struct in_addr daddrs[],
1337 int verbose,
1338 iptc_handle_t *handle)
1339{
1340 unsigned int i, j;
1341 int ret = 1;
1342
1343 for (i = 0; i < nsaddrs; i++) {
1344 fw->ip.src.s_addr = saddrs[i].s_addr;
1345 for (j = 0; j < ndaddrs; j++) {
1346 fw->ip.dst.s_addr = daddrs[j].s_addr;
1347 if (verbose)
1348 print_firewall_line(fw, *handle);
1349 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1350 }
1351 }
1352
1353 return ret;
1354}
1355
Rusty Russell2e0a3212000-04-19 11:23:18 +00001356static unsigned char *
1357make_delete_mask(struct ipt_entry *fw)
1358{
1359 /* Establish mask for comparison */
1360 unsigned int size;
1361 struct iptables_match *m;
1362 unsigned char *mask, *mptr;
1363
1364 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001365 for (m = iptables_matches; m; m = m->next) {
1366 if (!m->used)
1367 continue;
1368
Rusty Russell73f72f52000-07-03 10:17:57 +00001369 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001370 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001371
Rusty Russell9e1d2142000-04-23 09:11:12 +00001372 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001373 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001374 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001375
Rusty Russell9e1d2142000-04-23 09:11:12 +00001376 memset(mask, 0xFF, sizeof(struct ipt_entry));
1377 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001378
Sven Kochfb1279a2001-02-27 12:25:12 +00001379 for (m = iptables_matches; m; m = m->next) {
1380 if (!m->used)
1381 continue;
1382
Rusty Russell2e0a3212000-04-19 11:23:18 +00001383 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001384 IPT_ALIGN(sizeof(struct ipt_entry_match))
1385 + m->userspacesize);
1386 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001387 }
1388
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001389 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001390 IPT_ALIGN(sizeof(struct ipt_entry_target))
1391 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001392
1393 return mask;
1394}
1395
Marc Bouchere6869a82000-03-20 06:03:29 +00001396static int
1397delete_entry(const ipt_chainlabel chain,
1398 struct ipt_entry *fw,
1399 unsigned int nsaddrs,
1400 const struct in_addr saddrs[],
1401 unsigned int ndaddrs,
1402 const struct in_addr daddrs[],
1403 int verbose,
1404 iptc_handle_t *handle)
1405{
1406 unsigned int i, j;
1407 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001408 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001409
Rusty Russell2e0a3212000-04-19 11:23:18 +00001410 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 for (i = 0; i < nsaddrs; i++) {
1412 fw->ip.src.s_addr = saddrs[i].s_addr;
1413 for (j = 0; j < ndaddrs; j++) {
1414 fw->ip.dst.s_addr = daddrs[j].s_addr;
1415 if (verbose)
1416 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001417 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001418 }
1419 }
1420 return ret;
1421}
1422
Harald Welteae1ff9f2000-12-01 14:26:20 +00001423int
Marc Bouchere6869a82000-03-20 06:03:29 +00001424for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001425 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001426{
1427 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001428 const char *chain;
1429 char *chains;
1430 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001431
Rusty Russell9e1d2142000-04-23 09:11:12 +00001432 chain = iptc_first_chain(handle);
1433 while (chain) {
1434 chaincount++;
1435 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 }
1437
Rusty Russell9e1d2142000-04-23 09:11:12 +00001438 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1439 i = 0;
1440 chain = iptc_first_chain(handle);
1441 while (chain) {
1442 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1443 i++;
1444 chain = iptc_next_chain(handle);
1445 }
1446
1447 for (i = 0; i < chaincount; i++) {
1448 if (!builtinstoo
1449 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1450 *handle))
1451 continue;
1452 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1453 }
1454
1455 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001456 return ret;
1457}
1458
Harald Welteae1ff9f2000-12-01 14:26:20 +00001459int
Marc Bouchere6869a82000-03-20 06:03:29 +00001460flush_entries(const ipt_chainlabel chain, int verbose,
1461 iptc_handle_t *handle)
1462{
1463 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001464 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001465
1466 if (verbose)
1467 fprintf(stdout, "Flushing chain `%s'\n", chain);
1468 return iptc_flush_entries(chain, handle);
1469}
Marc Bouchere6869a82000-03-20 06:03:29 +00001470
1471static int
1472zero_entries(const ipt_chainlabel chain, int verbose,
1473 iptc_handle_t *handle)
1474{
1475 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001476 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001477
Marc Bouchere6869a82000-03-20 06:03:29 +00001478 if (verbose)
1479 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1480 return iptc_zero_entries(chain, handle);
1481}
1482
Harald Welteae1ff9f2000-12-01 14:26:20 +00001483int
Marc Bouchere6869a82000-03-20 06:03:29 +00001484delete_chain(const ipt_chainlabel chain, int verbose,
1485 iptc_handle_t *handle)
1486{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001487 if (!chain)
1488 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001489
1490 if (verbose)
1491 fprintf(stdout, "Deleting chain `%s'\n", chain);
1492 return iptc_delete_chain(chain, handle);
1493}
1494
1495static int
1496list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1497 int expanded, int linenumbers, iptc_handle_t *handle)
1498{
1499 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001500 unsigned int format;
1501 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001502
1503 format = FMT_OPTIONS;
1504 if (!verbose)
1505 format |= FMT_NOCOUNTS;
1506 else
1507 format |= FMT_VIA;
1508
1509 if (numeric)
1510 format |= FMT_NUMERIC;
1511
1512 if (!expanded)
1513 format |= FMT_KILOMEGAGIGA;
1514
1515 if (linenumbers)
1516 format |= FMT_LINENUMBERS;
1517
Rusty Russell9e1d2142000-04-23 09:11:12 +00001518 for (this = iptc_first_chain(handle);
1519 this;
1520 this = iptc_next_chain(handle)) {
1521 const struct ipt_entry *i;
1522 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001523
Marc Bouchere6869a82000-03-20 06:03:29 +00001524 if (chain && strcmp(chain, this) != 0)
1525 continue;
1526
1527 if (found) printf("\n");
1528
1529 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001530 i = iptc_first_rule(this, handle);
1531
1532 num = 0;
1533 while (i) {
1534 print_firewall(i,
1535 iptc_get_target(i, handle),
1536 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001537 format,
1538 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001539 i = iptc_next_rule(i, handle);
1540 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001541 found = 1;
1542 }
1543
1544 errno = ENOENT;
1545 return found;
1546}
1547
Harald Welte82dd2ec2000-12-19 05:18:15 +00001548static char *get_modprobe(void)
1549{
1550 int procfile;
1551 char *ret;
1552
1553 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1554 if (procfile < 0)
1555 return NULL;
1556
1557 ret = malloc(1024);
1558 if (ret) {
1559 switch (read(procfile, ret, 1024)) {
1560 case -1: goto fail;
1561 case 1024: goto fail; /* Partial read. Wierd */
1562 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001563 if (ret[strlen(ret)-1]=='\n')
1564 ret[strlen(ret)-1]=0;
1565 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001566 return ret;
1567 }
1568 fail:
1569 free(ret);
1570 close(procfile);
1571 return NULL;
1572}
1573
Harald Welte58918652001-06-16 18:25:25 +00001574int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001575{
1576 char *buf = NULL;
1577 char *argv[3];
1578
1579 /* If they don't explicitly set it, read out of kernel */
1580 if (!modprobe) {
1581 buf = get_modprobe();
1582 if (!buf)
1583 return -1;
1584 modprobe = buf;
1585 }
1586
1587 switch (fork()) {
1588 case 0:
1589 argv[0] = (char *)modprobe;
1590 argv[1] = (char *)modname;
1591 argv[2] = NULL;
1592 execv(argv[0], argv);
1593
1594 /* not usually reached */
1595 exit(0);
1596 case -1:
1597 return -1;
1598
1599 default: /* parent */
1600 wait(NULL);
1601 }
1602
1603 free(buf);
1604 return 0;
1605}
1606
Marc Bouchere6869a82000-03-20 06:03:29 +00001607static struct ipt_entry *
1608generate_entry(const struct ipt_entry *fw,
1609 struct iptables_match *matches,
1610 struct ipt_entry_target *target)
1611{
1612 unsigned int size;
1613 struct iptables_match *m;
1614 struct ipt_entry *e;
1615
1616 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001617 for (m = matches; m; m = m->next) {
1618 if (!m->used)
1619 continue;
1620
Rusty Russell228e98d2000-04-27 10:28:06 +00001621 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001622 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001623
Rusty Russell228e98d2000-04-27 10:28:06 +00001624 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001625 *e = *fw;
1626 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001627 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001628
1629 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001630 for (m = matches; m; m = m->next) {
1631 if (!m->used)
1632 continue;
1633
Rusty Russell228e98d2000-04-27 10:28:06 +00001634 memcpy(e->elems + size, m->m, m->m->u.match_size);
1635 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001636 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001637 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001638
1639 return e;
1640}
1641
1642int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1643{
1644 struct ipt_entry fw, *e = NULL;
1645 int invert = 0;
1646 unsigned int nsaddrs = 0, ndaddrs = 0;
1647 struct in_addr *saddrs = NULL, *daddrs = NULL;
1648
1649 int c, verbose = 0;
1650 const char *chain = NULL;
1651 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1652 const char *policy = NULL, *newname = NULL;
1653 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001654 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001655 int ret = 1;
1656 struct iptables_match *m;
1657 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001658 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 const char *jumpto = "";
1660 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001661 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001662
1663 memset(&fw, 0, sizeof(fw));
1664
Sven Kochfb1279a2001-02-27 12:25:12 +00001665 opts = original_opts;
1666 global_option_offset = 0;
1667
Harald Welteae1ff9f2000-12-01 14:26:20 +00001668 /* re-set optind to 0 in case do_command gets called
1669 * a second time */
1670 optind = 0;
1671
1672 /* clear mflags in case do_command gets called a second time
1673 * (we clear the global list of all matches for security)*/
1674 for (m = iptables_matches; m; m = m->next) {
1675 m->mflags = 0;
1676 m->used = 0;
1677 }
1678
1679 for (t = iptables_targets; t; t = t->next) {
1680 t->tflags = 0;
1681 t->used = 0;
1682 }
1683
Marc Bouchere6869a82000-03-20 06:03:29 +00001684 /* Suppress error messages: we may add new options if we
1685 demand-load a protocol. */
1686 opterr = 0;
1687
1688 while ((c = getopt_long(argc, argv,
Bart De Schuymerbe8ee532002-06-15 12:29:42 +00001689 "-A:C:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001690 opts, NULL)) != -1) {
1691 switch (c) {
1692 /*
1693 * Command selection
1694 */
1695 case 'A':
1696 add_command(&command, CMD_APPEND, CMD_NONE,
1697 invert);
1698 chain = optarg;
1699 break;
1700
1701 case 'D':
1702 add_command(&command, CMD_DELETE, CMD_NONE,
1703 invert);
1704 chain = optarg;
1705 if (optind < argc && argv[optind][0] != '-'
1706 && argv[optind][0] != '!') {
1707 rulenum = parse_rulenumber(argv[optind++]);
1708 command = CMD_DELETE_NUM;
1709 }
1710 break;
1711
Marc Bouchere6869a82000-03-20 06:03:29 +00001712 case 'R':
1713 add_command(&command, CMD_REPLACE, CMD_NONE,
1714 invert);
1715 chain = optarg;
1716 if (optind < argc && argv[optind][0] != '-'
1717 && argv[optind][0] != '!')
1718 rulenum = parse_rulenumber(argv[optind++]);
1719 else
1720 exit_error(PARAMETER_PROBLEM,
1721 "-%c requires a rule number",
1722 cmd2char(CMD_REPLACE));
1723 break;
1724
1725 case 'I':
1726 add_command(&command, CMD_INSERT, CMD_NONE,
1727 invert);
1728 chain = optarg;
1729 if (optind < argc && argv[optind][0] != '-'
1730 && argv[optind][0] != '!')
1731 rulenum = parse_rulenumber(argv[optind++]);
1732 else rulenum = 1;
1733 break;
1734
1735 case 'L':
1736 add_command(&command, CMD_LIST, CMD_ZERO,
1737 invert);
1738 if (optarg) chain = optarg;
1739 else if (optind < argc && argv[optind][0] != '-'
1740 && argv[optind][0] != '!')
1741 chain = argv[optind++];
1742 break;
1743
1744 case 'F':
1745 add_command(&command, CMD_FLUSH, CMD_NONE,
1746 invert);
1747 if (optarg) chain = optarg;
1748 else if (optind < argc && argv[optind][0] != '-'
1749 && argv[optind][0] != '!')
1750 chain = argv[optind++];
1751 break;
1752
1753 case 'Z':
1754 add_command(&command, CMD_ZERO, CMD_LIST,
1755 invert);
1756 if (optarg) chain = optarg;
1757 else if (optind < argc && argv[optind][0] != '-'
1758 && argv[optind][0] != '!')
1759 chain = argv[optind++];
1760 break;
1761
1762 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001763 if (optarg && *optarg == '-')
1764 exit_error(PARAMETER_PROBLEM,
1765 "chain name not allowed to start "
1766 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001767 if (find_target(optarg, TRY_LOAD))
1768 exit_error(PARAMETER_PROBLEM,
1769 "chain name may not clash "
1770 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001771 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1772 invert);
1773 chain = optarg;
1774 break;
1775
1776 case 'X':
1777 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1778 invert);
1779 if (optarg) chain = optarg;
1780 else if (optind < argc && argv[optind][0] != '-'
1781 && argv[optind][0] != '!')
1782 chain = argv[optind++];
1783 break;
1784
1785 case 'E':
1786 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1787 invert);
1788 chain = optarg;
1789 if (optind < argc && argv[optind][0] != '-'
1790 && argv[optind][0] != '!')
1791 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001792 else
1793 exit_error(PARAMETER_PROBLEM,
1794 "-%c requires old-chain-name and "
1795 "new-chain-name",
1796 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001797 break;
1798
1799 case 'P':
1800 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1801 invert);
1802 chain = optarg;
1803 if (optind < argc && argv[optind][0] != '-'
1804 && argv[optind][0] != '!')
1805 policy = argv[optind++];
1806 else
1807 exit_error(PARAMETER_PROBLEM,
1808 "-%c requires a chain and a policy",
1809 cmd2char(CMD_SET_POLICY));
1810 break;
1811
1812 case 'h':
1813 if (!optarg)
1814 optarg = argv[optind];
1815
Rusty Russell2e0a3212000-04-19 11:23:18 +00001816 /* iptables -p icmp -h */
1817 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001818 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001819
Marc Bouchere6869a82000-03-20 06:03:29 +00001820 exit_printhelp();
1821
1822 /*
1823 * Option selection
1824 */
1825 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001826 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001827 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1828 invert);
1829
1830 /* Canonicalize into lower case */
1831 for (protocol = argv[optind-1]; *protocol; protocol++)
1832 *protocol = tolower(*protocol);
1833
1834 protocol = argv[optind-1];
1835 fw.ip.proto = parse_protocol(protocol);
1836
1837 if (fw.ip.proto == 0
1838 && (fw.ip.invflags & IPT_INV_PROTO))
1839 exit_error(PARAMETER_PROBLEM,
1840 "rule would never match protocol");
1841 fw.nfcache |= NFC_IP_PROTO;
Harald Welted4ab5ad2002-08-07 09:07:24 +00001842
1843 /* try to load match with protocol name */
1844 if ((m = find_proto(protocol, TRY_LOAD,
1845 options&OPT_NUMERIC))) {
1846 size_t size;
1847 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1848 + m->size;
1849 m->m = fw_calloc(1, size);
1850 m->m->u.match_size = size;
1851 strcpy(m->m->u.user.name, m->name);
1852 m->init(m->m, &fw.nfcache);
1853 opts = merge_options(opts, m->extra_opts, &m->option_offset);
1854 }
1855
Marc Bouchere6869a82000-03-20 06:03:29 +00001856 break;
1857
1858 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001859 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1861 invert);
1862 shostnetworkmask = argv[optind-1];
1863 fw.nfcache |= NFC_IP_SRC;
1864 break;
1865
1866 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001867 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001868 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1869 invert);
1870 dhostnetworkmask = argv[optind-1];
1871 fw.nfcache |= NFC_IP_DST;
1872 break;
1873
1874 case 'j':
1875 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1876 invert);
1877 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001878 /* TRY_LOAD (may be chain name) */
1879 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001880
1881 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001882 size_t size;
1883
Rusty Russell73f72f52000-07-03 10:17:57 +00001884 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1885 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001886
Rusty Russell2e0a3212000-04-19 11:23:18 +00001887 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001888 target->t->u.target_size = size;
1889 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001890 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001891 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001892 }
1893 break;
1894
1895
1896 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001897 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001898 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1899 invert);
1900 parse_interface(argv[optind-1],
1901 fw.ip.iniface,
1902 fw.ip.iniface_mask);
1903 fw.nfcache |= NFC_IP_IF_IN;
1904 break;
1905
1906 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001907 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001908 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1909 invert);
1910 parse_interface(argv[optind-1],
1911 fw.ip.outiface,
1912 fw.ip.outiface_mask);
1913 fw.nfcache |= NFC_IP_IF_OUT;
1914 break;
1915
1916 case 'f':
1917 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1918 invert);
1919 fw.ip.flags |= IPT_F_FRAG;
1920 fw.nfcache |= NFC_IP_FRAG;
1921 break;
1922
1923 case 'v':
1924 if (!verbose)
1925 set_option(&options, OPT_VERBOSE,
1926 &fw.ip.invflags, invert);
1927 verbose++;
1928 break;
1929
Rusty Russell52a51492000-05-02 16:44:29 +00001930 case 'm': {
1931 size_t size;
1932
Marc Bouchere6869a82000-03-20 06:03:29 +00001933 if (invert)
1934 exit_error(PARAMETER_PROBLEM,
1935 "unexpected ! flag before --match");
1936
Rusty Russell52a51492000-05-02 16:44:29 +00001937 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001938 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1939 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001940 m->m = fw_calloc(1, size);
1941 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001942 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001943 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001944 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001945 }
1946 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001947
1948 case 'n':
1949 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1950 invert);
1951 break;
1952
1953 case 't':
1954 if (invert)
1955 exit_error(PARAMETER_PROBLEM,
1956 "unexpected ! flag before --table");
1957 *table = argv[optind-1];
1958 break;
1959
1960 case 'x':
1961 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1962 invert);
1963 break;
1964
1965 case 'V':
1966 if (invert)
1967 printf("Not %s ;-)\n", program_version);
1968 else
1969 printf("%s v%s\n",
1970 program_name, program_version);
1971 exit(0);
1972
1973 case '0':
1974 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1975 invert);
1976 break;
1977
Harald Welte82dd2ec2000-12-19 05:18:15 +00001978 case 'M':
1979 modprobe = optarg;
1980 break;
1981
Harald Welteccd49e52001-01-23 22:54:34 +00001982 case 'c':
1983
1984 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1985 invert);
1986 pcnt = optarg;
1987 if (optind < argc && argv[optind][0] != '-'
1988 && argv[optind][0] != '!')
1989 bcnt = argv[optind++];
1990 else
1991 exit_error(PARAMETER_PROBLEM,
1992 "-%c requires packet and byte counter",
1993 opt2char(OPT_COUNTERS));
1994
1995 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1996 exit_error(PARAMETER_PROBLEM,
1997 "-%c packet counter not numeric",
1998 opt2char(OPT_COUNTERS));
1999
2000 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2001 exit_error(PARAMETER_PROBLEM,
2002 "-%c byte counter not numeric",
2003 opt2char(OPT_COUNTERS));
2004
2005 break;
2006
2007
Marc Bouchere6869a82000-03-20 06:03:29 +00002008 case 1: /* non option */
2009 if (optarg[0] == '!' && optarg[1] == '\0') {
2010 if (invert)
2011 exit_error(PARAMETER_PROBLEM,
2012 "multiple consecutive ! not"
2013 " allowed");
2014 invert = TRUE;
2015 optarg[0] = '\0';
2016 continue;
2017 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002018 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002019 exit_tryhelp(2);
2020
2021 default:
2022 /* FIXME: This scheme doesn't allow two of the same
2023 matches --RR */
2024 if (!target
2025 || !(target->parse(c - target->option_offset,
2026 argv, invert,
2027 &target->tflags,
2028 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002029 for (m = iptables_matches; m; m = m->next) {
2030 if (!m->used)
2031 continue;
2032
Marc Bouchere6869a82000-03-20 06:03:29 +00002033 if (m->parse(c - m->option_offset,
2034 argv, invert,
2035 &m->mflags,
2036 &fw,
2037 &fw.nfcache,
2038 &m->m))
2039 break;
2040 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 if (!m)
2042 exit_error(PARAMETER_PROBLEM,
2043 "Unknown arg `%s'",
2044 argv[optind-1]);
2045 }
2046 }
2047 invert = FALSE;
2048 }
2049
Sven Kochfb1279a2001-02-27 12:25:12 +00002050 for (m = iptables_matches; m; m = m->next) {
2051 if (!m->used)
2052 continue;
2053
Marc Bouchere6869a82000-03-20 06:03:29 +00002054 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002055 }
2056
Marc Bouchere6869a82000-03-20 06:03:29 +00002057 if (target)
2058 target->final_check(target->tflags);
2059
2060 /* Fix me: must put inverse options checking here --MN */
2061
2062 if (optind < argc)
2063 exit_error(PARAMETER_PROBLEM,
2064 "unknown arguments found on commandline");
2065 if (!command)
2066 exit_error(PARAMETER_PROBLEM, "no command specified");
2067 if (invert)
2068 exit_error(PARAMETER_PROBLEM,
2069 "nothing appropriate following !");
2070
Harald Welte6336bfd2002-05-07 14:41:43 +00002071 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002072 if (!(options & OPT_DESTINATION))
2073 dhostnetworkmask = "0.0.0.0/0";
2074 if (!(options & OPT_SOURCE))
2075 shostnetworkmask = "0.0.0.0/0";
2076 }
2077
2078 if (shostnetworkmask)
2079 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2080 &(fw.ip.smsk), &nsaddrs);
2081
2082 if (dhostnetworkmask)
2083 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2084 &(fw.ip.dmsk), &ndaddrs);
2085
2086 if ((nsaddrs > 1 || ndaddrs > 1) &&
2087 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2088 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2089 " source or destination IP addresses");
2090
Marc Bouchere6869a82000-03-20 06:03:29 +00002091 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2092 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2093 "specify a unique address");
2094
2095 generic_opt_check(command, options);
2096
2097 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2098 exit_error(PARAMETER_PROBLEM,
2099 "chain name `%s' too long (must be under %i chars)",
2100 chain, IPT_FUNCTION_MAXNAMELEN);
2101
Harald Welteae1ff9f2000-12-01 14:26:20 +00002102 /* only allocate handle if we weren't called with a handle */
2103 if (!*handle)
2104 *handle = iptc_init(*table);
2105
Harald Welte82dd2ec2000-12-19 05:18:15 +00002106 if (!*handle) {
2107 /* try to insmod the module if iptc_init failed */
2108 iptables_insmod("ip_tables", modprobe);
2109 *handle = iptc_init(*table);
2110 }
2111
Marc Bouchere6869a82000-03-20 06:03:29 +00002112 if (!*handle)
2113 exit_error(VERSION_PROBLEM,
2114 "can't initialize iptables table `%s': %s",
2115 *table, iptc_strerror(errno));
2116
Harald Welte6336bfd2002-05-07 14:41:43 +00002117 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002118 || command == CMD_DELETE
2119 || command == CMD_INSERT
2120 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002121 if (strcmp(chain, "PREROUTING") == 0
2122 || strcmp(chain, "INPUT") == 0) {
2123 /* -o not valid with incoming packets. */
2124 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002125 exit_error(PARAMETER_PROBLEM,
2126 "Can't use -%c with %s\n",
2127 opt2char(OPT_VIANAMEOUT),
2128 chain);
2129 }
2130
Rusty Russella4860fd2000-06-17 16:13:02 +00002131 if (strcmp(chain, "POSTROUTING") == 0
2132 || strcmp(chain, "OUTPUT") == 0) {
2133 /* -i not valid with outgoing packets */
2134 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002135 exit_error(PARAMETER_PROBLEM,
2136 "Can't use -%c with %s\n",
2137 opt2char(OPT_VIANAMEIN),
2138 chain);
2139 }
2140
2141 if (target && iptc_is_chain(jumpto, *handle)) {
2142 printf("Warning: using chain %s, not extension\n",
2143 jumpto);
2144
2145 target = NULL;
2146 }
2147
2148 /* If they didn't specify a target, or it's a chain
2149 name, use standard. */
2150 if (!target
2151 && (strlen(jumpto) == 0
2152 || iptc_is_chain(jumpto, *handle))) {
2153 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002154
Rusty Russell52a51492000-05-02 16:44:29 +00002155 target = find_target(IPT_STANDARD_TARGET,
2156 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002157
2158 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002159 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002160 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002161 target->t->u.target_size = size;
2162 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002163 target->init(target->t, &fw.nfcache);
2164 }
2165
Rusty Russell7e53bf92000-03-20 07:03:28 +00002166 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002167 /* it is no chain, and we can't load a plugin.
2168 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002169 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002170 * chain. */
2171 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002172 } else {
2173 e = generate_entry(&fw, iptables_matches, target->t);
2174 }
2175 }
2176
2177 switch (command) {
2178 case CMD_APPEND:
2179 ret = append_entry(chain, e,
2180 nsaddrs, saddrs, ndaddrs, daddrs,
2181 options&OPT_VERBOSE,
2182 handle);
2183 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002184 case CMD_DELETE:
2185 ret = delete_entry(chain, e,
2186 nsaddrs, saddrs, ndaddrs, daddrs,
2187 options&OPT_VERBOSE,
2188 handle);
2189 break;
2190 case CMD_DELETE_NUM:
2191 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2192 break;
2193 case CMD_REPLACE:
2194 ret = replace_entry(chain, e, rulenum - 1,
2195 saddrs, daddrs, options&OPT_VERBOSE,
2196 handle);
2197 break;
2198 case CMD_INSERT:
2199 ret = insert_entry(chain, e, rulenum - 1,
2200 nsaddrs, saddrs, ndaddrs, daddrs,
2201 options&OPT_VERBOSE,
2202 handle);
2203 break;
2204 case CMD_LIST:
2205 ret = list_entries(chain,
2206 options&OPT_VERBOSE,
2207 options&OPT_NUMERIC,
2208 options&OPT_EXPANDED,
2209 options&OPT_LINENUMBERS,
2210 handle);
2211 break;
2212 case CMD_FLUSH:
2213 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2214 break;
2215 case CMD_ZERO:
2216 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2217 break;
2218 case CMD_LIST|CMD_ZERO:
2219 ret = list_entries(chain,
2220 options&OPT_VERBOSE,
2221 options&OPT_NUMERIC,
2222 options&OPT_EXPANDED,
2223 options&OPT_LINENUMBERS,
2224 handle);
2225 if (ret)
2226 ret = zero_entries(chain,
2227 options&OPT_VERBOSE, handle);
2228 break;
2229 case CMD_NEW_CHAIN:
2230 ret = iptc_create_chain(chain, handle);
2231 break;
2232 case CMD_DELETE_CHAIN:
2233 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2234 break;
2235 case CMD_RENAME_CHAIN:
2236 ret = iptc_rename_chain(chain, newname, handle);
2237 break;
2238 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002239 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002240 break;
2241 default:
2242 /* We should never reach this... */
2243 exit_tryhelp(2);
2244 }
2245
2246 if (verbose > 1)
2247 dump_entries(*handle);
2248
2249 return ret;
2250}