blob: 3ff05898b7245d9b00829077ba7933dd598bafb0 [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>
Harald Welte82dd2ec2000-12-19 05:18:15 +000031#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000032#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000033#include <fcntl.h>
34#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035
36#ifndef TRUE
37#define TRUE 1
38#endif
39#ifndef FALSE
40#define FALSE 0
41#endif
42
43#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte82dd2ec2000-12-19 05:18:15 +000047#ifndef PROC_SYS_MODPROBE
48#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
49#endif
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051#define FMT_NUMERIC 0x0001
52#define FMT_NOCOUNTS 0x0002
53#define FMT_KILOMEGAGIGA 0x0004
54#define FMT_OPTIONS 0x0008
55#define FMT_NOTABLE 0x0010
56#define FMT_NOTARGET 0x0020
57#define FMT_VIA 0x0040
58#define FMT_NONEWLINE 0x0080
59#define FMT_LINENUMBERS 0x0100
60
61#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
62 | FMT_NUMERIC | FMT_NOTABLE)
63#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
64
65
66#define CMD_NONE 0x0000U
67#define CMD_INSERT 0x0001U
68#define CMD_DELETE 0x0002U
69#define CMD_DELETE_NUM 0x0004U
70#define CMD_REPLACE 0x0008U
71#define CMD_APPEND 0x0010U
72#define CMD_LIST 0x0020U
73#define CMD_FLUSH 0x0040U
74#define CMD_ZERO 0x0080U
75#define CMD_NEW_CHAIN 0x0100U
76#define CMD_DELETE_CHAIN 0x0200U
77#define CMD_SET_POLICY 0x0400U
78#define CMD_CHECK 0x0800U
79#define CMD_RENAME_CHAIN 0x1000U
80#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
82 'N', 'X', 'P', 'C', 'E' };
83
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
104 { "append", 1, 0, 'A' },
105 { "delete", 1, 0, 'D' },
106 { "insert", 1, 0, 'I' },
107 { "replace", 1, 0, 'R' },
108 { "list", 2, 0, 'L' },
109 { "flush", 2, 0, 'F' },
110 { "zero", 2, 0, 'Z' },
111 { "check", 1, 0, 'C' },
112 { "new-chain", 1, 0, 'N' },
113 { "delete-chain", 2, 0, 'X' },
114 { "rename-chain", 2, 0, 'E' },
115 { "policy", 1, 0, 'P' },
116 { "source", 1, 0, 's' },
117 { "destination", 1, 0, 'd' },
118 { "src", 1, 0, 's' }, /* synonym */
119 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000120 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 { "in-interface", 1, 0, 'i' },
122 { "jump", 1, 0, 'j' },
123 { "table", 1, 0, 't' },
124 { "match", 1, 0, 'm' },
125 { "numeric", 0, 0, 'n' },
126 { "out-interface", 1, 0, 'o' },
127 { "verbose", 0, 0, 'v' },
128 { "exact", 0, 0, 'x' },
129 { "fragments", 0, 0, 'f' },
130 { "version", 0, 0, 'V' },
131 { "help", 2, 0, 'h' },
132 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000133 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000134 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 { 0 }
136};
137
Rusty Russell4e242f82000-05-31 06:33:50 +0000138#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000139struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000140ipt_get_target(struct ipt_entry *e)
141{
142 return (void *)e + e->target_offset;
143}
144#endif
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
Rusty Russell2e0a3212000-04-19 11:23:18 +0000195/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000196struct iptables_match *iptables_matches = NULL;
197struct iptables_target *iptables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries(const iptc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000209/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000210/* defined in netinet/in.h */
211#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
223 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "all", 0 },
227};
228
229static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000230proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int i;
233
Rusty Russell28381a42000-05-10 00:19:50 +0000234 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247struct in_addr *
248dotted_to_addr(const char *dotted)
249{
250 static struct in_addr addr;
251 unsigned char *addrp;
252 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000253 unsigned int onebyte;
254 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000255 char buf[20];
256
257 /* copy dotted string, because we need to modify it */
258 strncpy(buf, dotted, sizeof(buf) - 1);
259 addrp = (unsigned char *) &(addr.s_addr);
260
261 p = buf;
262 for (i = 0; i < 3; i++) {
263 if ((q = strchr(p, '.')) == NULL)
264 return (struct in_addr *) NULL;
265
266 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000267 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000268 return (struct in_addr *) NULL;
269
270 addrp[i] = (unsigned char) onebyte;
271 p = q + 1;
272 }
273
274 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000275 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 return (struct in_addr *) NULL;
277
278 addrp[3] = (unsigned char) onebyte;
279
280 return &addr;
281}
282
283static struct in_addr *
284network_to_addr(const char *name)
285{
286 struct netent *net;
287 static struct in_addr addr;
288
289 if ((net = getnetbyname(name)) != NULL) {
290 if (net->n_addrtype != AF_INET)
291 return (struct in_addr *) NULL;
292 addr.s_addr = htonl((unsigned long) net->n_net);
293 return &addr;
294 }
295
296 return (struct in_addr *) NULL;
297}
298
299static void
300inaddrcpy(struct in_addr *dst, struct in_addr *src)
301{
302 /* memcpy(dst, src, sizeof(struct in_addr)); */
303 dst->s_addr = src->s_addr;
304}
305
306void
307exit_error(enum exittype status, char *msg, ...)
308{
309 va_list args;
310
311 va_start(args, msg);
312 fprintf(stderr, "%s v%s: ", program_name, program_version);
313 vfprintf(stderr, msg, args);
314 va_end(args);
315 fprintf(stderr, "\n");
316 if (status == PARAMETER_PROBLEM)
317 exit_tryhelp(status);
318 if (status == VERSION_PROBLEM)
319 fprintf(stderr,
320 "Perhaps iptables or your kernel needs to be upgraded.\n");
321 exit(status);
322}
323
324void
325exit_tryhelp(int status)
326{
327 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
328 program_name, program_name );
329 exit(status);
330}
331
332void
333exit_printhelp(void)
334{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000335 struct iptables_match *m = NULL;
336 struct iptables_target *t = NULL;
337
Marc Bouchere6869a82000-03-20 06:03:29 +0000338 printf("%s v%s\n\n"
339"Usage: %s -[ADC] chain rule-specification [options]\n"
340" %s -[RI] chain rulenum rule-specification [options]\n"
341" %s -D chain rulenum [options]\n"
342" %s -[LFZ] [chain] [options]\n"
343" %s -[NX] chain\n"
344" %s -E old-chain-name new-chain-name\n"
345" %s -P chain target [options]\n"
346" %s -h (print this help information)\n\n",
347 program_name, program_version, program_name, program_name,
348 program_name, program_name, program_name, program_name,
349 program_name, program_name);
350
351 printf(
352"Commands:\n"
353"Either long or short options are allowed.\n"
354" --append -A chain Append to chain\n"
355" --delete -D chain Delete matching rule from chain\n"
356" --delete -D chain rulenum\n"
357" Delete rule rulenum (1 = first) from chain\n"
358" --insert -I chain [rulenum]\n"
359" Insert in chain as rulenum (default 1=first)\n"
360" --replace -R chain rulenum\n"
361" Replace rule rulenum (1 = first) in chain\n"
362" --list -L [chain] List the rules in a chain or all chains\n"
363" --flush -F [chain] Delete all rules in chain or all chains\n"
364" --zero -Z [chain] Zero counters in chain or all chains\n"
365" --check -C chain Test this packet on chain\n"
366" --new -N chain Create a new user-defined chain\n"
367" --delete-chain\n"
368" -X [chain] Delete a user-defined chain\n"
369" --policy -P chain target\n"
370" Change policy on chain to target\n"
371" --rename-chain\n"
372" -E old-chain new-chain\n"
373" Change chain name, (moving any references)\n"
374
375"Options:\n"
376" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
377" --source -s [!] address[/mask]\n"
378" source specification\n"
379" --destination -d [!] address[/mask]\n"
380" destination specification\n"
381" --in-interface -i [!] input name[+]\n"
382" network interface name ([+] for wildcard)\n"
383" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000384" target for rule (may load target extension)\n"
385" --match -m match\n"
386" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000387" --numeric -n numeric output of addresses and ports\n"
388" --out-interface -o [!] output name[+]\n"
389" network interface name ([+] for wildcard)\n"
390" --table -t table table to manipulate (default: `filter')\n"
391" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000392" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000393" --exact -x expand numbers (display exact values)\n"
394"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000395" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000396" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000397"[!] --version -V print package version.\n");
398
Rusty Russell363112d2000-08-11 13:49:26 +0000399 /* Print out any special helps. A user might like to be able
400 to add a --help to the commandline, and see expected
401 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000402 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000404 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000406 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000408 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000409 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000410 exit(0);
411}
412
413static void
414generic_opt_check(int command, int options)
415{
416 int i, j, legal = 0;
417
418 /* Check that commands are valid with options. Complicated by the
419 * fact that if an option is legal with *any* command given, it is
420 * legal overall (ie. -z and -l).
421 */
422 for (i = 0; i < NUMBER_OF_OPT; i++) {
423 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
424
425 for (j = 0; j < NUMBER_OF_CMD; j++) {
426 if (!(command & (1<<j)))
427 continue;
428
429 if (!(options & (1<<i))) {
430 if (commands_v_options[j][i] == '+')
431 exit_error(PARAMETER_PROBLEM,
432 "You need to supply the `-%c' "
433 "option for this command\n",
434 optflags[i]);
435 } else {
436 if (commands_v_options[j][i] != 'x')
437 legal = 1;
438 else if (legal == 0)
439 legal = -1;
440 }
441 }
442 if (legal == -1)
443 exit_error(PARAMETER_PROBLEM,
444 "Illegal option `-%c' with this command\n",
445 optflags[i]);
446 }
447}
448
449static char
450opt2char(int option)
451{
452 const char *ptr;
453 for (ptr = optflags; option > 1; option >>= 1, ptr++);
454
455 return *ptr;
456}
457
458static char
459cmd2char(int option)
460{
461 const char *ptr;
462 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
463
464 return *ptr;
465}
466
467static void
468add_command(int *cmd, const int newcmd, const int othercmds, int invert)
469{
470 if (invert)
471 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
472 if (*cmd & (~othercmds))
473 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
474 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
475 *cmd |= newcmd;
476}
477
478int
479check_inverse(const char option[], int *invert)
480{
481 if (option && strcmp(option, "!") == 0) {
482 if (*invert)
483 exit_error(PARAMETER_PROBLEM,
484 "Multiple `!' flags not allowed");
485
486 *invert = TRUE;
487 return TRUE;
488 }
489 return FALSE;
490}
491
492static void *
493fw_calloc(size_t count, size_t size)
494{
495 void *p;
496
497 if ((p = calloc(count, size)) == NULL) {
498 perror("iptables: calloc failed");
499 exit(1);
500 }
501 return p;
502}
503
504static void *
505fw_malloc(size_t size)
506{
507 void *p;
508
509 if ((p = malloc(size)) == NULL) {
510 perror("iptables: malloc failed");
511 exit(1);
512 }
513 return p;
514}
515
516static struct in_addr *
517host_to_addr(const char *name, unsigned int *naddr)
518{
519 struct hostent *host;
520 struct in_addr *addr;
521 unsigned int i;
522
523 *naddr = 0;
524 if ((host = gethostbyname(name)) != NULL) {
525 if (host->h_addrtype != AF_INET ||
526 host->h_length != sizeof(struct in_addr))
527 return (struct in_addr *) NULL;
528
529 while (host->h_addr_list[*naddr] != (char *) NULL)
530 (*naddr)++;
531 addr = fw_calloc(*naddr, sizeof(struct in_addr));
532 for (i = 0; i < *naddr; i++)
533 inaddrcpy(&(addr[i]),
534 (struct in_addr *) host->h_addr_list[i]);
535 return addr;
536 }
537
538 return (struct in_addr *) NULL;
539}
540
541static char *
542addr_to_host(const struct in_addr *addr)
543{
544 struct hostent *host;
545
546 if ((host = gethostbyaddr((char *) addr,
547 sizeof(struct in_addr), AF_INET)) != NULL)
548 return (char *) host->h_name;
549
550 return (char *) NULL;
551}
552
553/*
554 * All functions starting with "parse" should succeed, otherwise
555 * the program fails.
556 * Most routines return pointers to static data that may change
557 * between calls to the same or other routines with a few exceptions:
558 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
559 * return global static data.
560*/
561
562static struct in_addr *
563parse_hostnetwork(const char *name, unsigned int *naddrs)
564{
565 struct in_addr *addrp, *addrptmp;
566
567 if ((addrptmp = dotted_to_addr(name)) != NULL ||
568 (addrptmp = network_to_addr(name)) != NULL) {
569 addrp = fw_malloc(sizeof(struct in_addr));
570 inaddrcpy(addrp, addrptmp);
571 *naddrs = 1;
572 return addrp;
573 }
574 if ((addrp = host_to_addr(name, naddrs)) != NULL)
575 return addrp;
576
577 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
578}
579
580static struct in_addr *
581parse_mask(char *mask)
582{
583 static struct in_addr maskaddr;
584 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000585 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000586
587 if (mask == NULL) {
588 /* no mask at all defaults to 32 bits */
589 maskaddr.s_addr = 0xFFFFFFFF;
590 return &maskaddr;
591 }
592 if ((addrp = dotted_to_addr(mask)) != NULL)
593 /* dotted_to_addr already returns a network byte order addr */
594 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000595 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000596 exit_error(PARAMETER_PROBLEM,
597 "invalid mask `%s' specified", mask);
598 if (bits != 0) {
599 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
600 return &maskaddr;
601 }
602
603 maskaddr.s_addr = 0L;
604 return &maskaddr;
605}
606
Marc Boucherb93c7982001-12-06 14:50:19 +0000607void
Marc Bouchere6869a82000-03-20 06:03:29 +0000608parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
609 struct in_addr *maskp, unsigned int *naddrs)
610{
611 struct in_addr *addrp;
612 char buf[256];
613 char *p;
614 int i, j, k, n;
615
616 strncpy(buf, name, sizeof(buf) - 1);
617 if ((p = strrchr(buf, '/')) != NULL) {
618 *p = '\0';
619 addrp = parse_mask(p + 1);
620 } else
621 addrp = parse_mask(NULL);
622 inaddrcpy(maskp, addrp);
623
624 /* if a null mask is given, the name is ignored, like in "any/0" */
625 if (maskp->s_addr == 0L)
626 strcpy(buf, "0.0.0.0");
627
628 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
629 n = *naddrs;
630 for (i = 0, j = 0; i < n; i++) {
631 addrp[j++].s_addr &= maskp->s_addr;
632 for (k = 0; k < j - 1; k++) {
633 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
634 (*naddrs)--;
635 j--;
636 break;
637 }
638 }
639 }
640}
641
642struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000643find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000644{
645 struct iptables_match *ptr;
646
647 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
648 if (strcmp(name, ptr->name) == 0)
649 break;
650 }
651
Harald Welte3efb6ea2001-08-06 18:50:21 +0000652#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000653 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000654 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
655 + strlen(name)];
656 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000657 if (dlopen(path, RTLD_NOW)) {
658 /* Found library. If it didn't register itself,
659 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000660 ptr = find_match(name, DONT_LOAD);
661
Rusty Russell9e1d2142000-04-23 09:11:12 +0000662 if (!ptr)
663 exit_error(PARAMETER_PROBLEM,
664 "Couldn't load match `%s'\n",
665 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000666 } else if (tryload == LOAD_MUST_SUCCEED)
667 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000668 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000669 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000670 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000671#else
672 if (ptr && !ptr->loaded) {
673 if (tryload != DONT_LOAD)
674 ptr->loaded = 1;
675 else
676 ptr = NULL;
677 }
678#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000679
Harald Welteae1ff9f2000-12-01 14:26:20 +0000680 if (ptr)
681 ptr->used = 1;
682
Marc Bouchere6869a82000-03-20 06:03:29 +0000683 return ptr;
684}
685
Rusty Russell28381a42000-05-10 00:19:50 +0000686/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
687static struct iptables_match *
688find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
689{
Harald Welteed498492001-07-23 01:24:22 +0000690 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000691
Harald Welteed498492001-07-23 01:24:22 +0000692 if (string_to_number(pname, 0, 255, &proto) != -1)
Rusty Russell28381a42000-05-10 00:19:50 +0000693 return find_match(proto_to_name(proto, nolookup), tryload);
694
695 return find_match(pname, tryload);
696}
697
Marc Boucherb93c7982001-12-06 14:50:19 +0000698u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000699parse_protocol(const char *s)
700{
Harald Welteed498492001-07-23 01:24:22 +0000701 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000702
Harald Welteed498492001-07-23 01:24:22 +0000703 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 struct protoent *pent;
705
706 if ((pent = getprotobyname(s)))
707 proto = pent->p_proto;
708 else {
709 unsigned int i;
710 for (i = 0;
711 i < sizeof(chain_protos)/sizeof(struct pprot);
712 i++) {
713 if (strcmp(s, chain_protos[i].name) == 0) {
714 proto = chain_protos[i].num;
715 break;
716 }
717 }
718 if (i == sizeof(chain_protos)/sizeof(struct pprot))
719 exit_error(PARAMETER_PROBLEM,
720 "unknown protocol `%s' specified",
721 s);
722 }
723 }
724
725 return (u_int16_t)proto;
726}
727
728static void
729parse_interface(const char *arg, char *vianame, unsigned char *mask)
730{
731 int vialen = strlen(arg);
732 unsigned int i;
733
734 memset(mask, 0, IFNAMSIZ);
735 memset(vianame, 0, IFNAMSIZ);
736
737 if (vialen + 1 > IFNAMSIZ)
738 exit_error(PARAMETER_PROBLEM,
739 "interface name `%s' must be shorter than IFNAMSIZ"
740 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000741
Marc Bouchere6869a82000-03-20 06:03:29 +0000742 strcpy(vianame, arg);
743 if (vialen == 0)
744 memset(mask, 0, IFNAMSIZ);
745 else if (vianame[vialen - 1] == '+') {
746 memset(mask, 0xFF, vialen - 1);
747 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000748 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 } else {
750 /* Include nul-terminator in match */
751 memset(mask, 0xFF, vialen + 1);
752 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000753 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000754 if (!isalnum(vianame[i])
755 && vianame[i] != '_'
756 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000757 printf("Warning: wierd character in interface"
758 " `%s' (No aliases, :, ! or *).\n",
759 vianame);
760 break;
761 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000762 }
763 }
764}
765
766/* Can't be zero. */
767static int
768parse_rulenumber(const char *rule)
769{
Harald Welteed498492001-07-23 01:24:22 +0000770 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000771
Harald Welteed498492001-07-23 01:24:22 +0000772 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000773 exit_error(PARAMETER_PROBLEM,
774 "Invalid rule number `%s'", rule);
775
776 return rulenum;
777}
778
779static const char *
780parse_target(const char *targetname)
781{
782 const char *ptr;
783
784 if (strlen(targetname) < 1)
785 exit_error(PARAMETER_PROBLEM,
786 "Invalid target name (too short)");
787
788 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
789 exit_error(PARAMETER_PROBLEM,
790 "Invalid target name `%s' (%i chars max)",
791 targetname, sizeof(ipt_chainlabel)-1);
792
793 for (ptr = targetname; *ptr; ptr++)
794 if (isspace(*ptr))
795 exit_error(PARAMETER_PROBLEM,
796 "Invalid target name `%s'", targetname);
797 return targetname;
798}
799
800static char *
801addr_to_network(const struct in_addr *addr)
802{
803 struct netent *net;
804
805 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
806 return (char *) net->n_name;
807
808 return (char *) NULL;
809}
810
811char *
812addr_to_dotted(const struct in_addr *addrp)
813{
814 static char buf[20];
815 const unsigned char *bytep;
816
817 bytep = (const unsigned char *) &(addrp->s_addr);
818 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
819 return buf;
820}
Marc Boucherb93c7982001-12-06 14:50:19 +0000821
822char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000823addr_to_anyname(const struct in_addr *addr)
824{
825 char *name;
826
827 if ((name = addr_to_host(addr)) != NULL ||
828 (name = addr_to_network(addr)) != NULL)
829 return name;
830
831 return addr_to_dotted(addr);
832}
833
Marc Boucherb93c7982001-12-06 14:50:19 +0000834char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000835mask_to_dotted(const struct in_addr *mask)
836{
837 int i;
838 static char buf[20];
839 u_int32_t maskaddr, bits;
840
841 maskaddr = ntohl(mask->s_addr);
842
843 if (maskaddr == 0xFFFFFFFFL)
844 /* we don't want to see "/32" */
845 return "";
846
847 i = 32;
848 bits = 0xFFFFFFFEL;
849 while (--i >= 0 && maskaddr != bits)
850 bits <<= 1;
851 if (i >= 0)
852 sprintf(buf, "/%d", i);
853 else
854 /* mask was not a decent combination of 1's and 0's */
855 sprintf(buf, "/%s", addr_to_dotted(mask));
856
857 return buf;
858}
859
860int
Harald Welteed498492001-07-23 01:24:22 +0000861string_to_number(const char *s, unsigned int min, unsigned int max,
862 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000863{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000864 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000865 char *end;
866
867 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000868 errno = 0;
869 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000870 if (*end == '\0' && end != s) {
871 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000872 if (errno != ERANGE && min <= number && number <= max) {
873 *ret = number;
874 return 0;
875 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000876 }
877 return -1;
878}
879
880static void
881set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
882 int invert)
883{
884 if (*options & option)
885 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
886 opt2char(option));
887 *options |= option;
888
889 if (invert) {
890 unsigned int i;
891 for (i = 0; 1 << i != option; i++);
892
893 if (!inverse_for_options[i])
894 exit_error(PARAMETER_PROBLEM,
895 "cannot have ! before -%c",
896 opt2char(option));
897 *invflg |= inverse_for_options[i];
898 }
899}
900
901struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000902find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000903{
904 struct iptables_target *ptr;
905
906 /* Standard target? */
907 if (strcmp(name, "") == 0
908 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
909 || strcmp(name, IPTC_LABEL_DROP) == 0
910 || strcmp(name, IPTC_LABEL_QUEUE) == 0
911 || strcmp(name, IPTC_LABEL_RETURN) == 0)
912 name = "standard";
913
914 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
915 if (strcmp(name, ptr->name) == 0)
916 break;
917 }
918
Harald Welte3efb6ea2001-08-06 18:50:21 +0000919#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000920 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000921 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
922 + strlen(name)];
923 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000924 if (dlopen(path, RTLD_NOW)) {
925 /* Found library. If it didn't register itself,
926 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000927 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000928 if (!ptr)
929 exit_error(PARAMETER_PROBLEM,
930 "Couldn't load target `%s'\n",
931 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000932 } else if (tryload == LOAD_MUST_SUCCEED)
933 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000934 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000935 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000936 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000937#else
938 if (ptr && !ptr->loaded) {
939 if (tryload != DONT_LOAD)
940 ptr->loaded = 1;
941 else
942 ptr = NULL;
943 }
944#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000945
Harald Welteae1ff9f2000-12-01 14:26:20 +0000946 if (ptr)
947 ptr->used = 1;
948
Marc Bouchere6869a82000-03-20 06:03:29 +0000949 return ptr;
950}
951
952static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000953merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000954 unsigned int *option_offset)
955{
956 unsigned int num_old, num_new, i;
957 struct option *merge;
958
959 for (num_old = 0; oldopts[num_old].name; num_old++);
960 for (num_new = 0; newopts[num_new].name; num_new++);
961
962 global_option_offset += OPTION_OFFSET;
963 *option_offset = global_option_offset;
964
965 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
966 memcpy(merge, oldopts, num_old * sizeof(struct option));
967 for (i = 0; i < num_new; i++) {
968 merge[num_old + i] = newopts[i];
969 merge[num_old + i].val += *option_offset;
970 }
971 memset(merge + num_old + num_new, 0, sizeof(struct option));
972
973 return merge;
974}
975
976void
977register_match(struct iptables_match *me)
978{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000979 struct iptables_match **i;
980
Marc Bouchere6869a82000-03-20 06:03:29 +0000981 if (strcmp(me->version, program_version) != 0) {
982 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
983 program_name, me->name, me->version, program_version);
984 exit(1);
985 }
986
Rusty Russell52a51492000-05-02 16:44:29 +0000987 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000988 fprintf(stderr, "%s: match `%s' already registered.\n",
989 program_name, me->name);
990 exit(1);
991 }
992
Rusty Russell73f72f52000-07-03 10:17:57 +0000993 if (me->size != IPT_ALIGN(me->size)) {
994 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
995 program_name, me->name, me->size);
996 exit(1);
997 }
998
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000999 /* Append to list. */
1000 for (i = &iptables_matches; *i; i = &(*i)->next);
1001 me->next = NULL;
1002 *i = me;
1003
Marc Bouchere6869a82000-03-20 06:03:29 +00001004 me->m = NULL;
1005 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001006}
1007
1008void
1009register_target(struct iptables_target *me)
1010{
1011 if (strcmp(me->version, program_version) != 0) {
1012 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1013 program_name, me->name, me->version, program_version);
1014 exit(1);
1015 }
1016
Rusty Russell52a51492000-05-02 16:44:29 +00001017 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001018 fprintf(stderr, "%s: target `%s' already registered.\n",
1019 program_name, me->name);
1020 exit(1);
1021 }
1022
Rusty Russell73f72f52000-07-03 10:17:57 +00001023 if (me->size != IPT_ALIGN(me->size)) {
1024 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1025 program_name, me->name, me->size);
1026 exit(1);
1027 }
1028
Marc Bouchere6869a82000-03-20 06:03:29 +00001029 /* Prepend to list. */
1030 me->next = iptables_targets;
1031 iptables_targets = me;
1032 me->t = NULL;
1033 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001034}
1035
1036static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001037print_num(u_int64_t number, unsigned int format)
1038{
1039 if (format & FMT_KILOMEGAGIGA) {
1040 if (number > 99999) {
1041 number = (number + 500) / 1000;
1042 if (number > 9999) {
1043 number = (number + 500) / 1000;
1044 if (number > 9999) {
1045 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001046 if (number > 9999) {
1047 number = (number + 500) / 1000;
1048 printf(FMT("%4lluT ","%lluT "), number);
1049 }
1050 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001051 }
1052 else printf(FMT("%4lluM ","%lluM "), number);
1053 } else
1054 printf(FMT("%4lluK ","%lluK "), number);
1055 } else
1056 printf(FMT("%5llu ","%llu "), number);
1057 } else
1058 printf(FMT("%8llu ","%llu "), number);
1059}
1060
1061
1062static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001063print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1064{
1065 struct ipt_counters counters;
1066 const char *pol = iptc_get_policy(chain, &counters, handle);
1067 printf("Chain %s", chain);
1068 if (pol) {
1069 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001070 if (!(format & FMT_NOCOUNTS)) {
1071 fputc(' ', stdout);
1072 print_num(counters.pcnt, (format|FMT_NOTABLE));
1073 fputs("packets, ", stdout);
1074 print_num(counters.bcnt, (format|FMT_NOTABLE));
1075 fputs("bytes", stdout);
1076 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001077 printf(")\n");
1078 } else {
1079 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001080 if (!iptc_get_references(&refs, chain, handle))
1081 printf(" (ERROR obtaining refs)\n");
1082 else
1083 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001084 }
1085
1086 if (format & FMT_LINENUMBERS)
1087 printf(FMT("%-4s ", "%s "), "num");
1088 if (!(format & FMT_NOCOUNTS)) {
1089 if (format & FMT_KILOMEGAGIGA) {
1090 printf(FMT("%5s ","%s "), "pkts");
1091 printf(FMT("%5s ","%s "), "bytes");
1092 } else {
1093 printf(FMT("%8s ","%s "), "pkts");
1094 printf(FMT("%10s ","%s "), "bytes");
1095 }
1096 }
1097 if (!(format & FMT_NOTARGET))
1098 printf(FMT("%-9s ","%s "), "target");
1099 fputs(" prot ", stdout);
1100 if (format & FMT_OPTIONS)
1101 fputs("opt", stdout);
1102 if (format & FMT_VIA) {
1103 printf(FMT(" %-6s ","%s "), "in");
1104 printf(FMT("%-6s ","%s "), "out");
1105 }
1106 printf(FMT(" %-19s ","%s "), "source");
1107 printf(FMT(" %-19s "," %s "), "destination");
1108 printf("\n");
1109}
1110
Marc Bouchere6869a82000-03-20 06:03:29 +00001111
1112static int
1113print_match(const struct ipt_entry_match *m,
1114 const struct ipt_ip *ip,
1115 int numeric)
1116{
Rusty Russell52a51492000-05-02 16:44:29 +00001117 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001118
1119 if (match) {
1120 if (match->print)
1121 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001122 else
Rusty Russellb039b022000-09-01 06:04:05 +00001123 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001124 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001125 if (m->u.user.name[0])
1126 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001127 }
1128 /* Don't stop iterating. */
1129 return 0;
1130}
1131
1132/* e is called `fw' here for hysterical raisins */
1133static void
1134print_firewall(const struct ipt_entry *fw,
1135 const char *targname,
1136 unsigned int num,
1137 unsigned int format,
1138 const iptc_handle_t handle)
1139{
1140 struct iptables_target *target = NULL;
1141 const struct ipt_entry_target *t;
1142 u_int8_t flags;
1143 char buf[BUFSIZ];
1144
1145 /* User creates a chain called "REJECT": this overrides the
1146 `REJECT' target module. Keep feeding them rope until the
1147 revolution... Bwahahahahah */
1148 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001149 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001150 else
Rusty Russell52a51492000-05-02 16:44:29 +00001151 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001152
1153 t = ipt_get_target((struct ipt_entry *)fw);
1154 flags = fw->ip.flags;
1155
1156 if (format & FMT_LINENUMBERS)
1157 printf(FMT("%-4u ", "%u "), num+1);
1158
1159 if (!(format & FMT_NOCOUNTS)) {
1160 print_num(fw->counters.pcnt, format);
1161 print_num(fw->counters.bcnt, format);
1162 }
1163
1164 if (!(format & FMT_NOTARGET))
1165 printf(FMT("%-9s ", "%s "), targname);
1166
1167 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1168 {
Rusty Russell28381a42000-05-10 00:19:50 +00001169 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001170 if (pname)
1171 printf(FMT("%-5s", "%s "), pname);
1172 else
1173 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1174 }
1175
1176 if (format & FMT_OPTIONS) {
1177 if (format & FMT_NOTABLE)
1178 fputs("opt ", stdout);
1179 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1180 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1181 fputc(' ', stdout);
1182 }
1183
1184 if (format & FMT_VIA) {
1185 char iface[IFNAMSIZ+2];
1186
1187 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1188 iface[0] = '!';
1189 iface[1] = '\0';
1190 }
1191 else iface[0] = '\0';
1192
1193 if (fw->ip.iniface[0] != '\0') {
1194 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001195 }
1196 else if (format & FMT_NUMERIC) strcat(iface, "*");
1197 else strcat(iface, "any");
1198 printf(FMT(" %-6s ","in %s "), iface);
1199
1200 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1201 iface[0] = '!';
1202 iface[1] = '\0';
1203 }
1204 else iface[0] = '\0';
1205
1206 if (fw->ip.outiface[0] != '\0') {
1207 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001208 }
1209 else if (format & FMT_NUMERIC) strcat(iface, "*");
1210 else strcat(iface, "any");
1211 printf(FMT("%-6s ","out %s "), iface);
1212 }
1213
1214 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1215 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1216 printf(FMT("%-19s ","%s "), "anywhere");
1217 else {
1218 if (format & FMT_NUMERIC)
1219 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1220 else
1221 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1222 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1223 printf(FMT("%-19s ","%s "), buf);
1224 }
1225
1226 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1227 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1228 printf(FMT("%-19s","-> %s"), "anywhere");
1229 else {
1230 if (format & FMT_NUMERIC)
1231 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1232 else
1233 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1234 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1235 printf(FMT("%-19s","-> %s"), buf);
1236 }
1237
1238 if (format & FMT_NOTABLE)
1239 fputs(" ", stdout);
1240
1241 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1242
1243 if (target) {
1244 if (target->print)
1245 /* Print the target information. */
1246 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001247 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001249 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001250
1251 if (!(format & FMT_NONEWLINE))
1252 fputc('\n', stdout);
1253}
1254
1255static void
1256print_firewall_line(const struct ipt_entry *fw,
1257 const iptc_handle_t h)
1258{
1259 struct ipt_entry_target *t;
1260
1261 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001262 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001263}
1264
1265static int
1266append_entry(const ipt_chainlabel chain,
1267 struct ipt_entry *fw,
1268 unsigned int nsaddrs,
1269 const struct in_addr saddrs[],
1270 unsigned int ndaddrs,
1271 const struct in_addr daddrs[],
1272 int verbose,
1273 iptc_handle_t *handle)
1274{
1275 unsigned int i, j;
1276 int ret = 1;
1277
1278 for (i = 0; i < nsaddrs; i++) {
1279 fw->ip.src.s_addr = saddrs[i].s_addr;
1280 for (j = 0; j < ndaddrs; j++) {
1281 fw->ip.dst.s_addr = daddrs[j].s_addr;
1282 if (verbose)
1283 print_firewall_line(fw, *handle);
1284 ret &= iptc_append_entry(chain, fw, handle);
1285 }
1286 }
1287
1288 return ret;
1289}
1290
1291static int
1292replace_entry(const ipt_chainlabel chain,
1293 struct ipt_entry *fw,
1294 unsigned int rulenum,
1295 const struct in_addr *saddr,
1296 const struct in_addr *daddr,
1297 int verbose,
1298 iptc_handle_t *handle)
1299{
1300 fw->ip.src.s_addr = saddr->s_addr;
1301 fw->ip.dst.s_addr = daddr->s_addr;
1302
1303 if (verbose)
1304 print_firewall_line(fw, *handle);
1305 return iptc_replace_entry(chain, fw, rulenum, handle);
1306}
1307
1308static int
1309insert_entry(const ipt_chainlabel chain,
1310 struct ipt_entry *fw,
1311 unsigned int rulenum,
1312 unsigned int nsaddrs,
1313 const struct in_addr saddrs[],
1314 unsigned int ndaddrs,
1315 const struct in_addr daddrs[],
1316 int verbose,
1317 iptc_handle_t *handle)
1318{
1319 unsigned int i, j;
1320 int ret = 1;
1321
1322 for (i = 0; i < nsaddrs; i++) {
1323 fw->ip.src.s_addr = saddrs[i].s_addr;
1324 for (j = 0; j < ndaddrs; j++) {
1325 fw->ip.dst.s_addr = daddrs[j].s_addr;
1326 if (verbose)
1327 print_firewall_line(fw, *handle);
1328 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1329 }
1330 }
1331
1332 return ret;
1333}
1334
Rusty Russell2e0a3212000-04-19 11:23:18 +00001335static unsigned char *
1336make_delete_mask(struct ipt_entry *fw)
1337{
1338 /* Establish mask for comparison */
1339 unsigned int size;
1340 struct iptables_match *m;
1341 unsigned char *mask, *mptr;
1342
1343 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001344 for (m = iptables_matches; m; m = m->next) {
1345 if (!m->used)
1346 continue;
1347
Rusty Russell73f72f52000-07-03 10:17:57 +00001348 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001349 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001350
Rusty Russell9e1d2142000-04-23 09:11:12 +00001351 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001352 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001353 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001354
Rusty Russell9e1d2142000-04-23 09:11:12 +00001355 memset(mask, 0xFF, sizeof(struct ipt_entry));
1356 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001357
Sven Kochfb1279a2001-02-27 12:25:12 +00001358 for (m = iptables_matches; m; m = m->next) {
1359 if (!m->used)
1360 continue;
1361
Rusty Russell2e0a3212000-04-19 11:23:18 +00001362 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001363 IPT_ALIGN(sizeof(struct ipt_entry_match))
1364 + m->userspacesize);
1365 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001366 }
1367
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001368 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001369 IPT_ALIGN(sizeof(struct ipt_entry_target))
1370 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001371
1372 return mask;
1373}
1374
Marc Bouchere6869a82000-03-20 06:03:29 +00001375static int
1376delete_entry(const ipt_chainlabel chain,
1377 struct ipt_entry *fw,
1378 unsigned int nsaddrs,
1379 const struct in_addr saddrs[],
1380 unsigned int ndaddrs,
1381 const struct in_addr daddrs[],
1382 int verbose,
1383 iptc_handle_t *handle)
1384{
1385 unsigned int i, j;
1386 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001387 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001388
Rusty Russell2e0a3212000-04-19 11:23:18 +00001389 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001390 for (i = 0; i < nsaddrs; i++) {
1391 fw->ip.src.s_addr = saddrs[i].s_addr;
1392 for (j = 0; j < ndaddrs; j++) {
1393 fw->ip.dst.s_addr = daddrs[j].s_addr;
1394 if (verbose)
1395 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001396 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001397 }
1398 }
1399 return ret;
1400}
1401
1402static int
1403check_packet(const ipt_chainlabel chain,
1404 struct ipt_entry *fw,
1405 unsigned int nsaddrs,
1406 const struct in_addr saddrs[],
1407 unsigned int ndaddrs,
1408 const struct in_addr daddrs[],
1409 int verbose,
1410 iptc_handle_t *handle)
1411{
1412 int ret = 1;
1413 unsigned int i, j;
1414 const char *msg;
1415
1416 for (i = 0; i < nsaddrs; i++) {
1417 fw->ip.src.s_addr = saddrs[i].s_addr;
1418 for (j = 0; j < ndaddrs; j++) {
1419 fw->ip.dst.s_addr = daddrs[j].s_addr;
1420 if (verbose)
1421 print_firewall_line(fw, *handle);
1422 msg = iptc_check_packet(chain, fw, handle);
1423 if (!msg) ret = 0;
1424 else printf("%s\n", msg);
1425 }
1426 }
1427
1428 return ret;
1429}
1430
Harald Welteae1ff9f2000-12-01 14:26:20 +00001431int
Marc Bouchere6869a82000-03-20 06:03:29 +00001432for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001433 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001434{
1435 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001436 const char *chain;
1437 char *chains;
1438 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001439
Rusty Russell9e1d2142000-04-23 09:11:12 +00001440 chain = iptc_first_chain(handle);
1441 while (chain) {
1442 chaincount++;
1443 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001444 }
1445
Rusty Russell9e1d2142000-04-23 09:11:12 +00001446 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1447 i = 0;
1448 chain = iptc_first_chain(handle);
1449 while (chain) {
1450 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1451 i++;
1452 chain = iptc_next_chain(handle);
1453 }
1454
1455 for (i = 0; i < chaincount; i++) {
1456 if (!builtinstoo
1457 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1458 *handle))
1459 continue;
1460 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1461 }
1462
1463 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001464 return ret;
1465}
1466
Harald Welteae1ff9f2000-12-01 14:26:20 +00001467int
Marc Bouchere6869a82000-03-20 06:03:29 +00001468flush_entries(const ipt_chainlabel chain, int verbose,
1469 iptc_handle_t *handle)
1470{
1471 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001472 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001473
1474 if (verbose)
1475 fprintf(stdout, "Flushing chain `%s'\n", chain);
1476 return iptc_flush_entries(chain, handle);
1477}
Marc Bouchere6869a82000-03-20 06:03:29 +00001478
1479static int
1480zero_entries(const ipt_chainlabel chain, int verbose,
1481 iptc_handle_t *handle)
1482{
1483 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001484 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001485
Marc Bouchere6869a82000-03-20 06:03:29 +00001486 if (verbose)
1487 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1488 return iptc_zero_entries(chain, handle);
1489}
1490
Harald Welteae1ff9f2000-12-01 14:26:20 +00001491int
Marc Bouchere6869a82000-03-20 06:03:29 +00001492delete_chain(const ipt_chainlabel chain, int verbose,
1493 iptc_handle_t *handle)
1494{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001495 if (!chain)
1496 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001497
1498 if (verbose)
1499 fprintf(stdout, "Deleting chain `%s'\n", chain);
1500 return iptc_delete_chain(chain, handle);
1501}
1502
1503static int
1504list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1505 int expanded, int linenumbers, iptc_handle_t *handle)
1506{
1507 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001508 unsigned int format;
1509 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001510
1511 format = FMT_OPTIONS;
1512 if (!verbose)
1513 format |= FMT_NOCOUNTS;
1514 else
1515 format |= FMT_VIA;
1516
1517 if (numeric)
1518 format |= FMT_NUMERIC;
1519
1520 if (!expanded)
1521 format |= FMT_KILOMEGAGIGA;
1522
1523 if (linenumbers)
1524 format |= FMT_LINENUMBERS;
1525
Rusty Russell9e1d2142000-04-23 09:11:12 +00001526 for (this = iptc_first_chain(handle);
1527 this;
1528 this = iptc_next_chain(handle)) {
1529 const struct ipt_entry *i;
1530 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001531
Marc Bouchere6869a82000-03-20 06:03:29 +00001532 if (chain && strcmp(chain, this) != 0)
1533 continue;
1534
1535 if (found) printf("\n");
1536
1537 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001538 i = iptc_first_rule(this, handle);
1539
1540 num = 0;
1541 while (i) {
1542 print_firewall(i,
1543 iptc_get_target(i, handle),
1544 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001545 format,
1546 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001547 i = iptc_next_rule(i, handle);
1548 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001549 found = 1;
1550 }
1551
1552 errno = ENOENT;
1553 return found;
1554}
1555
Harald Welte82dd2ec2000-12-19 05:18:15 +00001556static char *get_modprobe(void)
1557{
1558 int procfile;
1559 char *ret;
1560
1561 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1562 if (procfile < 0)
1563 return NULL;
1564
1565 ret = malloc(1024);
1566 if (ret) {
1567 switch (read(procfile, ret, 1024)) {
1568 case -1: goto fail;
1569 case 1024: goto fail; /* Partial read. Wierd */
1570 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001571 if (ret[strlen(ret)-1]=='\n')
1572 ret[strlen(ret)-1]=0;
1573 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001574 return ret;
1575 }
1576 fail:
1577 free(ret);
1578 close(procfile);
1579 return NULL;
1580}
1581
Harald Welte58918652001-06-16 18:25:25 +00001582int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001583{
1584 char *buf = NULL;
1585 char *argv[3];
1586
1587 /* If they don't explicitly set it, read out of kernel */
1588 if (!modprobe) {
1589 buf = get_modprobe();
1590 if (!buf)
1591 return -1;
1592 modprobe = buf;
1593 }
1594
1595 switch (fork()) {
1596 case 0:
1597 argv[0] = (char *)modprobe;
1598 argv[1] = (char *)modname;
1599 argv[2] = NULL;
1600 execv(argv[0], argv);
1601
1602 /* not usually reached */
1603 exit(0);
1604 case -1:
1605 return -1;
1606
1607 default: /* parent */
1608 wait(NULL);
1609 }
1610
1611 free(buf);
1612 return 0;
1613}
1614
Marc Bouchere6869a82000-03-20 06:03:29 +00001615static struct ipt_entry *
1616generate_entry(const struct ipt_entry *fw,
1617 struct iptables_match *matches,
1618 struct ipt_entry_target *target)
1619{
1620 unsigned int size;
1621 struct iptables_match *m;
1622 struct ipt_entry *e;
1623
1624 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001625 for (m = matches; m; m = m->next) {
1626 if (!m->used)
1627 continue;
1628
Rusty Russell228e98d2000-04-27 10:28:06 +00001629 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001630 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001631
Rusty Russell228e98d2000-04-27 10:28:06 +00001632 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 *e = *fw;
1634 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001635 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001636
1637 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001638 for (m = matches; m; m = m->next) {
1639 if (!m->used)
1640 continue;
1641
Rusty Russell228e98d2000-04-27 10:28:06 +00001642 memcpy(e->elems + size, m->m, m->m->u.match_size);
1643 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001644 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001645 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001646
1647 return e;
1648}
1649
1650int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1651{
1652 struct ipt_entry fw, *e = NULL;
1653 int invert = 0;
1654 unsigned int nsaddrs = 0, ndaddrs = 0;
1655 struct in_addr *saddrs = NULL, *daddrs = NULL;
1656
1657 int c, verbose = 0;
1658 const char *chain = NULL;
1659 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1660 const char *policy = NULL, *newname = NULL;
1661 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001662 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001663 int ret = 1;
1664 struct iptables_match *m;
1665 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001666 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667 const char *jumpto = "";
1668 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001669 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001670
1671 memset(&fw, 0, sizeof(fw));
1672
Sven Kochfb1279a2001-02-27 12:25:12 +00001673 opts = original_opts;
1674 global_option_offset = 0;
1675
Harald Welteae1ff9f2000-12-01 14:26:20 +00001676 /* re-set optind to 0 in case do_command gets called
1677 * a second time */
1678 optind = 0;
1679
1680 /* clear mflags in case do_command gets called a second time
1681 * (we clear the global list of all matches for security)*/
1682 for (m = iptables_matches; m; m = m->next) {
1683 m->mflags = 0;
1684 m->used = 0;
1685 }
1686
1687 for (t = iptables_targets; t; t = t->next) {
1688 t->tflags = 0;
1689 t->used = 0;
1690 }
1691
Marc Bouchere6869a82000-03-20 06:03:29 +00001692 /* Suppress error messages: we may add new options if we
1693 demand-load a protocol. */
1694 opterr = 0;
1695
1696 while ((c = getopt_long(argc, argv,
Harald Welteccd49e52001-01-23 22:54:34 +00001697 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001698 opts, NULL)) != -1) {
1699 switch (c) {
1700 /*
1701 * Command selection
1702 */
1703 case 'A':
1704 add_command(&command, CMD_APPEND, CMD_NONE,
1705 invert);
1706 chain = optarg;
1707 break;
1708
1709 case 'D':
1710 add_command(&command, CMD_DELETE, CMD_NONE,
1711 invert);
1712 chain = optarg;
1713 if (optind < argc && argv[optind][0] != '-'
1714 && argv[optind][0] != '!') {
1715 rulenum = parse_rulenumber(argv[optind++]);
1716 command = CMD_DELETE_NUM;
1717 }
1718 break;
1719
1720 case 'C':
1721 add_command(&command, CMD_CHECK, CMD_NONE,
1722 invert);
1723 chain = optarg;
1724 break;
1725
1726 case 'R':
1727 add_command(&command, CMD_REPLACE, CMD_NONE,
1728 invert);
1729 chain = optarg;
1730 if (optind < argc && argv[optind][0] != '-'
1731 && argv[optind][0] != '!')
1732 rulenum = parse_rulenumber(argv[optind++]);
1733 else
1734 exit_error(PARAMETER_PROBLEM,
1735 "-%c requires a rule number",
1736 cmd2char(CMD_REPLACE));
1737 break;
1738
1739 case 'I':
1740 add_command(&command, CMD_INSERT, CMD_NONE,
1741 invert);
1742 chain = optarg;
1743 if (optind < argc && argv[optind][0] != '-'
1744 && argv[optind][0] != '!')
1745 rulenum = parse_rulenumber(argv[optind++]);
1746 else rulenum = 1;
1747 break;
1748
1749 case 'L':
1750 add_command(&command, CMD_LIST, CMD_ZERO,
1751 invert);
1752 if (optarg) chain = optarg;
1753 else if (optind < argc && argv[optind][0] != '-'
1754 && argv[optind][0] != '!')
1755 chain = argv[optind++];
1756 break;
1757
1758 case 'F':
1759 add_command(&command, CMD_FLUSH, CMD_NONE,
1760 invert);
1761 if (optarg) chain = optarg;
1762 else if (optind < argc && argv[optind][0] != '-'
1763 && argv[optind][0] != '!')
1764 chain = argv[optind++];
1765 break;
1766
1767 case 'Z':
1768 add_command(&command, CMD_ZERO, CMD_LIST,
1769 invert);
1770 if (optarg) chain = optarg;
1771 else if (optind < argc && argv[optind][0] != '-'
1772 && argv[optind][0] != '!')
1773 chain = argv[optind++];
1774 break;
1775
1776 case 'N':
1777 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1778 invert);
1779 chain = optarg;
1780 break;
1781
1782 case 'X':
1783 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1784 invert);
1785 if (optarg) chain = optarg;
1786 else if (optind < argc && argv[optind][0] != '-'
1787 && argv[optind][0] != '!')
1788 chain = argv[optind++];
1789 break;
1790
1791 case 'E':
1792 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1793 invert);
1794 chain = optarg;
1795 if (optind < argc && argv[optind][0] != '-'
1796 && argv[optind][0] != '!')
1797 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001798 else
1799 exit_error(PARAMETER_PROBLEM,
1800 "-%c requires old-chain-name and "
1801 "new-chain-name",
1802 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001803 break;
1804
1805 case 'P':
1806 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1807 invert);
1808 chain = optarg;
1809 if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 policy = argv[optind++];
1812 else
1813 exit_error(PARAMETER_PROBLEM,
1814 "-%c requires a chain and a policy",
1815 cmd2char(CMD_SET_POLICY));
1816 break;
1817
1818 case 'h':
1819 if (!optarg)
1820 optarg = argv[optind];
1821
Rusty Russell2e0a3212000-04-19 11:23:18 +00001822 /* iptables -p icmp -h */
1823 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001824 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001825
Marc Bouchere6869a82000-03-20 06:03:29 +00001826 exit_printhelp();
1827
1828 /*
1829 * Option selection
1830 */
1831 case 'p':
1832 if (check_inverse(optarg, &invert))
1833 optind++;
1834 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1835 invert);
1836
1837 /* Canonicalize into lower case */
1838 for (protocol = argv[optind-1]; *protocol; protocol++)
1839 *protocol = tolower(*protocol);
1840
1841 protocol = argv[optind-1];
1842 fw.ip.proto = parse_protocol(protocol);
1843
1844 if (fw.ip.proto == 0
1845 && (fw.ip.invflags & IPT_INV_PROTO))
1846 exit_error(PARAMETER_PROBLEM,
1847 "rule would never match protocol");
1848 fw.nfcache |= NFC_IP_PROTO;
1849 break;
1850
1851 case 's':
1852 if (check_inverse(optarg, &invert))
1853 optind++;
1854 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1855 invert);
1856 shostnetworkmask = argv[optind-1];
1857 fw.nfcache |= NFC_IP_SRC;
1858 break;
1859
1860 case 'd':
1861 if (check_inverse(optarg, &invert))
1862 optind++;
1863 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1864 invert);
1865 dhostnetworkmask = argv[optind-1];
1866 fw.nfcache |= NFC_IP_DST;
1867 break;
1868
1869 case 'j':
1870 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1871 invert);
1872 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001873 /* TRY_LOAD (may be chain name) */
1874 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001875
1876 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001877 size_t size;
1878
Rusty Russell73f72f52000-07-03 10:17:57 +00001879 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1880 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001881
Rusty Russell2e0a3212000-04-19 11:23:18 +00001882 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001883 target->t->u.target_size = size;
1884 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001886 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001887 }
1888 break;
1889
1890
1891 case 'i':
1892 if (check_inverse(optarg, &invert))
1893 optind++;
1894 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1895 invert);
1896 parse_interface(argv[optind-1],
1897 fw.ip.iniface,
1898 fw.ip.iniface_mask);
1899 fw.nfcache |= NFC_IP_IF_IN;
1900 break;
1901
1902 case 'o':
1903 if (check_inverse(optarg, &invert))
1904 optind++;
1905 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1906 invert);
1907 parse_interface(argv[optind-1],
1908 fw.ip.outiface,
1909 fw.ip.outiface_mask);
1910 fw.nfcache |= NFC_IP_IF_OUT;
1911 break;
1912
1913 case 'f':
1914 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1915 invert);
1916 fw.ip.flags |= IPT_F_FRAG;
1917 fw.nfcache |= NFC_IP_FRAG;
1918 break;
1919
1920 case 'v':
1921 if (!verbose)
1922 set_option(&options, OPT_VERBOSE,
1923 &fw.ip.invflags, invert);
1924 verbose++;
1925 break;
1926
Rusty Russell52a51492000-05-02 16:44:29 +00001927 case 'm': {
1928 size_t size;
1929
Marc Bouchere6869a82000-03-20 06:03:29 +00001930 if (invert)
1931 exit_error(PARAMETER_PROBLEM,
1932 "unexpected ! flag before --match");
1933
Rusty Russell52a51492000-05-02 16:44:29 +00001934 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001935 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1936 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001937 m->m = fw_calloc(1, size);
1938 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001939 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001940 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001941 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001942 }
1943 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001944
1945 case 'n':
1946 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1947 invert);
1948 break;
1949
1950 case 't':
1951 if (invert)
1952 exit_error(PARAMETER_PROBLEM,
1953 "unexpected ! flag before --table");
1954 *table = argv[optind-1];
1955 break;
1956
1957 case 'x':
1958 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1959 invert);
1960 break;
1961
1962 case 'V':
1963 if (invert)
1964 printf("Not %s ;-)\n", program_version);
1965 else
1966 printf("%s v%s\n",
1967 program_name, program_version);
1968 exit(0);
1969
1970 case '0':
1971 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1972 invert);
1973 break;
1974
Harald Welte82dd2ec2000-12-19 05:18:15 +00001975 case 'M':
1976 modprobe = optarg;
1977 break;
1978
Harald Welteccd49e52001-01-23 22:54:34 +00001979 case 'c':
1980
1981 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1982 invert);
1983 pcnt = optarg;
1984 if (optind < argc && argv[optind][0] != '-'
1985 && argv[optind][0] != '!')
1986 bcnt = argv[optind++];
1987 else
1988 exit_error(PARAMETER_PROBLEM,
1989 "-%c requires packet and byte counter",
1990 opt2char(OPT_COUNTERS));
1991
1992 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1993 exit_error(PARAMETER_PROBLEM,
1994 "-%c packet counter not numeric",
1995 opt2char(OPT_COUNTERS));
1996
1997 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1998 exit_error(PARAMETER_PROBLEM,
1999 "-%c byte counter not numeric",
2000 opt2char(OPT_COUNTERS));
2001
2002 break;
2003
2004
Marc Bouchere6869a82000-03-20 06:03:29 +00002005 case 1: /* non option */
2006 if (optarg[0] == '!' && optarg[1] == '\0') {
2007 if (invert)
2008 exit_error(PARAMETER_PROBLEM,
2009 "multiple consecutive ! not"
2010 " allowed");
2011 invert = TRUE;
2012 optarg[0] = '\0';
2013 continue;
2014 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002015 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 exit_tryhelp(2);
2017
2018 default:
2019 /* FIXME: This scheme doesn't allow two of the same
2020 matches --RR */
2021 if (!target
2022 || !(target->parse(c - target->option_offset,
2023 argv, invert,
2024 &target->tflags,
2025 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002026 for (m = iptables_matches; m; m = m->next) {
2027 if (!m->used)
2028 continue;
2029
Marc Bouchere6869a82000-03-20 06:03:29 +00002030 if (m->parse(c - m->option_offset,
2031 argv, invert,
2032 &m->mflags,
2033 &fw,
2034 &fw.nfcache,
2035 &m->m))
2036 break;
2037 }
2038
2039 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00002040 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00002041 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00002042 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00002043 && !find_proto(protocol, DONT_LOAD,
2044 options&OPT_NUMERIC)
2045 && (m = find_proto(protocol, TRY_LOAD,
2046 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002047 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00002048 size_t size;
2049
Rusty Russell73f72f52000-07-03 10:17:57 +00002050 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2051 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002052
Rusty Russell2e0a3212000-04-19 11:23:18 +00002053 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002054 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002055 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002056 m->init(m->m, &fw.nfcache);
2057
Sven Kochdbe857a2001-03-02 09:41:08 +00002058 opts = merge_options(opts,
2059 m->extra_opts, &m->option_offset);
2060
Marc Bouchere6869a82000-03-20 06:03:29 +00002061 optind--;
2062 continue;
2063 }
2064 if (!m)
2065 exit_error(PARAMETER_PROBLEM,
2066 "Unknown arg `%s'",
2067 argv[optind-1]);
2068 }
2069 }
2070 invert = FALSE;
2071 }
2072
Sven Kochfb1279a2001-02-27 12:25:12 +00002073 for (m = iptables_matches; m; m = m->next) {
2074 if (!m->used)
2075 continue;
2076
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002078 }
2079
Marc Bouchere6869a82000-03-20 06:03:29 +00002080 if (target)
2081 target->final_check(target->tflags);
2082
2083 /* Fix me: must put inverse options checking here --MN */
2084
2085 if (optind < argc)
2086 exit_error(PARAMETER_PROBLEM,
2087 "unknown arguments found on commandline");
2088 if (!command)
2089 exit_error(PARAMETER_PROBLEM, "no command specified");
2090 if (invert)
2091 exit_error(PARAMETER_PROBLEM,
2092 "nothing appropriate following !");
2093
Marc Boucher744bd022000-04-22 22:36:10 +00002094 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2095 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002096 if (!(options & OPT_DESTINATION))
2097 dhostnetworkmask = "0.0.0.0/0";
2098 if (!(options & OPT_SOURCE))
2099 shostnetworkmask = "0.0.0.0/0";
2100 }
2101
2102 if (shostnetworkmask)
2103 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2104 &(fw.ip.smsk), &nsaddrs);
2105
2106 if (dhostnetworkmask)
2107 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2108 &(fw.ip.dmsk), &ndaddrs);
2109
2110 if ((nsaddrs > 1 || ndaddrs > 1) &&
2111 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2112 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2113 " source or destination IP addresses");
2114
2115 if (command == CMD_CHECK && fw.ip.invflags != 0)
2116 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2117 cmd2char(CMD_CHECK));
2118
2119 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2120 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2121 "specify a unique address");
2122
2123 generic_opt_check(command, options);
2124
2125 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2126 exit_error(PARAMETER_PROBLEM,
2127 "chain name `%s' too long (must be under %i chars)",
2128 chain, IPT_FUNCTION_MAXNAMELEN);
2129
Harald Welteae1ff9f2000-12-01 14:26:20 +00002130 /* only allocate handle if we weren't called with a handle */
2131 if (!*handle)
2132 *handle = iptc_init(*table);
2133
Harald Welte82dd2ec2000-12-19 05:18:15 +00002134 if (!*handle) {
2135 /* try to insmod the module if iptc_init failed */
2136 iptables_insmod("ip_tables", modprobe);
2137 *handle = iptc_init(*table);
2138 }
2139
Marc Bouchere6869a82000-03-20 06:03:29 +00002140 if (!*handle)
2141 exit_error(VERSION_PROBLEM,
2142 "can't initialize iptables table `%s': %s",
2143 *table, iptc_strerror(errno));
2144
Marc Boucher744bd022000-04-22 22:36:10 +00002145 if (command == CMD_CHECK
2146 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002147 || command == CMD_DELETE
2148 || command == CMD_INSERT
2149 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002150 if (strcmp(chain, "PREROUTING") == 0
2151 || strcmp(chain, "INPUT") == 0) {
2152 /* -o not valid with incoming packets. */
2153 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002154 exit_error(PARAMETER_PROBLEM,
2155 "Can't use -%c with %s\n",
2156 opt2char(OPT_VIANAMEOUT),
2157 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002158 /* -i required with -C */
2159 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2160 exit_error(PARAMETER_PROBLEM,
2161 "Need -%c with %s\n",
2162 opt2char(OPT_VIANAMEIN),
2163 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002164 }
2165
Rusty Russella4860fd2000-06-17 16:13:02 +00002166 if (strcmp(chain, "POSTROUTING") == 0
2167 || strcmp(chain, "OUTPUT") == 0) {
2168 /* -i not valid with outgoing packets */
2169 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002170 exit_error(PARAMETER_PROBLEM,
2171 "Can't use -%c with %s\n",
2172 opt2char(OPT_VIANAMEIN),
2173 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002174 /* -o required with -C */
2175 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2176 exit_error(PARAMETER_PROBLEM,
2177 "Need -%c with %s\n",
2178 opt2char(OPT_VIANAMEOUT),
2179 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002180 }
2181
2182 if (target && iptc_is_chain(jumpto, *handle)) {
2183 printf("Warning: using chain %s, not extension\n",
2184 jumpto);
2185
2186 target = NULL;
2187 }
2188
2189 /* If they didn't specify a target, or it's a chain
2190 name, use standard. */
2191 if (!target
2192 && (strlen(jumpto) == 0
2193 || iptc_is_chain(jumpto, *handle))) {
2194 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002195
Rusty Russell52a51492000-05-02 16:44:29 +00002196 target = find_target(IPT_STANDARD_TARGET,
2197 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002198
2199 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002200 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002201 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002202 target->t->u.target_size = size;
2203 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002204 target->init(target->t, &fw.nfcache);
2205 }
2206
Rusty Russell7e53bf92000-03-20 07:03:28 +00002207 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002208 /* it is no chain, and we can't load a plugin.
2209 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002210 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002211 * chain. */
2212 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002213 } else {
2214 e = generate_entry(&fw, iptables_matches, target->t);
2215 }
2216 }
2217
2218 switch (command) {
2219 case CMD_APPEND:
2220 ret = append_entry(chain, e,
2221 nsaddrs, saddrs, ndaddrs, daddrs,
2222 options&OPT_VERBOSE,
2223 handle);
2224 break;
2225 case CMD_CHECK:
2226 ret = check_packet(chain, e,
2227 nsaddrs, saddrs, ndaddrs, daddrs,
2228 options&OPT_VERBOSE, handle);
2229 break;
2230 case CMD_DELETE:
2231 ret = delete_entry(chain, e,
2232 nsaddrs, saddrs, ndaddrs, daddrs,
2233 options&OPT_VERBOSE,
2234 handle);
2235 break;
2236 case CMD_DELETE_NUM:
2237 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2238 break;
2239 case CMD_REPLACE:
2240 ret = replace_entry(chain, e, rulenum - 1,
2241 saddrs, daddrs, options&OPT_VERBOSE,
2242 handle);
2243 break;
2244 case CMD_INSERT:
2245 ret = insert_entry(chain, e, rulenum - 1,
2246 nsaddrs, saddrs, ndaddrs, daddrs,
2247 options&OPT_VERBOSE,
2248 handle);
2249 break;
2250 case CMD_LIST:
2251 ret = list_entries(chain,
2252 options&OPT_VERBOSE,
2253 options&OPT_NUMERIC,
2254 options&OPT_EXPANDED,
2255 options&OPT_LINENUMBERS,
2256 handle);
2257 break;
2258 case CMD_FLUSH:
2259 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2260 break;
2261 case CMD_ZERO:
2262 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2263 break;
2264 case CMD_LIST|CMD_ZERO:
2265 ret = list_entries(chain,
2266 options&OPT_VERBOSE,
2267 options&OPT_NUMERIC,
2268 options&OPT_EXPANDED,
2269 options&OPT_LINENUMBERS,
2270 handle);
2271 if (ret)
2272 ret = zero_entries(chain,
2273 options&OPT_VERBOSE, handle);
2274 break;
2275 case CMD_NEW_CHAIN:
2276 ret = iptc_create_chain(chain, handle);
2277 break;
2278 case CMD_DELETE_CHAIN:
2279 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2280 break;
2281 case CMD_RENAME_CHAIN:
2282 ret = iptc_rename_chain(chain, newname, handle);
2283 break;
2284 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002285 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002286 break;
2287 default:
2288 /* We should never reach this... */
2289 exit_tryhelp(2);
2290 }
2291
2292 if (verbose > 1)
2293 dump_entries(*handle);
2294
2295 return ret;
2296}