1 /* Code to take an iptables-style command line and do it. */
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
6 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 #include <sys/utsname.h>
51 #ifndef PROC_SYS_MODPROBE
52 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55 #define FMT_NUMERIC 0x0001
56 #define FMT_NOCOUNTS 0x0002
57 #define FMT_KILOMEGAGIGA 0x0004
58 #define FMT_OPTIONS 0x0008
59 #define FMT_NOTABLE 0x0010
60 #define FMT_NOTARGET 0x0020
61 #define FMT_VIA 0x0040
62 #define FMT_NONEWLINE 0x0080
63 #define FMT_LINENUMBERS 0x0100
65 #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67 #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
70 #define CMD_NONE 0x0000U
71 #define CMD_INSERT 0x0001U
72 #define CMD_DELETE 0x0002U
73 #define CMD_DELETE_NUM 0x0004U
74 #define CMD_REPLACE 0x0008U
75 #define CMD_APPEND 0x0010U
76 #define CMD_LIST 0x0020U
77 #define CMD_FLUSH 0x0040U
78 #define CMD_ZERO 0x0080U
79 #define CMD_NEW_CHAIN 0x0100U
80 #define CMD_DELETE_CHAIN 0x0200U
81 #define CMD_SET_POLICY 0x0400U
82 #define CMD_RENAME_CHAIN 0x0800U
83 #define NUMBER_OF_CMD 13
84 static const char cmdflags
[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
87 #define OPTION_OFFSET 256
89 #define OPT_NONE 0x00000U
90 #define OPT_NUMERIC 0x00001U
91 #define OPT_SOURCE 0x00002U
92 #define OPT_DESTINATION 0x00004U
93 #define OPT_PROTOCOL 0x00008U
94 #define OPT_JUMP 0x00010U
95 #define OPT_VERBOSE 0x00020U
96 #define OPT_EXPANDED 0x00040U
97 #define OPT_VIANAMEIN 0x00080U
98 #define OPT_VIANAMEOUT 0x00100U
99 #define OPT_FRAGMENT 0x00200U
100 #define OPT_LINENUMBERS 0x00400U
101 #define OPT_COUNTERS 0x00800U
102 #define NUMBER_OF_OPT 12
103 static const char optflags
[NUMBER_OF_OPT
]
104 = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
106 static struct option original_opts
[] = {
107 { "append", 1, 0, 'A' },
108 { "delete", 1, 0, 'D' },
109 { "insert", 1, 0, 'I' },
110 { "replace", 1, 0, 'R' },
111 { "list", 2, 0, 'L' },
112 { "flush", 2, 0, 'F' },
113 { "zero", 2, 0, 'Z' },
114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
116 { "rename-chain", 1, 0, 'E' },
117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
122 { "protocol", 1, 0, 'p' },
123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "fragments", 0, 0, 'f' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
135 { "modprobe", 1, 0, 'M' },
136 { "set-counters", 1, 0, 'c' },
137 { "goto", 1, 0, 'g' },
141 /* we need this for iptables-restore. iptables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. iptables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
147 static struct option
*opts
= original_opts
;
148 static unsigned int global_option_offset
= 0;
150 /* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
159 static char commands_v_options
[NUMBER_OF_CMD
][NUMBER_OF_OPT
] =
160 /* Well, it's better than "Re: Linux vs FreeBSD" */
162 /* -n -s -d -p -j -v -x -i -o -f --line -c */
163 /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
164 /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
165 /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
166 /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
167 /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
168 /*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
169 /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
170 /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
171 /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
172 /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173 /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
174 /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
177 static int inverse_for_options
[NUMBER_OF_OPT
] =
180 /* -s */ IPT_INV_SRCIP
,
181 /* -d */ IPT_INV_DSTIP
,
182 /* -p */ IPT_INV_PROTO
,
186 /* -i */ IPT_INV_VIA_IN
,
187 /* -o */ IPT_INV_VIA_OUT
,
188 /* -f */ IPT_INV_FRAG
,
193 const char *program_version
;
194 const char *program_name
;
199 /* the path to command to load kernel module */
200 const char *modprobe
= NULL
;
202 /* Keeping track of external matches and targets: linked lists. */
203 struct iptables_match
*iptables_matches
= NULL
;
204 struct iptables_target
*iptables_targets
= NULL
;
206 /* Extra debugging from libiptc */
207 extern void dump_entries(const iptc_handle_t handle
);
209 /* A few hardcoded protocols for 'all' and in case the user has no
216 /* Primitive headers... */
217 /* defined in netinet/in.h */
220 #define IPPROTO_ESP 50
223 #define IPPROTO_AH 51
227 static const struct pprot chain_protos
[] = {
228 { "tcp", IPPROTO_TCP
},
229 { "udp", IPPROTO_UDP
},
230 { "udplite", IPPROTO_UDPLITE
},
231 { "icmp", IPPROTO_ICMP
},
232 { "esp", IPPROTO_ESP
},
233 { "ah", IPPROTO_AH
},
234 { "sctp", IPPROTO_SCTP
},
239 proto_to_name(u_int8_t proto
, int nolookup
)
243 if (proto
&& !nolookup
) {
244 struct protoent
*pent
= getprotobynumber(proto
);
249 for (i
= 0; i
< sizeof(chain_protos
)/sizeof(struct pprot
); i
++)
250 if (chain_protos
[i
].num
== proto
)
251 return chain_protos
[i
].name
;
257 service_to_port(const char *name
, const char *proto
)
259 struct servent
*service
;
261 if ((service
= getservbyname(name
, proto
)) != NULL
)
262 return ntohs((unsigned short) service
->s_port
);
268 parse_port(const char *port
, const char *proto
)
270 unsigned int portnum
;
272 if ((string_to_number(port
, 0, 65535, &portnum
)) != -1 ||
273 (portnum
= service_to_port(port
, proto
)) != -1)
274 return (u_int16_t
)portnum
;
276 exit_error(PARAMETER_PROBLEM
,
277 "invalid port/service `%s' specified", port
);
285 static struct in_addr
*
286 __dotted_to_addr(const char *dotted
, int type
)
288 static struct in_addr addr
;
289 unsigned char *addrp
;
291 unsigned int onebyte
;
295 /* copy dotted string, because we need to modify it */
296 strncpy(buf
, dotted
, sizeof(buf
) - 1);
297 buf
[sizeof(buf
) - 1] = '\0';
298 addrp
= (unsigned char *) &(addr
.s_addr
);
301 for (i
= 0; i
< 3; i
++) {
302 if ((q
= strchr(p
, '.')) == NULL
) {
303 if (type
== IPT_DOTTED_ADDR
) {
304 /* autocomplete, this is a network address */
305 if (string_to_number(p
, 0, 255, &onebyte
) == -1)
306 return (struct in_addr
*) NULL
;
308 addrp
[i
] = (unsigned char) onebyte
;
314 return (struct in_addr
*) NULL
;
318 if (string_to_number(p
, 0, 255, &onebyte
) == -1)
319 return (struct in_addr
*) NULL
;
321 addrp
[i
] = (unsigned char) onebyte
;
325 /* we've checked 3 bytes, now we check the last one */
326 if (string_to_number(p
, 0, 255, &onebyte
) == -1)
327 return (struct in_addr
*) NULL
;
329 addrp
[3] = (unsigned char) onebyte
;
335 dotted_to_addr(const char *dotted
)
337 return __dotted_to_addr(dotted
, IPT_DOTTED_ADDR
);
341 dotted_to_mask(const char *dotted
)
343 return __dotted_to_addr(dotted
, IPT_DOTTED_MASK
);
346 static struct in_addr
*
347 network_to_addr(const char *name
)
350 static struct in_addr addr
;
352 if ((net
= getnetbyname(name
)) != NULL
) {
353 if (net
->n_addrtype
!= AF_INET
)
354 return (struct in_addr
*) NULL
;
355 addr
.s_addr
= htonl((unsigned long) net
->n_net
);
359 return (struct in_addr
*) NULL
;
363 inaddrcpy(struct in_addr
*dst
, struct in_addr
*src
)
365 /* memcpy(dst, src, sizeof(struct in_addr)); */
366 dst
->s_addr
= src
->s_addr
;
369 static void free_opts(int reset_offset
)
371 if (opts
!= original_opts
) {
373 opts
= original_opts
;
375 global_option_offset
= 0;
380 exit_error(enum exittype status
, char *msg
, ...)
385 fprintf(stderr
, "%s v%s: ", program_name
, program_version
);
386 vfprintf(stderr
, msg
, args
);
388 fprintf(stderr
, "\n");
389 if (status
== PARAMETER_PROBLEM
)
390 exit_tryhelp(status
);
391 if (status
== VERSION_PROBLEM
)
393 "Perhaps iptables or your kernel needs to be upgraded.\n");
394 /* On error paths, make sure that we don't leak memory */
400 exit_tryhelp(int status
)
403 fprintf(stderr
, "Error occurred at line: %d\n", line
);
404 fprintf(stderr
, "Try `%s -h' or '%s --help' for more information.\n",
405 program_name
, program_name
);
411 exit_printhelp(struct iptables_rule_match
*matches
)
413 struct iptables_rule_match
*matchp
= NULL
;
414 struct iptables_target
*t
= NULL
;
417 "Usage: %s -[AD] chain rule-specification [options]\n"
418 " %s -[RI] chain rulenum rule-specification [options]\n"
419 " %s -D chain rulenum [options]\n"
420 " %s -[LFZ] [chain] [options]\n"
422 " %s -E old-chain-name new-chain-name\n"
423 " %s -P chain target [options]\n"
424 " %s -h (print this help information)\n\n",
425 program_name
, program_version
, program_name
, program_name
,
426 program_name
, program_name
, program_name
, program_name
,
427 program_name
, program_name
);
431 "Either long or short options are allowed.\n"
432 " --append -A chain Append to chain\n"
433 " --delete -D chain Delete matching rule from chain\n"
434 " --delete -D chain rulenum\n"
435 " Delete rule rulenum (1 = first) from chain\n"
436 " --insert -I chain [rulenum]\n"
437 " Insert in chain as rulenum (default 1=first)\n"
438 " --replace -R chain rulenum\n"
439 " Replace rule rulenum (1 = first) in chain\n"
440 " --list -L [chain] List the rules in a chain or all chains\n"
441 " --flush -F [chain] Delete all rules in chain or all chains\n"
442 " --zero -Z [chain] Zero counters in chain or all chains\n"
443 " --new -N chain Create a new user-defined chain\n"
445 " -X [chain] Delete a user-defined chain\n"
446 " --policy -P chain target\n"
447 " Change policy on chain to target\n"
449 " -E old-chain new-chain\n"
450 " Change chain name, (moving any references)\n"
453 " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
454 " --source -s [!] address[/mask]\n"
455 " source specification\n"
456 " --destination -d [!] address[/mask]\n"
457 " destination specification\n"
458 " --in-interface -i [!] input name[+]\n"
459 " network interface name ([+] for wildcard)\n"
460 " --jump -j target\n"
461 " target for rule (may load target extension)\n"
464 " jump to chain with no return\n"
466 " --match -m match\n"
467 " extended match (may load extension)\n"
468 " --numeric -n numeric output of addresses and ports\n"
469 " --out-interface -o [!] output name[+]\n"
470 " network interface name ([+] for wildcard)\n"
471 " --table -t table table to manipulate (default: `filter')\n"
472 " --verbose -v verbose mode\n"
473 " --line-numbers print line numbers when listing\n"
474 " --exact -x expand numbers (display exact values)\n"
475 "[!] --fragment -f match second or further fragments only\n"
476 " --modprobe=<command> try to insert modules using this command\n"
477 " --set-counters PKTS BYTES set the counter during insert/append\n"
478 "[!] --version -V print package version.\n");
480 /* Print out any special helps. A user might like to be able
481 to add a --help to the commandline, and see expected
482 results. So we call help for all specified matches & targets */
483 for (t
= iptables_targets
; t
;t
= t
->next
) {
489 for (matchp
= matches
; matchp
; matchp
= matchp
->next
) {
491 matchp
->match
->help();
497 generic_opt_check(int command
, int options
)
501 /* Check that commands are valid with options. Complicated by the
502 * fact that if an option is legal with *any* command given, it is
503 * legal overall (ie. -z and -l).
505 for (i
= 0; i
< NUMBER_OF_OPT
; i
++) {
506 legal
= 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
508 for (j
= 0; j
< NUMBER_OF_CMD
; j
++) {
509 if (!(command
& (1<<j
)))
512 if (!(options
& (1<<i
))) {
513 if (commands_v_options
[j
][i
] == '+')
514 exit_error(PARAMETER_PROBLEM
,
515 "You need to supply the `-%c' "
516 "option for this command\n",
519 if (commands_v_options
[j
][i
] != 'x')
526 exit_error(PARAMETER_PROBLEM
,
527 "Illegal option `-%c' with this command\n",
536 for (ptr
= optflags
; option
> 1; option
>>= 1, ptr
++);
545 for (ptr
= cmdflags
; option
> 1; option
>>= 1, ptr
++);
551 add_command(unsigned int *cmd
, const int newcmd
, const int othercmds
,
555 exit_error(PARAMETER_PROBLEM
, "unexpected ! flag");
556 if (*cmd
& (~othercmds
))
557 exit_error(PARAMETER_PROBLEM
, "Can't use -%c with -%c\n",
558 cmd2char(newcmd
), cmd2char(*cmd
& (~othercmds
)));
563 check_inverse(const char option
[], int *invert
, int *optind
, int argc
)
565 if (option
&& strcmp(option
, "!") == 0) {
567 exit_error(PARAMETER_PROBLEM
,
568 "Multiple `!' flags not allowed");
572 if (argc
&& *optind
> argc
)
573 exit_error(PARAMETER_PROBLEM
,
574 "no argument following `!'");
583 fw_calloc(size_t count
, size_t size
)
587 if ((p
= calloc(count
, size
)) == NULL
) {
588 perror("iptables: calloc failed");
595 fw_malloc(size_t size
)
599 if ((p
= malloc(size
)) == NULL
) {
600 perror("iptables: malloc failed");
606 static struct in_addr
*
607 host_to_addr(const char *name
, unsigned int *naddr
)
609 struct hostent
*host
;
610 struct in_addr
*addr
;
614 if ((host
= gethostbyname(name
)) != NULL
) {
615 if (host
->h_addrtype
!= AF_INET
||
616 host
->h_length
!= sizeof(struct in_addr
))
617 return (struct in_addr
*) NULL
;
619 while (host
->h_addr_list
[*naddr
] != (char *) NULL
)
621 addr
= fw_calloc(*naddr
, sizeof(struct in_addr
) * *naddr
);
622 for (i
= 0; i
< *naddr
; i
++)
623 inaddrcpy(&(addr
[i
]),
624 (struct in_addr
*) host
->h_addr_list
[i
]);
628 return (struct in_addr
*) NULL
;
632 addr_to_host(const struct in_addr
*addr
)
634 struct hostent
*host
;
636 if ((host
= gethostbyaddr((char *) addr
,
637 sizeof(struct in_addr
), AF_INET
)) != NULL
)
638 return (char *) host
->h_name
;
640 return (char *) NULL
;
644 * All functions starting with "parse" should succeed, otherwise
646 * Most routines return pointers to static data that may change
647 * between calls to the same or other routines with a few exceptions:
648 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
649 * return global static data.
652 static struct in_addr
*
653 parse_hostnetwork(const char *name
, unsigned int *naddrs
)
655 struct in_addr
*addrp
, *addrptmp
;
657 if ((addrptmp
= dotted_to_addr(name
)) != NULL
||
658 (addrptmp
= network_to_addr(name
)) != NULL
) {
659 addrp
= fw_malloc(sizeof(struct in_addr
));
660 inaddrcpy(addrp
, addrptmp
);
664 if ((addrp
= host_to_addr(name
, naddrs
)) != NULL
)
667 exit_error(PARAMETER_PROBLEM
, "host/network `%s' not found", name
);
670 static struct in_addr
*
671 parse_mask(char *mask
)
673 static struct in_addr maskaddr
;
674 struct in_addr
*addrp
;
678 /* no mask at all defaults to 32 bits */
679 maskaddr
.s_addr
= 0xFFFFFFFF;
682 if ((addrp
= dotted_to_mask(mask
)) != NULL
)
683 /* dotted_to_addr already returns a network byte order addr */
685 if (string_to_number(mask
, 0, 32, &bits
) == -1)
686 exit_error(PARAMETER_PROBLEM
,
687 "invalid mask `%s' specified", mask
);
689 maskaddr
.s_addr
= htonl(0xFFFFFFFF << (32 - bits
));
693 maskaddr
.s_addr
= 0L;
698 parse_hostnetworkmask(const char *name
, struct in_addr
**addrpp
,
699 struct in_addr
*maskp
, unsigned int *naddrs
)
701 struct in_addr
*addrp
;
706 strncpy(buf
, name
, sizeof(buf
) - 1);
707 buf
[sizeof(buf
) - 1] = '\0';
708 if ((p
= strrchr(buf
, '/')) != NULL
) {
710 addrp
= parse_mask(p
+ 1);
712 addrp
= parse_mask(NULL
);
713 inaddrcpy(maskp
, addrp
);
715 /* if a null mask is given, the name is ignored, like in "any/0" */
716 if (maskp
->s_addr
== 0L)
717 strcpy(buf
, "0.0.0.0");
719 addrp
= *addrpp
= parse_hostnetwork(buf
, naddrs
);
721 for (i
= 0, j
= 0; i
< n
; i
++) {
722 addrp
[j
++].s_addr
&= maskp
->s_addr
;
723 for (k
= 0; k
< j
- 1; k
++) {
724 if (addrp
[k
].s_addr
== addrp
[j
- 1].s_addr
) {
733 struct iptables_match
*
734 find_match(const char *name
, enum ipt_tryload tryload
, struct iptables_rule_match
**matches
)
736 struct iptables_match
*ptr
;
738 for (ptr
= iptables_matches
; ptr
; ptr
= ptr
->next
) {
739 if (strcmp(name
, ptr
->name
) == 0) {
740 struct iptables_match
*clone
;
742 /* First match of this type: */
746 /* Second and subsequent clones */
747 clone
= fw_malloc(sizeof(struct iptables_match
));
748 memcpy(clone
, ptr
, sizeof(struct iptables_match
));
750 /* This is a clone: */
758 #ifndef NO_SHARED_LIBS
759 if (!ptr
&& tryload
!= DONT_LOAD
&& tryload
!= DURING_LOAD
) {
760 char path
[strlen(lib_dir
) + sizeof("/libipt_.so")
762 #if 1 // for easier testing zzz
764 sprintf(s
, "%s/libipt_%s.so", "/etc/iptext/", name
);
765 if (dlopen(s
, RTLD_NOW
)) goto OPENED
;
767 sprintf(path
, "%s/libipt_%s.so", lib_dir
, name
);
768 if (dlopen(path
, RTLD_NOW
)) {
770 /* Found library. If it didn't register itself,
771 maybe they specified target as match. */
772 ptr
= find_match(name
, DONT_LOAD
, NULL
);
775 exit_error(PARAMETER_PROBLEM
,
776 "Couldn't load match `%s'\n",
778 } else if (tryload
== LOAD_MUST_SUCCEED
)
779 exit_error(PARAMETER_PROBLEM
,
780 "Couldn't load match `%s':%s\n",
784 if (ptr
&& !ptr
->loaded
) {
785 if (tryload
!= DONT_LOAD
)
790 if(!ptr
&& (tryload
== LOAD_MUST_SUCCEED
)) {
791 exit_error(PARAMETER_PROBLEM
,
792 "Couldn't find match `%s'\n", name
);
796 if (ptr
&& matches
) {
797 struct iptables_rule_match
**i
;
798 struct iptables_rule_match
*newentry
;
800 newentry
= fw_malloc(sizeof(struct iptables_rule_match
));
802 for (i
= matches
; *i
; i
= &(*i
)->next
) {
803 if (strcmp(name
, (*i
)->match
->name
) == 0)
806 newentry
->match
= ptr
;
807 newentry
->completed
= 0;
808 newentry
->next
= NULL
;
815 /* Christophe Burki wants `-p 6' to imply `-m tcp'. */
816 static struct iptables_match
*
817 find_proto(const char *pname
, enum ipt_tryload tryload
, int nolookup
, struct iptables_rule_match
**matches
)
821 if (string_to_number(pname
, 0, 255, &proto
) != -1) {
822 char *protoname
= proto_to_name(proto
, nolookup
);
825 return find_match(protoname
, tryload
, matches
);
827 return find_match(pname
, tryload
, matches
);
833 parse_protocol(const char *s
)
837 if (string_to_number(s
, 0, 255, &proto
) == -1) {
838 struct protoent
*pent
;
840 /* first deal with the special case of 'all' to prevent
841 * people from being able to redefine 'all' in nsswitch
842 * and/or provoke expensive [not working] ldap/nis/...
844 if (!strcmp(s
, "all"))
847 if ((pent
= getprotobyname(s
)))
848 proto
= pent
->p_proto
;
852 i
< sizeof(chain_protos
)/sizeof(struct pprot
);
854 if (strcmp(s
, chain_protos
[i
].name
) == 0) {
855 proto
= chain_protos
[i
].num
;
859 if (i
== sizeof(chain_protos
)/sizeof(struct pprot
))
860 exit_error(PARAMETER_PROBLEM
,
861 "unknown protocol `%s' specified",
866 return (u_int16_t
)proto
;
869 void parse_interface(const char *arg
, char *vianame
, unsigned char *mask
)
871 int vialen
= strlen(arg
);
874 memset(mask
, 0, IFNAMSIZ
);
875 memset(vianame
, 0, IFNAMSIZ
);
877 if (vialen
+ 1 > IFNAMSIZ
)
878 exit_error(PARAMETER_PROBLEM
,
879 "interface name `%s' must be shorter than IFNAMSIZ"
880 " (%i)", arg
, IFNAMSIZ
-1);
882 strcpy(vianame
, arg
);
883 if ((vialen
== 0) || (vialen
== 1 && vianame
[0] == '+'))
884 memset(mask
, 0, IFNAMSIZ
);
885 else if (vianame
[vialen
- 1] == '+') {
886 memset(mask
, 0xFF, vialen
- 1);
887 memset(mask
+ vialen
- 1, 0, IFNAMSIZ
- vialen
+ 1);
888 /* Don't remove `+' here! -HW */
890 /* Include nul-terminator in match */
891 memset(mask
, 0xFF, vialen
+ 1);
892 memset(mask
+ vialen
+ 1, 0, IFNAMSIZ
- vialen
- 1);
893 for (i
= 0; vianame
[i
]; i
++) {
894 if (vianame
[i
] == ':' ||
897 printf("Warning: weird character in interface"
898 " `%s' (No aliases, :, ! or *).\n",
908 parse_rulenumber(const char *rule
)
910 unsigned int rulenum
;
912 if (string_to_number(rule
, 1, INT_MAX
, &rulenum
) == -1)
913 exit_error(PARAMETER_PROBLEM
,
914 "Invalid rule number `%s'", rule
);
920 parse_target(const char *targetname
)
924 if (strlen(targetname
) < 1)
925 exit_error(PARAMETER_PROBLEM
,
926 "Invalid target name (too short)");
928 if (strlen(targetname
)+1 > sizeof(ipt_chainlabel
))
929 exit_error(PARAMETER_PROBLEM
,
930 "Invalid target name `%s' (%u chars max)",
931 targetname
, (unsigned int)sizeof(ipt_chainlabel
)-1);
933 for (ptr
= targetname
; *ptr
; ptr
++)
935 exit_error(PARAMETER_PROBLEM
,
936 "Invalid target name `%s'", targetname
);
941 addr_to_network(const struct in_addr
*addr
)
945 if ((net
= getnetbyaddr((long) ntohl(addr
->s_addr
), AF_INET
)) != NULL
)
946 return (char *) net
->n_name
;
948 return (char *) NULL
;
952 addr_to_dotted(const struct in_addr
*addrp
)
955 const unsigned char *bytep
;
957 bytep
= (const unsigned char *) &(addrp
->s_addr
);
958 sprintf(buf
, "%d.%d.%d.%d", bytep
[0], bytep
[1], bytep
[2], bytep
[3]);
963 addr_to_anyname(const struct in_addr
*addr
)
967 if ((name
= addr_to_host(addr
)) != NULL
||
968 (name
= addr_to_network(addr
)) != NULL
)
971 return addr_to_dotted(addr
);
975 mask_to_dotted(const struct in_addr
*mask
)
979 u_int32_t maskaddr
, bits
;
981 maskaddr
= ntohl(mask
->s_addr
);
983 if (maskaddr
== 0xFFFFFFFFL
)
984 /* we don't want to see "/32" */
989 while (--i
>= 0 && maskaddr
!= bits
)
992 sprintf(buf
, "/%d", i
);
994 /* mask was not a decent combination of 1's and 0's */
995 sprintf(buf
, "/%s", addr_to_dotted(mask
));
1001 string_to_number_ll(const char *s
, unsigned long long min
, unsigned long long max
,
1002 unsigned long long *ret
)
1004 unsigned long long number
;
1007 /* Handle hex, octal, etc. */
1009 number
= strtoull(s
, &end
, 0);
1010 if (*end
== '\0' && end
!= s
) {
1011 /* we parsed a number, let's see if we want this */
1012 if (errno
!= ERANGE
&& min
<= number
&& (!max
|| number
<= max
)) {
1021 string_to_number_l(const char *s
, unsigned long min
, unsigned long max
,
1025 unsigned long long number
;
1027 result
= string_to_number_ll(s
, min
, max
, &number
);
1028 *ret
= (unsigned long)number
;
1033 int string_to_number(const char *s
, unsigned int min
, unsigned int max
,
1037 unsigned long number
;
1039 result
= string_to_number_l(s
, min
, max
, &number
);
1040 *ret
= (unsigned int)number
;
1046 set_option(unsigned int *options
, unsigned int option
, u_int8_t
*invflg
,
1049 if (*options
& option
)
1050 exit_error(PARAMETER_PROBLEM
, "multiple -%c flags not allowed",
1056 for (i
= 0; 1 << i
!= option
; i
++);
1058 if (!inverse_for_options
[i
])
1059 exit_error(PARAMETER_PROBLEM
,
1060 "cannot have ! before -%c",
1062 *invflg
|= inverse_for_options
[i
];
1066 struct iptables_target
*
1067 find_target(const char *name
, enum ipt_tryload tryload
)
1069 struct iptables_target
*ptr
;
1071 /* Standard target? */
1072 if (strcmp(name
, "") == 0
1073 || strcmp(name
, IPTC_LABEL_ACCEPT
) == 0
1074 || strcmp(name
, IPTC_LABEL_DROP
) == 0
1075 || strcmp(name
, IPTC_LABEL_QUEUE
) == 0
1076 || strcmp(name
, IPTC_LABEL_RETURN
) == 0)
1079 for (ptr
= iptables_targets
; ptr
; ptr
= ptr
->next
) {
1080 if (strcmp(name
, ptr
->name
) == 0)
1084 #ifndef NO_SHARED_LIBS
1085 if (!ptr
&& tryload
!= DONT_LOAD
&& tryload
!= DURING_LOAD
) {
1086 char path
[strlen(lib_dir
) + sizeof("/libipt_.so")
1088 #if 1 // for easier testing zzz
1090 sprintf(s
, "%s/libipt_%s.so", "/etc/iptext/", name
);
1091 if (dlopen(s
, RTLD_NOW
)) goto OPENED
;
1093 sprintf(path
, "%s/libipt_%s.so", lib_dir
, name
);
1094 if (dlopen(path
, RTLD_NOW
)) {
1096 /* Found library. If it didn't register itself,
1097 maybe they specified match as a target. */
1098 ptr
= find_target(name
, DONT_LOAD
);
1100 exit_error(PARAMETER_PROBLEM
,
1101 "Couldn't load target `%s'\n",
1103 } else if (tryload
== LOAD_MUST_SUCCEED
)
1104 exit_error(PARAMETER_PROBLEM
,
1105 "Couldn't load target `%s':%s\n",
1109 if (ptr
&& !ptr
->loaded
) {
1110 if (tryload
!= DONT_LOAD
)
1115 if(!ptr
&& (tryload
== LOAD_MUST_SUCCEED
)) {
1116 exit_error(PARAMETER_PROBLEM
,
1117 "Couldn't find target `%s'\n", name
);
1127 static struct option
*
1128 merge_options(struct option
*oldopts
, const struct option
*newopts
,
1129 unsigned int *option_offset
)
1131 unsigned int num_old
, num_new
, i
;
1132 struct option
*merge
;
1134 for (num_old
= 0; oldopts
[num_old
].name
; num_old
++);
1135 for (num_new
= 0; newopts
[num_new
].name
; num_new
++);
1137 global_option_offset
+= OPTION_OFFSET
;
1138 *option_offset
= global_option_offset
;
1140 merge
= malloc(sizeof(struct option
) * (num_new
+ num_old
+ 1));
1141 memcpy(merge
, oldopts
, num_old
* sizeof(struct option
));
1142 free_opts(0); /* Release previous options merged if any */
1143 for (i
= 0; i
< num_new
; i
++) {
1144 merge
[num_old
+ i
] = newopts
[i
];
1145 merge
[num_old
+ i
].val
+= *option_offset
;
1147 memset(merge
+ num_old
+ num_new
, 0, sizeof(struct option
));
1152 static int compatible_revision(const char *name
, u_int8_t revision
, int opt
)
1154 struct ipt_get_revision rev
;
1155 socklen_t s
= sizeof(rev
);
1156 int max_rev
, sockfd
;
1158 sockfd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_RAW
);
1160 fprintf(stderr
, "Could not open socket to kernel: %s\n",
1165 load_iptables_ko(modprobe
, 1);
1167 strcpy(rev
.name
, name
);
1168 rev
.revision
= revision
;
1170 max_rev
= getsockopt(sockfd
, IPPROTO_IP
, opt
, &rev
, &s
);
1172 /* Definitely don't support this? */
1173 if (errno
== EPROTONOSUPPORT
) {
1176 } else if (errno
== ENOPROTOOPT
) {
1178 /* Assume only revision 0 support (old kernel) */
1179 return (revision
== 0);
1181 fprintf(stderr
, "getsockopt failed strangely: %s\n",
1190 static int compatible_match_revision(const char *name
, u_int8_t revision
)
1192 return compatible_revision(name
, revision
, IPT_SO_GET_REVISION_MATCH
);
1195 static int compatible_target_revision(const char *name
, u_int8_t revision
)
1197 return compatible_revision(name
, revision
, IPT_SO_GET_REVISION_TARGET
);
1201 register_match(struct iptables_match
*me
)
1203 struct iptables_match
**i
, *old
;
1205 if (strcmp(me
->version
, program_version
) != 0) {
1206 fprintf(stderr
, "%s: match `%s' v%s (I'm v%s).\n",
1207 program_name
, me
->name
, me
->version
, program_version
);
1211 /* Revision field stole a char from name. */
1212 if (strlen(me
->name
) >= IPT_FUNCTION_MAXNAMELEN
-1) {
1213 fprintf(stderr
, "%s: target `%s' has invalid name\n",
1214 program_name
, me
->name
);
1218 old
= find_match(me
->name
, DURING_LOAD
, NULL
);
1220 if (old
->revision
== me
->revision
) {
1222 "%s: match `%s' already registered.\n",
1223 program_name
, me
->name
);
1227 /* Now we have two (or more) options, check compatibility. */
1228 if (compatible_match_revision(old
->name
, old
->revision
)
1229 && old
->revision
> me
->revision
)
1232 /* Replace if compatible. */
1233 if (!compatible_match_revision(me
->name
, me
->revision
))
1236 /* Delete old one. */
1237 for (i
= &iptables_matches
; *i
!=old
; i
= &(*i
)->next
);
1241 if (me
->size
!= IPT_ALIGN(me
->size
)) {
1242 fprintf(stderr
, "%s: match `%s' has invalid size %u.\n",
1243 program_name
, me
->name
, (unsigned int)me
->size
);
1247 /* Append to list. */
1248 for (i
= &iptables_matches
; *i
; i
= &(*i
)->next
);
1257 register_target(struct iptables_target
*me
)
1259 struct iptables_target
*old
;
1261 if (strcmp(me
->version
, program_version
) != 0) {
1262 fprintf(stderr
, "%s: target `%s' v%s (I'm v%s).\n",
1263 program_name
, me
->name
, me
->version
, program_version
);
1267 /* Revision field stole a char from name. */
1268 if (strlen(me
->name
) >= IPT_FUNCTION_MAXNAMELEN
-1) {
1269 fprintf(stderr
, "%s: target `%s' has invalid name\n",
1270 program_name
, me
->name
);
1274 old
= find_target(me
->name
, DURING_LOAD
);
1276 struct iptables_target
**i
;
1278 if (old
->revision
== me
->revision
) {
1280 "%s: target `%s' already registered.\n",
1281 program_name
, me
->name
);
1285 /* Now we have two (or more) options, check compatibility. */
1286 if (compatible_target_revision(old
->name
, old
->revision
)
1287 && old
->revision
> me
->revision
)
1290 /* Replace if compatible. */
1291 if (!compatible_target_revision(me
->name
, me
->revision
))
1294 /* Delete old one. */
1295 for (i
= &iptables_targets
; *i
!=old
; i
= &(*i
)->next
);
1299 if (me
->size
!= IPT_ALIGN(me
->size
)) {
1300 fprintf(stderr
, "%s: target `%s' has invalid size %u.\n",
1301 program_name
, me
->name
, (unsigned int)me
->size
);
1305 /* Prepend to list. */
1306 me
->next
= iptables_targets
;
1307 iptables_targets
= me
;
1313 print_num(u_int64_t number
, unsigned int format
)
1315 if (format
& FMT_KILOMEGAGIGA
) {
1316 if (number
> 99999) {
1317 number
= (number
+ 500) / 1000;
1318 if (number
> 9999) {
1319 number
= (number
+ 500) / 1000;
1320 if (number
> 9999) {
1321 number
= (number
+ 500) / 1000;
1322 if (number
> 9999) {
1323 number
= (number
+ 500) / 1000;
1324 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number
);
1326 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number
);
1328 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number
);
1330 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number
);
1332 printf(FMT("%5llu ","%llu "), (unsigned long long)number
);
1334 printf(FMT("%8llu ","%llu "), (unsigned long long)number
);
1339 print_header(unsigned int format
, const char *chain
, iptc_handle_t
*handle
)
1341 struct ipt_counters counters
;
1342 const char *pol
= iptc_get_policy(chain
, &counters
, handle
);
1343 printf("Chain %s", chain
);
1345 printf(" (policy %s", pol
);
1346 if (!(format
& FMT_NOCOUNTS
)) {
1348 print_num(counters
.pcnt
, (format
|FMT_NOTABLE
));
1349 fputs("packets, ", stdout
);
1350 print_num(counters
.bcnt
, (format
|FMT_NOTABLE
));
1351 fputs("bytes", stdout
);
1356 if (!iptc_get_references(&refs
, chain
, handle
))
1357 printf(" (ERROR obtaining refs)\n");
1359 printf(" (%u references)\n", refs
);
1362 if (format
& FMT_LINENUMBERS
)
1363 printf(FMT("%-4s ", "%s "), "num");
1364 if (!(format
& FMT_NOCOUNTS
)) {
1365 if (format
& FMT_KILOMEGAGIGA
) {
1366 printf(FMT("%5s ","%s "), "pkts");
1367 printf(FMT("%5s ","%s "), "bytes");
1369 printf(FMT("%8s ","%s "), "pkts");
1370 printf(FMT("%10s ","%s "), "bytes");
1373 if (!(format
& FMT_NOTARGET
))
1374 printf(FMT("%-9s ","%s "), "target");
1375 fputs(" prot ", stdout
);
1376 if (format
& FMT_OPTIONS
)
1377 fputs("opt", stdout
);
1378 if (format
& FMT_VIA
) {
1379 printf(FMT(" %-6s ","%s "), "in");
1380 printf(FMT("%-6s ","%s "), "out");
1382 printf(FMT(" %-19s ","%s "), "source");
1383 printf(FMT(" %-19s "," %s "), "destination");
1389 print_match(const struct ipt_entry_match
*m
,
1390 const struct ipt_ip
*ip
,
1393 struct iptables_match
*match
= find_match(m
->u
.user
.name
, TRY_LOAD
, NULL
);
1397 match
->print(ip
, m
, numeric
);
1399 printf("%s ", match
->name
);
1401 if (m
->u
.user
.name
[0])
1402 printf("UNKNOWN match `%s' ", m
->u
.user
.name
);
1404 /* Don't stop iterating. */
1408 /* e is called `fw' here for hysterical raisins */
1410 print_firewall(const struct ipt_entry
*fw
,
1411 const char *targname
,
1413 unsigned int format
,
1414 const iptc_handle_t handle
)
1416 struct iptables_target
*target
= NULL
;
1417 const struct ipt_entry_target
*t
;
1421 if (!iptc_is_chain(targname
, handle
))
1422 target
= find_target(targname
, TRY_LOAD
);
1424 target
= find_target(IPT_STANDARD_TARGET
, LOAD_MUST_SUCCEED
);
1426 t
= ipt_get_target((struct ipt_entry
*)fw
);
1427 flags
= fw
->ip
.flags
;
1429 if (format
& FMT_LINENUMBERS
)
1430 printf(FMT("%-4u ", "%u "), num
+1);
1432 if (!(format
& FMT_NOCOUNTS
)) {
1433 print_num(fw
->counters
.pcnt
, format
);
1434 print_num(fw
->counters
.bcnt
, format
);
1437 if (!(format
& FMT_NOTARGET
))
1438 printf(FMT("%-9s ", "%s "), targname
);
1440 fputc(fw
->ip
.invflags
& IPT_INV_PROTO
? '!' : ' ', stdout
);
1442 char *pname
= proto_to_name(fw
->ip
.proto
, format
&FMT_NUMERIC
);
1444 printf(FMT("%-5s", "%s "), pname
);
1446 printf(FMT("%-5hu", "%hu "), fw
->ip
.proto
);
1449 if (format
& FMT_OPTIONS
) {
1450 if (format
& FMT_NOTABLE
)
1451 fputs("opt ", stdout
);
1452 fputc(fw
->ip
.invflags
& IPT_INV_FRAG
? '!' : '-', stdout
);
1453 fputc(flags
& IPT_F_FRAG
? 'f' : '-', stdout
);
1457 if (format
& FMT_VIA
) {
1458 char iface
[IFNAMSIZ
+2];
1460 if (fw
->ip
.invflags
& IPT_INV_VIA_IN
) {
1464 else iface
[0] = '\0';
1466 if (fw
->ip
.iniface
[0] != '\0') {
1467 strcat(iface
, fw
->ip
.iniface
);
1469 else if (format
& FMT_NUMERIC
) strcat(iface
, "*");
1470 else strcat(iface
, "any");
1471 printf(FMT(" %-6s ","in %s "), iface
);
1473 if (fw
->ip
.invflags
& IPT_INV_VIA_OUT
) {
1477 else iface
[0] = '\0';
1479 if (fw
->ip
.outiface
[0] != '\0') {
1480 strcat(iface
, fw
->ip
.outiface
);
1482 else if (format
& FMT_NUMERIC
) strcat(iface
, "*");
1483 else strcat(iface
, "any");
1484 printf(FMT("%-6s ","out %s "), iface
);
1487 fputc(fw
->ip
.invflags
& IPT_INV_SRCIP
? '!' : ' ', stdout
);
1488 if (fw
->ip
.smsk
.s_addr
== 0L && !(format
& FMT_NUMERIC
))
1489 printf(FMT("%-19s ","%s "), "anywhere");
1491 if (format
& FMT_NUMERIC
)
1492 sprintf(buf
, "%s", addr_to_dotted(&(fw
->ip
.src
)));
1494 sprintf(buf
, "%s", addr_to_anyname(&(fw
->ip
.src
)));
1495 strcat(buf
, mask_to_dotted(&(fw
->ip
.smsk
)));
1496 printf(FMT("%-19s ","%s "), buf
);
1499 fputc(fw
->ip
.invflags
& IPT_INV_DSTIP
? '!' : ' ', stdout
);
1500 if (fw
->ip
.dmsk
.s_addr
== 0L && !(format
& FMT_NUMERIC
))
1501 printf(FMT("%-19s ","-> %s"), "anywhere");
1503 if (format
& FMT_NUMERIC
)
1504 sprintf(buf
, "%s", addr_to_dotted(&(fw
->ip
.dst
)));
1506 sprintf(buf
, "%s", addr_to_anyname(&(fw
->ip
.dst
)));
1507 strcat(buf
, mask_to_dotted(&(fw
->ip
.dmsk
)));
1508 printf(FMT("%-19s ","-> %s"), buf
);
1511 if (format
& FMT_NOTABLE
)
1515 if(fw
->ip
.flags
& IPT_F_GOTO
)
1519 IPT_MATCH_ITERATE(fw
, print_match
, &fw
->ip
, format
& FMT_NUMERIC
);
1523 /* Print the target information. */
1524 target
->print(&fw
->ip
, t
, format
& FMT_NUMERIC
);
1525 } else if (t
->u
.target_size
!= sizeof(*t
))
1526 printf("[%u bytes of unknown target data] ",
1527 (unsigned int)(t
->u
.target_size
- sizeof(*t
)));
1529 if (!(format
& FMT_NONEWLINE
))
1530 fputc('\n', stdout
);
1534 print_firewall_line(const struct ipt_entry
*fw
,
1535 const iptc_handle_t h
)
1537 struct ipt_entry_target
*t
;
1539 t
= ipt_get_target((struct ipt_entry
*)fw
);
1540 print_firewall(fw
, t
->u
.user
.name
, 0, FMT_PRINT_RULE
, h
);
1544 append_entry(const ipt_chainlabel chain
,
1545 struct ipt_entry
*fw
,
1546 unsigned int nsaddrs
,
1547 const struct in_addr saddrs
[],
1548 unsigned int ndaddrs
,
1549 const struct in_addr daddrs
[],
1551 iptc_handle_t
*handle
)
1556 for (i
= 0; i
< nsaddrs
; i
++) {
1557 fw
->ip
.src
.s_addr
= saddrs
[i
].s_addr
;
1558 for (j
= 0; j
< ndaddrs
; j
++) {
1559 fw
->ip
.dst
.s_addr
= daddrs
[j
].s_addr
;
1561 print_firewall_line(fw
, *handle
);
1562 ret
&= iptc_append_entry(chain
, fw
, handle
);
1570 replace_entry(const ipt_chainlabel chain
,
1571 struct ipt_entry
*fw
,
1572 unsigned int rulenum
,
1573 const struct in_addr
*saddr
,
1574 const struct in_addr
*daddr
,
1576 iptc_handle_t
*handle
)
1578 fw
->ip
.src
.s_addr
= saddr
->s_addr
;
1579 fw
->ip
.dst
.s_addr
= daddr
->s_addr
;
1582 print_firewall_line(fw
, *handle
);
1583 return iptc_replace_entry(chain
, fw
, rulenum
, handle
);
1587 insert_entry(const ipt_chainlabel chain
,
1588 struct ipt_entry
*fw
,
1589 unsigned int rulenum
,
1590 unsigned int nsaddrs
,
1591 const struct in_addr saddrs
[],
1592 unsigned int ndaddrs
,
1593 const struct in_addr daddrs
[],
1595 iptc_handle_t
*handle
)
1600 for (i
= 0; i
< nsaddrs
; i
++) {
1601 fw
->ip
.src
.s_addr
= saddrs
[i
].s_addr
;
1602 for (j
= 0; j
< ndaddrs
; j
++) {
1603 fw
->ip
.dst
.s_addr
= daddrs
[j
].s_addr
;
1605 print_firewall_line(fw
, *handle
);
1606 ret
&= iptc_insert_entry(chain
, fw
, rulenum
, handle
);
1613 static unsigned char *
1614 make_delete_mask(struct ipt_entry
*fw
, struct iptables_rule_match
*matches
)
1616 /* Establish mask for comparison */
1618 struct iptables_rule_match
*matchp
;
1619 unsigned char *mask
, *mptr
;
1621 size
= sizeof(struct ipt_entry
);
1622 for (matchp
= matches
; matchp
; matchp
= matchp
->next
)
1623 size
+= IPT_ALIGN(sizeof(struct ipt_entry_match
)) + matchp
->match
->size
;
1625 mask
= fw_calloc(1, size
1626 + IPT_ALIGN(sizeof(struct ipt_entry_target
))
1627 + iptables_targets
->size
);
1629 memset(mask
, 0xFF, sizeof(struct ipt_entry
));
1630 mptr
= mask
+ sizeof(struct ipt_entry
);
1632 for (matchp
= matches
; matchp
; matchp
= matchp
->next
) {
1634 IPT_ALIGN(sizeof(struct ipt_entry_match
))
1635 + matchp
->match
->userspacesize
);
1636 mptr
+= IPT_ALIGN(sizeof(struct ipt_entry_match
)) + matchp
->match
->size
;
1640 IPT_ALIGN(sizeof(struct ipt_entry_target
))
1641 + iptables_targets
->userspacesize
);
1647 delete_entry(const ipt_chainlabel chain
,
1648 struct ipt_entry
*fw
,
1649 unsigned int nsaddrs
,
1650 const struct in_addr saddrs
[],
1651 unsigned int ndaddrs
,
1652 const struct in_addr daddrs
[],
1654 iptc_handle_t
*handle
,
1655 struct iptables_rule_match
*matches
)
1659 unsigned char *mask
;
1661 mask
= make_delete_mask(fw
, matches
);
1662 for (i
= 0; i
< nsaddrs
; i
++) {
1663 fw
->ip
.src
.s_addr
= saddrs
[i
].s_addr
;
1664 for (j
= 0; j
< ndaddrs
; j
++) {
1665 fw
->ip
.dst
.s_addr
= daddrs
[j
].s_addr
;
1667 print_firewall_line(fw
, *handle
);
1668 ret
&= iptc_delete_entry(chain
, fw
, mask
, handle
);
1677 for_each_chain(int (*fn
)(const ipt_chainlabel
, int, iptc_handle_t
*),
1678 int verbose
, int builtinstoo
, iptc_handle_t
*handle
)
1683 unsigned int i
, chaincount
= 0;
1685 chain
= iptc_first_chain(handle
);
1688 chain
= iptc_next_chain(handle
);
1691 chains
= fw_malloc(sizeof(ipt_chainlabel
) * chaincount
);
1693 chain
= iptc_first_chain(handle
);
1695 strcpy(chains
+ i
*sizeof(ipt_chainlabel
), chain
);
1697 chain
= iptc_next_chain(handle
);
1700 for (i
= 0; i
< chaincount
; i
++) {
1702 && iptc_builtin(chains
+ i
*sizeof(ipt_chainlabel
),
1705 ret
&= fn(chains
+ i
*sizeof(ipt_chainlabel
), verbose
, handle
);
1713 flush_entries(const ipt_chainlabel chain
, int verbose
,
1714 iptc_handle_t
*handle
)
1717 return for_each_chain(flush_entries
, verbose
, 1, handle
);
1720 fprintf(stdout
, "Flushing chain `%s'\n", chain
);
1721 return iptc_flush_entries(chain
, handle
);
1725 zero_entries(const ipt_chainlabel chain
, int verbose
,
1726 iptc_handle_t
*handle
)
1729 return for_each_chain(zero_entries
, verbose
, 1, handle
);
1732 fprintf(stdout
, "Zeroing chain `%s'\n", chain
);
1733 return iptc_zero_entries(chain
, handle
);
1737 delete_chain(const ipt_chainlabel chain
, int verbose
,
1738 iptc_handle_t
*handle
)
1741 return for_each_chain(delete_chain
, verbose
, 0, handle
);
1744 fprintf(stdout
, "Deleting chain `%s'\n", chain
);
1745 return iptc_delete_chain(chain
, handle
);
1749 list_entries(const ipt_chainlabel chain
, int verbose
, int numeric
,
1750 int expanded
, int linenumbers
, iptc_handle_t
*handle
)
1753 unsigned int format
;
1756 format
= FMT_OPTIONS
;
1758 format
|= FMT_NOCOUNTS
;
1763 format
|= FMT_NUMERIC
;
1766 format
|= FMT_KILOMEGAGIGA
;
1769 format
|= FMT_LINENUMBERS
;
1771 for (this = iptc_first_chain(handle
);
1773 this = iptc_next_chain(handle
)) {
1774 const struct ipt_entry
*i
;
1777 if (chain
&& strcmp(chain
, this) != 0)
1780 if (found
) printf("\n");
1782 print_header(format
, this, handle
);
1783 i
= iptc_first_rule(this, handle
);
1788 iptc_get_target(i
, handle
),
1792 i
= iptc_next_rule(i
, handle
);
1801 static char *get_modprobe(void)
1803 return strdup("/sbin/modprobe");
1808 #define PROCFILE_BUFSIZ 1024
1809 procfile
= open(PROC_SYS_MODPROBE
, O_RDONLY
);
1813 ret
= (char *) malloc(PROCFILE_BUFSIZ
);
1815 memset(ret
, 0, PROCFILE_BUFSIZ
);
1816 switch (read(procfile
, ret
, PROCFILE_BUFSIZ
)) {
1818 case PROCFILE_BUFSIZ
: goto fail
; /* Partial read. Wierd */
1820 if (ret
[strlen(ret
)-1]=='\n')
1821 ret
[strlen(ret
)-1]=0;
1832 int iptables_insmod(const char *modname
, const char *modprobe
, int quiet
)
1838 /* If they don't explicitly set it, read out of kernel */
1840 buf
= get_modprobe();
1848 argv
[0] = (char *)modprobe
;
1849 argv
[1] = (char *)modname
;
1857 execv(argv
[0], argv
);
1859 /* not usually reached */
1864 default: /* parent */
1869 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0)
1874 int load_iptables_ko(const char *modprobe
, int quiet
)
1876 // not needed, modprobe will be just noisy -- zzz
1879 static int loaded = 0;
1880 static int ret = -1;
1883 ret = iptables_insmod("ip_tables", modprobe, quiet);
1884 loaded = (ret == 0);
1891 static struct ipt_entry
*
1892 generate_entry(const struct ipt_entry
*fw
,
1893 struct iptables_rule_match
*matches
,
1894 struct ipt_entry_target
*target
)
1897 struct iptables_rule_match
*matchp
;
1898 struct ipt_entry
*e
;
1900 size
= sizeof(struct ipt_entry
);
1901 for (matchp
= matches
; matchp
; matchp
= matchp
->next
)
1902 size
+= matchp
->match
->m
->u
.match_size
;
1904 e
= fw_malloc(size
+ target
->u
.target_size
);
1906 e
->target_offset
= size
;
1907 e
->next_offset
= size
+ target
->u
.target_size
;
1910 for (matchp
= matches
; matchp
; matchp
= matchp
->next
) {
1911 memcpy(e
->elems
+ size
, matchp
->match
->m
, matchp
->match
->m
->u
.match_size
);
1912 size
+= matchp
->match
->m
->u
.match_size
;
1914 memcpy(e
->elems
+ size
, target
, target
->u
.target_size
);
1919 void clear_rule_matches(struct iptables_rule_match
**matches
)
1921 struct iptables_rule_match
*matchp
, *tmp
;
1923 for (matchp
= *matches
; matchp
;) {
1925 if (matchp
->match
->m
) {
1926 free(matchp
->match
->m
);
1927 matchp
->match
->m
= NULL
;
1929 if (matchp
->match
== matchp
->match
->next
) {
1930 free(matchp
->match
);
1931 matchp
->match
= NULL
;
1940 static void set_revision(char *name
, u_int8_t revision
)
1942 /* Old kernel sources don't have ".revision" field,
1943 but we stole a byte from name. */
1944 name
[IPT_FUNCTION_MAXNAMELEN
- 2] = '\0';
1945 name
[IPT_FUNCTION_MAXNAMELEN
- 1] = revision
;
1949 get_kernel_version(void) {
1950 static struct utsname uts
;
1951 int x
= 0, y
= 0, z
= 0;
1953 if (uname(&uts
) == -1) {
1954 fprintf(stderr
, "Unable to retrieve kernel version.\n");
1959 sscanf(uts
.release
, "%d.%d.%d", &x
, &y
, &z
);
1960 kernel_version
= LINUX_VERSION(x
, y
, z
);
1963 int do_command(int argc
, char *argv
[], char **table
, iptc_handle_t
*handle
)
1965 struct ipt_entry fw
, *e
= NULL
;
1967 unsigned int nsaddrs
= 0, ndaddrs
= 0;
1968 struct in_addr
*saddrs
= NULL
, *daddrs
= NULL
;
1971 const char *chain
= NULL
;
1972 const char *shostnetworkmask
= NULL
, *dhostnetworkmask
= NULL
;
1973 const char *policy
= NULL
, *newname
= NULL
;
1974 unsigned int rulenum
= 0, options
= 0, command
= 0;
1975 const char *pcnt
= NULL
, *bcnt
= NULL
;
1977 struct iptables_match
*m
;
1978 struct iptables_rule_match
*matches
= NULL
;
1979 struct iptables_rule_match
*matchp
;
1980 struct iptables_target
*target
= NULL
;
1981 struct iptables_target
*t
;
1982 const char *jumpto
= "";
1983 char *protocol
= NULL
;
1986 memset(&fw
, 0, sizeof(fw
));
1988 /* re-set optind to 0 in case do_command gets called
1992 /* clear mflags in case do_command gets called a second time
1993 * (we clear the global list of all matches for security)*/
1994 for (m
= iptables_matches
; m
; m
= m
->next
)
1997 for (t
= iptables_targets
; t
; t
= t
->next
) {
2002 /* Suppress error messages: we may add new options if we
2003 demand-load a protocol. */
2006 while ((c
= getopt_long(argc
, argv
,
2007 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
2008 opts
, NULL
)) != -1) {
2014 add_command(&command
, CMD_APPEND
, CMD_NONE
,
2020 add_command(&command
, CMD_DELETE
, CMD_NONE
,
2023 if (optind
< argc
&& argv
[optind
][0] != '-'
2024 && argv
[optind
][0] != '!') {
2025 rulenum
= parse_rulenumber(argv
[optind
++]);
2026 command
= CMD_DELETE_NUM
;
2031 add_command(&command
, CMD_REPLACE
, CMD_NONE
,
2034 if (optind
< argc
&& argv
[optind
][0] != '-'
2035 && argv
[optind
][0] != '!')
2036 rulenum
= parse_rulenumber(argv
[optind
++]);
2038 exit_error(PARAMETER_PROBLEM
,
2039 "-%c requires a rule number",
2040 cmd2char(CMD_REPLACE
));
2044 add_command(&command
, CMD_INSERT
, CMD_NONE
,
2047 if (optind
< argc
&& argv
[optind
][0] != '-'
2048 && argv
[optind
][0] != '!')
2049 rulenum
= parse_rulenumber(argv
[optind
++]);
2054 add_command(&command
, CMD_LIST
, CMD_ZERO
,
2056 if (optarg
) chain
= optarg
;
2057 else if (optind
< argc
&& argv
[optind
][0] != '-'
2058 && argv
[optind
][0] != '!')
2059 chain
= argv
[optind
++];
2063 add_command(&command
, CMD_FLUSH
, CMD_NONE
,
2065 if (optarg
) chain
= optarg
;
2066 else if (optind
< argc
&& argv
[optind
][0] != '-'
2067 && argv
[optind
][0] != '!')
2068 chain
= argv
[optind
++];
2072 add_command(&command
, CMD_ZERO
, CMD_LIST
,
2074 if (optarg
) chain
= optarg
;
2075 else if (optind
< argc
&& argv
[optind
][0] != '-'
2076 && argv
[optind
][0] != '!')
2077 chain
= argv
[optind
++];
2081 if (optarg
&& (*optarg
== '-' || *optarg
== '!'))
2082 exit_error(PARAMETER_PROBLEM
,
2083 "chain name not allowed to start "
2084 "with `%c'\n", *optarg
);
2085 if (find_target(optarg
, TRY_LOAD
))
2086 exit_error(PARAMETER_PROBLEM
,
2087 "chain name may not clash "
2088 "with target name\n");
2089 add_command(&command
, CMD_NEW_CHAIN
, CMD_NONE
,
2095 add_command(&command
, CMD_DELETE_CHAIN
, CMD_NONE
,
2097 if (optarg
) chain
= optarg
;
2098 else if (optind
< argc
&& argv
[optind
][0] != '-'
2099 && argv
[optind
][0] != '!')
2100 chain
= argv
[optind
++];
2104 add_command(&command
, CMD_RENAME_CHAIN
, CMD_NONE
,
2107 if (optind
< argc
&& argv
[optind
][0] != '-'
2108 && argv
[optind
][0] != '!')
2109 newname
= argv
[optind
++];
2111 exit_error(PARAMETER_PROBLEM
,
2112 "-%c requires old-chain-name and "
2114 cmd2char(CMD_RENAME_CHAIN
));
2118 add_command(&command
, CMD_SET_POLICY
, CMD_NONE
,
2121 if (optind
< argc
&& argv
[optind
][0] != '-'
2122 && argv
[optind
][0] != '!')
2123 policy
= argv
[optind
++];
2125 exit_error(PARAMETER_PROBLEM
,
2126 "-%c requires a chain and a policy",
2127 cmd2char(CMD_SET_POLICY
));
2132 optarg
= argv
[optind
];
2134 /* iptables -p icmp -h */
2135 if (!matches
&& protocol
)
2136 find_match(protocol
, TRY_LOAD
, &matches
);
2138 exit_printhelp(matches
);
2144 check_inverse(optarg
, &invert
, &optind
, argc
);
2145 set_option(&options
, OPT_PROTOCOL
, &fw
.ip
.invflags
,
2148 /* Canonicalize into lower case */
2149 for (protocol
= argv
[optind
-1]; *protocol
; protocol
++)
2150 *protocol
= tolower(*protocol
);
2152 protocol
= argv
[optind
-1];
2153 fw
.ip
.proto
= parse_protocol(protocol
);
2155 if (fw
.ip
.proto
== 0
2156 && (fw
.ip
.invflags
& IPT_INV_PROTO
))
2157 exit_error(PARAMETER_PROBLEM
,
2158 "rule would never match protocol");
2162 check_inverse(optarg
, &invert
, &optind
, argc
);
2163 set_option(&options
, OPT_SOURCE
, &fw
.ip
.invflags
,
2165 shostnetworkmask
= argv
[optind
-1];
2169 check_inverse(optarg
, &invert
, &optind
, argc
);
2170 set_option(&options
, OPT_DESTINATION
, &fw
.ip
.invflags
,
2172 dhostnetworkmask
= argv
[optind
-1];
2177 set_option(&options
, OPT_JUMP
, &fw
.ip
.invflags
,
2179 fw
.ip
.flags
|= IPT_F_GOTO
;
2180 jumpto
= parse_target(optarg
);
2185 set_option(&options
, OPT_JUMP
, &fw
.ip
.invflags
,
2187 jumpto
= parse_target(optarg
);
2188 /* TRY_LOAD (may be chain name) */
2189 target
= find_target(jumpto
, TRY_LOAD
);
2194 size
= IPT_ALIGN(sizeof(struct ipt_entry_target
))
2197 target
->t
= fw_calloc(1, size
);
2198 target
->t
->u
.target_size
= size
;
2199 strcpy(target
->t
->u
.user
.name
, jumpto
);
2200 set_revision(target
->t
->u
.user
.name
,
2202 if (target
->init
!= NULL
)
2203 target
->init(target
->t
, &fw
.nfcache
);
2204 opts
= merge_options(opts
, target
->extra_opts
, &target
->option_offset
);
2210 check_inverse(optarg
, &invert
, &optind
, argc
);
2211 set_option(&options
, OPT_VIANAMEIN
, &fw
.ip
.invflags
,
2213 parse_interface(argv
[optind
-1],
2215 fw
.ip
.iniface_mask
);
2219 check_inverse(optarg
, &invert
, &optind
, argc
);
2220 set_option(&options
, OPT_VIANAMEOUT
, &fw
.ip
.invflags
,
2222 parse_interface(argv
[optind
-1],
2224 fw
.ip
.outiface_mask
);
2228 set_option(&options
, OPT_FRAGMENT
, &fw
.ip
.invflags
,
2230 fw
.ip
.flags
|= IPT_F_FRAG
;
2235 set_option(&options
, OPT_VERBOSE
,
2236 &fw
.ip
.invflags
, invert
);
2244 exit_error(PARAMETER_PROBLEM
,
2245 "unexpected ! flag before --match");
2247 m
= find_match(optarg
, LOAD_MUST_SUCCEED
, &matches
);
2248 size
= IPT_ALIGN(sizeof(struct ipt_entry_match
))
2250 m
->m
= fw_calloc(1, size
);
2251 m
->m
->u
.match_size
= size
;
2252 strcpy(m
->m
->u
.user
.name
, m
->name
);
2253 set_revision(m
->m
->u
.user
.name
, m
->revision
);
2254 if (m
->init
!= NULL
)
2255 m
->init(m
->m
, &fw
.nfcache
);
2257 /* Merge options for non-cloned matches */
2258 opts
= merge_options(opts
, m
->extra_opts
, &m
->option_offset
);
2263 set_option(&options
, OPT_NUMERIC
, &fw
.ip
.invflags
,
2269 exit_error(PARAMETER_PROBLEM
,
2270 "unexpected ! flag before --table");
2271 *table
= argv
[optind
-1];
2275 set_option(&options
, OPT_EXPANDED
, &fw
.ip
.invflags
,
2281 printf("Not %s ;-)\n", program_version
);
2284 program_name
, program_version
);
2288 set_option(&options
, OPT_LINENUMBERS
, &fw
.ip
.invflags
,
2298 set_option(&options
, OPT_COUNTERS
, &fw
.ip
.invflags
,
2301 if (optind
< argc
&& argv
[optind
][0] != '-'
2302 && argv
[optind
][0] != '!')
2303 bcnt
= argv
[optind
++];
2305 exit_error(PARAMETER_PROBLEM
,
2306 "-%c requires packet and byte counter",
2307 opt2char(OPT_COUNTERS
));
2309 if (sscanf(pcnt
, "%llu", (unsigned long long *)&fw
.counters
.pcnt
) != 1)
2310 exit_error(PARAMETER_PROBLEM
,
2311 "-%c packet counter not numeric",
2312 opt2char(OPT_COUNTERS
));
2314 if (sscanf(bcnt
, "%llu", (unsigned long long *)&fw
.counters
.bcnt
) != 1)
2315 exit_error(PARAMETER_PROBLEM
,
2316 "-%c byte counter not numeric",
2317 opt2char(OPT_COUNTERS
));
2322 case 1: /* non option */
2323 if (optarg
[0] == '!' && optarg
[1] == '\0') {
2325 exit_error(PARAMETER_PROBLEM
,
2326 "multiple consecutive ! not"
2332 printf("Bad argument `%s'\n", optarg
);
2337 || !(target
->parse(c
- target
->option_offset
,
2340 &fw
, &target
->t
))) {
2341 for (matchp
= matches
; matchp
; matchp
= matchp
->next
) {
2342 if (matchp
->completed
)
2344 if (matchp
->match
->parse(c
- matchp
->match
->option_offset
,
2346 &matchp
->match
->mflags
,
2352 m
= matchp
? matchp
->match
: NULL
;
2354 /* If you listen carefully, you can
2355 actually hear this code suck. */
2357 /* some explanations (after four different bugs
2358 * in 3 different releases): If we encounter a
2359 * parameter, that has not been parsed yet,
2360 * it's not an option of an explicitly loaded
2361 * match or a target. However, we support
2362 * implicit loading of the protocol match
2363 * extension. '-p tcp' means 'l4 proto 6' and
2364 * at the same time 'load tcp protocol match on
2365 * demand if we specify --dport'.
2367 * To make this work, we need to make sure:
2368 * - the parameter has not been parsed by
2370 * - a protocol has been specified
2371 * - the protocol extension has not been
2372 * loaded yet, or is loaded and unused
2373 * [think of iptables-restore!]
2374 * - the protocol extension can be successively
2379 && (!find_proto(protocol
, DONT_LOAD
,
2380 options
&OPT_NUMERIC
, NULL
)
2381 || (find_proto(protocol
, DONT_LOAD
,
2382 options
&OPT_NUMERIC
, NULL
)
2383 && (proto_used
== 0))
2385 && (m
= find_proto(protocol
, TRY_LOAD
,
2386 options
&OPT_NUMERIC
, &matches
))) {
2387 /* Try loading protocol */
2392 size
= IPT_ALIGN(sizeof(struct ipt_entry_match
))
2395 m
->m
= fw_calloc(1, size
);
2396 m
->m
->u
.match_size
= size
;
2397 strcpy(m
->m
->u
.user
.name
, m
->name
);
2398 set_revision(m
->m
->u
.user
.name
,
2400 if (m
->init
!= NULL
)
2401 m
->init(m
->m
, &fw
.nfcache
);
2403 opts
= merge_options(opts
,
2404 m
->extra_opts
, &m
->option_offset
);
2410 exit_error(PARAMETER_PROBLEM
,
2418 for (matchp
= matches
; matchp
; matchp
= matchp
->next
)
2419 matchp
->match
->final_check(matchp
->match
->mflags
);
2422 target
->final_check(target
->tflags
);
2424 /* Fix me: must put inverse options checking here --MN */
2427 exit_error(PARAMETER_PROBLEM
,
2428 "unknown arguments found on commandline");
2430 exit_error(PARAMETER_PROBLEM
, "no command specified");
2432 exit_error(PARAMETER_PROBLEM
,
2433 "nothing appropriate following !");
2435 if (command
& (CMD_REPLACE
| CMD_INSERT
| CMD_DELETE
| CMD_APPEND
)) {
2436 if (!(options
& OPT_DESTINATION
))
2437 dhostnetworkmask
= "0.0.0.0/0";
2438 if (!(options
& OPT_SOURCE
))
2439 shostnetworkmask
= "0.0.0.0/0";
2442 if (shostnetworkmask
)
2443 parse_hostnetworkmask(shostnetworkmask
, &saddrs
,
2444 &(fw
.ip
.smsk
), &nsaddrs
);
2446 if (dhostnetworkmask
)
2447 parse_hostnetworkmask(dhostnetworkmask
, &daddrs
,
2448 &(fw
.ip
.dmsk
), &ndaddrs
);
2450 if ((nsaddrs
> 1 || ndaddrs
> 1) &&
2451 (fw
.ip
.invflags
& (IPT_INV_SRCIP
| IPT_INV_DSTIP
)))
2452 exit_error(PARAMETER_PROBLEM
, "! not allowed with multiple"
2453 " source or destination IP addresses");
2455 if (command
== CMD_REPLACE
&& (nsaddrs
!= 1 || ndaddrs
!= 1))
2456 exit_error(PARAMETER_PROBLEM
, "Replacement rule does not "
2457 "specify a unique address");
2459 generic_opt_check(command
, options
);
2461 if (chain
&& strlen(chain
) > IPT_FUNCTION_MAXNAMELEN
)
2462 exit_error(PARAMETER_PROBLEM
,
2463 "chain name `%s' too long (must be under %i chars)",
2464 chain
, IPT_FUNCTION_MAXNAMELEN
);
2466 /* only allocate handle if we weren't called with a handle */
2468 *handle
= iptc_init(*table
);
2470 /* try to insmod the module if iptc_init failed */
2471 if (!*handle
&& load_iptables_ko(modprobe
, 0) != -1)
2472 *handle
= iptc_init(*table
);
2475 exit_error(VERSION_PROBLEM
,
2476 "can't initialize iptables table `%s': %s",
2477 *table
, iptc_strerror(errno
));
2479 if (command
== CMD_APPEND
2480 || command
== CMD_DELETE
2481 || command
== CMD_INSERT
2482 || command
== CMD_REPLACE
) {
2483 if (strcmp(chain
, "PREROUTING") == 0
2484 || strcmp(chain
, "INPUT") == 0) {
2485 /* -o not valid with incoming packets. */
2486 if (options
& OPT_VIANAMEOUT
)
2487 exit_error(PARAMETER_PROBLEM
,
2488 "Can't use -%c with %s\n",
2489 opt2char(OPT_VIANAMEOUT
),
2493 if (strcmp(chain
, "POSTROUTING") == 0
2494 || strcmp(chain
, "OUTPUT") == 0) {
2495 /* -i not valid with outgoing packets */
2496 if (options
& OPT_VIANAMEIN
)
2497 exit_error(PARAMETER_PROBLEM
,
2498 "Can't use -%c with %s\n",
2499 opt2char(OPT_VIANAMEIN
),
2503 if (target
&& iptc_is_chain(jumpto
, *handle
)) {
2504 printf("Warning: using chain %s, not extension\n",
2513 /* If they didn't specify a target, or it's a chain
2514 name, use standard. */
2516 && (strlen(jumpto
) == 0
2517 || iptc_is_chain(jumpto
, *handle
))) {
2520 target
= find_target(IPT_STANDARD_TARGET
,
2523 size
= sizeof(struct ipt_entry_target
)
2525 target
->t
= fw_calloc(1, size
);
2526 target
->t
->u
.target_size
= size
;
2527 strcpy(target
->t
->u
.user
.name
, jumpto
);
2528 if (!iptc_is_chain(jumpto
, *handle
))
2529 set_revision(target
->t
->u
.user
.name
,
2531 if (target
->init
!= NULL
)
2532 target
->init(target
->t
, &fw
.nfcache
);
2536 /* it is no chain, and we can't load a plugin.
2537 * We cannot know if the plugin is corrupt, non
2538 * existant OR if the user just misspelled a
2541 if (fw
.ip
.flags
& IPT_F_GOTO
)
2542 exit_error(PARAMETER_PROBLEM
,
2543 "goto '%s' is not a chain\n", jumpto
);
2545 find_target(jumpto
, LOAD_MUST_SUCCEED
);
2547 e
= generate_entry(&fw
, matches
, target
->t
);
2554 ret
= append_entry(chain
, e
,
2555 nsaddrs
, saddrs
, ndaddrs
, daddrs
,
2556 options
&OPT_VERBOSE
,
2560 ret
= delete_entry(chain
, e
,
2561 nsaddrs
, saddrs
, ndaddrs
, daddrs
,
2562 options
&OPT_VERBOSE
,
2565 case CMD_DELETE_NUM
:
2566 ret
= iptc_delete_num_entry(chain
, rulenum
- 1, handle
);
2569 ret
= replace_entry(chain
, e
, rulenum
- 1,
2570 saddrs
, daddrs
, options
&OPT_VERBOSE
,
2574 ret
= insert_entry(chain
, e
, rulenum
- 1,
2575 nsaddrs
, saddrs
, ndaddrs
, daddrs
,
2576 options
&OPT_VERBOSE
,
2580 ret
= list_entries(chain
,
2581 options
&OPT_VERBOSE
,
2582 options
&OPT_NUMERIC
,
2583 options
&OPT_EXPANDED
,
2584 options
&OPT_LINENUMBERS
,
2588 ret
= flush_entries(chain
, options
&OPT_VERBOSE
, handle
);
2591 ret
= zero_entries(chain
, options
&OPT_VERBOSE
, handle
);
2593 case CMD_LIST
|CMD_ZERO
:
2594 ret
= list_entries(chain
,
2595 options
&OPT_VERBOSE
,
2596 options
&OPT_NUMERIC
,
2597 options
&OPT_EXPANDED
,
2598 options
&OPT_LINENUMBERS
,
2601 ret
= zero_entries(chain
,
2602 options
&OPT_VERBOSE
, handle
);
2605 ret
= iptc_create_chain(chain
, handle
);
2607 case CMD_DELETE_CHAIN
:
2608 ret
= delete_chain(chain
, options
&OPT_VERBOSE
, handle
);
2610 case CMD_RENAME_CHAIN
:
2611 ret
= iptc_rename_chain(chain
, newname
, handle
);
2613 case CMD_SET_POLICY
:
2614 ret
= iptc_set_policy(chain
, policy
, NULL
, handle
);
2617 /* We should never reach this... */
2622 dump_entries(*handle
);
2624 clear_rule_matches(&matches
);