blob: b9c190b2f85ed2fa5e42329cf88b6bb7f28b3420 [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',
Harald Welte6336bfd2002-05-07 14:41:43 +000082 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000083
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' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000111 { "new-chain", 1, 0, 'N' },
112 { "delete-chain", 2, 0, 'X' },
113 { "rename-chain", 2, 0, 'E' },
114 { "policy", 1, 0, 'P' },
115 { "source", 1, 0, 's' },
116 { "destination", 1, 0, 'd' },
117 { "src", 1, 0, 's' }, /* synonym */
118 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000119 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 { "in-interface", 1, 0, 'i' },
121 { "jump", 1, 0, 'j' },
122 { "table", 1, 0, 't' },
123 { "match", 1, 0, 'm' },
124 { "numeric", 0, 0, 'n' },
125 { "out-interface", 1, 0, 'o' },
126 { "verbose", 0, 0, 'v' },
127 { "exact", 0, 0, 'x' },
128 { "fragments", 0, 0, 'f' },
129 { "version", 0, 0, 'V' },
130 { "help", 2, 0, 'h' },
131 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000132 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000133 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000134 { 0 }
135};
136
Rusty Russell4e242f82000-05-31 06:33:50 +0000137#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000138struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000139ipt_get_target(struct ipt_entry *e)
140{
141 return (void *)e + e->target_offset;
142}
143#endif
144
Marc Bouchere6869a82000-03-20 06:03:29 +0000145static struct option *opts = original_opts;
146static unsigned int global_option_offset = 0;
147
148/* Table of legal combinations of commands and options. If any of the
149 * given commands make an option legal, that option is legal (applies to
150 * CMD_LIST and CMD_ZERO only).
151 * Key:
152 * + compulsory
153 * x illegal
154 * optional
155 */
156
157static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
158/* Well, it's better than "Re: Linux vs FreeBSD" */
159{
160 /* -n -s -d -p -j -v -x -i -o -f --line */
161/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
162/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
164/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
165/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
167/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000172/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000173/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
174};
175
176static int inverse_for_options[NUMBER_OF_OPT] =
177{
178/* -n */ 0,
179/* -s */ IPT_INV_SRCIP,
180/* -d */ IPT_INV_DSTIP,
181/* -p */ IPT_INV_PROTO,
182/* -j */ 0,
183/* -v */ 0,
184/* -x */ 0,
185/* -i */ IPT_INV_VIA_IN,
186/* -o */ IPT_INV_VIA_OUT,
187/* -f */ IPT_INV_FRAG,
188/*--line*/ 0
189};
190
191const char *program_version;
192const char *program_name;
193
Rusty Russell2e0a3212000-04-19 11:23:18 +0000194/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000195struct iptables_match *iptables_matches = NULL;
196struct iptables_target *iptables_targets = NULL;
197
198/* Extra debugging from libiptc */
199extern void dump_entries(const iptc_handle_t handle);
200
201/* A few hardcoded protocols for 'all' and in case the user has no
202 /etc/protocols */
203struct pprot {
204 char *name;
205 u_int8_t num;
206};
207
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000208/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000209/* defined in netinet/in.h */
210#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000211#ifndef IPPROTO_ESP
212#define IPPROTO_ESP 50
213#endif
214#ifndef IPPROTO_AH
215#define IPPROTO_AH 51
216#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000217#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000218
Marc Bouchere6869a82000-03-20 06:03:29 +0000219static const struct pprot chain_protos[] = {
220 { "tcp", IPPROTO_TCP },
221 { "udp", IPPROTO_UDP },
222 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000223 { "esp", IPPROTO_ESP },
224 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000225 { "all", 0 },
226};
227
228static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000229proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000230{
231 unsigned int i;
232
Rusty Russell28381a42000-05-10 00:19:50 +0000233 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000234 struct protoent *pent = getprotobynumber(proto);
235 if (pent)
236 return pent->p_name;
237 }
238
239 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
240 if (chain_protos[i].num == proto)
241 return chain_protos[i].name;
242
243 return NULL;
244}
245
246struct in_addr *
247dotted_to_addr(const char *dotted)
248{
249 static struct in_addr addr;
250 unsigned char *addrp;
251 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000252 unsigned int onebyte;
253 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000254 char buf[20];
255
256 /* copy dotted string, because we need to modify it */
257 strncpy(buf, dotted, sizeof(buf) - 1);
258 addrp = (unsigned char *) &(addr.s_addr);
259
260 p = buf;
261 for (i = 0; i < 3; i++) {
262 if ((q = strchr(p, '.')) == NULL)
263 return (struct in_addr *) NULL;
264
265 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000266 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000267 return (struct in_addr *) NULL;
268
269 addrp[i] = (unsigned char) onebyte;
270 p = q + 1;
271 }
272
273 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000274 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000275 return (struct in_addr *) NULL;
276
277 addrp[3] = (unsigned char) onebyte;
278
279 return &addr;
280}
281
282static struct in_addr *
283network_to_addr(const char *name)
284{
285 struct netent *net;
286 static struct in_addr addr;
287
288 if ((net = getnetbyname(name)) != NULL) {
289 if (net->n_addrtype != AF_INET)
290 return (struct in_addr *) NULL;
291 addr.s_addr = htonl((unsigned long) net->n_net);
292 return &addr;
293 }
294
295 return (struct in_addr *) NULL;
296}
297
298static void
299inaddrcpy(struct in_addr *dst, struct in_addr *src)
300{
301 /* memcpy(dst, src, sizeof(struct in_addr)); */
302 dst->s_addr = src->s_addr;
303}
304
305void
306exit_error(enum exittype status, char *msg, ...)
307{
308 va_list args;
309
310 va_start(args, msg);
311 fprintf(stderr, "%s v%s: ", program_name, program_version);
312 vfprintf(stderr, msg, args);
313 va_end(args);
314 fprintf(stderr, "\n");
315 if (status == PARAMETER_PROBLEM)
316 exit_tryhelp(status);
317 if (status == VERSION_PROBLEM)
318 fprintf(stderr,
319 "Perhaps iptables or your kernel needs to be upgraded.\n");
320 exit(status);
321}
322
323void
324exit_tryhelp(int status)
325{
326 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
327 program_name, program_name );
328 exit(status);
329}
330
331void
332exit_printhelp(void)
333{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000334 struct iptables_match *m = NULL;
335 struct iptables_target *t = NULL;
336
Marc Bouchere6869a82000-03-20 06:03:29 +0000337 printf("%s v%s\n\n"
338"Usage: %s -[ADC] chain rule-specification [options]\n"
339" %s -[RI] chain rulenum rule-specification [options]\n"
340" %s -D chain rulenum [options]\n"
341" %s -[LFZ] [chain] [options]\n"
342" %s -[NX] chain\n"
343" %s -E old-chain-name new-chain-name\n"
344" %s -P chain target [options]\n"
345" %s -h (print this help information)\n\n",
346 program_name, program_version, program_name, program_name,
347 program_name, program_name, program_name, program_name,
348 program_name, program_name);
349
350 printf(
351"Commands:\n"
352"Either long or short options are allowed.\n"
353" --append -A chain Append to chain\n"
354" --delete -D chain Delete matching rule from chain\n"
355" --delete -D chain rulenum\n"
356" Delete rule rulenum (1 = first) from chain\n"
357" --insert -I chain [rulenum]\n"
358" Insert in chain as rulenum (default 1=first)\n"
359" --replace -R chain rulenum\n"
360" Replace rule rulenum (1 = first) in chain\n"
361" --list -L [chain] List the rules in a chain or all chains\n"
362" --flush -F [chain] Delete all rules in chain or all chains\n"
363" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000364" --new -N chain Create a new user-defined chain\n"
365" --delete-chain\n"
366" -X [chain] Delete a user-defined chain\n"
367" --policy -P chain target\n"
368" Change policy on chain to target\n"
369" --rename-chain\n"
370" -E old-chain new-chain\n"
371" Change chain name, (moving any references)\n"
372
373"Options:\n"
374" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
375" --source -s [!] address[/mask]\n"
376" source specification\n"
377" --destination -d [!] address[/mask]\n"
378" destination specification\n"
379" --in-interface -i [!] input name[+]\n"
380" network interface name ([+] for wildcard)\n"
381" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000382" target for rule (may load target extension)\n"
383" --match -m match\n"
384" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000385" --numeric -n numeric output of addresses and ports\n"
386" --out-interface -o [!] output name[+]\n"
387" network interface name ([+] for wildcard)\n"
388" --table -t table table to manipulate (default: `filter')\n"
389" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000390" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000391" --exact -x expand numbers (display exact values)\n"
392"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000393" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000394" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000395"[!] --version -V print package version.\n");
396
Rusty Russell363112d2000-08-11 13:49:26 +0000397 /* Print out any special helps. A user might like to be able
398 to add a --help to the commandline, and see expected
399 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000400 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000401 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000402 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000404 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000406 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000408 exit(0);
409}
410
411static void
412generic_opt_check(int command, int options)
413{
414 int i, j, legal = 0;
415
416 /* Check that commands are valid with options. Complicated by the
417 * fact that if an option is legal with *any* command given, it is
418 * legal overall (ie. -z and -l).
419 */
420 for (i = 0; i < NUMBER_OF_OPT; i++) {
421 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
422
423 for (j = 0; j < NUMBER_OF_CMD; j++) {
424 if (!(command & (1<<j)))
425 continue;
426
427 if (!(options & (1<<i))) {
428 if (commands_v_options[j][i] == '+')
429 exit_error(PARAMETER_PROBLEM,
430 "You need to supply the `-%c' "
431 "option for this command\n",
432 optflags[i]);
433 } else {
434 if (commands_v_options[j][i] != 'x')
435 legal = 1;
436 else if (legal == 0)
437 legal = -1;
438 }
439 }
440 if (legal == -1)
441 exit_error(PARAMETER_PROBLEM,
442 "Illegal option `-%c' with this command\n",
443 optflags[i]);
444 }
445}
446
447static char
448opt2char(int option)
449{
450 const char *ptr;
451 for (ptr = optflags; option > 1; option >>= 1, ptr++);
452
453 return *ptr;
454}
455
456static char
457cmd2char(int option)
458{
459 const char *ptr;
460 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
461
462 return *ptr;
463}
464
465static void
466add_command(int *cmd, const int newcmd, const int othercmds, int invert)
467{
468 if (invert)
469 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
470 if (*cmd & (~othercmds))
471 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
472 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
473 *cmd |= newcmd;
474}
475
476int
Harald Welteb77f1da2002-03-14 11:35:58 +0000477check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000478{
479 if (option && strcmp(option, "!") == 0) {
480 if (*invert)
481 exit_error(PARAMETER_PROBLEM,
482 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000483 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000484 if (optind) {
485 *optind = *optind+1;
486 if (argc && *optind > argc)
487 exit_error(PARAMETER_PROBLEM,
488 "no argument following `!'");
489 }
490
Marc Bouchere6869a82000-03-20 06:03:29 +0000491 return TRUE;
492 }
493 return FALSE;
494}
495
496static void *
497fw_calloc(size_t count, size_t size)
498{
499 void *p;
500
501 if ((p = calloc(count, size)) == NULL) {
502 perror("iptables: calloc failed");
503 exit(1);
504 }
505 return p;
506}
507
508static void *
509fw_malloc(size_t size)
510{
511 void *p;
512
513 if ((p = malloc(size)) == NULL) {
514 perror("iptables: malloc failed");
515 exit(1);
516 }
517 return p;
518}
519
520static struct in_addr *
521host_to_addr(const char *name, unsigned int *naddr)
522{
523 struct hostent *host;
524 struct in_addr *addr;
525 unsigned int i;
526
527 *naddr = 0;
528 if ((host = gethostbyname(name)) != NULL) {
529 if (host->h_addrtype != AF_INET ||
530 host->h_length != sizeof(struct in_addr))
531 return (struct in_addr *) NULL;
532
533 while (host->h_addr_list[*naddr] != (char *) NULL)
534 (*naddr)++;
535 addr = fw_calloc(*naddr, sizeof(struct in_addr));
536 for (i = 0; i < *naddr; i++)
537 inaddrcpy(&(addr[i]),
538 (struct in_addr *) host->h_addr_list[i]);
539 return addr;
540 }
541
542 return (struct in_addr *) NULL;
543}
544
545static char *
546addr_to_host(const struct in_addr *addr)
547{
548 struct hostent *host;
549
550 if ((host = gethostbyaddr((char *) addr,
551 sizeof(struct in_addr), AF_INET)) != NULL)
552 return (char *) host->h_name;
553
554 return (char *) NULL;
555}
556
557/*
558 * All functions starting with "parse" should succeed, otherwise
559 * the program fails.
560 * Most routines return pointers to static data that may change
561 * between calls to the same or other routines with a few exceptions:
562 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
563 * return global static data.
564*/
565
566static struct in_addr *
567parse_hostnetwork(const char *name, unsigned int *naddrs)
568{
569 struct in_addr *addrp, *addrptmp;
570
571 if ((addrptmp = dotted_to_addr(name)) != NULL ||
572 (addrptmp = network_to_addr(name)) != NULL) {
573 addrp = fw_malloc(sizeof(struct in_addr));
574 inaddrcpy(addrp, addrptmp);
575 *naddrs = 1;
576 return addrp;
577 }
578 if ((addrp = host_to_addr(name, naddrs)) != NULL)
579 return addrp;
580
581 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
582}
583
584static struct in_addr *
585parse_mask(char *mask)
586{
587 static struct in_addr maskaddr;
588 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000589 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000590
591 if (mask == NULL) {
592 /* no mask at all defaults to 32 bits */
593 maskaddr.s_addr = 0xFFFFFFFF;
594 return &maskaddr;
595 }
596 if ((addrp = dotted_to_addr(mask)) != NULL)
597 /* dotted_to_addr already returns a network byte order addr */
598 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000599 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000600 exit_error(PARAMETER_PROBLEM,
601 "invalid mask `%s' specified", mask);
602 if (bits != 0) {
603 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
604 return &maskaddr;
605 }
606
607 maskaddr.s_addr = 0L;
608 return &maskaddr;
609}
610
Marc Boucherb93c7982001-12-06 14:50:19 +0000611void
Marc Bouchere6869a82000-03-20 06:03:29 +0000612parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
613 struct in_addr *maskp, unsigned int *naddrs)
614{
615 struct in_addr *addrp;
616 char buf[256];
617 char *p;
618 int i, j, k, n;
619
620 strncpy(buf, name, sizeof(buf) - 1);
621 if ((p = strrchr(buf, '/')) != NULL) {
622 *p = '\0';
623 addrp = parse_mask(p + 1);
624 } else
625 addrp = parse_mask(NULL);
626 inaddrcpy(maskp, addrp);
627
628 /* if a null mask is given, the name is ignored, like in "any/0" */
629 if (maskp->s_addr == 0L)
630 strcpy(buf, "0.0.0.0");
631
632 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
633 n = *naddrs;
634 for (i = 0, j = 0; i < n; i++) {
635 addrp[j++].s_addr &= maskp->s_addr;
636 for (k = 0; k < j - 1; k++) {
637 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
638 (*naddrs)--;
639 j--;
640 break;
641 }
642 }
643 }
644}
645
646struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000647find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000648{
649 struct iptables_match *ptr;
650
651 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
652 if (strcmp(name, ptr->name) == 0)
653 break;
654 }
655
Harald Welte3efb6ea2001-08-06 18:50:21 +0000656#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000657 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000658 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
659 + strlen(name)];
660 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000661 if (dlopen(path, RTLD_NOW)) {
662 /* Found library. If it didn't register itself,
663 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000664 ptr = find_match(name, DONT_LOAD);
665
Rusty Russell9e1d2142000-04-23 09:11:12 +0000666 if (!ptr)
667 exit_error(PARAMETER_PROBLEM,
668 "Couldn't load match `%s'\n",
669 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000670 } else if (tryload == LOAD_MUST_SUCCEED)
671 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000672 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000673 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000674 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000675#else
676 if (ptr && !ptr->loaded) {
677 if (tryload != DONT_LOAD)
678 ptr->loaded = 1;
679 else
680 ptr = NULL;
681 }
Marc Boucher067477b2002-03-24 15:09:31 +0000682 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
683 exit_error(PARAMETER_PROBLEM,
684 "Couldn't find match `%s'\n", name);
685 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000686#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000687
Harald Welteae1ff9f2000-12-01 14:26:20 +0000688 if (ptr)
689 ptr->used = 1;
690
Marc Bouchere6869a82000-03-20 06:03:29 +0000691 return ptr;
692}
693
Rusty Russell28381a42000-05-10 00:19:50 +0000694/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
695static struct iptables_match *
696find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
697{
Harald Welteed498492001-07-23 01:24:22 +0000698 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000699
Harald Welte0b0013a2002-02-18 16:15:31 +0000700 if (string_to_number(pname, 0, 255, &proto) != -1) {
701 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000702
Harald Welte0b0013a2002-02-18 16:15:31 +0000703 if (protoname)
704 return find_match(protoname, tryload);
705 } else
706 return find_match(pname, tryload);
707
708 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000709}
710
Marc Boucherb93c7982001-12-06 14:50:19 +0000711u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000712parse_protocol(const char *s)
713{
Harald Welteed498492001-07-23 01:24:22 +0000714 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000715
Harald Welteed498492001-07-23 01:24:22 +0000716 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000717 struct protoent *pent;
718
719 if ((pent = getprotobyname(s)))
720 proto = pent->p_proto;
721 else {
722 unsigned int i;
723 for (i = 0;
724 i < sizeof(chain_protos)/sizeof(struct pprot);
725 i++) {
726 if (strcmp(s, chain_protos[i].name) == 0) {
727 proto = chain_protos[i].num;
728 break;
729 }
730 }
731 if (i == sizeof(chain_protos)/sizeof(struct pprot))
732 exit_error(PARAMETER_PROBLEM,
733 "unknown protocol `%s' specified",
734 s);
735 }
736 }
737
738 return (u_int16_t)proto;
739}
740
741static void
742parse_interface(const char *arg, char *vianame, unsigned char *mask)
743{
744 int vialen = strlen(arg);
745 unsigned int i;
746
747 memset(mask, 0, IFNAMSIZ);
748 memset(vianame, 0, IFNAMSIZ);
749
750 if (vialen + 1 > IFNAMSIZ)
751 exit_error(PARAMETER_PROBLEM,
752 "interface name `%s' must be shorter than IFNAMSIZ"
753 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000754
Marc Bouchere6869a82000-03-20 06:03:29 +0000755 strcpy(vianame, arg);
756 if (vialen == 0)
757 memset(mask, 0, IFNAMSIZ);
758 else if (vianame[vialen - 1] == '+') {
759 memset(mask, 0xFF, vialen - 1);
760 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000761 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000762 } else {
763 /* Include nul-terminator in match */
764 memset(mask, 0xFF, vialen + 1);
765 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000766 for (i = 0; vianame[i]; i++) {
Harald Welte2892e6a2001-11-27 15:09:06 +0000767 if (!isalnum(vianame[i])
768 && vianame[i] != '_'
769 && vianame[i] != '.') {
Harald Weltede1578f2001-05-23 23:07:33 +0000770 printf("Warning: wierd character in interface"
771 " `%s' (No aliases, :, ! or *).\n",
772 vianame);
773 break;
774 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000775 }
776 }
777}
778
779/* Can't be zero. */
780static int
781parse_rulenumber(const char *rule)
782{
Harald Welteed498492001-07-23 01:24:22 +0000783 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000784
Harald Welteed498492001-07-23 01:24:22 +0000785 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000786 exit_error(PARAMETER_PROBLEM,
787 "Invalid rule number `%s'", rule);
788
789 return rulenum;
790}
791
792static const char *
793parse_target(const char *targetname)
794{
795 const char *ptr;
796
797 if (strlen(targetname) < 1)
798 exit_error(PARAMETER_PROBLEM,
799 "Invalid target name (too short)");
800
801 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
802 exit_error(PARAMETER_PROBLEM,
803 "Invalid target name `%s' (%i chars max)",
804 targetname, sizeof(ipt_chainlabel)-1);
805
806 for (ptr = targetname; *ptr; ptr++)
807 if (isspace(*ptr))
808 exit_error(PARAMETER_PROBLEM,
809 "Invalid target name `%s'", targetname);
810 return targetname;
811}
812
813static char *
814addr_to_network(const struct in_addr *addr)
815{
816 struct netent *net;
817
818 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
819 return (char *) net->n_name;
820
821 return (char *) NULL;
822}
823
824char *
825addr_to_dotted(const struct in_addr *addrp)
826{
827 static char buf[20];
828 const unsigned char *bytep;
829
830 bytep = (const unsigned char *) &(addrp->s_addr);
831 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
832 return buf;
833}
Marc Boucherb93c7982001-12-06 14:50:19 +0000834
835char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000836addr_to_anyname(const struct in_addr *addr)
837{
838 char *name;
839
840 if ((name = addr_to_host(addr)) != NULL ||
841 (name = addr_to_network(addr)) != NULL)
842 return name;
843
844 return addr_to_dotted(addr);
845}
846
Marc Boucherb93c7982001-12-06 14:50:19 +0000847char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000848mask_to_dotted(const struct in_addr *mask)
849{
850 int i;
851 static char buf[20];
852 u_int32_t maskaddr, bits;
853
854 maskaddr = ntohl(mask->s_addr);
855
856 if (maskaddr == 0xFFFFFFFFL)
857 /* we don't want to see "/32" */
858 return "";
859
860 i = 32;
861 bits = 0xFFFFFFFEL;
862 while (--i >= 0 && maskaddr != bits)
863 bits <<= 1;
864 if (i >= 0)
865 sprintf(buf, "/%d", i);
866 else
867 /* mask was not a decent combination of 1's and 0's */
868 sprintf(buf, "/%s", addr_to_dotted(mask));
869
870 return buf;
871}
872
873int
Harald Welteed498492001-07-23 01:24:22 +0000874string_to_number(const char *s, unsigned int min, unsigned int max,
875 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000876{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000877 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000878 char *end;
879
880 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000881 errno = 0;
882 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000883 if (*end == '\0' && end != s) {
884 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000885 if (errno != ERANGE && min <= number && number <= max) {
886 *ret = number;
887 return 0;
888 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000889 }
890 return -1;
891}
892
893static void
894set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
895 int invert)
896{
897 if (*options & option)
898 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
899 opt2char(option));
900 *options |= option;
901
902 if (invert) {
903 unsigned int i;
904 for (i = 0; 1 << i != option; i++);
905
906 if (!inverse_for_options[i])
907 exit_error(PARAMETER_PROBLEM,
908 "cannot have ! before -%c",
909 opt2char(option));
910 *invflg |= inverse_for_options[i];
911 }
912}
913
914struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000915find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000916{
917 struct iptables_target *ptr;
918
919 /* Standard target? */
920 if (strcmp(name, "") == 0
921 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
922 || strcmp(name, IPTC_LABEL_DROP) == 0
923 || strcmp(name, IPTC_LABEL_QUEUE) == 0
924 || strcmp(name, IPTC_LABEL_RETURN) == 0)
925 name = "standard";
926
927 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
928 if (strcmp(name, ptr->name) == 0)
929 break;
930 }
931
Harald Welte3efb6ea2001-08-06 18:50:21 +0000932#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000933 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000934 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
935 + strlen(name)];
936 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000937 if (dlopen(path, RTLD_NOW)) {
938 /* Found library. If it didn't register itself,
939 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000940 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000941 if (!ptr)
942 exit_error(PARAMETER_PROBLEM,
943 "Couldn't load target `%s'\n",
944 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000945 } else if (tryload == LOAD_MUST_SUCCEED)
946 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000947 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000948 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000949 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000950#else
951 if (ptr && !ptr->loaded) {
952 if (tryload != DONT_LOAD)
953 ptr->loaded = 1;
954 else
955 ptr = NULL;
956 }
Marc Boucher067477b2002-03-24 15:09:31 +0000957 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
958 exit_error(PARAMETER_PROBLEM,
959 "Couldn't find target `%s'\n", name);
960 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000961#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000962
Harald Welteae1ff9f2000-12-01 14:26:20 +0000963 if (ptr)
964 ptr->used = 1;
965
Marc Bouchere6869a82000-03-20 06:03:29 +0000966 return ptr;
967}
968
969static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000970merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000971 unsigned int *option_offset)
972{
973 unsigned int num_old, num_new, i;
974 struct option *merge;
975
976 for (num_old = 0; oldopts[num_old].name; num_old++);
977 for (num_new = 0; newopts[num_new].name; num_new++);
978
979 global_option_offset += OPTION_OFFSET;
980 *option_offset = global_option_offset;
981
982 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
983 memcpy(merge, oldopts, num_old * sizeof(struct option));
984 for (i = 0; i < num_new; i++) {
985 merge[num_old + i] = newopts[i];
986 merge[num_old + i].val += *option_offset;
987 }
988 memset(merge + num_old + num_new, 0, sizeof(struct option));
989
990 return merge;
991}
992
993void
994register_match(struct iptables_match *me)
995{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000996 struct iptables_match **i;
997
Marc Bouchere6869a82000-03-20 06:03:29 +0000998 if (strcmp(me->version, program_version) != 0) {
999 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1000 program_name, me->name, me->version, program_version);
1001 exit(1);
1002 }
1003
Rusty Russell52a51492000-05-02 16:44:29 +00001004 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001005 fprintf(stderr, "%s: match `%s' already registered.\n",
1006 program_name, me->name);
1007 exit(1);
1008 }
1009
Rusty Russell73f72f52000-07-03 10:17:57 +00001010 if (me->size != IPT_ALIGN(me->size)) {
1011 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1012 program_name, me->name, me->size);
1013 exit(1);
1014 }
1015
Rusty Russell9f60bbf2000-07-07 02:17:46 +00001016 /* Append to list. */
1017 for (i = &iptables_matches; *i; i = &(*i)->next);
1018 me->next = NULL;
1019 *i = me;
1020
Marc Bouchere6869a82000-03-20 06:03:29 +00001021 me->m = NULL;
1022 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001023}
1024
1025void
1026register_target(struct iptables_target *me)
1027{
1028 if (strcmp(me->version, program_version) != 0) {
1029 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1030 program_name, me->name, me->version, program_version);
1031 exit(1);
1032 }
1033
Rusty Russell52a51492000-05-02 16:44:29 +00001034 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001035 fprintf(stderr, "%s: target `%s' already registered.\n",
1036 program_name, me->name);
1037 exit(1);
1038 }
1039
Rusty Russell73f72f52000-07-03 10:17:57 +00001040 if (me->size != IPT_ALIGN(me->size)) {
1041 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1042 program_name, me->name, me->size);
1043 exit(1);
1044 }
1045
Marc Bouchere6869a82000-03-20 06:03:29 +00001046 /* Prepend to list. */
1047 me->next = iptables_targets;
1048 iptables_targets = me;
1049 me->t = NULL;
1050 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001051}
1052
1053static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001054print_num(u_int64_t number, unsigned int format)
1055{
1056 if (format & FMT_KILOMEGAGIGA) {
1057 if (number > 99999) {
1058 number = (number + 500) / 1000;
1059 if (number > 9999) {
1060 number = (number + 500) / 1000;
1061 if (number > 9999) {
1062 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001063 if (number > 9999) {
1064 number = (number + 500) / 1000;
1065 printf(FMT("%4lluT ","%lluT "), number);
1066 }
1067 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001068 }
1069 else printf(FMT("%4lluM ","%lluM "), number);
1070 } else
1071 printf(FMT("%4lluK ","%lluK "), number);
1072 } else
1073 printf(FMT("%5llu ","%llu "), number);
1074 } else
1075 printf(FMT("%8llu ","%llu "), number);
1076}
1077
1078
1079static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001080print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1081{
1082 struct ipt_counters counters;
1083 const char *pol = iptc_get_policy(chain, &counters, handle);
1084 printf("Chain %s", chain);
1085 if (pol) {
1086 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001087 if (!(format & FMT_NOCOUNTS)) {
1088 fputc(' ', stdout);
1089 print_num(counters.pcnt, (format|FMT_NOTABLE));
1090 fputs("packets, ", stdout);
1091 print_num(counters.bcnt, (format|FMT_NOTABLE));
1092 fputs("bytes", stdout);
1093 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001094 printf(")\n");
1095 } else {
1096 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001097 if (!iptc_get_references(&refs, chain, handle))
1098 printf(" (ERROR obtaining refs)\n");
1099 else
1100 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001101 }
1102
1103 if (format & FMT_LINENUMBERS)
1104 printf(FMT("%-4s ", "%s "), "num");
1105 if (!(format & FMT_NOCOUNTS)) {
1106 if (format & FMT_KILOMEGAGIGA) {
1107 printf(FMT("%5s ","%s "), "pkts");
1108 printf(FMT("%5s ","%s "), "bytes");
1109 } else {
1110 printf(FMT("%8s ","%s "), "pkts");
1111 printf(FMT("%10s ","%s "), "bytes");
1112 }
1113 }
1114 if (!(format & FMT_NOTARGET))
1115 printf(FMT("%-9s ","%s "), "target");
1116 fputs(" prot ", stdout);
1117 if (format & FMT_OPTIONS)
1118 fputs("opt", stdout);
1119 if (format & FMT_VIA) {
1120 printf(FMT(" %-6s ","%s "), "in");
1121 printf(FMT("%-6s ","%s "), "out");
1122 }
1123 printf(FMT(" %-19s ","%s "), "source");
1124 printf(FMT(" %-19s "," %s "), "destination");
1125 printf("\n");
1126}
1127
Marc Bouchere6869a82000-03-20 06:03:29 +00001128
1129static int
1130print_match(const struct ipt_entry_match *m,
1131 const struct ipt_ip *ip,
1132 int numeric)
1133{
Rusty Russell52a51492000-05-02 16:44:29 +00001134 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001135
1136 if (match) {
1137 if (match->print)
1138 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001139 else
Rusty Russellb039b022000-09-01 06:04:05 +00001140 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001141 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001142 if (m->u.user.name[0])
1143 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001144 }
1145 /* Don't stop iterating. */
1146 return 0;
1147}
1148
1149/* e is called `fw' here for hysterical raisins */
1150static void
1151print_firewall(const struct ipt_entry *fw,
1152 const char *targname,
1153 unsigned int num,
1154 unsigned int format,
1155 const iptc_handle_t handle)
1156{
1157 struct iptables_target *target = NULL;
1158 const struct ipt_entry_target *t;
1159 u_int8_t flags;
1160 char buf[BUFSIZ];
1161
Marc Bouchere6869a82000-03-20 06:03:29 +00001162 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001163 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001164 else
Rusty Russell52a51492000-05-02 16:44:29 +00001165 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001166
1167 t = ipt_get_target((struct ipt_entry *)fw);
1168 flags = fw->ip.flags;
1169
1170 if (format & FMT_LINENUMBERS)
1171 printf(FMT("%-4u ", "%u "), num+1);
1172
1173 if (!(format & FMT_NOCOUNTS)) {
1174 print_num(fw->counters.pcnt, format);
1175 print_num(fw->counters.bcnt, format);
1176 }
1177
1178 if (!(format & FMT_NOTARGET))
1179 printf(FMT("%-9s ", "%s "), targname);
1180
1181 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1182 {
Rusty Russell28381a42000-05-10 00:19:50 +00001183 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001184 if (pname)
1185 printf(FMT("%-5s", "%s "), pname);
1186 else
1187 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1188 }
1189
1190 if (format & FMT_OPTIONS) {
1191 if (format & FMT_NOTABLE)
1192 fputs("opt ", stdout);
1193 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1194 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1195 fputc(' ', stdout);
1196 }
1197
1198 if (format & FMT_VIA) {
1199 char iface[IFNAMSIZ+2];
1200
1201 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1202 iface[0] = '!';
1203 iface[1] = '\0';
1204 }
1205 else iface[0] = '\0';
1206
1207 if (fw->ip.iniface[0] != '\0') {
1208 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 }
1210 else if (format & FMT_NUMERIC) strcat(iface, "*");
1211 else strcat(iface, "any");
1212 printf(FMT(" %-6s ","in %s "), iface);
1213
1214 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1215 iface[0] = '!';
1216 iface[1] = '\0';
1217 }
1218 else iface[0] = '\0';
1219
1220 if (fw->ip.outiface[0] != '\0') {
1221 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001222 }
1223 else if (format & FMT_NUMERIC) strcat(iface, "*");
1224 else strcat(iface, "any");
1225 printf(FMT("%-6s ","out %s "), iface);
1226 }
1227
1228 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1229 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1230 printf(FMT("%-19s ","%s "), "anywhere");
1231 else {
1232 if (format & FMT_NUMERIC)
1233 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1234 else
1235 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1236 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1237 printf(FMT("%-19s ","%s "), buf);
1238 }
1239
1240 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1241 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1242 printf(FMT("%-19s","-> %s"), "anywhere");
1243 else {
1244 if (format & FMT_NUMERIC)
1245 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1246 else
1247 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1248 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1249 printf(FMT("%-19s","-> %s"), buf);
1250 }
1251
1252 if (format & FMT_NOTABLE)
1253 fputs(" ", stdout);
1254
1255 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1256
1257 if (target) {
1258 if (target->print)
1259 /* Print the target information. */
1260 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001261 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001262 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001263 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001264
1265 if (!(format & FMT_NONEWLINE))
1266 fputc('\n', stdout);
1267}
1268
1269static void
1270print_firewall_line(const struct ipt_entry *fw,
1271 const iptc_handle_t h)
1272{
1273 struct ipt_entry_target *t;
1274
1275 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001276 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001277}
1278
1279static int
1280append_entry(const ipt_chainlabel chain,
1281 struct ipt_entry *fw,
1282 unsigned int nsaddrs,
1283 const struct in_addr saddrs[],
1284 unsigned int ndaddrs,
1285 const struct in_addr daddrs[],
1286 int verbose,
1287 iptc_handle_t *handle)
1288{
1289 unsigned int i, j;
1290 int ret = 1;
1291
1292 for (i = 0; i < nsaddrs; i++) {
1293 fw->ip.src.s_addr = saddrs[i].s_addr;
1294 for (j = 0; j < ndaddrs; j++) {
1295 fw->ip.dst.s_addr = daddrs[j].s_addr;
1296 if (verbose)
1297 print_firewall_line(fw, *handle);
1298 ret &= iptc_append_entry(chain, fw, handle);
1299 }
1300 }
1301
1302 return ret;
1303}
1304
1305static int
1306replace_entry(const ipt_chainlabel chain,
1307 struct ipt_entry *fw,
1308 unsigned int rulenum,
1309 const struct in_addr *saddr,
1310 const struct in_addr *daddr,
1311 int verbose,
1312 iptc_handle_t *handle)
1313{
1314 fw->ip.src.s_addr = saddr->s_addr;
1315 fw->ip.dst.s_addr = daddr->s_addr;
1316
1317 if (verbose)
1318 print_firewall_line(fw, *handle);
1319 return iptc_replace_entry(chain, fw, rulenum, handle);
1320}
1321
1322static int
1323insert_entry(const ipt_chainlabel chain,
1324 struct ipt_entry *fw,
1325 unsigned int rulenum,
1326 unsigned int nsaddrs,
1327 const struct in_addr saddrs[],
1328 unsigned int ndaddrs,
1329 const struct in_addr daddrs[],
1330 int verbose,
1331 iptc_handle_t *handle)
1332{
1333 unsigned int i, j;
1334 int ret = 1;
1335
1336 for (i = 0; i < nsaddrs; i++) {
1337 fw->ip.src.s_addr = saddrs[i].s_addr;
1338 for (j = 0; j < ndaddrs; j++) {
1339 fw->ip.dst.s_addr = daddrs[j].s_addr;
1340 if (verbose)
1341 print_firewall_line(fw, *handle);
1342 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1343 }
1344 }
1345
1346 return ret;
1347}
1348
Rusty Russell2e0a3212000-04-19 11:23:18 +00001349static unsigned char *
1350make_delete_mask(struct ipt_entry *fw)
1351{
1352 /* Establish mask for comparison */
1353 unsigned int size;
1354 struct iptables_match *m;
1355 unsigned char *mask, *mptr;
1356
1357 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001358 for (m = iptables_matches; m; m = m->next) {
1359 if (!m->used)
1360 continue;
1361
Rusty Russell73f72f52000-07-03 10:17:57 +00001362 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001363 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001364
Rusty Russell9e1d2142000-04-23 09:11:12 +00001365 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001366 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001367 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001368
Rusty Russell9e1d2142000-04-23 09:11:12 +00001369 memset(mask, 0xFF, sizeof(struct ipt_entry));
1370 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001371
Sven Kochfb1279a2001-02-27 12:25:12 +00001372 for (m = iptables_matches; m; m = m->next) {
1373 if (!m->used)
1374 continue;
1375
Rusty Russell2e0a3212000-04-19 11:23:18 +00001376 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001377 IPT_ALIGN(sizeof(struct ipt_entry_match))
1378 + m->userspacesize);
1379 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001380 }
1381
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001382 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001383 IPT_ALIGN(sizeof(struct ipt_entry_target))
1384 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001385
1386 return mask;
1387}
1388
Marc Bouchere6869a82000-03-20 06:03:29 +00001389static int
1390delete_entry(const ipt_chainlabel chain,
1391 struct ipt_entry *fw,
1392 unsigned int nsaddrs,
1393 const struct in_addr saddrs[],
1394 unsigned int ndaddrs,
1395 const struct in_addr daddrs[],
1396 int verbose,
1397 iptc_handle_t *handle)
1398{
1399 unsigned int i, j;
1400 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001401 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001402
Rusty Russell2e0a3212000-04-19 11:23:18 +00001403 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001404 for (i = 0; i < nsaddrs; i++) {
1405 fw->ip.src.s_addr = saddrs[i].s_addr;
1406 for (j = 0; j < ndaddrs; j++) {
1407 fw->ip.dst.s_addr = daddrs[j].s_addr;
1408 if (verbose)
1409 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001410 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001411 }
1412 }
1413 return ret;
1414}
1415
Harald Welteae1ff9f2000-12-01 14:26:20 +00001416int
Marc Bouchere6869a82000-03-20 06:03:29 +00001417for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001418 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001419{
1420 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001421 const char *chain;
1422 char *chains;
1423 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001424
Rusty Russell9e1d2142000-04-23 09:11:12 +00001425 chain = iptc_first_chain(handle);
1426 while (chain) {
1427 chaincount++;
1428 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001429 }
1430
Rusty Russell9e1d2142000-04-23 09:11:12 +00001431 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1432 i = 0;
1433 chain = iptc_first_chain(handle);
1434 while (chain) {
1435 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1436 i++;
1437 chain = iptc_next_chain(handle);
1438 }
1439
1440 for (i = 0; i < chaincount; i++) {
1441 if (!builtinstoo
1442 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1443 *handle))
1444 continue;
1445 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1446 }
1447
1448 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001449 return ret;
1450}
1451
Harald Welteae1ff9f2000-12-01 14:26:20 +00001452int
Marc Bouchere6869a82000-03-20 06:03:29 +00001453flush_entries(const ipt_chainlabel chain, int verbose,
1454 iptc_handle_t *handle)
1455{
1456 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001457 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001458
1459 if (verbose)
1460 fprintf(stdout, "Flushing chain `%s'\n", chain);
1461 return iptc_flush_entries(chain, handle);
1462}
Marc Bouchere6869a82000-03-20 06:03:29 +00001463
1464static int
1465zero_entries(const ipt_chainlabel chain, int verbose,
1466 iptc_handle_t *handle)
1467{
1468 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001469 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001470
Marc Bouchere6869a82000-03-20 06:03:29 +00001471 if (verbose)
1472 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1473 return iptc_zero_entries(chain, handle);
1474}
1475
Harald Welteae1ff9f2000-12-01 14:26:20 +00001476int
Marc Bouchere6869a82000-03-20 06:03:29 +00001477delete_chain(const ipt_chainlabel chain, int verbose,
1478 iptc_handle_t *handle)
1479{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001480 if (!chain)
1481 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001482
1483 if (verbose)
1484 fprintf(stdout, "Deleting chain `%s'\n", chain);
1485 return iptc_delete_chain(chain, handle);
1486}
1487
1488static int
1489list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1490 int expanded, int linenumbers, iptc_handle_t *handle)
1491{
1492 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001493 unsigned int format;
1494 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001495
1496 format = FMT_OPTIONS;
1497 if (!verbose)
1498 format |= FMT_NOCOUNTS;
1499 else
1500 format |= FMT_VIA;
1501
1502 if (numeric)
1503 format |= FMT_NUMERIC;
1504
1505 if (!expanded)
1506 format |= FMT_KILOMEGAGIGA;
1507
1508 if (linenumbers)
1509 format |= FMT_LINENUMBERS;
1510
Rusty Russell9e1d2142000-04-23 09:11:12 +00001511 for (this = iptc_first_chain(handle);
1512 this;
1513 this = iptc_next_chain(handle)) {
1514 const struct ipt_entry *i;
1515 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001516
Marc Bouchere6869a82000-03-20 06:03:29 +00001517 if (chain && strcmp(chain, this) != 0)
1518 continue;
1519
1520 if (found) printf("\n");
1521
1522 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001523 i = iptc_first_rule(this, handle);
1524
1525 num = 0;
1526 while (i) {
1527 print_firewall(i,
1528 iptc_get_target(i, handle),
1529 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001530 format,
1531 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001532 i = iptc_next_rule(i, handle);
1533 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001534 found = 1;
1535 }
1536
1537 errno = ENOENT;
1538 return found;
1539}
1540
Harald Welte82dd2ec2000-12-19 05:18:15 +00001541static char *get_modprobe(void)
1542{
1543 int procfile;
1544 char *ret;
1545
1546 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1547 if (procfile < 0)
1548 return NULL;
1549
1550 ret = malloc(1024);
1551 if (ret) {
1552 switch (read(procfile, ret, 1024)) {
1553 case -1: goto fail;
1554 case 1024: goto fail; /* Partial read. Wierd */
1555 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001556 if (ret[strlen(ret)-1]=='\n')
1557 ret[strlen(ret)-1]=0;
1558 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001559 return ret;
1560 }
1561 fail:
1562 free(ret);
1563 close(procfile);
1564 return NULL;
1565}
1566
Harald Welte58918652001-06-16 18:25:25 +00001567int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001568{
1569 char *buf = NULL;
1570 char *argv[3];
1571
1572 /* If they don't explicitly set it, read out of kernel */
1573 if (!modprobe) {
1574 buf = get_modprobe();
1575 if (!buf)
1576 return -1;
1577 modprobe = buf;
1578 }
1579
1580 switch (fork()) {
1581 case 0:
1582 argv[0] = (char *)modprobe;
1583 argv[1] = (char *)modname;
1584 argv[2] = NULL;
1585 execv(argv[0], argv);
1586
1587 /* not usually reached */
1588 exit(0);
1589 case -1:
1590 return -1;
1591
1592 default: /* parent */
1593 wait(NULL);
1594 }
1595
1596 free(buf);
1597 return 0;
1598}
1599
Marc Bouchere6869a82000-03-20 06:03:29 +00001600static struct ipt_entry *
1601generate_entry(const struct ipt_entry *fw,
1602 struct iptables_match *matches,
1603 struct ipt_entry_target *target)
1604{
1605 unsigned int size;
1606 struct iptables_match *m;
1607 struct ipt_entry *e;
1608
1609 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001610 for (m = matches; m; m = m->next) {
1611 if (!m->used)
1612 continue;
1613
Rusty Russell228e98d2000-04-27 10:28:06 +00001614 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001615 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001616
Rusty Russell228e98d2000-04-27 10:28:06 +00001617 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001618 *e = *fw;
1619 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001620 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001621
1622 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001623 for (m = matches; m; m = m->next) {
1624 if (!m->used)
1625 continue;
1626
Rusty Russell228e98d2000-04-27 10:28:06 +00001627 memcpy(e->elems + size, m->m, m->m->u.match_size);
1628 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001629 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001630 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001631
1632 return e;
1633}
1634
1635int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1636{
1637 struct ipt_entry fw, *e = NULL;
1638 int invert = 0;
1639 unsigned int nsaddrs = 0, ndaddrs = 0;
1640 struct in_addr *saddrs = NULL, *daddrs = NULL;
1641
1642 int c, verbose = 0;
1643 const char *chain = NULL;
1644 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1645 const char *policy = NULL, *newname = NULL;
1646 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001647 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001648 int ret = 1;
1649 struct iptables_match *m;
1650 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001651 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 const char *jumpto = "";
1653 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001654 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001655
1656 memset(&fw, 0, sizeof(fw));
1657
Sven Kochfb1279a2001-02-27 12:25:12 +00001658 opts = original_opts;
1659 global_option_offset = 0;
1660
Harald Welteae1ff9f2000-12-01 14:26:20 +00001661 /* re-set optind to 0 in case do_command gets called
1662 * a second time */
1663 optind = 0;
1664
1665 /* clear mflags in case do_command gets called a second time
1666 * (we clear the global list of all matches for security)*/
1667 for (m = iptables_matches; m; m = m->next) {
1668 m->mflags = 0;
1669 m->used = 0;
1670 }
1671
1672 for (t = iptables_targets; t; t = t->next) {
1673 t->tflags = 0;
1674 t->used = 0;
1675 }
1676
Marc Bouchere6869a82000-03-20 06:03:29 +00001677 /* Suppress error messages: we may add new options if we
1678 demand-load a protocol. */
1679 opterr = 0;
1680
1681 while ((c = getopt_long(argc, argv,
Bart De Schuymerbe8ee532002-06-15 12:29:42 +00001682 "-A:C:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001683 opts, NULL)) != -1) {
1684 switch (c) {
1685 /*
1686 * Command selection
1687 */
1688 case 'A':
1689 add_command(&command, CMD_APPEND, CMD_NONE,
1690 invert);
1691 chain = optarg;
1692 break;
1693
1694 case 'D':
1695 add_command(&command, CMD_DELETE, CMD_NONE,
1696 invert);
1697 chain = optarg;
1698 if (optind < argc && argv[optind][0] != '-'
1699 && argv[optind][0] != '!') {
1700 rulenum = parse_rulenumber(argv[optind++]);
1701 command = CMD_DELETE_NUM;
1702 }
1703 break;
1704
Marc Bouchere6869a82000-03-20 06:03:29 +00001705 case 'R':
1706 add_command(&command, CMD_REPLACE, CMD_NONE,
1707 invert);
1708 chain = optarg;
1709 if (optind < argc && argv[optind][0] != '-'
1710 && argv[optind][0] != '!')
1711 rulenum = parse_rulenumber(argv[optind++]);
1712 else
1713 exit_error(PARAMETER_PROBLEM,
1714 "-%c requires a rule number",
1715 cmd2char(CMD_REPLACE));
1716 break;
1717
1718 case 'I':
1719 add_command(&command, CMD_INSERT, CMD_NONE,
1720 invert);
1721 chain = optarg;
1722 if (optind < argc && argv[optind][0] != '-'
1723 && argv[optind][0] != '!')
1724 rulenum = parse_rulenumber(argv[optind++]);
1725 else rulenum = 1;
1726 break;
1727
1728 case 'L':
1729 add_command(&command, CMD_LIST, CMD_ZERO,
1730 invert);
1731 if (optarg) chain = optarg;
1732 else if (optind < argc && argv[optind][0] != '-'
1733 && argv[optind][0] != '!')
1734 chain = argv[optind++];
1735 break;
1736
1737 case 'F':
1738 add_command(&command, CMD_FLUSH, CMD_NONE,
1739 invert);
1740 if (optarg) chain = optarg;
1741 else if (optind < argc && argv[optind][0] != '-'
1742 && argv[optind][0] != '!')
1743 chain = argv[optind++];
1744 break;
1745
1746 case 'Z':
1747 add_command(&command, CMD_ZERO, CMD_LIST,
1748 invert);
1749 if (optarg) chain = optarg;
1750 else if (optind < argc && argv[optind][0] != '-'
1751 && argv[optind][0] != '!')
1752 chain = argv[optind++];
1753 break;
1754
1755 case 'N':
Harald Welte6336bfd2002-05-07 14:41:43 +00001756 if (optarg && *optarg == '-')
1757 exit_error(PARAMETER_PROBLEM,
1758 "chain name not allowed to start "
1759 "with `-'\n");
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001760 if (find_target(optarg, TRY_LOAD))
1761 exit_error(PARAMETER_PROBLEM,
1762 "chain name may not clash "
1763 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1765 invert);
1766 chain = optarg;
1767 break;
1768
1769 case 'X':
1770 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1771 invert);
1772 if (optarg) chain = optarg;
1773 else if (optind < argc && argv[optind][0] != '-'
1774 && argv[optind][0] != '!')
1775 chain = argv[optind++];
1776 break;
1777
1778 case 'E':
1779 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1780 invert);
1781 chain = optarg;
1782 if (optind < argc && argv[optind][0] != '-'
1783 && argv[optind][0] != '!')
1784 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001785 else
1786 exit_error(PARAMETER_PROBLEM,
1787 "-%c requires old-chain-name and "
1788 "new-chain-name",
1789 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001790 break;
1791
1792 case 'P':
1793 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1794 invert);
1795 chain = optarg;
1796 if (optind < argc && argv[optind][0] != '-'
1797 && argv[optind][0] != '!')
1798 policy = argv[optind++];
1799 else
1800 exit_error(PARAMETER_PROBLEM,
1801 "-%c requires a chain and a policy",
1802 cmd2char(CMD_SET_POLICY));
1803 break;
1804
1805 case 'h':
1806 if (!optarg)
1807 optarg = argv[optind];
1808
Rusty Russell2e0a3212000-04-19 11:23:18 +00001809 /* iptables -p icmp -h */
1810 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001811 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001812
Marc Bouchere6869a82000-03-20 06:03:29 +00001813 exit_printhelp();
1814
1815 /*
1816 * Option selection
1817 */
1818 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001819 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001820 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1821 invert);
1822
1823 /* Canonicalize into lower case */
1824 for (protocol = argv[optind-1]; *protocol; protocol++)
1825 *protocol = tolower(*protocol);
1826
1827 protocol = argv[optind-1];
1828 fw.ip.proto = parse_protocol(protocol);
1829
1830 if (fw.ip.proto == 0
1831 && (fw.ip.invflags & IPT_INV_PROTO))
1832 exit_error(PARAMETER_PROBLEM,
1833 "rule would never match protocol");
1834 fw.nfcache |= NFC_IP_PROTO;
1835 break;
1836
1837 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001838 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001839 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1840 invert);
1841 shostnetworkmask = argv[optind-1];
1842 fw.nfcache |= NFC_IP_SRC;
1843 break;
1844
1845 case 'd':
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_DESTINATION, &fw.ip.invflags,
1848 invert);
1849 dhostnetworkmask = argv[optind-1];
1850 fw.nfcache |= NFC_IP_DST;
1851 break;
1852
1853 case 'j':
1854 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1855 invert);
1856 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001857 /* TRY_LOAD (may be chain name) */
1858 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001859
1860 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001861 size_t size;
1862
Rusty Russell73f72f52000-07-03 10:17:57 +00001863 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1864 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001865
Rusty Russell2e0a3212000-04-19 11:23:18 +00001866 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001867 target->t->u.target_size = size;
1868 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001869 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001870 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001871 }
1872 break;
1873
1874
1875 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001876 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001877 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1878 invert);
1879 parse_interface(argv[optind-1],
1880 fw.ip.iniface,
1881 fw.ip.iniface_mask);
1882 fw.nfcache |= NFC_IP_IF_IN;
1883 break;
1884
1885 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001886 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001887 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1888 invert);
1889 parse_interface(argv[optind-1],
1890 fw.ip.outiface,
1891 fw.ip.outiface_mask);
1892 fw.nfcache |= NFC_IP_IF_OUT;
1893 break;
1894
1895 case 'f':
1896 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1897 invert);
1898 fw.ip.flags |= IPT_F_FRAG;
1899 fw.nfcache |= NFC_IP_FRAG;
1900 break;
1901
1902 case 'v':
1903 if (!verbose)
1904 set_option(&options, OPT_VERBOSE,
1905 &fw.ip.invflags, invert);
1906 verbose++;
1907 break;
1908
Rusty Russell52a51492000-05-02 16:44:29 +00001909 case 'm': {
1910 size_t size;
1911
Marc Bouchere6869a82000-03-20 06:03:29 +00001912 if (invert)
1913 exit_error(PARAMETER_PROBLEM,
1914 "unexpected ! flag before --match");
1915
Rusty Russell52a51492000-05-02 16:44:29 +00001916 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001917 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1918 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001919 m->m = fw_calloc(1, size);
1920 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001921 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001922 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001923 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001924 }
1925 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001926
1927 case 'n':
1928 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1929 invert);
1930 break;
1931
1932 case 't':
1933 if (invert)
1934 exit_error(PARAMETER_PROBLEM,
1935 "unexpected ! flag before --table");
1936 *table = argv[optind-1];
1937 break;
1938
1939 case 'x':
1940 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1941 invert);
1942 break;
1943
1944 case 'V':
1945 if (invert)
1946 printf("Not %s ;-)\n", program_version);
1947 else
1948 printf("%s v%s\n",
1949 program_name, program_version);
1950 exit(0);
1951
1952 case '0':
1953 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1954 invert);
1955 break;
1956
Harald Welte82dd2ec2000-12-19 05:18:15 +00001957 case 'M':
1958 modprobe = optarg;
1959 break;
1960
Harald Welteccd49e52001-01-23 22:54:34 +00001961 case 'c':
1962
1963 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1964 invert);
1965 pcnt = optarg;
1966 if (optind < argc && argv[optind][0] != '-'
1967 && argv[optind][0] != '!')
1968 bcnt = argv[optind++];
1969 else
1970 exit_error(PARAMETER_PROBLEM,
1971 "-%c requires packet and byte counter",
1972 opt2char(OPT_COUNTERS));
1973
1974 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1975 exit_error(PARAMETER_PROBLEM,
1976 "-%c packet counter not numeric",
1977 opt2char(OPT_COUNTERS));
1978
1979 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1980 exit_error(PARAMETER_PROBLEM,
1981 "-%c byte counter not numeric",
1982 opt2char(OPT_COUNTERS));
1983
1984 break;
1985
1986
Marc Bouchere6869a82000-03-20 06:03:29 +00001987 case 1: /* non option */
1988 if (optarg[0] == '!' && optarg[1] == '\0') {
1989 if (invert)
1990 exit_error(PARAMETER_PROBLEM,
1991 "multiple consecutive ! not"
1992 " allowed");
1993 invert = TRUE;
1994 optarg[0] = '\0';
1995 continue;
1996 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00001997 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001998 exit_tryhelp(2);
1999
2000 default:
2001 /* FIXME: This scheme doesn't allow two of the same
2002 matches --RR */
2003 if (!target
2004 || !(target->parse(c - target->option_offset,
2005 argv, invert,
2006 &target->tflags,
2007 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002008 for (m = iptables_matches; m; m = m->next) {
2009 if (!m->used)
2010 continue;
2011
Marc Bouchere6869a82000-03-20 06:03:29 +00002012 if (m->parse(c - m->option_offset,
2013 argv, invert,
2014 &m->mflags,
2015 &fw,
2016 &fw.nfcache,
2017 &m->m))
2018 break;
2019 }
2020
2021 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00002022 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00002023 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00002024 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00002025 && (m = find_proto(protocol, TRY_LOAD,
2026 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00002028 size_t size;
2029
Rusty Russell73f72f52000-07-03 10:17:57 +00002030 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2031 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002032
Rusty Russell2e0a3212000-04-19 11:23:18 +00002033 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002034 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002035 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002036 m->init(m->m, &fw.nfcache);
2037
Sven Kochdbe857a2001-03-02 09:41:08 +00002038 opts = merge_options(opts,
2039 m->extra_opts, &m->option_offset);
2040
Marc Bouchere6869a82000-03-20 06:03:29 +00002041 optind--;
2042 continue;
2043 }
2044 if (!m)
2045 exit_error(PARAMETER_PROBLEM,
2046 "Unknown arg `%s'",
2047 argv[optind-1]);
2048 }
2049 }
2050 invert = FALSE;
2051 }
2052
Sven Kochfb1279a2001-02-27 12:25:12 +00002053 for (m = iptables_matches; m; m = m->next) {
2054 if (!m->used)
2055 continue;
2056
Marc Bouchere6869a82000-03-20 06:03:29 +00002057 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002058 }
2059
Marc Bouchere6869a82000-03-20 06:03:29 +00002060 if (target)
2061 target->final_check(target->tflags);
2062
2063 /* Fix me: must put inverse options checking here --MN */
2064
2065 if (optind < argc)
2066 exit_error(PARAMETER_PROBLEM,
2067 "unknown arguments found on commandline");
2068 if (!command)
2069 exit_error(PARAMETER_PROBLEM, "no command specified");
2070 if (invert)
2071 exit_error(PARAMETER_PROBLEM,
2072 "nothing appropriate following !");
2073
Harald Welte6336bfd2002-05-07 14:41:43 +00002074 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002075 if (!(options & OPT_DESTINATION))
2076 dhostnetworkmask = "0.0.0.0/0";
2077 if (!(options & OPT_SOURCE))
2078 shostnetworkmask = "0.0.0.0/0";
2079 }
2080
2081 if (shostnetworkmask)
2082 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2083 &(fw.ip.smsk), &nsaddrs);
2084
2085 if (dhostnetworkmask)
2086 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2087 &(fw.ip.dmsk), &ndaddrs);
2088
2089 if ((nsaddrs > 1 || ndaddrs > 1) &&
2090 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2091 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2092 " source or destination IP addresses");
2093
Marc Bouchere6869a82000-03-20 06:03:29 +00002094 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2095 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2096 "specify a unique address");
2097
2098 generic_opt_check(command, options);
2099
2100 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2101 exit_error(PARAMETER_PROBLEM,
2102 "chain name `%s' too long (must be under %i chars)",
2103 chain, IPT_FUNCTION_MAXNAMELEN);
2104
Harald Welteae1ff9f2000-12-01 14:26:20 +00002105 /* only allocate handle if we weren't called with a handle */
2106 if (!*handle)
2107 *handle = iptc_init(*table);
2108
Harald Welte82dd2ec2000-12-19 05:18:15 +00002109 if (!*handle) {
2110 /* try to insmod the module if iptc_init failed */
2111 iptables_insmod("ip_tables", modprobe);
2112 *handle = iptc_init(*table);
2113 }
2114
Marc Bouchere6869a82000-03-20 06:03:29 +00002115 if (!*handle)
2116 exit_error(VERSION_PROBLEM,
2117 "can't initialize iptables table `%s': %s",
2118 *table, iptc_strerror(errno));
2119
Harald Welte6336bfd2002-05-07 14:41:43 +00002120 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002121 || command == CMD_DELETE
2122 || command == CMD_INSERT
2123 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002124 if (strcmp(chain, "PREROUTING") == 0
2125 || strcmp(chain, "INPUT") == 0) {
2126 /* -o not valid with incoming packets. */
2127 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002128 exit_error(PARAMETER_PROBLEM,
2129 "Can't use -%c with %s\n",
2130 opt2char(OPT_VIANAMEOUT),
2131 chain);
2132 }
2133
Rusty Russella4860fd2000-06-17 16:13:02 +00002134 if (strcmp(chain, "POSTROUTING") == 0
2135 || strcmp(chain, "OUTPUT") == 0) {
2136 /* -i not valid with outgoing packets */
2137 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002138 exit_error(PARAMETER_PROBLEM,
2139 "Can't use -%c with %s\n",
2140 opt2char(OPT_VIANAMEIN),
2141 chain);
2142 }
2143
2144 if (target && iptc_is_chain(jumpto, *handle)) {
2145 printf("Warning: using chain %s, not extension\n",
2146 jumpto);
2147
2148 target = NULL;
2149 }
2150
2151 /* If they didn't specify a target, or it's a chain
2152 name, use standard. */
2153 if (!target
2154 && (strlen(jumpto) == 0
2155 || iptc_is_chain(jumpto, *handle))) {
2156 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002157
Rusty Russell52a51492000-05-02 16:44:29 +00002158 target = find_target(IPT_STANDARD_TARGET,
2159 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002160
2161 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002162 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002163 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002164 target->t->u.target_size = size;
2165 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002166 target->init(target->t, &fw.nfcache);
2167 }
2168
Rusty Russell7e53bf92000-03-20 07:03:28 +00002169 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002170 /* it is no chain, and we can't load a plugin.
2171 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002172 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002173 * chain. */
2174 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002175 } else {
2176 e = generate_entry(&fw, iptables_matches, target->t);
2177 }
2178 }
2179
2180 switch (command) {
2181 case CMD_APPEND:
2182 ret = append_entry(chain, e,
2183 nsaddrs, saddrs, ndaddrs, daddrs,
2184 options&OPT_VERBOSE,
2185 handle);
2186 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002187 case CMD_DELETE:
2188 ret = delete_entry(chain, e,
2189 nsaddrs, saddrs, ndaddrs, daddrs,
2190 options&OPT_VERBOSE,
2191 handle);
2192 break;
2193 case CMD_DELETE_NUM:
2194 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2195 break;
2196 case CMD_REPLACE:
2197 ret = replace_entry(chain, e, rulenum - 1,
2198 saddrs, daddrs, options&OPT_VERBOSE,
2199 handle);
2200 break;
2201 case CMD_INSERT:
2202 ret = insert_entry(chain, e, rulenum - 1,
2203 nsaddrs, saddrs, ndaddrs, daddrs,
2204 options&OPT_VERBOSE,
2205 handle);
2206 break;
2207 case CMD_LIST:
2208 ret = list_entries(chain,
2209 options&OPT_VERBOSE,
2210 options&OPT_NUMERIC,
2211 options&OPT_EXPANDED,
2212 options&OPT_LINENUMBERS,
2213 handle);
2214 break;
2215 case CMD_FLUSH:
2216 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2217 break;
2218 case CMD_ZERO:
2219 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2220 break;
2221 case CMD_LIST|CMD_ZERO:
2222 ret = list_entries(chain,
2223 options&OPT_VERBOSE,
2224 options&OPT_NUMERIC,
2225 options&OPT_EXPANDED,
2226 options&OPT_LINENUMBERS,
2227 handle);
2228 if (ret)
2229 ret = zero_entries(chain,
2230 options&OPT_VERBOSE, handle);
2231 break;
2232 case CMD_NEW_CHAIN:
2233 ret = iptc_create_chain(chain, handle);
2234 break;
2235 case CMD_DELETE_CHAIN:
2236 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2237 break;
2238 case CMD_RENAME_CHAIN:
2239 ret = iptc_rename_chain(chain, newname, handle);
2240 break;
2241 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002242 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002243 break;
2244 default:
2245 /* We should never reach this... */
2246 exit_tryhelp(2);
2247 }
2248
2249 if (verbose > 1)
2250 dump_entries(*handle);
2251
2252 return ret;
2253}