blob: 7096121cbc5f23b90ab94ecdae8979f46c7a36cd [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000034#include <ctype.h>
35#include <stdarg.h>
36#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000037#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000038#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000041#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050#define FMT_NUMERIC 0x0001
51#define FMT_NOCOUNTS 0x0002
52#define FMT_KILOMEGAGIGA 0x0004
53#define FMT_OPTIONS 0x0008
54#define FMT_NOTABLE 0x0010
55#define FMT_NOTARGET 0x0020
56#define FMT_VIA 0x0040
57#define FMT_NONEWLINE 0x0080
58#define FMT_LINENUMBERS 0x0100
59
60#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
61 | FMT_NUMERIC | FMT_NOTABLE)
62#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
63
64
65#define CMD_NONE 0x0000U
66#define CMD_INSERT 0x0001U
67#define CMD_DELETE 0x0002U
68#define CMD_DELETE_NUM 0x0004U
69#define CMD_REPLACE 0x0008U
70#define CMD_APPEND 0x0010U
71#define CMD_LIST 0x0020U
72#define CMD_FLUSH 0x0040U
73#define CMD_ZERO 0x0080U
74#define CMD_NEW_CHAIN 0x0100U
75#define CMD_DELETE_CHAIN 0x0200U
76#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000077#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000078#define NUMBER_OF_CMD 13
79static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000080 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000081
82#define OPTION_OFFSET 256
83
84#define OPT_NONE 0x00000U
85#define OPT_NUMERIC 0x00001U
86#define OPT_SOURCE 0x00002U
87#define OPT_DESTINATION 0x00004U
88#define OPT_PROTOCOL 0x00008U
89#define OPT_JUMP 0x00010U
90#define OPT_VERBOSE 0x00020U
91#define OPT_EXPANDED 0x00040U
92#define OPT_VIANAMEIN 0x00080U
93#define OPT_VIANAMEOUT 0x00100U
94#define OPT_FRAGMENT 0x00200U
95#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000096#define OPT_COUNTERS 0x00800U
97#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +000098static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000099= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000100
101static struct option original_opts[] = {
Patrick McHardy0b639362007-09-08 16:00:01 +0000102 { "append", 1, NULL, 'A' },
103 { "delete", 1, NULL, 'D' },
104 { "insert", 1, NULL, 'I' },
105 { "replace", 1, NULL, 'R' },
106 { "list", 2, NULL, 'L' },
107 { "flush", 2, NULL, 'F' },
108 { "zero", 2, NULL, 'Z' },
109 { "new-chain", 1, NULL, 'N' },
110 { "delete-chain", 2, NULL, 'X' },
111 { "rename-chain", 1, NULL, 'E' },
112 { "policy", 1, NULL, 'P' },
113 { "source", 1, NULL, 's' },
114 { "destination", 1, NULL, 'd' },
115 { "src", 1, NULL, 's' }, /* synonym */
116 { "dst", 1, NULL, 'd' }, /* synonym */
117 { "protocol", 1, NULL, 'p' },
118 { "in-interface", 1, NULL, 'i' },
119 { "jump", 1, NULL, 'j' },
120 { "table", 1, NULL, 't' },
121 { "match", 1, NULL, 'm' },
122 { "numeric", 0, NULL, 'n' },
123 { "out-interface", 1, NULL, 'o' },
124 { "verbose", 0, NULL, 'v' },
125 { "exact", 0, NULL, 'x' },
126 { "fragments", 0, NULL, 'f' },
127 { "version", 0, NULL, 'V' },
128 { "help", 2, NULL, 'h' },
129 { "line-numbers", 0, NULL, '0' },
130 { "modprobe", 1, NULL, 'M' },
131 { "set-counters", 1, NULL, 'c' },
132 { "goto", 1, NULL, 'g' },
133 { }
Marc Bouchere6869a82000-03-20 06:03:29 +0000134};
135
Illes Marci63e90632003-03-03 08:08:37 +0000136/* we need this for iptables-restore. iptables-restore.c sets line to the
137 * current line of the input file, in order to give a more precise error
138 * message. iptables itself doesn't need this, so it is initialized to the
139 * magic number of -1 */
140int line = -1;
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142static struct option *opts = original_opts;
143static unsigned int global_option_offset = 0;
144
145/* Table of legal combinations of commands and options. If any of the
146 * given commands make an option legal, that option is legal (applies to
147 * CMD_LIST and CMD_ZERO only).
148 * Key:
149 * + compulsory
150 * x illegal
151 * optional
152 */
153
154static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
155/* Well, it's better than "Re: Linux vs FreeBSD" */
156{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157 /* -n -s -d -p -j -v -x -i -o -f --line -c */
158/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
159/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
160/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
161/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
162/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
163/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
164/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
165/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000170};
171
172static int inverse_for_options[NUMBER_OF_OPT] =
173{
174/* -n */ 0,
175/* -s */ IPT_INV_SRCIP,
176/* -d */ IPT_INV_DSTIP,
177/* -p */ IPT_INV_PROTO,
178/* -j */ 0,
179/* -v */ 0,
180/* -x */ 0,
181/* -i */ IPT_INV_VIA_IN,
182/* -o */ IPT_INV_VIA_OUT,
183/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000184/*--line*/ 0,
185/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000186};
187
188const char *program_version;
189const char *program_name;
190
Phil Oester8cf65912005-09-19 15:00:33 +0000191int kernel_version;
192
Marc Bouchere6869a82000-03-20 06:03:29 +0000193extern void dump_entries(const iptc_handle_t handle);
194
195/* A few hardcoded protocols for 'all' and in case the user has no
196 /etc/protocols */
197struct pprot {
198 char *name;
199 u_int8_t num;
200};
201
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000202struct afinfo afinfo = {
203 .family = AF_INET,
204 .libprefix = "libipt_",
205 .ipproto = IPPROTO_IP,
206 .kmod = "ip_tables",
207 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
208 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
209};
210
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000211/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000212/* defined in netinet/in.h */
213#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214#ifndef IPPROTO_ESP
215#define IPPROTO_ESP 50
216#endif
217#ifndef IPPROTO_AH
218#define IPPROTO_AH 51
219#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000220#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000221
Marc Bouchere6869a82000-03-20 06:03:29 +0000222static const struct pprot chain_protos[] = {
223 { "tcp", IPPROTO_TCP },
224 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000225 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000227 { "esp", IPPROTO_ESP },
228 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000229 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000230 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231};
232
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000233static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000234proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000235{
236 unsigned int i;
237
Rusty Russell28381a42000-05-10 00:19:50 +0000238 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000251enum {
252 IPT_DOTTED_ADDR = 0,
253 IPT_DOTTED_MASK
254};
255
256static struct in_addr *
257__dotted_to_addr(const char *dotted, int type)
Marc Bouchere6869a82000-03-20 06:03:29 +0000258{
259 static struct in_addr addr;
260 unsigned char *addrp;
261 char *p, *q;
Harald Welteed498492001-07-23 01:24:22 +0000262 unsigned int onebyte;
263 int i;
Marc Bouchere6869a82000-03-20 06:03:29 +0000264 char buf[20];
265
266 /* copy dotted string, because we need to modify it */
267 strncpy(buf, dotted, sizeof(buf) - 1);
Karsten Desler9cc354f2004-01-31 13:22:18 +0000268 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000269 addrp = (unsigned char *) &(addr.s_addr);
270
271 p = buf;
272 for (i = 0; i < 3; i++) {
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000273 if ((q = strchr(p, '.')) == NULL) {
274 if (type == IPT_DOTTED_ADDR) {
275 /* autocomplete, this is a network address */
276 if (string_to_number(p, 0, 255, &onebyte) == -1)
277 return (struct in_addr *) NULL;
278
279 addrp[i] = (unsigned char) onebyte;
280 while (i < 3)
281 addrp[++i] = 0;
282
283 return &addr;
284 } else
285 return (struct in_addr *) NULL;
286 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000287
288 *q = '\0';
Harald Welteed498492001-07-23 01:24:22 +0000289 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000290 return (struct in_addr *) NULL;
291
292 addrp[i] = (unsigned char) onebyte;
293 p = q + 1;
294 }
295
296 /* we've checked 3 bytes, now we check the last one */
Harald Welteed498492001-07-23 01:24:22 +0000297 if (string_to_number(p, 0, 255, &onebyte) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000298 return (struct in_addr *) NULL;
299
300 addrp[3] = (unsigned char) onebyte;
301
302 return &addr;
303}
304
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000305struct in_addr *
306dotted_to_addr(const char *dotted)
307{
308 return __dotted_to_addr(dotted, IPT_DOTTED_ADDR);
309}
310
311struct in_addr *
312dotted_to_mask(const char *dotted)
313{
314 return __dotted_to_addr(dotted, IPT_DOTTED_MASK);
315}
316
Marc Bouchere6869a82000-03-20 06:03:29 +0000317static struct in_addr *
318network_to_addr(const char *name)
319{
320 struct netent *net;
321 static struct in_addr addr;
322
323 if ((net = getnetbyname(name)) != NULL) {
324 if (net->n_addrtype != AF_INET)
325 return (struct in_addr *) NULL;
326 addr.s_addr = htonl((unsigned long) net->n_net);
327 return &addr;
328 }
329
330 return (struct in_addr *) NULL;
331}
332
333static void
334inaddrcpy(struct in_addr *dst, struct in_addr *src)
335{
336 /* memcpy(dst, src, sizeof(struct in_addr)); */
337 dst->s_addr = src->s_addr;
338}
339
Pablo Neiradfdcd642005-05-29 19:05:23 +0000340static void free_opts(int reset_offset)
341{
342 if (opts != original_opts) {
343 free(opts);
344 opts = original_opts;
345 if (reset_offset)
346 global_option_offset = 0;
347 }
348}
349
Patrick McHardy0b639362007-09-08 16:00:01 +0000350static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000351exit_tryhelp(int status)
352{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000353 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000354 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000355 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
356 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000357 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000358 exit(status);
359}
360
Patrick McHardy0b639362007-09-08 16:00:01 +0000361static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000362exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000363{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000364 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000365 struct iptables_target *t = NULL;
366
Marc Bouchere6869a82000-03-20 06:03:29 +0000367 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000368"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000369" %s -[RI] chain rulenum rule-specification [options]\n"
370" %s -D chain rulenum [options]\n"
371" %s -[LFZ] [chain] [options]\n"
372" %s -[NX] chain\n"
373" %s -E old-chain-name new-chain-name\n"
374" %s -P chain target [options]\n"
375" %s -h (print this help information)\n\n",
376 program_name, program_version, program_name, program_name,
377 program_name, program_name, program_name, program_name,
378 program_name, program_name);
379
380 printf(
381"Commands:\n"
382"Either long or short options are allowed.\n"
383" --append -A chain Append to chain\n"
384" --delete -D chain Delete matching rule from chain\n"
385" --delete -D chain rulenum\n"
386" Delete rule rulenum (1 = first) from chain\n"
387" --insert -I chain [rulenum]\n"
388" Insert in chain as rulenum (default 1=first)\n"
389" --replace -R chain rulenum\n"
390" Replace rule rulenum (1 = first) in chain\n"
391" --list -L [chain] List the rules in a chain or all chains\n"
392" --flush -F [chain] Delete all rules in chain or all chains\n"
393" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000394" --new -N chain Create a new user-defined chain\n"
395" --delete-chain\n"
396" -X [chain] Delete a user-defined chain\n"
397" --policy -P chain target\n"
398" Change policy on chain to target\n"
399" --rename-chain\n"
400" -E old-chain new-chain\n"
401" Change chain name, (moving any references)\n"
402
403"Options:\n"
404" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
405" --source -s [!] address[/mask]\n"
406" source specification\n"
407" --destination -d [!] address[/mask]\n"
408" destination specification\n"
409" --in-interface -i [!] input name[+]\n"
410" network interface name ([+] for wildcard)\n"
411" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000412" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000413#ifdef IPT_F_GOTO
414" --goto -g chain\n"
415" jump to chain with no return\n"
416#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000417" --match -m match\n"
418" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000419" --numeric -n numeric output of addresses and ports\n"
420" --out-interface -o [!] output name[+]\n"
421" network interface name ([+] for wildcard)\n"
422" --table -t table table to manipulate (default: `filter')\n"
423" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000424" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000425" --exact -x expand numbers (display exact values)\n"
426"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000427" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000428" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000429"[!] --version -V print package version.\n");
430
Rusty Russell363112d2000-08-11 13:49:26 +0000431 /* Print out any special helps. A user might like to be able
432 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000433 results. So we call help for all specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000434 for (t = xtables_targets; t ;t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000435 if (t->used) {
436 printf("\n");
437 t->help();
438 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000439 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000440 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000441 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000442 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000443 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000444 exit(0);
445}
446
Patrick McHardy0b639362007-09-08 16:00:01 +0000447void
448exit_error(enum exittype status, const char *msg, ...)
449{
450 va_list args;
451
452 va_start(args, msg);
453 fprintf(stderr, "%s v%s: ", program_name, program_version);
454 vfprintf(stderr, msg, args);
455 va_end(args);
456 fprintf(stderr, "\n");
457 if (status == PARAMETER_PROBLEM)
458 exit_tryhelp(status);
459 if (status == VERSION_PROBLEM)
460 fprintf(stderr,
461 "Perhaps iptables or your kernel needs to be upgraded.\n");
462 /* On error paths, make sure that we don't leak memory */
463 free_opts(1);
464 exit(status);
465}
466
Marc Bouchere6869a82000-03-20 06:03:29 +0000467static void
468generic_opt_check(int command, int options)
469{
470 int i, j, legal = 0;
471
472 /* Check that commands are valid with options. Complicated by the
473 * fact that if an option is legal with *any* command given, it is
474 * legal overall (ie. -z and -l).
475 */
476 for (i = 0; i < NUMBER_OF_OPT; i++) {
477 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
478
479 for (j = 0; j < NUMBER_OF_CMD; j++) {
480 if (!(command & (1<<j)))
481 continue;
482
483 if (!(options & (1<<i))) {
484 if (commands_v_options[j][i] == '+')
485 exit_error(PARAMETER_PROBLEM,
486 "You need to supply the `-%c' "
487 "option for this command\n",
488 optflags[i]);
489 } else {
490 if (commands_v_options[j][i] != 'x')
491 legal = 1;
492 else if (legal == 0)
493 legal = -1;
494 }
495 }
496 if (legal == -1)
497 exit_error(PARAMETER_PROBLEM,
498 "Illegal option `-%c' with this command\n",
499 optflags[i]);
500 }
501}
502
503static char
504opt2char(int option)
505{
506 const char *ptr;
507 for (ptr = optflags; option > 1; option >>= 1, ptr++);
508
509 return *ptr;
510}
511
512static char
513cmd2char(int option)
514{
515 const char *ptr;
516 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
517
518 return *ptr;
519}
520
521static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000522add_command(unsigned int *cmd, const int newcmd, const int othercmds,
523 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000524{
525 if (invert)
526 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
527 if (*cmd & (~othercmds))
528 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
529 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
530 *cmd |= newcmd;
531}
532
533int
Harald Welteb77f1da2002-03-14 11:35:58 +0000534check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000535{
536 if (option && strcmp(option, "!") == 0) {
537 if (*invert)
538 exit_error(PARAMETER_PROBLEM,
539 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000540 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000541 if (optind) {
542 *optind = *optind+1;
543 if (argc && *optind > argc)
544 exit_error(PARAMETER_PROBLEM,
545 "no argument following `!'");
546 }
547
Marc Bouchere6869a82000-03-20 06:03:29 +0000548 return TRUE;
549 }
550 return FALSE;
551}
552
Marc Bouchere6869a82000-03-20 06:03:29 +0000553static struct in_addr *
554host_to_addr(const char *name, unsigned int *naddr)
555{
556 struct hostent *host;
557 struct in_addr *addr;
558 unsigned int i;
559
560 *naddr = 0;
561 if ((host = gethostbyname(name)) != NULL) {
562 if (host->h_addrtype != AF_INET ||
563 host->h_length != sizeof(struct in_addr))
564 return (struct in_addr *) NULL;
565
566 while (host->h_addr_list[*naddr] != (char *) NULL)
567 (*naddr)++;
Patrick McHardy80938442004-08-03 22:38:39 +0000568 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr);
Marc Bouchere6869a82000-03-20 06:03:29 +0000569 for (i = 0; i < *naddr; i++)
570 inaddrcpy(&(addr[i]),
571 (struct in_addr *) host->h_addr_list[i]);
572 return addr;
573 }
574
575 return (struct in_addr *) NULL;
576}
577
578static char *
579addr_to_host(const struct in_addr *addr)
580{
581 struct hostent *host;
582
583 if ((host = gethostbyaddr((char *) addr,
584 sizeof(struct in_addr), AF_INET)) != NULL)
585 return (char *) host->h_name;
586
587 return (char *) NULL;
588}
589
590/*
591 * All functions starting with "parse" should succeed, otherwise
592 * the program fails.
593 * Most routines return pointers to static data that may change
594 * between calls to the same or other routines with a few exceptions:
595 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
596 * return global static data.
597*/
598
599static struct in_addr *
600parse_hostnetwork(const char *name, unsigned int *naddrs)
601{
602 struct in_addr *addrp, *addrptmp;
603
604 if ((addrptmp = dotted_to_addr(name)) != NULL ||
605 (addrptmp = network_to_addr(name)) != NULL) {
606 addrp = fw_malloc(sizeof(struct in_addr));
607 inaddrcpy(addrp, addrptmp);
608 *naddrs = 1;
609 return addrp;
610 }
611 if ((addrp = host_to_addr(name, naddrs)) != NULL)
612 return addrp;
613
614 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
615}
616
617static struct in_addr *
618parse_mask(char *mask)
619{
620 static struct in_addr maskaddr;
621 struct in_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000622 unsigned int bits;
Marc Bouchere6869a82000-03-20 06:03:29 +0000623
624 if (mask == NULL) {
625 /* no mask at all defaults to 32 bits */
626 maskaddr.s_addr = 0xFFFFFFFF;
627 return &maskaddr;
628 }
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000629 if ((addrp = dotted_to_mask(mask)) != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +0000630 /* dotted_to_addr already returns a network byte order addr */
631 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000632 if (string_to_number(mask, 0, 32, &bits) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000633 exit_error(PARAMETER_PROBLEM,
634 "invalid mask `%s' specified", mask);
635 if (bits != 0) {
636 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
637 return &maskaddr;
638 }
639
640 maskaddr.s_addr = 0L;
641 return &maskaddr;
642}
643
Marc Boucherb93c7982001-12-06 14:50:19 +0000644void
Marc Bouchere6869a82000-03-20 06:03:29 +0000645parse_hostnetworkmask(const char *name, struct in_addr **addrpp,
646 struct in_addr *maskp, unsigned int *naddrs)
647{
648 struct in_addr *addrp;
649 char buf[256];
650 char *p;
651 int i, j, k, n;
652
653 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler617b7dd2004-01-31 15:14:38 +0000654 buf[sizeof(buf) - 1] = '\0';
Marc Bouchere6869a82000-03-20 06:03:29 +0000655 if ((p = strrchr(buf, '/')) != NULL) {
656 *p = '\0';
657 addrp = parse_mask(p + 1);
658 } else
659 addrp = parse_mask(NULL);
660 inaddrcpy(maskp, addrp);
661
662 /* if a null mask is given, the name is ignored, like in "any/0" */
663 if (maskp->s_addr == 0L)
664 strcpy(buf, "0.0.0.0");
665
666 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
667 n = *naddrs;
668 for (i = 0, j = 0; i < n; i++) {
669 addrp[j++].s_addr &= maskp->s_addr;
670 for (k = 0; k < j - 1; k++) {
671 if (addrp[k].s_addr == addrp[j - 1].s_addr) {
672 (*naddrs)--;
673 j--;
674 break;
675 }
676 }
677 }
678}
679
Rusty Russell28381a42000-05-10 00:19:50 +0000680/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
681static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000682find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000683{
Harald Welteed498492001-07-23 01:24:22 +0000684 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000685
Harald Welte0b0013a2002-02-18 16:15:31 +0000686 if (string_to_number(pname, 0, 255, &proto) != -1) {
687 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000688
Harald Welte0b0013a2002-02-18 16:15:31 +0000689 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000690 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000691 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000692 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000693
694 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000695}
696
Marc Boucherb93c7982001-12-06 14:50:19 +0000697u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000698parse_protocol(const char *s)
699{
Harald Welteed498492001-07-23 01:24:22 +0000700 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000701
Harald Welteed498492001-07-23 01:24:22 +0000702 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 struct protoent *pent;
704
Harald Weltecbe1ec72006-02-11 09:50:11 +0000705 /* first deal with the special case of 'all' to prevent
706 * people from being able to redefine 'all' in nsswitch
707 * and/or provoke expensive [not working] ldap/nis/...
708 * lookups */
709 if (!strcmp(s, "all"))
710 return 0;
711
Marc Bouchere6869a82000-03-20 06:03:29 +0000712 if ((pent = getprotobyname(s)))
713 proto = pent->p_proto;
714 else {
715 unsigned int i;
716 for (i = 0;
717 i < sizeof(chain_protos)/sizeof(struct pprot);
718 i++) {
719 if (strcmp(s, chain_protos[i].name) == 0) {
720 proto = chain_protos[i].num;
721 break;
722 }
723 }
724 if (i == sizeof(chain_protos)/sizeof(struct pprot))
725 exit_error(PARAMETER_PROBLEM,
726 "unknown protocol `%s' specified",
727 s);
728 }
729 }
730
731 return (u_int16_t)proto;
732}
733
Marc Bouchere6869a82000-03-20 06:03:29 +0000734/* Can't be zero. */
735static int
736parse_rulenumber(const char *rule)
737{
Harald Welteed498492001-07-23 01:24:22 +0000738 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000739
Harald Welteed498492001-07-23 01:24:22 +0000740 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000741 exit_error(PARAMETER_PROBLEM,
742 "Invalid rule number `%s'", rule);
743
744 return rulenum;
745}
746
747static const char *
748parse_target(const char *targetname)
749{
750 const char *ptr;
751
752 if (strlen(targetname) < 1)
753 exit_error(PARAMETER_PROBLEM,
754 "Invalid target name (too short)");
755
756 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
757 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000758 "Invalid target name `%s' (%u chars max)",
759 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000760
761 for (ptr = targetname; *ptr; ptr++)
762 if (isspace(*ptr))
763 exit_error(PARAMETER_PROBLEM,
764 "Invalid target name `%s'", targetname);
765 return targetname;
766}
767
768static char *
769addr_to_network(const struct in_addr *addr)
770{
771 struct netent *net;
772
773 if ((net = getnetbyaddr((long) ntohl(addr->s_addr), AF_INET)) != NULL)
774 return (char *) net->n_name;
775
776 return (char *) NULL;
777}
778
779char *
780addr_to_dotted(const struct in_addr *addrp)
781{
782 static char buf[20];
783 const unsigned char *bytep;
784
785 bytep = (const unsigned char *) &(addrp->s_addr);
786 sprintf(buf, "%d.%d.%d.%d", bytep[0], bytep[1], bytep[2], bytep[3]);
787 return buf;
788}
Marc Boucherb93c7982001-12-06 14:50:19 +0000789
790char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000791addr_to_anyname(const struct in_addr *addr)
792{
793 char *name;
794
795 if ((name = addr_to_host(addr)) != NULL ||
796 (name = addr_to_network(addr)) != NULL)
797 return name;
798
799 return addr_to_dotted(addr);
800}
801
Marc Boucherb93c7982001-12-06 14:50:19 +0000802char *
Marc Bouchere6869a82000-03-20 06:03:29 +0000803mask_to_dotted(const struct in_addr *mask)
804{
805 int i;
806 static char buf[20];
807 u_int32_t maskaddr, bits;
808
809 maskaddr = ntohl(mask->s_addr);
810
811 if (maskaddr == 0xFFFFFFFFL)
812 /* we don't want to see "/32" */
813 return "";
814
815 i = 32;
816 bits = 0xFFFFFFFEL;
817 while (--i >= 0 && maskaddr != bits)
818 bits <<= 1;
819 if (i >= 0)
820 sprintf(buf, "/%d", i);
821 else
822 /* mask was not a decent combination of 1's and 0's */
823 sprintf(buf, "/%s", addr_to_dotted(mask));
824
825 return buf;
826}
827
Marc Bouchere6869a82000-03-20 06:03:29 +0000828static void
829set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
830 int invert)
831{
832 if (*options & option)
833 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
834 opt2char(option));
835 *options |= option;
836
837 if (invert) {
838 unsigned int i;
839 for (i = 0; 1 << i != option; i++);
840
841 if (!inverse_for_options[i])
842 exit_error(PARAMETER_PROBLEM,
843 "cannot have ! before -%c",
844 opt2char(option));
845 *invflg |= inverse_for_options[i];
846 }
847}
848
Marc Bouchere6869a82000-03-20 06:03:29 +0000849static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000850merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000851 unsigned int *option_offset)
852{
853 unsigned int num_old, num_new, i;
854 struct option *merge;
855
Jan Engelhardtd0145402007-07-30 14:32:26 +0000856 if (newopts == NULL)
857 return oldopts;
858
Marc Bouchere6869a82000-03-20 06:03:29 +0000859 for (num_old = 0; oldopts[num_old].name; num_old++);
860 for (num_new = 0; newopts[num_new].name; num_new++);
861
862 global_option_offset += OPTION_OFFSET;
863 *option_offset = global_option_offset;
864
865 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
866 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000867 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000868 for (i = 0; i < num_new; i++) {
869 merge[num_old + i] = newopts[i];
870 merge[num_old + i].val += *option_offset;
871 }
872 memset(merge + num_old + num_new, 0, sizeof(struct option));
873
874 return merge;
875}
876
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000877void register_match(struct iptables_match *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000878{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000879 me->family = PF_INET;
880 xtables_register_match(me);
Rusty Russell3aef54d2005-01-03 03:48:40 +0000881}
882
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000883void register_target(struct iptables_target *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000884{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000885 me->family = PF_INET;
886 xtables_register_target(me);
Marc Bouchere6869a82000-03-20 06:03:29 +0000887}
888
889static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000890print_num(u_int64_t number, unsigned int format)
891{
892 if (format & FMT_KILOMEGAGIGA) {
893 if (number > 99999) {
894 number = (number + 500) / 1000;
895 if (number > 9999) {
896 number = (number + 500) / 1000;
897 if (number > 9999) {
898 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000899 if (number > 9999) {
900 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000901 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000902 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000903 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000904 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000905 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000906 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000907 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000908 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000909 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000910 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000911 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000912}
913
914
915static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000916print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
917{
918 struct ipt_counters counters;
919 const char *pol = iptc_get_policy(chain, &counters, handle);
920 printf("Chain %s", chain);
921 if (pol) {
922 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000923 if (!(format & FMT_NOCOUNTS)) {
924 fputc(' ', stdout);
925 print_num(counters.pcnt, (format|FMT_NOTABLE));
926 fputs("packets, ", stdout);
927 print_num(counters.bcnt, (format|FMT_NOTABLE));
928 fputs("bytes", stdout);
929 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000930 printf(")\n");
931 } else {
932 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000933 if (!iptc_get_references(&refs, chain, handle))
934 printf(" (ERROR obtaining refs)\n");
935 else
936 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000937 }
938
939 if (format & FMT_LINENUMBERS)
940 printf(FMT("%-4s ", "%s "), "num");
941 if (!(format & FMT_NOCOUNTS)) {
942 if (format & FMT_KILOMEGAGIGA) {
943 printf(FMT("%5s ","%s "), "pkts");
944 printf(FMT("%5s ","%s "), "bytes");
945 } else {
946 printf(FMT("%8s ","%s "), "pkts");
947 printf(FMT("%10s ","%s "), "bytes");
948 }
949 }
950 if (!(format & FMT_NOTARGET))
951 printf(FMT("%-9s ","%s "), "target");
952 fputs(" prot ", stdout);
953 if (format & FMT_OPTIONS)
954 fputs("opt", stdout);
955 if (format & FMT_VIA) {
956 printf(FMT(" %-6s ","%s "), "in");
957 printf(FMT("%-6s ","%s "), "out");
958 }
959 printf(FMT(" %-19s ","%s "), "source");
960 printf(FMT(" %-19s "," %s "), "destination");
961 printf("\n");
962}
963
Marc Bouchere6869a82000-03-20 06:03:29 +0000964
965static int
966print_match(const struct ipt_entry_match *m,
967 const struct ipt_ip *ip,
968 int numeric)
969{
Martin Josefsson78cafda2004-02-02 20:01:18 +0000970 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000971
972 if (match) {
973 if (match->print)
974 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000975 else
Rusty Russellb039b022000-09-01 06:04:05 +0000976 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000977 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000978 if (m->u.user.name[0])
979 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000980 }
981 /* Don't stop iterating. */
982 return 0;
983}
984
985/* e is called `fw' here for hysterical raisins */
986static void
987print_firewall(const struct ipt_entry *fw,
988 const char *targname,
989 unsigned int num,
990 unsigned int format,
991 const iptc_handle_t handle)
992{
993 struct iptables_target *target = NULL;
994 const struct ipt_entry_target *t;
995 u_int8_t flags;
996 char buf[BUFSIZ];
997
Marc Bouchere6869a82000-03-20 06:03:29 +0000998 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +0000999 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001000 else
Rusty Russell52a51492000-05-02 16:44:29 +00001001 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001002
1003 t = ipt_get_target((struct ipt_entry *)fw);
1004 flags = fw->ip.flags;
1005
1006 if (format & FMT_LINENUMBERS)
1007 printf(FMT("%-4u ", "%u "), num+1);
1008
1009 if (!(format & FMT_NOCOUNTS)) {
1010 print_num(fw->counters.pcnt, format);
1011 print_num(fw->counters.bcnt, format);
1012 }
1013
1014 if (!(format & FMT_NOTARGET))
1015 printf(FMT("%-9s ", "%s "), targname);
1016
1017 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1018 {
Rusty Russell28381a42000-05-10 00:19:50 +00001019 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001020 if (pname)
1021 printf(FMT("%-5s", "%s "), pname);
1022 else
1023 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1024 }
1025
1026 if (format & FMT_OPTIONS) {
1027 if (format & FMT_NOTABLE)
1028 fputs("opt ", stdout);
1029 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1030 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1031 fputc(' ', stdout);
1032 }
1033
1034 if (format & FMT_VIA) {
1035 char iface[IFNAMSIZ+2];
1036
1037 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1038 iface[0] = '!';
1039 iface[1] = '\0';
1040 }
1041 else iface[0] = '\0';
1042
1043 if (fw->ip.iniface[0] != '\0') {
1044 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001045 }
1046 else if (format & FMT_NUMERIC) strcat(iface, "*");
1047 else strcat(iface, "any");
1048 printf(FMT(" %-6s ","in %s "), iface);
1049
1050 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1051 iface[0] = '!';
1052 iface[1] = '\0';
1053 }
1054 else iface[0] = '\0';
1055
1056 if (fw->ip.outiface[0] != '\0') {
1057 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001058 }
1059 else if (format & FMT_NUMERIC) strcat(iface, "*");
1060 else strcat(iface, "any");
1061 printf(FMT("%-6s ","out %s "), iface);
1062 }
1063
1064 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1065 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1066 printf(FMT("%-19s ","%s "), "anywhere");
1067 else {
1068 if (format & FMT_NUMERIC)
1069 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1070 else
1071 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1072 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1073 printf(FMT("%-19s ","%s "), buf);
1074 }
1075
1076 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1077 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001078 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001079 else {
1080 if (format & FMT_NUMERIC)
1081 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1082 else
1083 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1084 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001085 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001086 }
1087
1088 if (format & FMT_NOTABLE)
1089 fputs(" ", stdout);
1090
Harald Welte72bd87e2005-11-24 17:04:05 +00001091#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001092 if(fw->ip.flags & IPT_F_GOTO)
1093 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001094#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001095
Marc Bouchere6869a82000-03-20 06:03:29 +00001096 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1097
1098 if (target) {
1099 if (target->print)
1100 /* Print the target information. */
1101 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001102 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001103 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001104 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001105
1106 if (!(format & FMT_NONEWLINE))
1107 fputc('\n', stdout);
1108}
1109
1110static void
1111print_firewall_line(const struct ipt_entry *fw,
1112 const iptc_handle_t h)
1113{
1114 struct ipt_entry_target *t;
1115
1116 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001117 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001118}
1119
1120static int
1121append_entry(const ipt_chainlabel chain,
1122 struct ipt_entry *fw,
1123 unsigned int nsaddrs,
1124 const struct in_addr saddrs[],
1125 unsigned int ndaddrs,
1126 const struct in_addr daddrs[],
1127 int verbose,
1128 iptc_handle_t *handle)
1129{
1130 unsigned int i, j;
1131 int ret = 1;
1132
1133 for (i = 0; i < nsaddrs; i++) {
1134 fw->ip.src.s_addr = saddrs[i].s_addr;
1135 for (j = 0; j < ndaddrs; j++) {
1136 fw->ip.dst.s_addr = daddrs[j].s_addr;
1137 if (verbose)
1138 print_firewall_line(fw, *handle);
1139 ret &= iptc_append_entry(chain, fw, handle);
1140 }
1141 }
1142
1143 return ret;
1144}
1145
1146static int
1147replace_entry(const ipt_chainlabel chain,
1148 struct ipt_entry *fw,
1149 unsigned int rulenum,
1150 const struct in_addr *saddr,
1151 const struct in_addr *daddr,
1152 int verbose,
1153 iptc_handle_t *handle)
1154{
1155 fw->ip.src.s_addr = saddr->s_addr;
1156 fw->ip.dst.s_addr = daddr->s_addr;
1157
1158 if (verbose)
1159 print_firewall_line(fw, *handle);
1160 return iptc_replace_entry(chain, fw, rulenum, handle);
1161}
1162
1163static int
1164insert_entry(const ipt_chainlabel chain,
1165 struct ipt_entry *fw,
1166 unsigned int rulenum,
1167 unsigned int nsaddrs,
1168 const struct in_addr saddrs[],
1169 unsigned int ndaddrs,
1170 const struct in_addr daddrs[],
1171 int verbose,
1172 iptc_handle_t *handle)
1173{
1174 unsigned int i, j;
1175 int ret = 1;
1176
1177 for (i = 0; i < nsaddrs; i++) {
1178 fw->ip.src.s_addr = saddrs[i].s_addr;
1179 for (j = 0; j < ndaddrs; j++) {
1180 fw->ip.dst.s_addr = daddrs[j].s_addr;
1181 if (verbose)
1182 print_firewall_line(fw, *handle);
1183 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1184 }
1185 }
1186
1187 return ret;
1188}
1189
Rusty Russell2e0a3212000-04-19 11:23:18 +00001190static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001191make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001192{
1193 /* Establish mask for comparison */
1194 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001195 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001196 unsigned char *mask, *mptr;
1197
1198 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001199 for (matchp = matches; matchp; matchp = matchp->next)
1200 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001201
Rusty Russell9e1d2142000-04-23 09:11:12 +00001202 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001203 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001204 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001205
Rusty Russell9e1d2142000-04-23 09:11:12 +00001206 memset(mask, 0xFF, sizeof(struct ipt_entry));
1207 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001208
Martin Josefsson78cafda2004-02-02 20:01:18 +00001209 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001210 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001211 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001212 + matchp->match->userspacesize);
1213 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001214 }
1215
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001216 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001217 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001218 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001219
1220 return mask;
1221}
1222
Marc Bouchere6869a82000-03-20 06:03:29 +00001223static int
1224delete_entry(const ipt_chainlabel chain,
1225 struct ipt_entry *fw,
1226 unsigned int nsaddrs,
1227 const struct in_addr saddrs[],
1228 unsigned int ndaddrs,
1229 const struct in_addr daddrs[],
1230 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001231 iptc_handle_t *handle,
1232 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001233{
1234 unsigned int i, j;
1235 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001236 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001237
Martin Josefsson78cafda2004-02-02 20:01:18 +00001238 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001239 for (i = 0; i < nsaddrs; i++) {
1240 fw->ip.src.s_addr = saddrs[i].s_addr;
1241 for (j = 0; j < ndaddrs; j++) {
1242 fw->ip.dst.s_addr = daddrs[j].s_addr;
1243 if (verbose)
1244 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001245 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001246 }
1247 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001248 free(mask);
1249
Marc Bouchere6869a82000-03-20 06:03:29 +00001250 return ret;
1251}
1252
Harald Welteae1ff9f2000-12-01 14:26:20 +00001253int
Marc Bouchere6869a82000-03-20 06:03:29 +00001254for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001255 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001256{
1257 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001258 const char *chain;
1259 char *chains;
1260 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001261
Rusty Russell9e1d2142000-04-23 09:11:12 +00001262 chain = iptc_first_chain(handle);
1263 while (chain) {
1264 chaincount++;
1265 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001266 }
1267
Rusty Russell9e1d2142000-04-23 09:11:12 +00001268 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1269 i = 0;
1270 chain = iptc_first_chain(handle);
1271 while (chain) {
1272 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1273 i++;
1274 chain = iptc_next_chain(handle);
1275 }
1276
1277 for (i = 0; i < chaincount; i++) {
1278 if (!builtinstoo
1279 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001280 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001281 continue;
1282 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1283 }
1284
1285 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001286 return ret;
1287}
1288
Harald Welteae1ff9f2000-12-01 14:26:20 +00001289int
Marc Bouchere6869a82000-03-20 06:03:29 +00001290flush_entries(const ipt_chainlabel chain, int verbose,
1291 iptc_handle_t *handle)
1292{
1293 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001294 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001295
1296 if (verbose)
1297 fprintf(stdout, "Flushing chain `%s'\n", chain);
1298 return iptc_flush_entries(chain, handle);
1299}
Marc Bouchere6869a82000-03-20 06:03:29 +00001300
1301static int
1302zero_entries(const ipt_chainlabel chain, int verbose,
1303 iptc_handle_t *handle)
1304{
1305 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001306 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001307
Marc Bouchere6869a82000-03-20 06:03:29 +00001308 if (verbose)
1309 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1310 return iptc_zero_entries(chain, handle);
1311}
1312
Harald Welteae1ff9f2000-12-01 14:26:20 +00001313int
Marc Bouchere6869a82000-03-20 06:03:29 +00001314delete_chain(const ipt_chainlabel chain, int verbose,
1315 iptc_handle_t *handle)
1316{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001317 if (!chain)
1318 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001319
1320 if (verbose)
1321 fprintf(stdout, "Deleting chain `%s'\n", chain);
1322 return iptc_delete_chain(chain, handle);
1323}
1324
1325static int
1326list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1327 int expanded, int linenumbers, iptc_handle_t *handle)
1328{
1329 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001330 unsigned int format;
1331 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001332
1333 format = FMT_OPTIONS;
1334 if (!verbose)
1335 format |= FMT_NOCOUNTS;
1336 else
1337 format |= FMT_VIA;
1338
1339 if (numeric)
1340 format |= FMT_NUMERIC;
1341
1342 if (!expanded)
1343 format |= FMT_KILOMEGAGIGA;
1344
1345 if (linenumbers)
1346 format |= FMT_LINENUMBERS;
1347
Rusty Russell9e1d2142000-04-23 09:11:12 +00001348 for (this = iptc_first_chain(handle);
1349 this;
1350 this = iptc_next_chain(handle)) {
1351 const struct ipt_entry *i;
1352 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001353
Marc Bouchere6869a82000-03-20 06:03:29 +00001354 if (chain && strcmp(chain, this) != 0)
1355 continue;
1356
1357 if (found) printf("\n");
1358
1359 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001360 i = iptc_first_rule(this, handle);
1361
1362 num = 0;
1363 while (i) {
1364 print_firewall(i,
1365 iptc_get_target(i, handle),
1366 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001367 format,
1368 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001369 i = iptc_next_rule(i, handle);
1370 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001371 found = 1;
1372 }
1373
1374 errno = ENOENT;
1375 return found;
1376}
1377
1378static struct ipt_entry *
1379generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001380 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001381 struct ipt_entry_target *target)
1382{
1383 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001384 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001385 struct ipt_entry *e;
1386
1387 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001388 for (matchp = matches; matchp; matchp = matchp->next)
1389 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001390
Rusty Russell228e98d2000-04-27 10:28:06 +00001391 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001392 *e = *fw;
1393 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001394 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001395
1396 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001397 for (matchp = matches; matchp; matchp = matchp->next) {
1398 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1399 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001400 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001401 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001402
1403 return e;
1404}
1405
Martin Josefsson78cafda2004-02-02 20:01:18 +00001406void clear_rule_matches(struct iptables_rule_match **matches)
1407{
1408 struct iptables_rule_match *matchp, *tmp;
1409
1410 for (matchp = *matches; matchp;) {
1411 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001412 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001413 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001414 matchp->match->m = NULL;
1415 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001416 if (matchp->match == matchp->match->next) {
1417 free(matchp->match);
1418 matchp->match = NULL;
1419 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001420 free(matchp);
1421 matchp = tmp;
1422 }
1423
1424 *matches = NULL;
1425}
1426
Rusty Russell3aef54d2005-01-03 03:48:40 +00001427static void set_revision(char *name, u_int8_t revision)
1428{
1429 /* Old kernel sources don't have ".revision" field,
1430 but we stole a byte from name. */
1431 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1432 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1433}
1434
Phil Oester8cf65912005-09-19 15:00:33 +00001435void
1436get_kernel_version(void) {
1437 static struct utsname uts;
1438 int x = 0, y = 0, z = 0;
1439
1440 if (uname(&uts) == -1) {
1441 fprintf(stderr, "Unable to retrieve kernel version.\n");
1442 free_opts(1);
1443 exit(1);
1444 }
1445
1446 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1447 kernel_version = LINUX_VERSION(x, y, z);
1448}
1449
Marc Bouchere6869a82000-03-20 06:03:29 +00001450int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1451{
1452 struct ipt_entry fw, *e = NULL;
1453 int invert = 0;
1454 unsigned int nsaddrs = 0, ndaddrs = 0;
1455 struct in_addr *saddrs = NULL, *daddrs = NULL;
1456
1457 int c, verbose = 0;
1458 const char *chain = NULL;
1459 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1460 const char *policy = NULL, *newname = NULL;
1461 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001462 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001463 int ret = 1;
1464 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001465 struct iptables_rule_match *matches = NULL;
1466 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001467 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001468 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001469 const char *jumpto = "";
1470 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001471 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001472 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001473
1474 memset(&fw, 0, sizeof(fw));
1475
Harald Welteae1ff9f2000-12-01 14:26:20 +00001476 /* re-set optind to 0 in case do_command gets called
1477 * a second time */
1478 optind = 0;
1479
1480 /* clear mflags in case do_command gets called a second time
1481 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001482 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001483 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001484
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001485 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001486 t->tflags = 0;
1487 t->used = 0;
1488 }
1489
Marc Bouchere6869a82000-03-20 06:03:29 +00001490 /* Suppress error messages: we may add new options if we
1491 demand-load a protocol. */
1492 opterr = 0;
1493
1494 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001495 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:xc:g:",
Marc Bouchere6869a82000-03-20 06:03:29 +00001496 opts, NULL)) != -1) {
1497 switch (c) {
1498 /*
1499 * Command selection
1500 */
1501 case 'A':
1502 add_command(&command, CMD_APPEND, CMD_NONE,
1503 invert);
1504 chain = optarg;
1505 break;
1506
1507 case 'D':
1508 add_command(&command, CMD_DELETE, CMD_NONE,
1509 invert);
1510 chain = optarg;
1511 if (optind < argc && argv[optind][0] != '-'
1512 && argv[optind][0] != '!') {
1513 rulenum = parse_rulenumber(argv[optind++]);
1514 command = CMD_DELETE_NUM;
1515 }
1516 break;
1517
Marc Bouchere6869a82000-03-20 06:03:29 +00001518 case 'R':
1519 add_command(&command, CMD_REPLACE, CMD_NONE,
1520 invert);
1521 chain = optarg;
1522 if (optind < argc && argv[optind][0] != '-'
1523 && argv[optind][0] != '!')
1524 rulenum = parse_rulenumber(argv[optind++]);
1525 else
1526 exit_error(PARAMETER_PROBLEM,
1527 "-%c requires a rule number",
1528 cmd2char(CMD_REPLACE));
1529 break;
1530
1531 case 'I':
1532 add_command(&command, CMD_INSERT, CMD_NONE,
1533 invert);
1534 chain = optarg;
1535 if (optind < argc && argv[optind][0] != '-'
1536 && argv[optind][0] != '!')
1537 rulenum = parse_rulenumber(argv[optind++]);
1538 else rulenum = 1;
1539 break;
1540
1541 case 'L':
1542 add_command(&command, CMD_LIST, CMD_ZERO,
1543 invert);
1544 if (optarg) chain = optarg;
1545 else if (optind < argc && argv[optind][0] != '-'
1546 && argv[optind][0] != '!')
1547 chain = argv[optind++];
1548 break;
1549
1550 case 'F':
1551 add_command(&command, CMD_FLUSH, CMD_NONE,
1552 invert);
1553 if (optarg) chain = optarg;
1554 else if (optind < argc && argv[optind][0] != '-'
1555 && argv[optind][0] != '!')
1556 chain = argv[optind++];
1557 break;
1558
1559 case 'Z':
1560 add_command(&command, CMD_ZERO, CMD_LIST,
1561 invert);
1562 if (optarg) chain = optarg;
1563 else if (optind < argc && argv[optind][0] != '-'
1564 && argv[optind][0] != '!')
1565 chain = argv[optind++];
1566 break;
1567
1568 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001569 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001570 exit_error(PARAMETER_PROBLEM,
1571 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001572 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001573 if (find_target(optarg, TRY_LOAD))
1574 exit_error(PARAMETER_PROBLEM,
1575 "chain name may not clash "
1576 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001577 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1578 invert);
1579 chain = optarg;
1580 break;
1581
1582 case 'X':
1583 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1584 invert);
1585 if (optarg) chain = optarg;
1586 else if (optind < argc && argv[optind][0] != '-'
1587 && argv[optind][0] != '!')
1588 chain = argv[optind++];
1589 break;
1590
1591 case 'E':
1592 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1593 invert);
1594 chain = optarg;
1595 if (optind < argc && argv[optind][0] != '-'
1596 && argv[optind][0] != '!')
1597 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001598 else
1599 exit_error(PARAMETER_PROBLEM,
1600 "-%c requires old-chain-name and "
1601 "new-chain-name",
1602 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001603 break;
1604
1605 case 'P':
1606 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1607 invert);
1608 chain = optarg;
1609 if (optind < argc && argv[optind][0] != '-'
1610 && argv[optind][0] != '!')
1611 policy = argv[optind++];
1612 else
1613 exit_error(PARAMETER_PROBLEM,
1614 "-%c requires a chain and a policy",
1615 cmd2char(CMD_SET_POLICY));
1616 break;
1617
1618 case 'h':
1619 if (!optarg)
1620 optarg = argv[optind];
1621
Rusty Russell2e0a3212000-04-19 11:23:18 +00001622 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001623 if (!matches && protocol)
1624 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001625
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001626 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001627
1628 /*
1629 * Option selection
1630 */
1631 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001632 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001633 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1634 invert);
1635
1636 /* Canonicalize into lower case */
1637 for (protocol = argv[optind-1]; *protocol; protocol++)
1638 *protocol = tolower(*protocol);
1639
1640 protocol = argv[optind-1];
1641 fw.ip.proto = parse_protocol(protocol);
1642
1643 if (fw.ip.proto == 0
1644 && (fw.ip.invflags & IPT_INV_PROTO))
1645 exit_error(PARAMETER_PROBLEM,
1646 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001647 break;
1648
1649 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001650 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001651 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1652 invert);
1653 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001654 break;
1655
1656 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001657 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001658 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1659 invert);
1660 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001661 break;
1662
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001663#ifdef IPT_F_GOTO
1664 case 'g':
1665 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1666 invert);
1667 fw.ip.flags |= IPT_F_GOTO;
1668 jumpto = parse_target(optarg);
1669 break;
1670#endif
1671
Marc Bouchere6869a82000-03-20 06:03:29 +00001672 case 'j':
1673 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1674 invert);
1675 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001676 /* TRY_LOAD (may be chain name) */
1677 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001678
1679 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001680 size_t size;
1681
Rusty Russell73f72f52000-07-03 10:17:57 +00001682 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1683 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001684
Rusty Russell2e0a3212000-04-19 11:23:18 +00001685 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001686 target->t->u.target_size = size;
1687 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001688 set_revision(target->t->u.user.name,
1689 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001690 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001691 target->init(target->t);
Sven Kochfb1279a2001-02-27 12:25:12 +00001692 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Marc Bouchere6869a82000-03-20 06:03:29 +00001693 }
1694 break;
1695
1696
1697 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001698 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001699 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1700 invert);
1701 parse_interface(argv[optind-1],
1702 fw.ip.iniface,
1703 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 break;
1705
1706 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001707 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001708 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1709 invert);
1710 parse_interface(argv[optind-1],
1711 fw.ip.outiface,
1712 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001713 break;
1714
1715 case 'f':
1716 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1717 invert);
1718 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001719 break;
1720
1721 case 'v':
1722 if (!verbose)
1723 set_option(&options, OPT_VERBOSE,
1724 &fw.ip.invflags, invert);
1725 verbose++;
1726 break;
1727
Rusty Russell52a51492000-05-02 16:44:29 +00001728 case 'm': {
1729 size_t size;
1730
Marc Bouchere6869a82000-03-20 06:03:29 +00001731 if (invert)
1732 exit_error(PARAMETER_PROBLEM,
1733 "unexpected ! flag before --match");
1734
Martin Josefsson78cafda2004-02-02 20:01:18 +00001735 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001736 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1737 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001738 m->m = fw_calloc(1, size);
1739 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001740 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001741 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001742 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001743 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001744 if (m != m->next)
1745 /* Merge options for non-cloned matches */
1746 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell52a51492000-05-02 16:44:29 +00001747 }
1748 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001749
1750 case 'n':
1751 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1752 invert);
1753 break;
1754
1755 case 't':
1756 if (invert)
1757 exit_error(PARAMETER_PROBLEM,
1758 "unexpected ! flag before --table");
1759 *table = argv[optind-1];
1760 break;
1761
1762 case 'x':
1763 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1764 invert);
1765 break;
1766
1767 case 'V':
1768 if (invert)
1769 printf("Not %s ;-)\n", program_version);
1770 else
1771 printf("%s v%s\n",
1772 program_name, program_version);
1773 exit(0);
1774
1775 case '0':
1776 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1777 invert);
1778 break;
1779
Harald Welte82dd2ec2000-12-19 05:18:15 +00001780 case 'M':
1781 modprobe = optarg;
1782 break;
1783
Harald Welteccd49e52001-01-23 22:54:34 +00001784 case 'c':
1785
1786 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1787 invert);
1788 pcnt = optarg;
1789 if (optind < argc && argv[optind][0] != '-'
1790 && argv[optind][0] != '!')
1791 bcnt = argv[optind++];
1792 else
1793 exit_error(PARAMETER_PROBLEM,
1794 "-%c requires packet and byte counter",
1795 opt2char(OPT_COUNTERS));
1796
Patrick McHardy875441e2007-10-17 08:48:58 +00001797 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001798 exit_error(PARAMETER_PROBLEM,
1799 "-%c packet counter not numeric",
1800 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001801 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001802
Patrick McHardy875441e2007-10-17 08:48:58 +00001803 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001804 exit_error(PARAMETER_PROBLEM,
1805 "-%c byte counter not numeric",
1806 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001807 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001808 break;
1809
1810
Marc Bouchere6869a82000-03-20 06:03:29 +00001811 case 1: /* non option */
1812 if (optarg[0] == '!' && optarg[1] == '\0') {
1813 if (invert)
1814 exit_error(PARAMETER_PROBLEM,
1815 "multiple consecutive ! not"
1816 " allowed");
1817 invert = TRUE;
1818 optarg[0] = '\0';
1819 continue;
1820 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001821 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001822 exit_tryhelp(2);
1823
1824 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001825 if (!target
1826 || !(target->parse(c - target->option_offset,
1827 argv, invert,
1828 &target->tflags,
1829 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001830 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001831 if (matchp->completed)
1832 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001833 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001834 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001835 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001836 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001837 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001838 break;
1839 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001840 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001841
1842 /* If you listen carefully, you can
1843 actually hear this code suck. */
1844
1845 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001846 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001847 * parameter, that has not been parsed yet,
1848 * it's not an option of an explicitly loaded
1849 * match or a target. However, we support
1850 * implicit loading of the protocol match
1851 * extension. '-p tcp' means 'l4 proto 6' and
1852 * at the same time 'load tcp protocol match on
1853 * demand if we specify --dport'.
1854 *
1855 * To make this work, we need to make sure:
1856 * - the parameter has not been parsed by
1857 * a match (m above)
1858 * - a protocol has been specified
1859 * - the protocol extension has not been
1860 * loaded yet, or is loaded and unused
1861 * [think of iptables-restore!]
1862 * - the protocol extension can be successively
1863 * loaded
1864 */
1865 if (m == NULL
1866 && protocol
1867 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001868 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001869 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001870 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001871 && (proto_used == 0))
1872 )
1873 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001874 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001875 /* Try loading protocol */
1876 size_t size;
1877
1878 proto_used = 1;
1879
1880 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1881 + m->size;
1882
1883 m->m = fw_calloc(1, size);
1884 m->m->u.match_size = size;
1885 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001886 set_revision(m->m->u.user.name,
1887 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001888 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001889 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001890
1891 opts = merge_options(opts,
1892 m->extra_opts, &m->option_offset);
1893
1894 optind--;
1895 continue;
1896 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001897 if (!m)
1898 exit_error(PARAMETER_PROBLEM,
1899 "Unknown arg `%s'",
1900 argv[optind-1]);
1901 }
1902 }
1903 invert = FALSE;
1904 }
1905
Martin Josefsson78cafda2004-02-02 20:01:18 +00001906 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001907 if (matchp->match->final_check != NULL)
1908 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001909
Jan Engelhardt830132a2007-10-04 16:24:50 +00001910 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001911 target->final_check(target->tflags);
1912
1913 /* Fix me: must put inverse options checking here --MN */
1914
1915 if (optind < argc)
1916 exit_error(PARAMETER_PROBLEM,
1917 "unknown arguments found on commandline");
1918 if (!command)
1919 exit_error(PARAMETER_PROBLEM, "no command specified");
1920 if (invert)
1921 exit_error(PARAMETER_PROBLEM,
1922 "nothing appropriate following !");
1923
Harald Welte6336bfd2002-05-07 14:41:43 +00001924 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001925 if (!(options & OPT_DESTINATION))
1926 dhostnetworkmask = "0.0.0.0/0";
1927 if (!(options & OPT_SOURCE))
1928 shostnetworkmask = "0.0.0.0/0";
1929 }
1930
1931 if (shostnetworkmask)
1932 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1933 &(fw.ip.smsk), &nsaddrs);
1934
1935 if (dhostnetworkmask)
1936 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1937 &(fw.ip.dmsk), &ndaddrs);
1938
1939 if ((nsaddrs > 1 || ndaddrs > 1) &&
1940 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1941 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1942 " source or destination IP addresses");
1943
Marc Bouchere6869a82000-03-20 06:03:29 +00001944 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1945 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1946 "specify a unique address");
1947
1948 generic_opt_check(command, options);
1949
1950 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1951 exit_error(PARAMETER_PROBLEM,
1952 "chain name `%s' too long (must be under %i chars)",
1953 chain, IPT_FUNCTION_MAXNAMELEN);
1954
Harald Welteae1ff9f2000-12-01 14:26:20 +00001955 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001956 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001957 *handle = iptc_init(*table);
1958
Rusty Russell8beb0492004-12-22 00:37:10 +00001959 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001960 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001961 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001962
Marc Bouchere6869a82000-03-20 06:03:29 +00001963 if (!*handle)
1964 exit_error(VERSION_PROBLEM,
1965 "can't initialize iptables table `%s': %s",
1966 *table, iptc_strerror(errno));
1967
Harald Welte6336bfd2002-05-07 14:41:43 +00001968 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001969 || command == CMD_DELETE
1970 || command == CMD_INSERT
1971 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001972 if (strcmp(chain, "PREROUTING") == 0
1973 || strcmp(chain, "INPUT") == 0) {
1974 /* -o not valid with incoming packets. */
1975 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001976 exit_error(PARAMETER_PROBLEM,
1977 "Can't use -%c with %s\n",
1978 opt2char(OPT_VIANAMEOUT),
1979 chain);
1980 }
1981
Rusty Russella4860fd2000-06-17 16:13:02 +00001982 if (strcmp(chain, "POSTROUTING") == 0
1983 || strcmp(chain, "OUTPUT") == 0) {
1984 /* -i not valid with outgoing packets */
1985 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001986 exit_error(PARAMETER_PROBLEM,
1987 "Can't use -%c with %s\n",
1988 opt2char(OPT_VIANAMEIN),
1989 chain);
1990 }
1991
1992 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001993 fprintf(stderr,
1994 "Warning: using chain %s, not extension\n",
1995 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001996
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001997 if (target->t)
1998 free(target->t);
1999
Marc Bouchere6869a82000-03-20 06:03:29 +00002000 target = NULL;
2001 }
2002
2003 /* If they didn't specify a target, or it's a chain
2004 name, use standard. */
2005 if (!target
2006 && (strlen(jumpto) == 0
2007 || iptc_is_chain(jumpto, *handle))) {
2008 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002009
Rusty Russell52a51492000-05-02 16:44:29 +00002010 target = find_target(IPT_STANDARD_TARGET,
2011 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002012
2013 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002014 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002015 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002016 target->t->u.target_size = size;
2017 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002018 if (!iptc_is_chain(jumpto, *handle))
2019 set_revision(target->t->u.user.name,
2020 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002021 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00002022 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002023 }
2024
Rusty Russell7e53bf92000-03-20 07:03:28 +00002025 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002026 /* it is no chain, and we can't load a plugin.
2027 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002028 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002029 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002030#ifdef IPT_F_GOTO
2031 if (fw.ip.flags & IPT_F_GOTO)
2032 exit_error(PARAMETER_PROBLEM,
2033 "goto '%s' is not a chain\n", jumpto);
2034#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002035 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002036 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002037 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002038 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002039 }
2040 }
2041
2042 switch (command) {
2043 case CMD_APPEND:
2044 ret = append_entry(chain, e,
2045 nsaddrs, saddrs, ndaddrs, daddrs,
2046 options&OPT_VERBOSE,
2047 handle);
2048 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002049 case CMD_DELETE:
2050 ret = delete_entry(chain, e,
2051 nsaddrs, saddrs, ndaddrs, daddrs,
2052 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002053 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002054 break;
2055 case CMD_DELETE_NUM:
2056 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2057 break;
2058 case CMD_REPLACE:
2059 ret = replace_entry(chain, e, rulenum - 1,
2060 saddrs, daddrs, options&OPT_VERBOSE,
2061 handle);
2062 break;
2063 case CMD_INSERT:
2064 ret = insert_entry(chain, e, rulenum - 1,
2065 nsaddrs, saddrs, ndaddrs, daddrs,
2066 options&OPT_VERBOSE,
2067 handle);
2068 break;
2069 case CMD_LIST:
2070 ret = list_entries(chain,
2071 options&OPT_VERBOSE,
2072 options&OPT_NUMERIC,
2073 options&OPT_EXPANDED,
2074 options&OPT_LINENUMBERS,
2075 handle);
2076 break;
2077 case CMD_FLUSH:
2078 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2079 break;
2080 case CMD_ZERO:
2081 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2082 break;
2083 case CMD_LIST|CMD_ZERO:
2084 ret = list_entries(chain,
2085 options&OPT_VERBOSE,
2086 options&OPT_NUMERIC,
2087 options&OPT_EXPANDED,
2088 options&OPT_LINENUMBERS,
2089 handle);
2090 if (ret)
2091 ret = zero_entries(chain,
2092 options&OPT_VERBOSE, handle);
2093 break;
2094 case CMD_NEW_CHAIN:
2095 ret = iptc_create_chain(chain, handle);
2096 break;
2097 case CMD_DELETE_CHAIN:
2098 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2099 break;
2100 case CMD_RENAME_CHAIN:
2101 ret = iptc_rename_chain(chain, newname, handle);
2102 break;
2103 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002104 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002105 break;
2106 default:
2107 /* We should never reach this... */
2108 exit_tryhelp(2);
2109 }
2110
2111 if (verbose > 1)
2112 dump_entries(*handle);
2113
Martin Josefsson78cafda2004-02-02 20:01:18 +00002114 clear_rule_matches(&matches);
2115
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002116 if (e != NULL) {
2117 free(e);
2118 e = NULL;
2119 }
2120
keso6997cdf2004-07-04 15:20:53 +00002121 free(saddrs);
2122 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002123 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002124
Marc Bouchere6869a82000-03-20 06:03:29 +00002125 return ret;
2126}