blob: fa570c5c297e71007ea84efb8d167d55866aca1e [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;
Harald Welte2d86b772002-08-26 12:21:44 +00001662 int proto_used = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001663
1664 memset(&fw, 0, sizeof(fw));
1665
Sven Kochfb1279a2001-02-27 12:25:12 +00001666 opts = original_opts;
1667 global_option_offset = 0;
1668
Harald Welteae1ff9f2000-12-01 14:26:20 +00001669 /* re-set optind to 0 in case do_command gets called
1670 * a second time */
1671 optind = 0;
1672
1673 /* clear mflags in case do_command gets called a second time
1674 * (we clear the global list of all matches for security)*/
1675 for (m = iptables_matches; m; m = m->next) {
1676 m->mflags = 0;
1677 m->used = 0;
1678 }
1679
1680 for (t = iptables_targets; t; t = t->next) {
1681 t->tflags = 0;
1682 t->used = 0;
1683 }
1684
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 /* Suppress error messages: we may add new options if we
1686 demand-load a protocol. */
1687 opterr = 0;
1688
1689 while ((c = getopt_long(argc, argv,
Harald Welte2d86b772002-08-26 12:21:44 +00001690 "-A: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 +00001691 opts, NULL)) != -1) {
1692 switch (c) {
1693 /*
1694 * Command selection
1695 */
1696 case 'A':
1697 add_command(&command, CMD_APPEND, CMD_NONE,
1698 invert);
1699 chain = optarg;
1700 break;
1701
1702 case 'D':
1703 add_command(&command, CMD_DELETE, CMD_NONE,
1704 invert);
1705 chain = optarg;
1706 if (optind < argc && argv[optind][0] != '-'
1707 && argv[optind][0] != '!') {
1708 rulenum = parse_rulenumber(argv[optind++]);
1709 command = CMD_DELETE_NUM;
1710 }
1711 break;
1712
Marc Bouchere6869a82000-03-20 06:03:29 +00001713 case 'R':
1714 add_command(&command, CMD_REPLACE, CMD_NONE,
1715 invert);
1716 chain = optarg;
1717 if (optind < argc && argv[optind][0] != '-'
1718 && argv[optind][0] != '!')
1719 rulenum = parse_rulenumber(argv[optind++]);
1720 else
1721 exit_error(PARAMETER_PROBLEM,
1722 "-%c requires a rule number",
1723 cmd2char(CMD_REPLACE));
1724 break;
1725
1726 case 'I':
1727 add_command(&command, CMD_INSERT, 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 rulenum = 1;
1734 break;
1735
1736 case 'L':
1737 add_command(&command, CMD_LIST, CMD_ZERO,
1738 invert);
1739 if (optarg) chain = optarg;
1740 else if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!')
1742 chain = argv[optind++];
1743 break;
1744
1745 case 'F':
1746 add_command(&command, CMD_FLUSH, CMD_NONE,
1747 invert);
1748 if (optarg) chain = optarg;
1749 else if (optind < argc && argv[optind][0] != '-'
1750 && argv[optind][0] != '!')
1751 chain = argv[optind++];
1752 break;
1753
1754 case 'Z':
1755 add_command(&command, CMD_ZERO, CMD_LIST,
1756 invert);
1757 if (optarg) chain = optarg;
1758 else if (optind < argc && argv[optind][0] != '-'
1759 && argv[optind][0] != '!')
1760 chain = argv[optind++];
1761 break;
1762
1763 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001764 if (optarg && *optarg == '-')
1765 exit_error(PARAMETER_PROBLEM,
1766 "chain name not allowed to start "
1767 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001768 if (find_target(optarg, TRY_LOAD))
1769 exit_error(PARAMETER_PROBLEM,
1770 "chain name may not clash "
1771 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001772 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1773 invert);
1774 chain = optarg;
1775 break;
1776
1777 case 'X':
1778 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1779 invert);
1780 if (optarg) chain = optarg;
1781 else if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 chain = argv[optind++];
1784 break;
1785
1786 case 'E':
1787 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1788 invert);
1789 chain = optarg;
1790 if (optind < argc && argv[optind][0] != '-'
1791 && argv[optind][0] != '!')
1792 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001793 else
1794 exit_error(PARAMETER_PROBLEM,
1795 "-%c requires old-chain-name and "
1796 "new-chain-name",
1797 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001798 break;
1799
1800 case 'P':
1801 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1802 invert);
1803 chain = optarg;
1804 if (optind < argc && argv[optind][0] != '-'
1805 && argv[optind][0] != '!')
1806 policy = argv[optind++];
1807 else
1808 exit_error(PARAMETER_PROBLEM,
1809 "-%c requires a chain and a policy",
1810 cmd2char(CMD_SET_POLICY));
1811 break;
1812
1813 case 'h':
1814 if (!optarg)
1815 optarg = argv[optind];
1816
Rusty Russell2e0a3212000-04-19 11:23:18 +00001817 /* iptables -p icmp -h */
1818 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001819 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001820
Marc Bouchere6869a82000-03-20 06:03:29 +00001821 exit_printhelp();
1822
1823 /*
1824 * Option selection
1825 */
1826 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001827 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001828 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1829 invert);
1830
1831 /* Canonicalize into lower case */
1832 for (protocol = argv[optind-1]; *protocol; protocol++)
1833 *protocol = tolower(*protocol);
1834
1835 protocol = argv[optind-1];
1836 fw.ip.proto = parse_protocol(protocol);
1837
1838 if (fw.ip.proto == 0
1839 && (fw.ip.invflags & IPT_INV_PROTO))
1840 exit_error(PARAMETER_PROBLEM,
1841 "rule would never match protocol");
1842 fw.nfcache |= NFC_IP_PROTO;
1843 break;
1844
1845 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001846 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1848 invert);
1849 shostnetworkmask = argv[optind-1];
1850 fw.nfcache |= NFC_IP_SRC;
1851 break;
1852
1853 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001854 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001855 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1856 invert);
1857 dhostnetworkmask = argv[optind-1];
1858 fw.nfcache |= NFC_IP_DST;
1859 break;
1860
1861 case 'j':
1862 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1863 invert);
1864 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001865 /* TRY_LOAD (may be chain name) */
1866 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001867
1868 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001869 size_t size;
1870
Rusty Russell73f72f52000-07-03 10:17:57 +00001871 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1872 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001873
Rusty Russell2e0a3212000-04-19 11:23:18 +00001874 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001875 target->t->u.target_size = size;
1876 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001877 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001878 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001879 }
1880 break;
1881
1882
1883 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001884 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001885 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1886 invert);
1887 parse_interface(argv[optind-1],
1888 fw.ip.iniface,
1889 fw.ip.iniface_mask);
1890 fw.nfcache |= NFC_IP_IF_IN;
1891 break;
1892
1893 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001894 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001895 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1896 invert);
1897 parse_interface(argv[optind-1],
1898 fw.ip.outiface,
1899 fw.ip.outiface_mask);
1900 fw.nfcache |= NFC_IP_IF_OUT;
1901 break;
1902
1903 case 'f':
1904 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1905 invert);
1906 fw.ip.flags |= IPT_F_FRAG;
1907 fw.nfcache |= NFC_IP_FRAG;
1908 break;
1909
1910 case 'v':
1911 if (!verbose)
1912 set_option(&options, OPT_VERBOSE,
1913 &fw.ip.invflags, invert);
1914 verbose++;
1915 break;
1916
Rusty Russell52a51492000-05-02 16:44:29 +00001917 case 'm': {
1918 size_t size;
1919
Marc Bouchere6869a82000-03-20 06:03:29 +00001920 if (invert)
1921 exit_error(PARAMETER_PROBLEM,
1922 "unexpected ! flag before --match");
1923
Rusty Russell52a51492000-05-02 16:44:29 +00001924 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001925 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1926 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001927 m->m = fw_calloc(1, size);
1928 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001929 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001930 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001931 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001932 }
1933 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001934
1935 case 'n':
1936 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1937 invert);
1938 break;
1939
1940 case 't':
1941 if (invert)
1942 exit_error(PARAMETER_PROBLEM,
1943 "unexpected ! flag before --table");
1944 *table = argv[optind-1];
1945 break;
1946
1947 case 'x':
1948 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1949 invert);
1950 break;
1951
1952 case 'V':
1953 if (invert)
1954 printf("Not %s ;-)\n", program_version);
1955 else
1956 printf("%s v%s\n",
1957 program_name, program_version);
1958 exit(0);
1959
1960 case '0':
1961 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1962 invert);
1963 break;
1964
Harald Welte82dd2ec2000-12-19 05:18:15 +00001965 case 'M':
1966 modprobe = optarg;
1967 break;
1968
Harald Welteccd49e52001-01-23 22:54:34 +00001969 case 'c':
1970
1971 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1972 invert);
1973 pcnt = optarg;
1974 if (optind < argc && argv[optind][0] != '-'
1975 && argv[optind][0] != '!')
1976 bcnt = argv[optind++];
1977 else
1978 exit_error(PARAMETER_PROBLEM,
1979 "-%c requires packet and byte counter",
1980 opt2char(OPT_COUNTERS));
1981
1982 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1983 exit_error(PARAMETER_PROBLEM,
1984 "-%c packet counter not numeric",
1985 opt2char(OPT_COUNTERS));
1986
1987 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1988 exit_error(PARAMETER_PROBLEM,
1989 "-%c byte counter not numeric",
1990 opt2char(OPT_COUNTERS));
1991
1992 break;
1993
1994
Marc Bouchere6869a82000-03-20 06:03:29 +00001995 case 1: /* non option */
1996 if (optarg[0] == '!' && optarg[1] == '\0') {
1997 if (invert)
1998 exit_error(PARAMETER_PROBLEM,
1999 "multiple consecutive ! not"
2000 " allowed");
2001 invert = TRUE;
2002 optarg[0] = '\0';
2003 continue;
2004 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002005 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002006 exit_tryhelp(2);
2007
2008 default:
2009 /* FIXME: This scheme doesn't allow two of the same
2010 matches --RR */
2011 if (!target
2012 || !(target->parse(c - target->option_offset,
2013 argv, invert,
2014 &target->tflags,
2015 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002016 for (m = iptables_matches; m; m = m->next) {
2017 if (!m->used)
2018 continue;
2019
Marc Bouchere6869a82000-03-20 06:03:29 +00002020 if (m->parse(c - m->option_offset,
2021 argv, invert,
2022 &m->mflags,
2023 &fw,
2024 &fw.nfcache,
2025 &m->m))
2026 break;
2027 }
Harald Welte2d86b772002-08-26 12:21:44 +00002028
2029 /* If you listen carefully, you can
2030 actually hear this code suck. */
2031
2032 /* some explanations (after four different bugs
2033 * in 3 different releases): If we encountere a
2034 * parameter, that has not been parsed yet,
2035 * it's not an option of an explicitly loaded
2036 * match or a target. However, we support
2037 * implicit loading of the protocol match
2038 * extension. '-p tcp' means 'l4 proto 6' and
2039 * at the same time 'load tcp protocol match on
2040 * demand if we specify --dport'.
2041 *
2042 * To make this work, we need to make sure:
2043 * - the parameter has not been parsed by
2044 * a match (m above)
2045 * - a protocol has been specified
2046 * - the protocol extension has not been
2047 * loaded yet, or is loaded and unused
2048 * [think of iptables-restore!]
2049 * - the protocol extension can be successively
2050 * loaded
2051 */
2052 if (m == NULL
2053 && protocol
2054 && (!find_proto(protocol, DONT_LOAD,
2055 options&OPT_NUMERIC)
2056 || (find_proto(protocol, DONT_LOAD,
2057 options&OPT_NUMERIC)
2058 && (proto_used == 0))
2059 )
2060 && (m = find_proto(protocol, TRY_LOAD,
2061 options&OPT_NUMERIC))) {
2062 /* Try loading protocol */
2063 size_t size;
2064
2065 proto_used = 1;
2066
2067 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2068 + m->size;
2069
2070 m->m = fw_calloc(1, size);
2071 m->m->u.match_size = size;
2072 strcpy(m->m->u.user.name, m->name);
2073 m->init(m->m, &fw.nfcache);
2074
2075 opts = merge_options(opts,
2076 m->extra_opts, &m->option_offset);
2077
2078 optind--;
2079 continue;
2080 }
Marc Bouchere6869a82000-03-20 06:03:29 +00002081 if (!m)
2082 exit_error(PARAMETER_PROBLEM,
2083 "Unknown arg `%s'",
2084 argv[optind-1]);
2085 }
2086 }
2087 invert = FALSE;
2088 }
2089
Sven Kochfb1279a2001-02-27 12:25:12 +00002090 for (m = iptables_matches; m; m = m->next) {
2091 if (!m->used)
2092 continue;
2093
Marc Bouchere6869a82000-03-20 06:03:29 +00002094 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002095 }
2096
Marc Bouchere6869a82000-03-20 06:03:29 +00002097 if (target)
2098 target->final_check(target->tflags);
2099
2100 /* Fix me: must put inverse options checking here --MN */
2101
2102 if (optind < argc)
2103 exit_error(PARAMETER_PROBLEM,
2104 "unknown arguments found on commandline");
2105 if (!command)
2106 exit_error(PARAMETER_PROBLEM, "no command specified");
2107 if (invert)
2108 exit_error(PARAMETER_PROBLEM,
2109 "nothing appropriate following !");
2110
Harald Welte6336bfd2002-05-07 14:41:43 +00002111 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002112 if (!(options & OPT_DESTINATION))
2113 dhostnetworkmask = "0.0.0.0/0";
2114 if (!(options & OPT_SOURCE))
2115 shostnetworkmask = "0.0.0.0/0";
2116 }
2117
2118 if (shostnetworkmask)
2119 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2120 &(fw.ip.smsk), &nsaddrs);
2121
2122 if (dhostnetworkmask)
2123 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2124 &(fw.ip.dmsk), &ndaddrs);
2125
2126 if ((nsaddrs > 1 || ndaddrs > 1) &&
2127 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2128 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2129 " source or destination IP addresses");
2130
Marc Bouchere6869a82000-03-20 06:03:29 +00002131 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2132 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2133 "specify a unique address");
2134
2135 generic_opt_check(command, options);
2136
2137 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2138 exit_error(PARAMETER_PROBLEM,
2139 "chain name `%s' too long (must be under %i chars)",
2140 chain, IPT_FUNCTION_MAXNAMELEN);
2141
Harald Welteae1ff9f2000-12-01 14:26:20 +00002142 /* only allocate handle if we weren't called with a handle */
2143 if (!*handle)
2144 *handle = iptc_init(*table);
2145
Harald Welte82dd2ec2000-12-19 05:18:15 +00002146 if (!*handle) {
2147 /* try to insmod the module if iptc_init failed */
2148 iptables_insmod("ip_tables", modprobe);
2149 *handle = iptc_init(*table);
2150 }
2151
Marc Bouchere6869a82000-03-20 06:03:29 +00002152 if (!*handle)
2153 exit_error(VERSION_PROBLEM,
2154 "can't initialize iptables table `%s': %s",
2155 *table, iptc_strerror(errno));
2156
Harald Welte6336bfd2002-05-07 14:41:43 +00002157 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002158 || command == CMD_DELETE
2159 || command == CMD_INSERT
2160 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002161 if (strcmp(chain, "PREROUTING") == 0
2162 || strcmp(chain, "INPUT") == 0) {
2163 /* -o not valid with incoming packets. */
2164 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002165 exit_error(PARAMETER_PROBLEM,
2166 "Can't use -%c with %s\n",
2167 opt2char(OPT_VIANAMEOUT),
2168 chain);
2169 }
2170
Rusty Russella4860fd2000-06-17 16:13:02 +00002171 if (strcmp(chain, "POSTROUTING") == 0
2172 || strcmp(chain, "OUTPUT") == 0) {
2173 /* -i not valid with outgoing packets */
2174 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002175 exit_error(PARAMETER_PROBLEM,
2176 "Can't use -%c with %s\n",
2177 opt2char(OPT_VIANAMEIN),
2178 chain);
2179 }
2180
2181 if (target && iptc_is_chain(jumpto, *handle)) {
2182 printf("Warning: using chain %s, not extension\n",
2183 jumpto);
2184
2185 target = NULL;
2186 }
2187
2188 /* If they didn't specify a target, or it's a chain
2189 name, use standard. */
2190 if (!target
2191 && (strlen(jumpto) == 0
2192 || iptc_is_chain(jumpto, *handle))) {
2193 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002194
Rusty Russell52a51492000-05-02 16:44:29 +00002195 target = find_target(IPT_STANDARD_TARGET,
2196 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002197
2198 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002199 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002200 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002201 target->t->u.target_size = size;
2202 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002203 target->init(target->t, &fw.nfcache);
2204 }
2205
Rusty Russell7e53bf92000-03-20 07:03:28 +00002206 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002207 /* it is no chain, and we can't load a plugin.
2208 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002209 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002210 * chain. */
2211 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002212 } else {
2213 e = generate_entry(&fw, iptables_matches, target->t);
2214 }
2215 }
2216
2217 switch (command) {
2218 case CMD_APPEND:
2219 ret = append_entry(chain, e,
2220 nsaddrs, saddrs, ndaddrs, daddrs,
2221 options&OPT_VERBOSE,
2222 handle);
2223 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002224 case CMD_DELETE:
2225 ret = delete_entry(chain, e,
2226 nsaddrs, saddrs, ndaddrs, daddrs,
2227 options&OPT_VERBOSE,
2228 handle);
2229 break;
2230 case CMD_DELETE_NUM:
2231 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2232 break;
2233 case CMD_REPLACE:
2234 ret = replace_entry(chain, e, rulenum - 1,
2235 saddrs, daddrs, options&OPT_VERBOSE,
2236 handle);
2237 break;
2238 case CMD_INSERT:
2239 ret = insert_entry(chain, e, rulenum - 1,
2240 nsaddrs, saddrs, ndaddrs, daddrs,
2241 options&OPT_VERBOSE,
2242 handle);
2243 break;
2244 case CMD_LIST:
2245 ret = list_entries(chain,
2246 options&OPT_VERBOSE,
2247 options&OPT_NUMERIC,
2248 options&OPT_EXPANDED,
2249 options&OPT_LINENUMBERS,
2250 handle);
2251 break;
2252 case CMD_FLUSH:
2253 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2254 break;
2255 case CMD_ZERO:
2256 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2257 break;
2258 case CMD_LIST|CMD_ZERO:
2259 ret = list_entries(chain,
2260 options&OPT_VERBOSE,
2261 options&OPT_NUMERIC,
2262 options&OPT_EXPANDED,
2263 options&OPT_LINENUMBERS,
2264 handle);
2265 if (ret)
2266 ret = zero_entries(chain,
2267 options&OPT_VERBOSE, handle);
2268 break;
2269 case CMD_NEW_CHAIN:
2270 ret = iptc_create_chain(chain, handle);
2271 break;
2272 case CMD_DELETE_CHAIN:
2273 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2274 break;
2275 case CMD_RENAME_CHAIN:
2276 ret = iptc_rename_chain(chain, newname, handle);
2277 break;
2278 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002279 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002280 break;
2281 default:
2282 /* We should never reach this... */
2283 exit_tryhelp(2);
2284 }
2285
2286 if (verbose > 1)
2287 dump_entries(*handle);
2288
2289 return ret;
2290}