blob: 35adc2be6bcb7aebfc439e4c40a381874090ce24 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000031#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000032#include <iptables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000033#include <fcntl.h>
34#include <sys/wait.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035
36#ifndef TRUE
37#define TRUE 1
38#endif
39#ifndef FALSE
40#define FALSE 0
41#endif
42
43#ifndef IPT_LIB_DIR
44#define IPT_LIB_DIR "/usr/local/lib/iptables"
45#endif
46
Harald Welte82dd2ec2000-12-19 05:18:15 +000047#ifndef PROC_SYS_MODPROBE
48#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
49#endif
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051#define FMT_NUMERIC 0x0001
52#define FMT_NOCOUNTS 0x0002
53#define FMT_KILOMEGAGIGA 0x0004
54#define FMT_OPTIONS 0x0008
55#define FMT_NOTABLE 0x0010
56#define FMT_NOTARGET 0x0020
57#define FMT_VIA 0x0040
58#define FMT_NONEWLINE 0x0080
59#define FMT_LINENUMBERS 0x0100
60
61#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
62 | FMT_NUMERIC | FMT_NOTABLE)
63#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
64
65
66#define CMD_NONE 0x0000U
67#define CMD_INSERT 0x0001U
68#define CMD_DELETE 0x0002U
69#define CMD_DELETE_NUM 0x0004U
70#define CMD_REPLACE 0x0008U
71#define CMD_APPEND 0x0010U
72#define CMD_LIST 0x0020U
73#define CMD_FLUSH 0x0040U
74#define CMD_ZERO 0x0080U
75#define CMD_NEW_CHAIN 0x0100U
76#define CMD_DELETE_CHAIN 0x0200U
77#define CMD_SET_POLICY 0x0400U
78#define CMD_CHECK 0x0800U
79#define CMD_RENAME_CHAIN 0x1000U
80#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
82 'N', 'X', 'P', 'C', 'E' };
83
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Harald Welteccd49e52001-01-23 22:54:34 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '3', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
104 { "append", 1, 0, 'A' },
105 { "delete", 1, 0, 'D' },
106 { "insert", 1, 0, 'I' },
107 { "replace", 1, 0, 'R' },
108 { "list", 2, 0, 'L' },
109 { "flush", 2, 0, 'F' },
110 { "zero", 2, 0, 'Z' },
111 { "check", 1, 0, 'C' },
112 { "new-chain", 1, 0, 'N' },
113 { "delete-chain", 2, 0, 'X' },
114 { "rename-chain", 2, 0, 'E' },
115 { "policy", 1, 0, 'P' },
116 { "source", 1, 0, 's' },
117 { "destination", 1, 0, 'd' },
118 { "src", 1, 0, 's' }, /* synonym */
119 { "dst", 1, 0, 'd' }, /* synonym */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000120 { "protocol", 1, 0, 'p' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000121 { "in-interface", 1, 0, 'i' },
122 { "jump", 1, 0, 'j' },
123 { "table", 1, 0, 't' },
124 { "match", 1, 0, 'm' },
125 { "numeric", 0, 0, 'n' },
126 { "out-interface", 1, 0, 'o' },
127 { "verbose", 0, 0, 'v' },
128 { "exact", 0, 0, 'x' },
129 { "fragments", 0, 0, 'f' },
130 { "version", 0, 0, 'V' },
131 { "help", 2, 0, 'h' },
132 { "line-numbers", 0, 0, '0' },
Harald Welte82dd2ec2000-12-19 05:18:15 +0000133 { "modprobe", 1, 0, 'M' },
Harald Welteccd49e52001-01-23 22:54:34 +0000134 { "set-counters", 1, 0, 'c' },
Marc Bouchere6869a82000-03-20 06:03:29 +0000135 { 0 }
136};
137
Rusty Russell4e242f82000-05-31 06:33:50 +0000138#ifndef __OPTIMIZE__
Harald Welteae1ff9f2000-12-01 14:26:20 +0000139struct ipt_entry_target *
Rusty Russell9e1d2142000-04-23 09:11:12 +0000140ipt_get_target(struct ipt_entry *e)
141{
142 return (void *)e + e->target_offset;
143}
144#endif
145
Marc Bouchere6869a82000-03-20 06:03:29 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
161 /* -n -s -d -p -j -v -x -i -o -f --line */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x'},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' '},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Rusty Russella4860fd2000-06-17 16:13:02 +0000173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ',' ','x'},
Marc Bouchere6869a82000-03-20 06:03:29 +0000174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IPT_INV_SRCIP,
181/* -d */ IPT_INV_DSTIP,
182/* -p */ IPT_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IPT_INV_VIA_IN,
187/* -o */ IPT_INV_VIA_OUT,
188/* -f */ IPT_INV_FRAG,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
Rusty Russell2e0a3212000-04-19 11:23:18 +0000195/* Keeping track of external matches and targets: linked lists. */
Marc Bouchere6869a82000-03-20 06:03:29 +0000196struct iptables_match *iptables_matches = NULL;
197struct iptables_target *iptables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries(const iptc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000209/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000210/* defined in netinet/in.h */
211#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
223 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "all", 0 },
227};
228
229static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000230proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000231{
232 unsigned int i;
233
Rusty Russell28381a42000-05-10 00:19:50 +0000234 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247struct in_addr *
248dotted_to_addr(const char *dotted)
249{
250 static struct in_addr addr;
251 unsigned char *addrp;
252 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000253 unsigned int onebyte;
254 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000255 char buf[20];
256
257 /* copy dotted string, because we need to modify it */
258 strncpy(buf, dotted, sizeof(buf) - 1);
259 addrp = (unsigned char *) &(addr.s_addr);
260
261 p = buf;
262 for (i = 0; i < 3; i++) {
263 if ((q = strchr(p, '.')) == NULL)
264 return (struct in_addr *) NULL;
265
266 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000267 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000268 return (struct in_addr *) NULL;
269
270 addrp[i] = (unsigned char) onebyte;
271 p = q + 1;
272 }
273
274 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000275 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 return (struct in_addr *) NULL;
277
278 addrp[3] = (unsigned char) onebyte;
279
280 return &addr;
281}
282
283static struct in_addr *
284network_to_addr(const char *name)
285{
286 struct netent *net;
287 static struct in_addr addr;
288
289 if ((net = getnetbyname(name)) != NULL) {
290 if (net->n_addrtype != AF_INET)
291 return (struct in_addr *) NULL;
292 addr.s_addr = htonl((unsigned long) net->n_net);
293 return &addr;
294 }
295
296 return (struct in_addr *) NULL;
297}
298
299static void
300inaddrcpy(struct in_addr *dst, struct in_addr *src)
301{
302 /* memcpy(dst, src, sizeof(struct in_addr)); */
303 dst->s_addr = src->s_addr;
304}
305
306void
307exit_error(enum exittype status, char *msg, ...)
308{
309 va_list args;
310
311 va_start(args, msg);
312 fprintf(stderr, "%s v%s: ", program_name, program_version);
313 vfprintf(stderr, msg, args);
314 va_end(args);
315 fprintf(stderr, "\n");
316 if (status == PARAMETER_PROBLEM)
317 exit_tryhelp(status);
318 if (status == VERSION_PROBLEM)
319 fprintf(stderr,
320 "Perhaps iptables or your kernel needs to be upgraded.\n");
321 exit(status);
322}
323
324void
325exit_tryhelp(int status)
326{
327 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
328 program_name, program_name );
329 exit(status);
330}
331
332void
333exit_printhelp(void)
334{
Rusty Russell2e0a3212000-04-19 11:23:18 +0000335 struct iptables_match *m = NULL;
336 struct iptables_target *t = NULL;
337
Marc Bouchere6869a82000-03-20 06:03:29 +0000338 printf("%s v%s\n\n"
339"Usage: %s -[ADC] chain rule-specification [options]\n"
340" %s -[RI] chain rulenum rule-specification [options]\n"
341" %s -D chain rulenum [options]\n"
342" %s -[LFZ] [chain] [options]\n"
343" %s -[NX] chain\n"
344" %s -E old-chain-name new-chain-name\n"
345" %s -P chain target [options]\n"
346" %s -h (print this help information)\n\n",
347 program_name, program_version, program_name, program_name,
348 program_name, program_name, program_name, program_name,
349 program_name, program_name);
350
351 printf(
352"Commands:\n"
353"Either long or short options are allowed.\n"
354" --append -A chain Append to chain\n"
355" --delete -D chain Delete matching rule from chain\n"
356" --delete -D chain rulenum\n"
357" Delete rule rulenum (1 = first) from chain\n"
358" --insert -I chain [rulenum]\n"
359" Insert in chain as rulenum (default 1=first)\n"
360" --replace -R chain rulenum\n"
361" Replace rule rulenum (1 = first) in chain\n"
362" --list -L [chain] List the rules in a chain or all chains\n"
363" --flush -F [chain] Delete all rules in chain or all chains\n"
364" --zero -Z [chain] Zero counters in chain or all chains\n"
365" --check -C chain Test this packet on chain\n"
366" --new -N chain Create a new user-defined chain\n"
367" --delete-chain\n"
368" -X [chain] Delete a user-defined chain\n"
369" --policy -P chain target\n"
370" Change policy on chain to target\n"
371" --rename-chain\n"
372" -E old-chain new-chain\n"
373" Change chain name, (moving any references)\n"
374
375"Options:\n"
376" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
377" --source -s [!] address[/mask]\n"
378" source specification\n"
379" --destination -d [!] address[/mask]\n"
380" destination specification\n"
381" --in-interface -i [!] input name[+]\n"
382" network interface name ([+] for wildcard)\n"
383" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000384" target for rule (may load target extension)\n"
385" --match -m match\n"
386" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000387" --numeric -n numeric output of addresses and ports\n"
388" --out-interface -o [!] output name[+]\n"
389" network interface name ([+] for wildcard)\n"
390" --table -t table table to manipulate (default: `filter')\n"
391" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000392" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000393" --exact -x expand numbers (display exact values)\n"
394"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000395" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000396" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000397"[!] --version -V print package version.\n");
398
Rusty Russell363112d2000-08-11 13:49:26 +0000399 /* Print out any special helps. A user might like to be able
400 to add a --help to the commandline, and see expected
401 results. So we call help for all matches & targets */
Rusty Russell2e0a3212000-04-19 11:23:18 +0000402 for (t=iptables_targets;t;t=t->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000403 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000404 t->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000405 }
Rusty Russell2e0a3212000-04-19 11:23:18 +0000406 for (m=iptables_matches;m;m=m->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000407 printf("\n");
Rusty Russell2e0a3212000-04-19 11:23:18 +0000408 m->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000409 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000410 exit(0);
411}
412
413static void
414generic_opt_check(int command, int options)
415{
416 int i, j, legal = 0;
417
418 /* Check that commands are valid with options. Complicated by the
419 * fact that if an option is legal with *any* command given, it is
420 * legal overall (ie. -z and -l).
421 */
422 for (i = 0; i < NUMBER_OF_OPT; i++) {
423 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
424
425 for (j = 0; j < NUMBER_OF_CMD; j++) {
426 if (!(command & (1<<j)))
427 continue;
428
429 if (!(options & (1<<i))) {
430 if (commands_v_options[j][i] == '+')
431 exit_error(PARAMETER_PROBLEM,
432 "You need to supply the `-%c' "
433 "option for this command\n",
434 optflags[i]);
435 } else {
436 if (commands_v_options[j][i] != 'x')
437 legal = 1;
438 else if (legal == 0)
439 legal = -1;
440 }
441 }
442 if (legal == -1)
443 exit_error(PARAMETER_PROBLEM,
444 "Illegal option `-%c' with this command\n",
445 optflags[i]);
446 }
447}
448
449static char
450opt2char(int option)
451{
452 const char *ptr;
453 for (ptr = optflags; option > 1; option >>= 1, ptr++);
454
455 return *ptr;
456}
457
458static char
459cmd2char(int option)
460{
461 const char *ptr;
462 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
463
464 return *ptr;
465}
466
467static void
468add_command(int *cmd, const int newcmd, const int othercmds, int invert)
469{
470 if (invert)
471 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
472 if (*cmd & (~othercmds))
473 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
474 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
475 *cmd |= newcmd;
476}
477
478int
479check_inverse(const char option[], int *invert)
480{
481 if (option && strcmp(option, "!") == 0) {
482 if (*invert)
483 exit_error(PARAMETER_PROBLEM,
484 "Multiple `!' flags not allowed");
485
486 *invert = TRUE;
487 return TRUE;
488 }
489 return FALSE;
490}
491
492static void *
493fw_calloc(size_t count, size_t size)
494{
495 void *p;
496
497 if ((p = calloc(count, size)) == NULL) {
498 perror("iptables: calloc failed");
499 exit(1);
500 }
501 return p;
502}
503
504static void *
505fw_malloc(size_t size)
506{
507 void *p;
508
509 if ((p = malloc(size)) == NULL) {
510 perror("iptables: malloc failed");
511 exit(1);
512 }
513 return p;
514}
515
516static struct in_addr *
517host_to_addr(const char *name, unsigned int *naddr)
518{
519 struct hostent *host;
520 struct in_addr *addr;
521 unsigned int i;
522
523 *naddr = 0;
524 if ((host = gethostbyname(name)) != NULL) {
525 if (host->h_addrtype != AF_INET ||
526 host->h_length != sizeof(struct in_addr))
527 return (struct in_addr *) NULL;
528
529 while (host->h_addr_list[*naddr] != (char *) NULL)
530 (*naddr)++;
531 addr = fw_calloc(*naddr, sizeof(struct in_addr));
532 for (i = 0; i < *naddr; i++)
533 inaddrcpy(&(addr[i]),
534 (struct in_addr *) host->h_addr_list[i]);
535 return addr;
536 }
537
538 return (struct in_addr *) NULL;
539}
540
541static char *
542addr_to_host(const struct in_addr *addr)
543{
544 struct hostent *host;
545
546 if ((host = gethostbyaddr((char *) addr,
547 sizeof(struct in_addr), AF_INET)) != NULL)
548 return (char *) host->h_name;
549
550 return (char *) NULL;
551}
552
553/*
554 * All functions starting with "parse" should succeed, otherwise
555 * the program fails.
556 * Most routines return pointers to static data that may change
557 * between calls to the same or other routines with a few exceptions:
558 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
559 * return global static data.
560*/
561
562static struct in_addr *
563parse_hostnetwork(const char *name, unsigned int *naddrs)
564{
565 struct in_addr *addrp, *addrptmp;
566
567 if ((addrptmp = dotted_to_addr(name)) != NULL ||
568 (addrptmp = network_to_addr(name)) != NULL) {
569 addrp = fw_malloc(sizeof(struct in_addr));
570 inaddrcpy(addrp, addrptmp);
571 *naddrs = 1;
572 return addrp;
573 }
574 if ((addrp = host_to_addr(name, naddrs)) != NULL)
575 return addrp;
576
577 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
578}
579
580static struct in_addr *
581parse_mask(char *mask)
582{
583 static struct in_addr maskaddr;
584 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000585 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000586
587 if (mask == NULL) {
588 /* no mask at all defaults to 32 bits */
589 maskaddr.s_addr = 0xFFFFFFFF;
590 return &maskaddr;
591 }
592 if ((addrp = dotted_to_addr(mask)) != NULL)
593 /* dotted_to_addr already returns a network byte order addr */
594 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000595 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000596 exit_error(PARAMETER_PROBLEM,
597 "invalid mask `%s' specified", mask);
598 if (bits != 0) {
599 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
600 return &maskaddr;
601 }
602
603 maskaddr.s_addr = 0L;
604 return &maskaddr;
605}
606
607static void
608parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
609 struct in_addr *maskp, unsigned int *naddrs)
610{
611 struct in_addr *addrp;
612 char buf[256];
613 char *p;
614 int i, j, k, n;
615
616 strncpy(buf, name, sizeof(buf) - 1);
617 if ((p = strrchr(buf, '/')) != NULL) {
618 *p = '\0';
619 addrp = parse_mask(p + 1);
620 } else
621 addrp = parse_mask(NULL);
622 inaddrcpy(maskp, addrp);
623
624 /* if a null mask is given, the name is ignored, like in "any/0" */
625 if (maskp->s_addr == 0L)
626 strcpy(buf, "0.0.0.0");
627
628 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
629 n = *naddrs;
630 for (i = 0, j = 0; i < n; i++) {
631 addrp[j++].s_addr &= maskp->s_addr;
632 for (k = 0; k < j - 1; k++) {
633 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
634 (*naddrs)--;
635 j--;
636 break;
637 }
638 }
639 }
640}
641
642struct iptables_match *
Rusty Russell52a51492000-05-02 16:44:29 +0000643find_match(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000644{
645 struct iptables_match *ptr;
646
647 for (ptr = iptables_matches; ptr; ptr = ptr->next) {
648 if (strcmp(name, ptr->name) == 0)
649 break;
650 }
651
Harald Welte3efb6ea2001-08-06 18:50:21 +0000652#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000653 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000654 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
655 + strlen(name)];
656 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000657 if (dlopen(path, RTLD_NOW)) {
658 /* Found library. If it didn't register itself,
659 maybe they specified target as match. */
Rusty Russell52a51492000-05-02 16:44:29 +0000660 ptr = find_match(name, DONT_LOAD);
661
Rusty Russell9e1d2142000-04-23 09:11:12 +0000662 if (!ptr)
663 exit_error(PARAMETER_PROBLEM,
664 "Couldn't load match `%s'\n",
665 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000666 } else if (tryload == LOAD_MUST_SUCCEED)
667 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000668 "Couldn't load match `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000669 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000670 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000671#else
672 if (ptr && !ptr->loaded) {
673 if (tryload != DONT_LOAD)
674 ptr->loaded = 1;
675 else
676 ptr = NULL;
677 }
678#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000679
Harald Welteae1ff9f2000-12-01 14:26:20 +0000680 if (ptr)
681 ptr->used = 1;
682
Marc Bouchere6869a82000-03-20 06:03:29 +0000683 return ptr;
684}
685
Rusty Russell28381a42000-05-10 00:19:50 +0000686/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
687static struct iptables_match *
688find_proto(const char *pname, enum ipt_tryload tryload, int nolookup)
689{
Harald Welteed498492001-07-23 01:24:22 +0000690 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000691
Harald Welteed498492001-07-23 01:24:22 +0000692 if (string_to_number(pname, 0, 255, &proto) != -1)
Rusty Russell28381a42000-05-10 00:19:50 +0000693 return find_match(proto_to_name(proto, nolookup), tryload);
694
695 return find_match(pname, tryload);
696}
697
Marc Bouchere6869a82000-03-20 06:03:29 +0000698static u_int16_t
699parse_protocol(const char *s)
700{
Harald Welteed498492001-07-23 01:24:22 +0000701 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000702
Harald Welteed498492001-07-23 01:24:22 +0000703 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000704 struct protoent *pent;
705
706 if ((pent = getprotobyname(s)))
707 proto = pent->p_proto;
708 else {
709 unsigned int i;
710 for (i = 0;
711 i < sizeof(chain_protos)/sizeof(struct pprot);
712 i++) {
713 if (strcmp(s, chain_protos[i].name) == 0) {
714 proto = chain_protos[i].num;
715 break;
716 }
717 }
718 if (i == sizeof(chain_protos)/sizeof(struct pprot))
719 exit_error(PARAMETER_PROBLEM,
720 "unknown protocol `%s' specified",
721 s);
722 }
723 }
724
725 return (u_int16_t)proto;
726}
727
728static void
729parse_interface(const char *arg, char *vianame, unsigned char *mask)
730{
731 int vialen = strlen(arg);
732 unsigned int i;
733
734 memset(mask, 0, IFNAMSIZ);
735 memset(vianame, 0, IFNAMSIZ);
736
737 if (vialen + 1 > IFNAMSIZ)
738 exit_error(PARAMETER_PROBLEM,
739 "interface name `%s' must be shorter than IFNAMSIZ"
740 " (%i)", arg, IFNAMSIZ-1);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000741
Marc Bouchere6869a82000-03-20 06:03:29 +0000742 strcpy(vianame, arg);
743 if (vialen == 0)
744 memset(mask, 0, IFNAMSIZ);
745 else if (vianame[vialen - 1] == '+') {
746 memset(mask, 0xFF, vialen - 1);
747 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000748 /* Don't remove `+' here! -HW */
Marc Bouchere6869a82000-03-20 06:03:29 +0000749 } else {
750 /* Include nul-terminator in match */
751 memset(mask, 0xFF, vialen + 1);
752 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Weltede1578f2001-05-23 23:07:33 +0000753 for (i = 0; vianame[i]; i++) {
754 if (!isalnum(vianame[i]) && vianame[i] != '_') {
755 printf("Warning: wierd character in interface"
756 " `%s' (No aliases, :, ! or *).\n",
757 vianame);
758 break;
759 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000760 }
761 }
762}
763
764/* Can't be zero. */
765static int
766parse_rulenumber(const char *rule)
767{
Harald Welteed498492001-07-23 01:24:22 +0000768 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000769
Harald Welteed498492001-07-23 01:24:22 +0000770 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000771 exit_error(PARAMETER_PROBLEM,
772 "Invalid rule number `%s'", rule);
773
774 return rulenum;
775}
776
777static const char *
778parse_target(const char *targetname)
779{
780 const char *ptr;
781
782 if (strlen(targetname) < 1)
783 exit_error(PARAMETER_PROBLEM,
784 "Invalid target name (too short)");
785
786 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
787 exit_error(PARAMETER_PROBLEM,
788 "Invalid target name `%s' (%i chars max)",
789 targetname, sizeof(ipt_chainlabel)-1);
790
791 for (ptr = targetname; *ptr; ptr++)
792 if (isspace(*ptr))
793 exit_error(PARAMETER_PROBLEM,
794 "Invalid target name `%s'", targetname);
795 return targetname;
796}
797
798static char *
799addr_to_network(const struct in_addr *addr)
800{
801 struct netent *net;
802
803 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
804 return (char *) net->n_name;
805
806 return (char *) NULL;
807}
808
809char *
810addr_to_dotted(const struct in_addr *addrp)
811{
812 static char buf[20];
813 const unsigned char *bytep;
814
815 bytep = (const unsigned char *) &(addrp->s_addr);
816 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
817 return buf;
818}
819static char *
820addr_to_anyname(const struct in_addr *addr)
821{
822 char *name;
823
824 if ((name = addr_to_host(addr)) != NULL ||
825 (name = addr_to_network(addr)) != NULL)
826 return name;
827
828 return addr_to_dotted(addr);
829}
830
831static char *
832mask_to_dotted(const struct in_addr *mask)
833{
834 int i;
835 static char buf[20];
836 u_int32_t maskaddr, bits;
837
838 maskaddr = ntohl(mask->s_addr);
839
840 if (maskaddr == 0xFFFFFFFFL)
841 /* we don't want to see "/32" */
842 return "";
843
844 i = 32;
845 bits = 0xFFFFFFFEL;
846 while (--i >= 0 && maskaddr != bits)
847 bits <<= 1;
848 if (i >= 0)
849 sprintf(buf, "/%d", i);
850 else
851 /* mask was not a decent combination of 1's and 0's */
852 sprintf(buf, "/%s", addr_to_dotted(mask));
853
854 return buf;
855}
856
857int
Harald Welteed498492001-07-23 01:24:22 +0000858string_to_number(const char *s, unsigned int min, unsigned int max,
859 unsigned int *ret)
Marc Bouchere6869a82000-03-20 06:03:29 +0000860{
Jan Echternach5a1041d2000-08-26 04:44:39 +0000861 long number;
Marc Bouchere6869a82000-03-20 06:03:29 +0000862 char *end;
863
864 /* Handle hex, octal, etc. */
Jan Echternach5a1041d2000-08-26 04:44:39 +0000865 errno = 0;
866 number = strtol(s, &end, 0);
Marc Bouchere6869a82000-03-20 06:03:29 +0000867 if (*end == '\0' && end != s) {
868 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000869 if (errno != ERANGE && min <= number && number <= max) {
870 *ret = number;
871 return 0;
872 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000873 }
874 return -1;
875}
876
877static void
878set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
879 int invert)
880{
881 if (*options & option)
882 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
883 opt2char(option));
884 *options |= option;
885
886 if (invert) {
887 unsigned int i;
888 for (i = 0; 1 << i != option; i++);
889
890 if (!inverse_for_options[i])
891 exit_error(PARAMETER_PROBLEM,
892 "cannot have ! before -%c",
893 opt2char(option));
894 *invflg |= inverse_for_options[i];
895 }
896}
897
898struct iptables_target *
Rusty Russell52a51492000-05-02 16:44:29 +0000899find_target(const char *name, enum ipt_tryload tryload)
Marc Bouchere6869a82000-03-20 06:03:29 +0000900{
901 struct iptables_target *ptr;
902
903 /* Standard target? */
904 if (strcmp(name, "") == 0
905 || strcmp(name, IPTC_LABEL_ACCEPT) == 0
906 || strcmp(name, IPTC_LABEL_DROP) == 0
907 || strcmp(name, IPTC_LABEL_QUEUE) == 0
908 || strcmp(name, IPTC_LABEL_RETURN) == 0)
909 name = "standard";
910
911 for (ptr = iptables_targets; ptr; ptr = ptr->next) {
912 if (strcmp(name, ptr->name) == 0)
913 break;
914 }
915
Harald Welte3efb6ea2001-08-06 18:50:21 +0000916#ifndef NO_SHARED_LIBS
Rusty Russell52a51492000-05-02 16:44:29 +0000917 if (!ptr && tryload != DONT_LOAD) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000918 char path[sizeof(IPT_LIB_DIR) + sizeof("/libipt_.so")
919 + strlen(name)];
920 sprintf(path, IPT_LIB_DIR "/libipt_%s.so", name);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000921 if (dlopen(path, RTLD_NOW)) {
922 /* Found library. If it didn't register itself,
923 maybe they specified match as a target. */
Rusty Russell52a51492000-05-02 16:44:29 +0000924 ptr = find_target(name, DONT_LOAD);
Rusty Russell9e1d2142000-04-23 09:11:12 +0000925 if (!ptr)
926 exit_error(PARAMETER_PROBLEM,
927 "Couldn't load target `%s'\n",
928 name);
Rusty Russell52a51492000-05-02 16:44:29 +0000929 } else if (tryload == LOAD_MUST_SUCCEED)
930 exit_error(PARAMETER_PROBLEM,
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000931 "Couldn't load target `%s':%s\n",
Harald Welteaa204722000-08-11 14:02:27 +0000932 name, dlerror());
Marc Bouchere6869a82000-03-20 06:03:29 +0000933 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000934#else
935 if (ptr && !ptr->loaded) {
936 if (tryload != DONT_LOAD)
937 ptr->loaded = 1;
938 else
939 ptr = NULL;
940 }
941#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000942
Harald Welteae1ff9f2000-12-01 14:26:20 +0000943 if (ptr)
944 ptr->used = 1;
945
Marc Bouchere6869a82000-03-20 06:03:29 +0000946 return ptr;
947}
948
949static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000950merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000951 unsigned int *option_offset)
952{
953 unsigned int num_old, num_new, i;
954 struct option *merge;
955
956 for (num_old = 0; oldopts[num_old].name; num_old++);
957 for (num_new = 0; newopts[num_new].name; num_new++);
958
959 global_option_offset += OPTION_OFFSET;
960 *option_offset = global_option_offset;
961
962 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
963 memcpy(merge, oldopts, num_old * sizeof(struct option));
964 for (i = 0; i < num_new; i++) {
965 merge[num_old + i] = newopts[i];
966 merge[num_old + i].val += *option_offset;
967 }
968 memset(merge + num_old + num_new, 0, sizeof(struct option));
969
970 return merge;
971}
972
973void
974register_match(struct iptables_match *me)
975{
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000976 struct iptables_match **i;
977
Marc Bouchere6869a82000-03-20 06:03:29 +0000978 if (strcmp(me->version, program_version) != 0) {
979 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
980 program_name, me->name, me->version, program_version);
981 exit(1);
982 }
983
Rusty Russell52a51492000-05-02 16:44:29 +0000984 if (find_match(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000985 fprintf(stderr, "%s: match `%s' already registered.\n",
986 program_name, me->name);
987 exit(1);
988 }
989
Rusty Russell73f72f52000-07-03 10:17:57 +0000990 if (me->size != IPT_ALIGN(me->size)) {
991 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
992 program_name, me->name, me->size);
993 exit(1);
994 }
995
Rusty Russell9f60bbf2000-07-07 02:17:46 +0000996 /* Append to list. */
997 for (i = &iptables_matches; *i; i = &(*i)->next);
998 me->next = NULL;
999 *i = me;
1000
Marc Bouchere6869a82000-03-20 06:03:29 +00001001 me->m = NULL;
1002 me->mflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001003}
1004
1005void
1006register_target(struct iptables_target *me)
1007{
1008 if (strcmp(me->version, program_version) != 0) {
1009 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1010 program_name, me->name, me->version, program_version);
1011 exit(1);
1012 }
1013
Rusty Russell52a51492000-05-02 16:44:29 +00001014 if (find_target(me->name, DONT_LOAD)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001015 fprintf(stderr, "%s: target `%s' already registered.\n",
1016 program_name, me->name);
1017 exit(1);
1018 }
1019
Rusty Russell73f72f52000-07-03 10:17:57 +00001020 if (me->size != IPT_ALIGN(me->size)) {
1021 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1022 program_name, me->name, me->size);
1023 exit(1);
1024 }
1025
Marc Bouchere6869a82000-03-20 06:03:29 +00001026 /* Prepend to list. */
1027 me->next = iptables_targets;
1028 iptables_targets = me;
1029 me->t = NULL;
1030 me->tflags = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001031}
1032
1033static void
Harald Weltea0b4f792001-03-25 19:55:04 +00001034print_num(u_int64_t number, unsigned int format)
1035{
1036 if (format & FMT_KILOMEGAGIGA) {
1037 if (number > 99999) {
1038 number = (number + 500) / 1000;
1039 if (number > 9999) {
1040 number = (number + 500) / 1000;
1041 if (number > 9999) {
1042 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +00001043 if (number > 9999) {
1044 number = (number + 500) / 1000;
1045 printf(FMT("%4lluT ","%lluT "), number);
1046 }
1047 else printf(FMT("%4lluG ","%lluG "), number);
Harald Weltea0b4f792001-03-25 19:55:04 +00001048 }
1049 else printf(FMT("%4lluM ","%lluM "), number);
1050 } else
1051 printf(FMT("%4lluK ","%lluK "), number);
1052 } else
1053 printf(FMT("%5llu ","%llu "), number);
1054 } else
1055 printf(FMT("%8llu ","%llu "), number);
1056}
1057
1058
1059static void
Marc Bouchere6869a82000-03-20 06:03:29 +00001060print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
1061{
1062 struct ipt_counters counters;
1063 const char *pol = iptc_get_policy(chain, &counters, handle);
1064 printf("Chain %s", chain);
1065 if (pol) {
1066 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +00001067 if (!(format & FMT_NOCOUNTS)) {
1068 fputc(' ', stdout);
1069 print_num(counters.pcnt, (format|FMT_NOTABLE));
1070 fputs("packets, ", stdout);
1071 print_num(counters.bcnt, (format|FMT_NOTABLE));
1072 fputs("bytes", stdout);
1073 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001074 printf(")\n");
1075 } else {
1076 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001077 if (!iptc_get_references(&refs, chain, handle))
1078 printf(" (ERROR obtaining refs)\n");
1079 else
1080 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001081 }
1082
1083 if (format & FMT_LINENUMBERS)
1084 printf(FMT("%-4s ", "%s "), "num");
1085 if (!(format & FMT_NOCOUNTS)) {
1086 if (format & FMT_KILOMEGAGIGA) {
1087 printf(FMT("%5s ","%s "), "pkts");
1088 printf(FMT("%5s ","%s "), "bytes");
1089 } else {
1090 printf(FMT("%8s ","%s "), "pkts");
1091 printf(FMT("%10s ","%s "), "bytes");
1092 }
1093 }
1094 if (!(format & FMT_NOTARGET))
1095 printf(FMT("%-9s ","%s "), "target");
1096 fputs(" prot ", stdout);
1097 if (format & FMT_OPTIONS)
1098 fputs("opt", stdout);
1099 if (format & FMT_VIA) {
1100 printf(FMT(" %-6s ","%s "), "in");
1101 printf(FMT("%-6s ","%s "), "out");
1102 }
1103 printf(FMT(" %-19s ","%s "), "source");
1104 printf(FMT(" %-19s "," %s "), "destination");
1105 printf("\n");
1106}
1107
Marc Bouchere6869a82000-03-20 06:03:29 +00001108
1109static int
1110print_match(const struct ipt_entry_match *m,
1111 const struct ipt_ip *ip,
1112 int numeric)
1113{
Rusty Russell52a51492000-05-02 16:44:29 +00001114 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001115
1116 if (match) {
1117 if (match->print)
1118 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +00001119 else
Rusty Russellb039b022000-09-01 06:04:05 +00001120 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001121 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +00001122 if (m->u.user.name[0])
1123 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +00001124 }
1125 /* Don't stop iterating. */
1126 return 0;
1127}
1128
1129/* e is called `fw' here for hysterical raisins */
1130static void
1131print_firewall(const struct ipt_entry *fw,
1132 const char *targname,
1133 unsigned int num,
1134 unsigned int format,
1135 const iptc_handle_t handle)
1136{
1137 struct iptables_target *target = NULL;
1138 const struct ipt_entry_target *t;
1139 u_int8_t flags;
1140 char buf[BUFSIZ];
1141
1142 /* User creates a chain called "REJECT": this overrides the
1143 `REJECT' target module. Keep feeding them rope until the
1144 revolution... Bwahahahahah */
1145 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001146 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001147 else
Rusty Russell52a51492000-05-02 16:44:29 +00001148 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001149
1150 t = ipt_get_target((struct ipt_entry *)fw);
1151 flags = fw->ip.flags;
1152
1153 if (format & FMT_LINENUMBERS)
1154 printf(FMT("%-4u ", "%u "), num+1);
1155
1156 if (!(format & FMT_NOCOUNTS)) {
1157 print_num(fw->counters.pcnt, format);
1158 print_num(fw->counters.bcnt, format);
1159 }
1160
1161 if (!(format & FMT_NOTARGET))
1162 printf(FMT("%-9s ", "%s "), targname);
1163
1164 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1165 {
Rusty Russell28381a42000-05-10 00:19:50 +00001166 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001167 if (pname)
1168 printf(FMT("%-5s", "%s "), pname);
1169 else
1170 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1171 }
1172
1173 if (format & FMT_OPTIONS) {
1174 if (format & FMT_NOTABLE)
1175 fputs("opt ", stdout);
1176 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1177 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1178 fputc(' ', stdout);
1179 }
1180
1181 if (format & FMT_VIA) {
1182 char iface[IFNAMSIZ+2];
1183
1184 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1185 iface[0] = '!';
1186 iface[1] = '\0';
1187 }
1188 else iface[0] = '\0';
1189
1190 if (fw->ip.iniface[0] != '\0') {
1191 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001192 }
1193 else if (format & FMT_NUMERIC) strcat(iface, "*");
1194 else strcat(iface, "any");
1195 printf(FMT(" %-6s ","in %s "), iface);
1196
1197 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1198 iface[0] = '!';
1199 iface[1] = '\0';
1200 }
1201 else iface[0] = '\0';
1202
1203 if (fw->ip.outiface[0] != '\0') {
1204 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001205 }
1206 else if (format & FMT_NUMERIC) strcat(iface, "*");
1207 else strcat(iface, "any");
1208 printf(FMT("%-6s ","out %s "), iface);
1209 }
1210
1211 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1212 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1213 printf(FMT("%-19s ","%s "), "anywhere");
1214 else {
1215 if (format & FMT_NUMERIC)
1216 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1217 else
1218 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1219 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1220 printf(FMT("%-19s ","%s "), buf);
1221 }
1222
1223 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1224 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
1225 printf(FMT("%-19s","-> %s"), "anywhere");
1226 else {
1227 if (format & FMT_NUMERIC)
1228 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1229 else
1230 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1231 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
1232 printf(FMT("%-19s","-> %s"), buf);
1233 }
1234
1235 if (format & FMT_NOTABLE)
1236 fputs(" ", stdout);
1237
1238 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1239
1240 if (target) {
1241 if (target->print)
1242 /* Print the target information. */
1243 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001244 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001245 printf("[%u bytes of unknown target data] ",
Rusty Russell228e98d2000-04-27 10:28:06 +00001246 t->u.target_size - sizeof(*t));
Marc Bouchere6869a82000-03-20 06:03:29 +00001247
1248 if (!(format & FMT_NONEWLINE))
1249 fputc('\n', stdout);
1250}
1251
1252static void
1253print_firewall_line(const struct ipt_entry *fw,
1254 const iptc_handle_t h)
1255{
1256 struct ipt_entry_target *t;
1257
1258 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001259 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001260}
1261
1262static int
1263append_entry(const ipt_chainlabel chain,
1264 struct ipt_entry *fw,
1265 unsigned int nsaddrs,
1266 const struct in_addr saddrs[],
1267 unsigned int ndaddrs,
1268 const struct in_addr daddrs[],
1269 int verbose,
1270 iptc_handle_t *handle)
1271{
1272 unsigned int i, j;
1273 int ret = 1;
1274
1275 for (i = 0; i < nsaddrs; i++) {
1276 fw->ip.src.s_addr = saddrs[i].s_addr;
1277 for (j = 0; j < ndaddrs; j++) {
1278 fw->ip.dst.s_addr = daddrs[j].s_addr;
1279 if (verbose)
1280 print_firewall_line(fw, *handle);
1281 ret &= iptc_append_entry(chain, fw, handle);
1282 }
1283 }
1284
1285 return ret;
1286}
1287
1288static int
1289replace_entry(const ipt_chainlabel chain,
1290 struct ipt_entry *fw,
1291 unsigned int rulenum,
1292 const struct in_addr *saddr,
1293 const struct in_addr *daddr,
1294 int verbose,
1295 iptc_handle_t *handle)
1296{
1297 fw->ip.src.s_addr = saddr->s_addr;
1298 fw->ip.dst.s_addr = daddr->s_addr;
1299
1300 if (verbose)
1301 print_firewall_line(fw, *handle);
1302 return iptc_replace_entry(chain, fw, rulenum, handle);
1303}
1304
1305static int
1306insert_entry(const ipt_chainlabel chain,
1307 struct ipt_entry *fw,
1308 unsigned int rulenum,
1309 unsigned int nsaddrs,
1310 const struct in_addr saddrs[],
1311 unsigned int ndaddrs,
1312 const struct in_addr daddrs[],
1313 int verbose,
1314 iptc_handle_t *handle)
1315{
1316 unsigned int i, j;
1317 int ret = 1;
1318
1319 for (i = 0; i < nsaddrs; i++) {
1320 fw->ip.src.s_addr = saddrs[i].s_addr;
1321 for (j = 0; j < ndaddrs; j++) {
1322 fw->ip.dst.s_addr = daddrs[j].s_addr;
1323 if (verbose)
1324 print_firewall_line(fw, *handle);
1325 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1326 }
1327 }
1328
1329 return ret;
1330}
1331
Rusty Russell2e0a3212000-04-19 11:23:18 +00001332static unsigned char *
1333make_delete_mask(struct ipt_entry *fw)
1334{
1335 /* Establish mask for comparison */
1336 unsigned int size;
1337 struct iptables_match *m;
1338 unsigned char *mask, *mptr;
1339
1340 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001341 for (m = iptables_matches; m; m = m->next) {
1342 if (!m->used)
1343 continue;
1344
Rusty Russell73f72f52000-07-03 10:17:57 +00001345 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001346 }
Rusty Russell2e0a3212000-04-19 11:23:18 +00001347
Rusty Russell9e1d2142000-04-23 09:11:12 +00001348 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001349 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Rusty Russell9e1d2142000-04-23 09:11:12 +00001350 + iptables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001351
Rusty Russell9e1d2142000-04-23 09:11:12 +00001352 memset(mask, 0xFF, sizeof(struct ipt_entry));
1353 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001354
Sven Kochfb1279a2001-02-27 12:25:12 +00001355 for (m = iptables_matches; m; m = m->next) {
1356 if (!m->used)
1357 continue;
1358
Rusty Russell2e0a3212000-04-19 11:23:18 +00001359 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001360 IPT_ALIGN(sizeof(struct ipt_entry_match))
1361 + m->userspacesize);
1362 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001363 }
1364
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001365 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001366 IPT_ALIGN(sizeof(struct ipt_entry_target))
1367 + iptables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001368
1369 return mask;
1370}
1371
Marc Bouchere6869a82000-03-20 06:03:29 +00001372static int
1373delete_entry(const ipt_chainlabel chain,
1374 struct ipt_entry *fw,
1375 unsigned int nsaddrs,
1376 const struct in_addr saddrs[],
1377 unsigned int ndaddrs,
1378 const struct in_addr daddrs[],
1379 int verbose,
1380 iptc_handle_t *handle)
1381{
1382 unsigned int i, j;
1383 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001384 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001385
Rusty Russell2e0a3212000-04-19 11:23:18 +00001386 mask = make_delete_mask(fw);
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 for (i = 0; i < nsaddrs; i++) {
1388 fw->ip.src.s_addr = saddrs[i].s_addr;
1389 for (j = 0; j < ndaddrs; j++) {
1390 fw->ip.dst.s_addr = daddrs[j].s_addr;
1391 if (verbose)
1392 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001393 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001394 }
1395 }
1396 return ret;
1397}
1398
1399static int
1400check_packet(const ipt_chainlabel chain,
1401 struct ipt_entry *fw,
1402 unsigned int nsaddrs,
1403 const struct in_addr saddrs[],
1404 unsigned int ndaddrs,
1405 const struct in_addr daddrs[],
1406 int verbose,
1407 iptc_handle_t *handle)
1408{
1409 int ret = 1;
1410 unsigned int i, j;
1411 const char *msg;
1412
1413 for (i = 0; i < nsaddrs; i++) {
1414 fw->ip.src.s_addr = saddrs[i].s_addr;
1415 for (j = 0; j < ndaddrs; j++) {
1416 fw->ip.dst.s_addr = daddrs[j].s_addr;
1417 if (verbose)
1418 print_firewall_line(fw, *handle);
1419 msg = iptc_check_packet(chain, fw, handle);
1420 if (!msg) ret = 0;
1421 else printf("%s\n", msg);
1422 }
1423 }
1424
1425 return ret;
1426}
1427
Harald Welteae1ff9f2000-12-01 14:26:20 +00001428int
Marc Bouchere6869a82000-03-20 06:03:29 +00001429for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001430 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001431{
1432 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001433 const char *chain;
1434 char *chains;
1435 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001436
Rusty Russell9e1d2142000-04-23 09:11:12 +00001437 chain = iptc_first_chain(handle);
1438 while (chain) {
1439 chaincount++;
1440 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001441 }
1442
Rusty Russell9e1d2142000-04-23 09:11:12 +00001443 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1444 i = 0;
1445 chain = iptc_first_chain(handle);
1446 while (chain) {
1447 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1448 i++;
1449 chain = iptc_next_chain(handle);
1450 }
1451
1452 for (i = 0; i < chaincount; i++) {
1453 if (!builtinstoo
1454 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
1455 *handle))
1456 continue;
1457 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1458 }
1459
1460 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001461 return ret;
1462}
1463
Harald Welteae1ff9f2000-12-01 14:26:20 +00001464int
Marc Bouchere6869a82000-03-20 06:03:29 +00001465flush_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(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001470
1471 if (verbose)
1472 fprintf(stdout, "Flushing chain `%s'\n", chain);
1473 return iptc_flush_entries(chain, handle);
1474}
Marc Bouchere6869a82000-03-20 06:03:29 +00001475
1476static int
1477zero_entries(const ipt_chainlabel chain, int verbose,
1478 iptc_handle_t *handle)
1479{
1480 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001481 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001482
Marc Bouchere6869a82000-03-20 06:03:29 +00001483 if (verbose)
1484 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1485 return iptc_zero_entries(chain, handle);
1486}
1487
Harald Welteae1ff9f2000-12-01 14:26:20 +00001488int
Marc Bouchere6869a82000-03-20 06:03:29 +00001489delete_chain(const ipt_chainlabel chain, int verbose,
1490 iptc_handle_t *handle)
1491{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001492 if (!chain)
1493 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001494
1495 if (verbose)
1496 fprintf(stdout, "Deleting chain `%s'\n", chain);
1497 return iptc_delete_chain(chain, handle);
1498}
1499
1500static int
1501list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1502 int expanded, int linenumbers, iptc_handle_t *handle)
1503{
1504 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001505 unsigned int format;
1506 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001507
1508 format = FMT_OPTIONS;
1509 if (!verbose)
1510 format |= FMT_NOCOUNTS;
1511 else
1512 format |= FMT_VIA;
1513
1514 if (numeric)
1515 format |= FMT_NUMERIC;
1516
1517 if (!expanded)
1518 format |= FMT_KILOMEGAGIGA;
1519
1520 if (linenumbers)
1521 format |= FMT_LINENUMBERS;
1522
Rusty Russell9e1d2142000-04-23 09:11:12 +00001523 for (this = iptc_first_chain(handle);
1524 this;
1525 this = iptc_next_chain(handle)) {
1526 const struct ipt_entry *i;
1527 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001528
Marc Bouchere6869a82000-03-20 06:03:29 +00001529 if (chain && strcmp(chain, this) != 0)
1530 continue;
1531
1532 if (found) printf("\n");
1533
1534 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001535 i = iptc_first_rule(this, handle);
1536
1537 num = 0;
1538 while (i) {
1539 print_firewall(i,
1540 iptc_get_target(i, handle),
1541 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001542 format,
1543 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001544 i = iptc_next_rule(i, handle);
1545 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001546 found = 1;
1547 }
1548
1549 errno = ENOENT;
1550 return found;
1551}
1552
Harald Welte82dd2ec2000-12-19 05:18:15 +00001553static char *get_modprobe(void)
1554{
1555 int procfile;
1556 char *ret;
1557
1558 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1559 if (procfile < 0)
1560 return NULL;
1561
1562 ret = malloc(1024);
1563 if (ret) {
1564 switch (read(procfile, ret, 1024)) {
1565 case -1: goto fail;
1566 case 1024: goto fail; /* Partial read. Wierd */
1567 }
Rusty Russell8cc887b2001-02-09 02:16:02 +00001568 if (ret[strlen(ret)-1]=='\n')
1569 ret[strlen(ret)-1]=0;
1570 close(procfile);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001571 return ret;
1572 }
1573 fail:
1574 free(ret);
1575 close(procfile);
1576 return NULL;
1577}
1578
Harald Welte58918652001-06-16 18:25:25 +00001579int iptables_insmod(const char *modname, const char *modprobe)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001580{
1581 char *buf = NULL;
1582 char *argv[3];
1583
1584 /* If they don't explicitly set it, read out of kernel */
1585 if (!modprobe) {
1586 buf = get_modprobe();
1587 if (!buf)
1588 return -1;
1589 modprobe = buf;
1590 }
1591
1592 switch (fork()) {
1593 case 0:
1594 argv[0] = (char *)modprobe;
1595 argv[1] = (char *)modname;
1596 argv[2] = NULL;
1597 execv(argv[0], argv);
1598
1599 /* not usually reached */
1600 exit(0);
1601 case -1:
1602 return -1;
1603
1604 default: /* parent */
1605 wait(NULL);
1606 }
1607
1608 free(buf);
1609 return 0;
1610}
1611
Marc Bouchere6869a82000-03-20 06:03:29 +00001612static struct ipt_entry *
1613generate_entry(const struct ipt_entry *fw,
1614 struct iptables_match *matches,
1615 struct ipt_entry_target *target)
1616{
1617 unsigned int size;
1618 struct iptables_match *m;
1619 struct ipt_entry *e;
1620
1621 size = sizeof(struct ipt_entry);
Sven Kochfb1279a2001-02-27 12:25:12 +00001622 for (m = matches; m; m = m->next) {
1623 if (!m->used)
1624 continue;
1625
Rusty Russell228e98d2000-04-27 10:28:06 +00001626 size += m->m->u.match_size;
Sven Kochfb1279a2001-02-27 12:25:12 +00001627 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001628
Rusty Russell228e98d2000-04-27 10:28:06 +00001629 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001630 *e = *fw;
1631 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001632 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001633
1634 size = 0;
Sven Kochfb1279a2001-02-27 12:25:12 +00001635 for (m = matches; m; m = m->next) {
1636 if (!m->used)
1637 continue;
1638
Rusty Russell228e98d2000-04-27 10:28:06 +00001639 memcpy(e->elems + size, m->m, m->m->u.match_size);
1640 size += m->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001641 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001642 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001643
1644 return e;
1645}
1646
1647int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1648{
1649 struct ipt_entry fw, *e = NULL;
1650 int invert = 0;
1651 unsigned int nsaddrs = 0, ndaddrs = 0;
1652 struct in_addr *saddrs = NULL, *daddrs = NULL;
1653
1654 int c, verbose = 0;
1655 const char *chain = NULL;
1656 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1657 const char *policy = NULL, *newname = NULL;
1658 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001659 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001660 int ret = 1;
1661 struct iptables_match *m;
1662 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001663 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 const char *jumpto = "";
1665 char *protocol = NULL;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001666 const char *modprobe = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001667
1668 memset(&fw, 0, sizeof(fw));
1669
Sven Kochfb1279a2001-02-27 12:25:12 +00001670 opts = original_opts;
1671 global_option_offset = 0;
1672
Harald Welteae1ff9f2000-12-01 14:26:20 +00001673 /* re-set optind to 0 in case do_command gets called
1674 * a second time */
1675 optind = 0;
1676
1677 /* clear mflags in case do_command gets called a second time
1678 * (we clear the global list of all matches for security)*/
1679 for (m = iptables_matches; m; m = m->next) {
1680 m->mflags = 0;
1681 m->used = 0;
1682 }
1683
1684 for (t = iptables_targets; t; t = t->next) {
1685 t->tflags = 0;
1686 t->used = 0;
1687 }
1688
Marc Bouchere6869a82000-03-20 06:03:29 +00001689 /* Suppress error messages: we may add new options if we
1690 demand-load a protocol. */
1691 opterr = 0;
1692
1693 while ((c = getopt_long(argc, argv,
Harald Welteccd49e52001-01-23 22:54:34 +00001694 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001695 opts, NULL)) != -1) {
1696 switch (c) {
1697 /*
1698 * Command selection
1699 */
1700 case 'A':
1701 add_command(&command, CMD_APPEND, CMD_NONE,
1702 invert);
1703 chain = optarg;
1704 break;
1705
1706 case 'D':
1707 add_command(&command, CMD_DELETE, CMD_NONE,
1708 invert);
1709 chain = optarg;
1710 if (optind < argc && argv[optind][0] != '-'
1711 && argv[optind][0] != '!') {
1712 rulenum = parse_rulenumber(argv[optind++]);
1713 command = CMD_DELETE_NUM;
1714 }
1715 break;
1716
1717 case 'C':
1718 add_command(&command, CMD_CHECK, CMD_NONE,
1719 invert);
1720 chain = optarg;
1721 break;
1722
1723 case 'R':
1724 add_command(&command, CMD_REPLACE, CMD_NONE,
1725 invert);
1726 chain = optarg;
1727 if (optind < argc && argv[optind][0] != '-'
1728 && argv[optind][0] != '!')
1729 rulenum = parse_rulenumber(argv[optind++]);
1730 else
1731 exit_error(PARAMETER_PROBLEM,
1732 "-%c requires a rule number",
1733 cmd2char(CMD_REPLACE));
1734 break;
1735
1736 case 'I':
1737 add_command(&command, CMD_INSERT, CMD_NONE,
1738 invert);
1739 chain = optarg;
1740 if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!')
1742 rulenum = parse_rulenumber(argv[optind++]);
1743 else rulenum = 1;
1744 break;
1745
1746 case 'L':
1747 add_command(&command, CMD_LIST, CMD_ZERO,
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 'F':
1756 add_command(&command, CMD_FLUSH, CMD_NONE,
1757 invert);
1758 if (optarg) chain = optarg;
1759 else if (optind < argc && argv[optind][0] != '-'
1760 && argv[optind][0] != '!')
1761 chain = argv[optind++];
1762 break;
1763
1764 case 'Z':
1765 add_command(&command, CMD_ZERO, CMD_LIST,
1766 invert);
1767 if (optarg) chain = optarg;
1768 else if (optind < argc && argv[optind][0] != '-'
1769 && argv[optind][0] != '!')
1770 chain = argv[optind++];
1771 break;
1772
1773 case 'N':
1774 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1775 invert);
1776 chain = optarg;
1777 break;
1778
1779 case 'X':
1780 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1781 invert);
1782 if (optarg) chain = optarg;
1783 else if (optind < argc && argv[optind][0] != '-'
1784 && argv[optind][0] != '!')
1785 chain = argv[optind++];
1786 break;
1787
1788 case 'E':
1789 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1790 invert);
1791 chain = optarg;
1792 if (optind < argc && argv[optind][0] != '-'
1793 && argv[optind][0] != '!')
1794 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001795 else
1796 exit_error(PARAMETER_PROBLEM,
1797 "-%c requires old-chain-name and "
1798 "new-chain-name",
1799 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001800 break;
1801
1802 case 'P':
1803 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1804 invert);
1805 chain = optarg;
1806 if (optind < argc && argv[optind][0] != '-'
1807 && argv[optind][0] != '!')
1808 policy = argv[optind++];
1809 else
1810 exit_error(PARAMETER_PROBLEM,
1811 "-%c requires a chain and a policy",
1812 cmd2char(CMD_SET_POLICY));
1813 break;
1814
1815 case 'h':
1816 if (!optarg)
1817 optarg = argv[optind];
1818
Rusty Russell2e0a3212000-04-19 11:23:18 +00001819 /* iptables -p icmp -h */
1820 if (!iptables_matches && protocol)
Rusty Russell52a51492000-05-02 16:44:29 +00001821 find_match(protocol, TRY_LOAD);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001822
Marc Bouchere6869a82000-03-20 06:03:29 +00001823 exit_printhelp();
1824
1825 /*
1826 * Option selection
1827 */
1828 case 'p':
1829 if (check_inverse(optarg, &invert))
1830 optind++;
1831 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1832 invert);
1833
1834 /* Canonicalize into lower case */
1835 for (protocol = argv[optind-1]; *protocol; protocol++)
1836 *protocol = tolower(*protocol);
1837
1838 protocol = argv[optind-1];
1839 fw.ip.proto = parse_protocol(protocol);
1840
1841 if (fw.ip.proto == 0
1842 && (fw.ip.invflags & IPT_INV_PROTO))
1843 exit_error(PARAMETER_PROBLEM,
1844 "rule would never match protocol");
1845 fw.nfcache |= NFC_IP_PROTO;
1846 break;
1847
1848 case 's':
1849 if (check_inverse(optarg, &invert))
1850 optind++;
1851 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1852 invert);
1853 shostnetworkmask = argv[optind-1];
1854 fw.nfcache |= NFC_IP_SRC;
1855 break;
1856
1857 case 'd':
1858 if (check_inverse(optarg, &invert))
1859 optind++;
1860 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1861 invert);
1862 dhostnetworkmask = argv[optind-1];
1863 fw.nfcache |= NFC_IP_DST;
1864 break;
1865
1866 case 'j':
1867 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1868 invert);
1869 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001870 /* TRY_LOAD (may be chain name) */
1871 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001872
1873 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001874 size_t size;
1875
Rusty Russell73f72f52000-07-03 10:17:57 +00001876 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1877 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001878
Rusty Russell2e0a3212000-04-19 11:23:18 +00001879 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001880 target->t->u.target_size = size;
1881 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001882 target->init(target->t, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001883 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001884 }
1885 break;
1886
1887
1888 case 'i':
1889 if (check_inverse(optarg, &invert))
1890 optind++;
1891 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1892 invert);
1893 parse_interface(argv[optind-1],
1894 fw.ip.iniface,
1895 fw.ip.iniface_mask);
1896 fw.nfcache |= NFC_IP_IF_IN;
1897 break;
1898
1899 case 'o':
1900 if (check_inverse(optarg, &invert))
1901 optind++;
1902 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1903 invert);
1904 parse_interface(argv[optind-1],
1905 fw.ip.outiface,
1906 fw.ip.outiface_mask);
1907 fw.nfcache |= NFC_IP_IF_OUT;
1908 break;
1909
1910 case 'f':
1911 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1912 invert);
1913 fw.ip.flags |= IPT_F_FRAG;
1914 fw.nfcache |= NFC_IP_FRAG;
1915 break;
1916
1917 case 'v':
1918 if (!verbose)
1919 set_option(&options, OPT_VERBOSE,
1920 &fw.ip.invflags, invert);
1921 verbose++;
1922 break;
1923
Rusty Russell52a51492000-05-02 16:44:29 +00001924 case 'm': {
1925 size_t size;
1926
Marc Bouchere6869a82000-03-20 06:03:29 +00001927 if (invert)
1928 exit_error(PARAMETER_PROBLEM,
1929 "unexpected ! flag before --match");
1930
Rusty Russell52a51492000-05-02 16:44:29 +00001931 m = find_match(optarg, LOAD_MUST_SUCCEED);
Rusty Russell73f72f52000-07-03 10:17:57 +00001932 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1933 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001934 m->m = fw_calloc(1, size);
1935 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001936 strcpy(m->m->u.user.name, m->name);
Rusty Russell52a51492000-05-02 16:44:29 +00001937 m->init(m->m, &fw.nfcache);
Sven Kochfb1279a2001-02-27 12:25:12 +00001938 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001939 }
1940 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001941
1942 case 'n':
1943 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1944 invert);
1945 break;
1946
1947 case 't':
1948 if (invert)
1949 exit_error(PARAMETER_PROBLEM,
1950 "unexpected ! flag before --table");
1951 *table = argv[optind-1];
1952 break;
1953
1954 case 'x':
1955 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1956 invert);
1957 break;
1958
1959 case 'V':
1960 if (invert)
1961 printf("Not %s ;-)\n", program_version);
1962 else
1963 printf("%s v%s\n",
1964 program_name, program_version);
1965 exit(0);
1966
1967 case '0':
1968 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1969 invert);
1970 break;
1971
Harald Welte82dd2ec2000-12-19 05:18:15 +00001972 case 'M':
1973 modprobe = optarg;
1974 break;
1975
Harald Welteccd49e52001-01-23 22:54:34 +00001976 case 'c':
1977
1978 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1979 invert);
1980 pcnt = optarg;
1981 if (optind < argc && argv[optind][0] != '-'
1982 && argv[optind][0] != '!')
1983 bcnt = argv[optind++];
1984 else
1985 exit_error(PARAMETER_PROBLEM,
1986 "-%c requires packet and byte counter",
1987 opt2char(OPT_COUNTERS));
1988
1989 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1990 exit_error(PARAMETER_PROBLEM,
1991 "-%c packet counter not numeric",
1992 opt2char(OPT_COUNTERS));
1993
1994 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1995 exit_error(PARAMETER_PROBLEM,
1996 "-%c byte counter not numeric",
1997 opt2char(OPT_COUNTERS));
1998
1999 break;
2000
2001
Marc Bouchere6869a82000-03-20 06:03:29 +00002002 case 1: /* non option */
2003 if (optarg[0] == '!' && optarg[1] == '\0') {
2004 if (invert)
2005 exit_error(PARAMETER_PROBLEM,
2006 "multiple consecutive ! not"
2007 " allowed");
2008 invert = TRUE;
2009 optarg[0] = '\0';
2010 continue;
2011 }
Rusty Russell9e1d2142000-04-23 09:11:12 +00002012 printf("Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00002013 exit_tryhelp(2);
2014
2015 default:
2016 /* FIXME: This scheme doesn't allow two of the same
2017 matches --RR */
2018 if (!target
2019 || !(target->parse(c - target->option_offset,
2020 argv, invert,
2021 &target->tflags,
2022 &fw, &target->t))) {
Sven Kochfb1279a2001-02-27 12:25:12 +00002023 for (m = iptables_matches; m; m = m->next) {
2024 if (!m->used)
2025 continue;
2026
Marc Bouchere6869a82000-03-20 06:03:29 +00002027 if (m->parse(c - m->option_offset,
2028 argv, invert,
2029 &m->mflags,
2030 &fw,
2031 &fw.nfcache,
2032 &m->m))
2033 break;
2034 }
2035
2036 /* If you listen carefully, you can
Rusty Russell28381a42000-05-10 00:19:50 +00002037 actually hear this code suck. */
Rusty Russell9e1d2142000-04-23 09:11:12 +00002038 if (m == NULL
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 && protocol
Rusty Russell28381a42000-05-10 00:19:50 +00002040 && !find_proto(protocol, DONT_LOAD,
2041 options&OPT_NUMERIC)
2042 && (m = find_proto(protocol, TRY_LOAD,
2043 options&OPT_NUMERIC))) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002044 /* Try loading protocol */
Rusty Russell228e98d2000-04-27 10:28:06 +00002045 size_t size;
2046
Rusty Russell73f72f52000-07-03 10:17:57 +00002047 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
2048 + m->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002049
Rusty Russell2e0a3212000-04-19 11:23:18 +00002050 m->m = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002051 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00002052 strcpy(m->m->u.user.name, m->name);
Marc Bouchere6869a82000-03-20 06:03:29 +00002053 m->init(m->m, &fw.nfcache);
2054
Sven Kochdbe857a2001-03-02 09:41:08 +00002055 opts = merge_options(opts,
2056 m->extra_opts, &m->option_offset);
2057
Marc Bouchere6869a82000-03-20 06:03:29 +00002058 optind--;
2059 continue;
2060 }
2061 if (!m)
2062 exit_error(PARAMETER_PROBLEM,
2063 "Unknown arg `%s'",
2064 argv[optind-1]);
2065 }
2066 }
2067 invert = FALSE;
2068 }
2069
Sven Kochfb1279a2001-02-27 12:25:12 +00002070 for (m = iptables_matches; m; m = m->next) {
2071 if (!m->used)
2072 continue;
2073
Marc Bouchere6869a82000-03-20 06:03:29 +00002074 m->final_check(m->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00002075 }
2076
Marc Bouchere6869a82000-03-20 06:03:29 +00002077 if (target)
2078 target->final_check(target->tflags);
2079
2080 /* Fix me: must put inverse options checking here --MN */
2081
2082 if (optind < argc)
2083 exit_error(PARAMETER_PROBLEM,
2084 "unknown arguments found on commandline");
2085 if (!command)
2086 exit_error(PARAMETER_PROBLEM, "no command specified");
2087 if (invert)
2088 exit_error(PARAMETER_PROBLEM,
2089 "nothing appropriate following !");
2090
Marc Boucher744bd022000-04-22 22:36:10 +00002091 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2092 CMD_CHECK)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00002093 if (!(options & OPT_DESTINATION))
2094 dhostnetworkmask = "0.0.0.0/0";
2095 if (!(options & OPT_SOURCE))
2096 shostnetworkmask = "0.0.0.0/0";
2097 }
2098
2099 if (shostnetworkmask)
2100 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2101 &(fw.ip.smsk), &nsaddrs);
2102
2103 if (dhostnetworkmask)
2104 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2105 &(fw.ip.dmsk), &ndaddrs);
2106
2107 if ((nsaddrs > 1 || ndaddrs > 1) &&
2108 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
2109 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2110 " source or destination IP addresses");
2111
2112 if (command == CMD_CHECK && fw.ip.invflags != 0)
2113 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2114 cmd2char(CMD_CHECK));
2115
2116 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2117 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2118 "specify a unique address");
2119
2120 generic_opt_check(command, options);
2121
2122 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
2123 exit_error(PARAMETER_PROBLEM,
2124 "chain name `%s' too long (must be under %i chars)",
2125 chain, IPT_FUNCTION_MAXNAMELEN);
2126
Harald Welteae1ff9f2000-12-01 14:26:20 +00002127 /* only allocate handle if we weren't called with a handle */
2128 if (!*handle)
2129 *handle = iptc_init(*table);
2130
Harald Welte82dd2ec2000-12-19 05:18:15 +00002131 if (!*handle) {
2132 /* try to insmod the module if iptc_init failed */
2133 iptables_insmod("ip_tables", modprobe);
2134 *handle = iptc_init(*table);
2135 }
2136
Marc Bouchere6869a82000-03-20 06:03:29 +00002137 if (!*handle)
2138 exit_error(VERSION_PROBLEM,
2139 "can't initialize iptables table `%s': %s",
2140 *table, iptc_strerror(errno));
2141
Marc Boucher744bd022000-04-22 22:36:10 +00002142 if (command == CMD_CHECK
2143 || command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002144 || command == CMD_DELETE
2145 || command == CMD_INSERT
2146 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002147 if (strcmp(chain, "PREROUTING") == 0
2148 || strcmp(chain, "INPUT") == 0) {
2149 /* -o not valid with incoming packets. */
2150 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002151 exit_error(PARAMETER_PROBLEM,
2152 "Can't use -%c with %s\n",
2153 opt2char(OPT_VIANAMEOUT),
2154 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002155 /* -i required with -C */
2156 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2157 exit_error(PARAMETER_PROBLEM,
2158 "Need -%c with %s\n",
2159 opt2char(OPT_VIANAMEIN),
2160 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002161 }
2162
Rusty Russella4860fd2000-06-17 16:13:02 +00002163 if (strcmp(chain, "POSTROUTING") == 0
2164 || strcmp(chain, "OUTPUT") == 0) {
2165 /* -i not valid with outgoing packets */
2166 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002167 exit_error(PARAMETER_PROBLEM,
2168 "Can't use -%c with %s\n",
2169 opt2char(OPT_VIANAMEIN),
2170 chain);
Rusty Russella4860fd2000-06-17 16:13:02 +00002171 /* -o required with -C */
2172 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2173 exit_error(PARAMETER_PROBLEM,
2174 "Need -%c with %s\n",
2175 opt2char(OPT_VIANAMEOUT),
2176 chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00002177 }
2178
2179 if (target && iptc_is_chain(jumpto, *handle)) {
2180 printf("Warning: using chain %s, not extension\n",
2181 jumpto);
2182
2183 target = NULL;
2184 }
2185
2186 /* If they didn't specify a target, or it's a chain
2187 name, use standard. */
2188 if (!target
2189 && (strlen(jumpto) == 0
2190 || iptc_is_chain(jumpto, *handle))) {
2191 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002192
Rusty Russell52a51492000-05-02 16:44:29 +00002193 target = find_target(IPT_STANDARD_TARGET,
2194 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002195
2196 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002197 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002198 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002199 target->t->u.target_size = size;
2200 strcpy(target->t->u.user.name, jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002201 target->init(target->t, &fw.nfcache);
2202 }
2203
Rusty Russell7e53bf92000-03-20 07:03:28 +00002204 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002205 /* it is no chain, and we can't load a plugin.
2206 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002207 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002208 * chain. */
2209 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002210 } else {
2211 e = generate_entry(&fw, iptables_matches, target->t);
2212 }
2213 }
2214
2215 switch (command) {
2216 case CMD_APPEND:
2217 ret = append_entry(chain, e,
2218 nsaddrs, saddrs, ndaddrs, daddrs,
2219 options&OPT_VERBOSE,
2220 handle);
2221 break;
2222 case CMD_CHECK:
2223 ret = check_packet(chain, e,
2224 nsaddrs, saddrs, ndaddrs, daddrs,
2225 options&OPT_VERBOSE, handle);
2226 break;
2227 case CMD_DELETE:
2228 ret = delete_entry(chain, e,
2229 nsaddrs, saddrs, ndaddrs, daddrs,
2230 options&OPT_VERBOSE,
2231 handle);
2232 break;
2233 case CMD_DELETE_NUM:
2234 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2235 break;
2236 case CMD_REPLACE:
2237 ret = replace_entry(chain, e, rulenum - 1,
2238 saddrs, daddrs, options&OPT_VERBOSE,
2239 handle);
2240 break;
2241 case CMD_INSERT:
2242 ret = insert_entry(chain, e, rulenum - 1,
2243 nsaddrs, saddrs, ndaddrs, daddrs,
2244 options&OPT_VERBOSE,
2245 handle);
2246 break;
2247 case CMD_LIST:
2248 ret = list_entries(chain,
2249 options&OPT_VERBOSE,
2250 options&OPT_NUMERIC,
2251 options&OPT_EXPANDED,
2252 options&OPT_LINENUMBERS,
2253 handle);
2254 break;
2255 case CMD_FLUSH:
2256 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2257 break;
2258 case CMD_ZERO:
2259 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2260 break;
2261 case CMD_LIST|CMD_ZERO:
2262 ret = list_entries(chain,
2263 options&OPT_VERBOSE,
2264 options&OPT_NUMERIC,
2265 options&OPT_EXPANDED,
2266 options&OPT_LINENUMBERS,
2267 handle);
2268 if (ret)
2269 ret = zero_entries(chain,
2270 options&OPT_VERBOSE, handle);
2271 break;
2272 case CMD_NEW_CHAIN:
2273 ret = iptc_create_chain(chain, handle);
2274 break;
2275 case CMD_DELETE_CHAIN:
2276 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2277 break;
2278 case CMD_RENAME_CHAIN:
2279 ret = iptc_rename_chain(chain, newname, handle);
2280 break;
2281 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002282 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002283 break;
2284 default:
2285 /* We should never reach this... */
2286 exit_tryhelp(2);
2287 }
2288
2289 if (verbose > 1)
2290 dump_entries(*handle);
2291
2292 return ret;
2293}