blob: 16726d3084af43bcf32851fe2f3d630a76da7192 [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));
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +0000866 if (merge == NULL)
867 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000868 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000869 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000870 for (i = 0; i < num_new; i++) {
871 merge[num_old + i] = newopts[i];
872 merge[num_old + i].val += *option_offset;
873 }
874 memset(merge + num_old + num_new, 0, sizeof(struct option));
875
876 return merge;
877}
878
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000879void register_match(struct iptables_match *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000880{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000881 me->family = PF_INET;
882 xtables_register_match(me);
Rusty Russell3aef54d2005-01-03 03:48:40 +0000883}
884
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000885void register_target(struct iptables_target *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000886{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000887 me->family = PF_INET;
888 xtables_register_target(me);
Marc Bouchere6869a82000-03-20 06:03:29 +0000889}
890
891static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000892print_num(u_int64_t number, unsigned int format)
893{
894 if (format & FMT_KILOMEGAGIGA) {
895 if (number > 99999) {
896 number = (number + 500) / 1000;
897 if (number > 9999) {
898 number = (number + 500) / 1000;
899 if (number > 9999) {
900 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000901 if (number > 9999) {
902 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000903 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000904 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000905 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000906 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000907 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000908 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000909 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000910 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000911 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000912 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000913 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000914}
915
916
917static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000918print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
919{
920 struct ipt_counters counters;
921 const char *pol = iptc_get_policy(chain, &counters, handle);
922 printf("Chain %s", chain);
923 if (pol) {
924 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000925 if (!(format & FMT_NOCOUNTS)) {
926 fputc(' ', stdout);
927 print_num(counters.pcnt, (format|FMT_NOTABLE));
928 fputs("packets, ", stdout);
929 print_num(counters.bcnt, (format|FMT_NOTABLE));
930 fputs("bytes", stdout);
931 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000932 printf(")\n");
933 } else {
934 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000935 if (!iptc_get_references(&refs, chain, handle))
936 printf(" (ERROR obtaining refs)\n");
937 else
938 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000939 }
940
941 if (format & FMT_LINENUMBERS)
942 printf(FMT("%-4s ", "%s "), "num");
943 if (!(format & FMT_NOCOUNTS)) {
944 if (format & FMT_KILOMEGAGIGA) {
945 printf(FMT("%5s ","%s "), "pkts");
946 printf(FMT("%5s ","%s "), "bytes");
947 } else {
948 printf(FMT("%8s ","%s "), "pkts");
949 printf(FMT("%10s ","%s "), "bytes");
950 }
951 }
952 if (!(format & FMT_NOTARGET))
953 printf(FMT("%-9s ","%s "), "target");
954 fputs(" prot ", stdout);
955 if (format & FMT_OPTIONS)
956 fputs("opt", stdout);
957 if (format & FMT_VIA) {
958 printf(FMT(" %-6s ","%s "), "in");
959 printf(FMT("%-6s ","%s "), "out");
960 }
961 printf(FMT(" %-19s ","%s "), "source");
962 printf(FMT(" %-19s "," %s "), "destination");
963 printf("\n");
964}
965
Marc Bouchere6869a82000-03-20 06:03:29 +0000966
967static int
968print_match(const struct ipt_entry_match *m,
969 const struct ipt_ip *ip,
970 int numeric)
971{
Martin Josefsson78cafda2004-02-02 20:01:18 +0000972 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000973
974 if (match) {
975 if (match->print)
976 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000977 else
Rusty Russellb039b022000-09-01 06:04:05 +0000978 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000979 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000980 if (m->u.user.name[0])
981 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000982 }
983 /* Don't stop iterating. */
984 return 0;
985}
986
987/* e is called `fw' here for hysterical raisins */
988static void
989print_firewall(const struct ipt_entry *fw,
990 const char *targname,
991 unsigned int num,
992 unsigned int format,
993 const iptc_handle_t handle)
994{
995 struct iptables_target *target = NULL;
996 const struct ipt_entry_target *t;
997 u_int8_t flags;
998 char buf[BUFSIZ];
999
Marc Bouchere6869a82000-03-20 06:03:29 +00001000 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +00001001 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001002 else
Rusty Russell52a51492000-05-02 16:44:29 +00001003 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001004
1005 t = ipt_get_target((struct ipt_entry *)fw);
1006 flags = fw->ip.flags;
1007
1008 if (format & FMT_LINENUMBERS)
1009 printf(FMT("%-4u ", "%u "), num+1);
1010
1011 if (!(format & FMT_NOCOUNTS)) {
1012 print_num(fw->counters.pcnt, format);
1013 print_num(fw->counters.bcnt, format);
1014 }
1015
1016 if (!(format & FMT_NOTARGET))
1017 printf(FMT("%-9s ", "%s "), targname);
1018
1019 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
1020 {
Rusty Russell28381a42000-05-10 00:19:50 +00001021 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +00001022 if (pname)
1023 printf(FMT("%-5s", "%s "), pname);
1024 else
1025 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
1026 }
1027
1028 if (format & FMT_OPTIONS) {
1029 if (format & FMT_NOTABLE)
1030 fputs("opt ", stdout);
1031 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
1032 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
1033 fputc(' ', stdout);
1034 }
1035
1036 if (format & FMT_VIA) {
1037 char iface[IFNAMSIZ+2];
1038
1039 if (fw->ip.invflags & IPT_INV_VIA_IN) {
1040 iface[0] = '!';
1041 iface[1] = '\0';
1042 }
1043 else iface[0] = '\0';
1044
1045 if (fw->ip.iniface[0] != '\0') {
1046 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001047 }
1048 else if (format & FMT_NUMERIC) strcat(iface, "*");
1049 else strcat(iface, "any");
1050 printf(FMT(" %-6s ","in %s "), iface);
1051
1052 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
1053 iface[0] = '!';
1054 iface[1] = '\0';
1055 }
1056 else iface[0] = '\0';
1057
1058 if (fw->ip.outiface[0] != '\0') {
1059 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +00001060 }
1061 else if (format & FMT_NUMERIC) strcat(iface, "*");
1062 else strcat(iface, "any");
1063 printf(FMT("%-6s ","out %s "), iface);
1064 }
1065
1066 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
1067 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
1068 printf(FMT("%-19s ","%s "), "anywhere");
1069 else {
1070 if (format & FMT_NUMERIC)
1071 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.src)));
1072 else
1073 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.src)));
1074 strcat(buf, mask_to_dotted(&(fw->ip.smsk)));
1075 printf(FMT("%-19s ","%s "), buf);
1076 }
1077
1078 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
1079 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +00001080 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +00001081 else {
1082 if (format & FMT_NUMERIC)
1083 sprintf(buf, "%s", addr_to_dotted(&(fw->ip.dst)));
1084 else
1085 sprintf(buf, "%s", addr_to_anyname(&(fw->ip.dst)));
1086 strcat(buf, mask_to_dotted(&(fw->ip.dmsk)));
Harald Welte25fc1d72003-05-31 21:30:33 +00001087 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +00001088 }
1089
1090 if (format & FMT_NOTABLE)
1091 fputs(" ", stdout);
1092
Harald Welte72bd87e2005-11-24 17:04:05 +00001093#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001094 if(fw->ip.flags & IPT_F_GOTO)
1095 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +00001096#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001097
Marc Bouchere6869a82000-03-20 06:03:29 +00001098 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
1099
1100 if (target) {
1101 if (target->print)
1102 /* Print the target information. */
1103 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +00001104 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +00001105 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001106 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +00001107
1108 if (!(format & FMT_NONEWLINE))
1109 fputc('\n', stdout);
1110}
1111
1112static void
1113print_firewall_line(const struct ipt_entry *fw,
1114 const iptc_handle_t h)
1115{
1116 struct ipt_entry_target *t;
1117
1118 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +00001119 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +00001120}
1121
1122static int
1123append_entry(const ipt_chainlabel chain,
1124 struct ipt_entry *fw,
1125 unsigned int nsaddrs,
1126 const struct in_addr saddrs[],
1127 unsigned int ndaddrs,
1128 const struct in_addr daddrs[],
1129 int verbose,
1130 iptc_handle_t *handle)
1131{
1132 unsigned int i, j;
1133 int ret = 1;
1134
1135 for (i = 0; i < nsaddrs; i++) {
1136 fw->ip.src.s_addr = saddrs[i].s_addr;
1137 for (j = 0; j < ndaddrs; j++) {
1138 fw->ip.dst.s_addr = daddrs[j].s_addr;
1139 if (verbose)
1140 print_firewall_line(fw, *handle);
1141 ret &= iptc_append_entry(chain, fw, handle);
1142 }
1143 }
1144
1145 return ret;
1146}
1147
1148static int
1149replace_entry(const ipt_chainlabel chain,
1150 struct ipt_entry *fw,
1151 unsigned int rulenum,
1152 const struct in_addr *saddr,
1153 const struct in_addr *daddr,
1154 int verbose,
1155 iptc_handle_t *handle)
1156{
1157 fw->ip.src.s_addr = saddr->s_addr;
1158 fw->ip.dst.s_addr = daddr->s_addr;
1159
1160 if (verbose)
1161 print_firewall_line(fw, *handle);
1162 return iptc_replace_entry(chain, fw, rulenum, handle);
1163}
1164
1165static int
1166insert_entry(const ipt_chainlabel chain,
1167 struct ipt_entry *fw,
1168 unsigned int rulenum,
1169 unsigned int nsaddrs,
1170 const struct in_addr saddrs[],
1171 unsigned int ndaddrs,
1172 const struct in_addr daddrs[],
1173 int verbose,
1174 iptc_handle_t *handle)
1175{
1176 unsigned int i, j;
1177 int ret = 1;
1178
1179 for (i = 0; i < nsaddrs; i++) {
1180 fw->ip.src.s_addr = saddrs[i].s_addr;
1181 for (j = 0; j < ndaddrs; j++) {
1182 fw->ip.dst.s_addr = daddrs[j].s_addr;
1183 if (verbose)
1184 print_firewall_line(fw, *handle);
1185 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
1186 }
1187 }
1188
1189 return ret;
1190}
1191
Rusty Russell2e0a3212000-04-19 11:23:18 +00001192static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +00001193make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +00001194{
1195 /* Establish mask for comparison */
1196 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001197 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001198 unsigned char *mask, *mptr;
1199
1200 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001201 for (matchp = matches; matchp; matchp = matchp->next)
1202 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001203
Rusty Russell9e1d2142000-04-23 09:11:12 +00001204 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +00001205 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001206 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001207
Rusty Russell9e1d2142000-04-23 09:11:12 +00001208 memset(mask, 0xFF, sizeof(struct ipt_entry));
1209 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001210
Martin Josefsson78cafda2004-02-02 20:01:18 +00001211 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +00001212 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001213 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +00001214 + matchp->match->userspacesize);
1215 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001216 }
1217
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001218 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +00001219 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001220 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001221
1222 return mask;
1223}
1224
Marc Bouchere6869a82000-03-20 06:03:29 +00001225static int
1226delete_entry(const ipt_chainlabel chain,
1227 struct ipt_entry *fw,
1228 unsigned int nsaddrs,
1229 const struct in_addr saddrs[],
1230 unsigned int ndaddrs,
1231 const struct in_addr daddrs[],
1232 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001233 iptc_handle_t *handle,
1234 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +00001235{
1236 unsigned int i, j;
1237 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001238 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +00001239
Martin Josefsson78cafda2004-02-02 20:01:18 +00001240 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001241 for (i = 0; i < nsaddrs; i++) {
1242 fw->ip.src.s_addr = saddrs[i].s_addr;
1243 for (j = 0; j < ndaddrs; j++) {
1244 fw->ip.dst.s_addr = daddrs[j].s_addr;
1245 if (verbose)
1246 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001247 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001248 }
1249 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001250 free(mask);
1251
Marc Bouchere6869a82000-03-20 06:03:29 +00001252 return ret;
1253}
1254
Harald Welteae1ff9f2000-12-01 14:26:20 +00001255int
Marc Bouchere6869a82000-03-20 06:03:29 +00001256for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +00001257 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001258{
1259 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001260 const char *chain;
1261 char *chains;
1262 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001263
Rusty Russell9e1d2142000-04-23 09:11:12 +00001264 chain = iptc_first_chain(handle);
1265 while (chain) {
1266 chaincount++;
1267 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001268 }
1269
Rusty Russell9e1d2142000-04-23 09:11:12 +00001270 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1271 i = 0;
1272 chain = iptc_first_chain(handle);
1273 while (chain) {
1274 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1275 i++;
1276 chain = iptc_next_chain(handle);
1277 }
1278
1279 for (i = 0; i < chaincount; i++) {
1280 if (!builtinstoo
1281 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001282 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001283 continue;
1284 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1285 }
1286
1287 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001288 return ret;
1289}
1290
Harald Welteae1ff9f2000-12-01 14:26:20 +00001291int
Marc Bouchere6869a82000-03-20 06:03:29 +00001292flush_entries(const ipt_chainlabel chain, int verbose,
1293 iptc_handle_t *handle)
1294{
1295 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001296 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001297
1298 if (verbose)
1299 fprintf(stdout, "Flushing chain `%s'\n", chain);
1300 return iptc_flush_entries(chain, handle);
1301}
Marc Bouchere6869a82000-03-20 06:03:29 +00001302
1303static int
1304zero_entries(const ipt_chainlabel chain, int verbose,
1305 iptc_handle_t *handle)
1306{
1307 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001308 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001309
Marc Bouchere6869a82000-03-20 06:03:29 +00001310 if (verbose)
1311 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1312 return iptc_zero_entries(chain, handle);
1313}
1314
Harald Welteae1ff9f2000-12-01 14:26:20 +00001315int
Marc Bouchere6869a82000-03-20 06:03:29 +00001316delete_chain(const ipt_chainlabel chain, int verbose,
1317 iptc_handle_t *handle)
1318{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001319 if (!chain)
1320 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001321
1322 if (verbose)
1323 fprintf(stdout, "Deleting chain `%s'\n", chain);
1324 return iptc_delete_chain(chain, handle);
1325}
1326
1327static int
1328list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1329 int expanded, int linenumbers, iptc_handle_t *handle)
1330{
1331 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001332 unsigned int format;
1333 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001334
1335 format = FMT_OPTIONS;
1336 if (!verbose)
1337 format |= FMT_NOCOUNTS;
1338 else
1339 format |= FMT_VIA;
1340
1341 if (numeric)
1342 format |= FMT_NUMERIC;
1343
1344 if (!expanded)
1345 format |= FMT_KILOMEGAGIGA;
1346
1347 if (linenumbers)
1348 format |= FMT_LINENUMBERS;
1349
Rusty Russell9e1d2142000-04-23 09:11:12 +00001350 for (this = iptc_first_chain(handle);
1351 this;
1352 this = iptc_next_chain(handle)) {
1353 const struct ipt_entry *i;
1354 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001355
Marc Bouchere6869a82000-03-20 06:03:29 +00001356 if (chain && strcmp(chain, this) != 0)
1357 continue;
1358
1359 if (found) printf("\n");
1360
1361 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001362 i = iptc_first_rule(this, handle);
1363
1364 num = 0;
1365 while (i) {
1366 print_firewall(i,
1367 iptc_get_target(i, handle),
1368 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001369 format,
1370 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001371 i = iptc_next_rule(i, handle);
1372 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 found = 1;
1374 }
1375
1376 errno = ENOENT;
1377 return found;
1378}
1379
1380static struct ipt_entry *
1381generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001382 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001383 struct ipt_entry_target *target)
1384{
1385 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001386 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 struct ipt_entry *e;
1388
1389 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001390 for (matchp = matches; matchp; matchp = matchp->next)
1391 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001392
Rusty Russell228e98d2000-04-27 10:28:06 +00001393 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001394 *e = *fw;
1395 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001396 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001397
1398 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001399 for (matchp = matches; matchp; matchp = matchp->next) {
1400 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1401 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001402 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001403 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001404
1405 return e;
1406}
1407
Martin Josefsson78cafda2004-02-02 20:01:18 +00001408void clear_rule_matches(struct iptables_rule_match **matches)
1409{
1410 struct iptables_rule_match *matchp, *tmp;
1411
1412 for (matchp = *matches; matchp;) {
1413 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001414 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001415 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001416 matchp->match->m = NULL;
1417 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001418 if (matchp->match == matchp->match->next) {
1419 free(matchp->match);
1420 matchp->match = NULL;
1421 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001422 free(matchp);
1423 matchp = tmp;
1424 }
1425
1426 *matches = NULL;
1427}
1428
Rusty Russell3aef54d2005-01-03 03:48:40 +00001429static void set_revision(char *name, u_int8_t revision)
1430{
1431 /* Old kernel sources don't have ".revision" field,
1432 but we stole a byte from name. */
1433 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1434 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1435}
1436
Phil Oester8cf65912005-09-19 15:00:33 +00001437void
1438get_kernel_version(void) {
1439 static struct utsname uts;
1440 int x = 0, y = 0, z = 0;
1441
1442 if (uname(&uts) == -1) {
1443 fprintf(stderr, "Unable to retrieve kernel version.\n");
1444 free_opts(1);
1445 exit(1);
1446 }
1447
1448 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1449 kernel_version = LINUX_VERSION(x, y, z);
1450}
1451
Marc Bouchere6869a82000-03-20 06:03:29 +00001452int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1453{
1454 struct ipt_entry fw, *e = NULL;
1455 int invert = 0;
1456 unsigned int nsaddrs = 0, ndaddrs = 0;
1457 struct in_addr *saddrs = NULL, *daddrs = NULL;
1458
1459 int c, verbose = 0;
1460 const char *chain = NULL;
1461 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1462 const char *policy = NULL, *newname = NULL;
1463 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001464 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001465 int ret = 1;
1466 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001467 struct iptables_rule_match *matches = NULL;
1468 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001469 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001470 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001471 const char *jumpto = "";
1472 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001473 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001474 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001475
1476 memset(&fw, 0, sizeof(fw));
1477
Harald Welteae1ff9f2000-12-01 14:26:20 +00001478 /* re-set optind to 0 in case do_command gets called
1479 * a second time */
1480 optind = 0;
1481
1482 /* clear mflags in case do_command gets called a second time
1483 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001484 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001485 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001486
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001487 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001488 t->tflags = 0;
1489 t->used = 0;
1490 }
1491
Marc Bouchere6869a82000-03-20 06:03:29 +00001492 /* Suppress error messages: we may add new options if we
1493 demand-load a protocol. */
1494 opterr = 0;
1495
1496 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001497 "-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 +00001498 opts, NULL)) != -1) {
1499 switch (c) {
1500 /*
1501 * Command selection
1502 */
1503 case 'A':
1504 add_command(&command, CMD_APPEND, CMD_NONE,
1505 invert);
1506 chain = optarg;
1507 break;
1508
1509 case 'D':
1510 add_command(&command, CMD_DELETE, CMD_NONE,
1511 invert);
1512 chain = optarg;
1513 if (optind < argc && argv[optind][0] != '-'
1514 && argv[optind][0] != '!') {
1515 rulenum = parse_rulenumber(argv[optind++]);
1516 command = CMD_DELETE_NUM;
1517 }
1518 break;
1519
Marc Bouchere6869a82000-03-20 06:03:29 +00001520 case 'R':
1521 add_command(&command, CMD_REPLACE, CMD_NONE,
1522 invert);
1523 chain = optarg;
1524 if (optind < argc && argv[optind][0] != '-'
1525 && argv[optind][0] != '!')
1526 rulenum = parse_rulenumber(argv[optind++]);
1527 else
1528 exit_error(PARAMETER_PROBLEM,
1529 "-%c requires a rule number",
1530 cmd2char(CMD_REPLACE));
1531 break;
1532
1533 case 'I':
1534 add_command(&command, CMD_INSERT, CMD_NONE,
1535 invert);
1536 chain = optarg;
1537 if (optind < argc && argv[optind][0] != '-'
1538 && argv[optind][0] != '!')
1539 rulenum = parse_rulenumber(argv[optind++]);
1540 else rulenum = 1;
1541 break;
1542
1543 case 'L':
1544 add_command(&command, CMD_LIST, CMD_ZERO,
1545 invert);
1546 if (optarg) chain = optarg;
1547 else if (optind < argc && argv[optind][0] != '-'
1548 && argv[optind][0] != '!')
1549 chain = argv[optind++];
1550 break;
1551
1552 case 'F':
1553 add_command(&command, CMD_FLUSH, CMD_NONE,
1554 invert);
1555 if (optarg) chain = optarg;
1556 else if (optind < argc && argv[optind][0] != '-'
1557 && argv[optind][0] != '!')
1558 chain = argv[optind++];
1559 break;
1560
1561 case 'Z':
1562 add_command(&command, CMD_ZERO, CMD_LIST,
1563 invert);
1564 if (optarg) chain = optarg;
1565 else if (optind < argc && argv[optind][0] != '-'
1566 && argv[optind][0] != '!')
1567 chain = argv[optind++];
1568 break;
1569
1570 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001571 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001572 exit_error(PARAMETER_PROBLEM,
1573 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001574 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001575 if (find_target(optarg, TRY_LOAD))
1576 exit_error(PARAMETER_PROBLEM,
1577 "chain name may not clash "
1578 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001579 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1580 invert);
1581 chain = optarg;
1582 break;
1583
1584 case 'X':
1585 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1586 invert);
1587 if (optarg) chain = optarg;
1588 else if (optind < argc && argv[optind][0] != '-'
1589 && argv[optind][0] != '!')
1590 chain = argv[optind++];
1591 break;
1592
1593 case 'E':
1594 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1595 invert);
1596 chain = optarg;
1597 if (optind < argc && argv[optind][0] != '-'
1598 && argv[optind][0] != '!')
1599 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001600 else
1601 exit_error(PARAMETER_PROBLEM,
1602 "-%c requires old-chain-name and "
1603 "new-chain-name",
1604 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001605 break;
1606
1607 case 'P':
1608 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1609 invert);
1610 chain = optarg;
1611 if (optind < argc && argv[optind][0] != '-'
1612 && argv[optind][0] != '!')
1613 policy = argv[optind++];
1614 else
1615 exit_error(PARAMETER_PROBLEM,
1616 "-%c requires a chain and a policy",
1617 cmd2char(CMD_SET_POLICY));
1618 break;
1619
1620 case 'h':
1621 if (!optarg)
1622 optarg = argv[optind];
1623
Rusty Russell2e0a3212000-04-19 11:23:18 +00001624 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001625 if (!matches && protocol)
1626 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001627
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001628 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001629
1630 /*
1631 * Option selection
1632 */
1633 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001634 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001635 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1636 invert);
1637
1638 /* Canonicalize into lower case */
1639 for (protocol = argv[optind-1]; *protocol; protocol++)
1640 *protocol = tolower(*protocol);
1641
1642 protocol = argv[optind-1];
1643 fw.ip.proto = parse_protocol(protocol);
1644
1645 if (fw.ip.proto == 0
1646 && (fw.ip.invflags & IPT_INV_PROTO))
1647 exit_error(PARAMETER_PROBLEM,
1648 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001649 break;
1650
1651 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001652 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001653 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1654 invert);
1655 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 break;
1657
1658 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001659 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001660 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1661 invert);
1662 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001663 break;
1664
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001665#ifdef IPT_F_GOTO
1666 case 'g':
1667 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1668 invert);
1669 fw.ip.flags |= IPT_F_GOTO;
1670 jumpto = parse_target(optarg);
1671 break;
1672#endif
1673
Marc Bouchere6869a82000-03-20 06:03:29 +00001674 case 'j':
1675 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1676 invert);
1677 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001678 /* TRY_LOAD (may be chain name) */
1679 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001680
1681 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001682 size_t size;
1683
Rusty Russell73f72f52000-07-03 10:17:57 +00001684 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1685 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001686
Rusty Russell2e0a3212000-04-19 11:23:18 +00001687 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001688 target->t->u.target_size = size;
1689 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001690 set_revision(target->t->u.user.name,
1691 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001692 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001693 target->init(target->t);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001694 opts = merge_options(opts,
1695 target->extra_opts,
1696 &target->option_offset);
1697 if (opts == NULL)
1698 exit_error(OTHER_PROBLEM,
1699 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001700 }
1701 break;
1702
1703
1704 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001705 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001706 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1707 invert);
1708 parse_interface(argv[optind-1],
1709 fw.ip.iniface,
1710 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 break;
1712
1713 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001714 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001715 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1716 invert);
1717 parse_interface(argv[optind-1],
1718 fw.ip.outiface,
1719 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001720 break;
1721
1722 case 'f':
1723 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1724 invert);
1725 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001726 break;
1727
1728 case 'v':
1729 if (!verbose)
1730 set_option(&options, OPT_VERBOSE,
1731 &fw.ip.invflags, invert);
1732 verbose++;
1733 break;
1734
Rusty Russell52a51492000-05-02 16:44:29 +00001735 case 'm': {
1736 size_t size;
1737
Marc Bouchere6869a82000-03-20 06:03:29 +00001738 if (invert)
1739 exit_error(PARAMETER_PROBLEM,
1740 "unexpected ! flag before --match");
1741
Martin Josefsson78cafda2004-02-02 20:01:18 +00001742 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001743 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1744 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001745 m->m = fw_calloc(1, size);
1746 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001747 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001748 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001749 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001750 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001751 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001752 /* Merge options for non-cloned matches */
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001753 opts = merge_options(opts,
1754 m->extra_opts,
1755 &m->option_offset);
1756 if (opts == NULL)
1757 exit_error(OTHER_PROBLEM,
1758 "can't alloc memory!");
1759 }
Rusty Russell52a51492000-05-02 16:44:29 +00001760 }
1761 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
1763 case 'n':
1764 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1765 invert);
1766 break;
1767
1768 case 't':
1769 if (invert)
1770 exit_error(PARAMETER_PROBLEM,
1771 "unexpected ! flag before --table");
1772 *table = argv[optind-1];
1773 break;
1774
1775 case 'x':
1776 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1777 invert);
1778 break;
1779
1780 case 'V':
1781 if (invert)
1782 printf("Not %s ;-)\n", program_version);
1783 else
1784 printf("%s v%s\n",
1785 program_name, program_version);
1786 exit(0);
1787
1788 case '0':
1789 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1790 invert);
1791 break;
1792
Harald Welte82dd2ec2000-12-19 05:18:15 +00001793 case 'M':
1794 modprobe = optarg;
1795 break;
1796
Harald Welteccd49e52001-01-23 22:54:34 +00001797 case 'c':
1798
1799 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1800 invert);
1801 pcnt = optarg;
1802 if (optind < argc && argv[optind][0] != '-'
1803 && argv[optind][0] != '!')
1804 bcnt = argv[optind++];
1805 else
1806 exit_error(PARAMETER_PROBLEM,
1807 "-%c requires packet and byte counter",
1808 opt2char(OPT_COUNTERS));
1809
Patrick McHardy875441e2007-10-17 08:48:58 +00001810 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001811 exit_error(PARAMETER_PROBLEM,
1812 "-%c packet counter not numeric",
1813 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001814 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001815
Patrick McHardy875441e2007-10-17 08:48:58 +00001816 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001817 exit_error(PARAMETER_PROBLEM,
1818 "-%c byte counter not numeric",
1819 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001820 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001821 break;
1822
1823
Marc Bouchere6869a82000-03-20 06:03:29 +00001824 case 1: /* non option */
1825 if (optarg[0] == '!' && optarg[1] == '\0') {
1826 if (invert)
1827 exit_error(PARAMETER_PROBLEM,
1828 "multiple consecutive ! not"
1829 " allowed");
1830 invert = TRUE;
1831 optarg[0] = '\0';
1832 continue;
1833 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001834 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001835 exit_tryhelp(2);
1836
1837 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001838 if (!target
1839 || !(target->parse(c - target->option_offset,
1840 argv, invert,
1841 &target->tflags,
1842 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001843 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001844 if (matchp->completed)
1845 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001846 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001847 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001848 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001849 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001850 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001851 break;
1852 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001853 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001854
1855 /* If you listen carefully, you can
1856 actually hear this code suck. */
1857
1858 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001859 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001860 * parameter, that has not been parsed yet,
1861 * it's not an option of an explicitly loaded
1862 * match or a target. However, we support
1863 * implicit loading of the protocol match
1864 * extension. '-p tcp' means 'l4 proto 6' and
1865 * at the same time 'load tcp protocol match on
1866 * demand if we specify --dport'.
1867 *
1868 * To make this work, we need to make sure:
1869 * - the parameter has not been parsed by
1870 * a match (m above)
1871 * - a protocol has been specified
1872 * - the protocol extension has not been
1873 * loaded yet, or is loaded and unused
1874 * [think of iptables-restore!]
1875 * - the protocol extension can be successively
1876 * loaded
1877 */
1878 if (m == NULL
1879 && protocol
1880 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001881 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001882 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001883 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001884 && (proto_used == 0))
1885 )
1886 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001887 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001888 /* Try loading protocol */
1889 size_t size;
1890
1891 proto_used = 1;
1892
1893 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1894 + m->size;
1895
1896 m->m = fw_calloc(1, size);
1897 m->m->u.match_size = size;
1898 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001899 set_revision(m->m->u.user.name,
1900 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001901 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001902 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001903
1904 opts = merge_options(opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001905 m->extra_opts,
1906 &m->option_offset);
1907 if (opts == NULL)
1908 exit_error(OTHER_PROBLEM,
1909 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001910
1911 optind--;
1912 continue;
1913 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001914 if (!m)
1915 exit_error(PARAMETER_PROBLEM,
1916 "Unknown arg `%s'",
1917 argv[optind-1]);
1918 }
1919 }
1920 invert = FALSE;
1921 }
1922
Martin Josefsson78cafda2004-02-02 20:01:18 +00001923 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001924 if (matchp->match->final_check != NULL)
1925 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001926
Jan Engelhardt830132a2007-10-04 16:24:50 +00001927 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001928 target->final_check(target->tflags);
1929
1930 /* Fix me: must put inverse options checking here --MN */
1931
1932 if (optind < argc)
1933 exit_error(PARAMETER_PROBLEM,
1934 "unknown arguments found on commandline");
1935 if (!command)
1936 exit_error(PARAMETER_PROBLEM, "no command specified");
1937 if (invert)
1938 exit_error(PARAMETER_PROBLEM,
1939 "nothing appropriate following !");
1940
Harald Welte6336bfd2002-05-07 14:41:43 +00001941 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001942 if (!(options & OPT_DESTINATION))
1943 dhostnetworkmask = "0.0.0.0/0";
1944 if (!(options & OPT_SOURCE))
1945 shostnetworkmask = "0.0.0.0/0";
1946 }
1947
1948 if (shostnetworkmask)
1949 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1950 &(fw.ip.smsk), &nsaddrs);
1951
1952 if (dhostnetworkmask)
1953 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1954 &(fw.ip.dmsk), &ndaddrs);
1955
1956 if ((nsaddrs > 1 || ndaddrs > 1) &&
1957 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1958 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1959 " source or destination IP addresses");
1960
Marc Bouchere6869a82000-03-20 06:03:29 +00001961 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1962 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1963 "specify a unique address");
1964
1965 generic_opt_check(command, options);
1966
1967 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1968 exit_error(PARAMETER_PROBLEM,
1969 "chain name `%s' too long (must be under %i chars)",
1970 chain, IPT_FUNCTION_MAXNAMELEN);
1971
Harald Welteae1ff9f2000-12-01 14:26:20 +00001972 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001973 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001974 *handle = iptc_init(*table);
1975
Rusty Russell8beb0492004-12-22 00:37:10 +00001976 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001977 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001978 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001979
Marc Bouchere6869a82000-03-20 06:03:29 +00001980 if (!*handle)
1981 exit_error(VERSION_PROBLEM,
1982 "can't initialize iptables table `%s': %s",
1983 *table, iptc_strerror(errno));
1984
Harald Welte6336bfd2002-05-07 14:41:43 +00001985 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001986 || command == CMD_DELETE
1987 || command == CMD_INSERT
1988 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001989 if (strcmp(chain, "PREROUTING") == 0
1990 || strcmp(chain, "INPUT") == 0) {
1991 /* -o not valid with incoming packets. */
1992 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001993 exit_error(PARAMETER_PROBLEM,
1994 "Can't use -%c with %s\n",
1995 opt2char(OPT_VIANAMEOUT),
1996 chain);
1997 }
1998
Rusty Russella4860fd2000-06-17 16:13:02 +00001999 if (strcmp(chain, "POSTROUTING") == 0
2000 || strcmp(chain, "OUTPUT") == 0) {
2001 /* -i not valid with outgoing packets */
2002 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002003 exit_error(PARAMETER_PROBLEM,
2004 "Can't use -%c with %s\n",
2005 opt2char(OPT_VIANAMEIN),
2006 chain);
2007 }
2008
2009 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00002010 fprintf(stderr,
2011 "Warning: using chain %s, not extension\n",
2012 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002013
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002014 if (target->t)
2015 free(target->t);
2016
Marc Bouchere6869a82000-03-20 06:03:29 +00002017 target = NULL;
2018 }
2019
2020 /* If they didn't specify a target, or it's a chain
2021 name, use standard. */
2022 if (!target
2023 && (strlen(jumpto) == 0
2024 || iptc_is_chain(jumpto, *handle))) {
2025 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002026
Rusty Russell52a51492000-05-02 16:44:29 +00002027 target = find_target(IPT_STANDARD_TARGET,
2028 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002029
2030 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002031 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00002032 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002033 target->t->u.target_size = size;
2034 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002035 if (!iptc_is_chain(jumpto, *handle))
2036 set_revision(target->t->u.user.name,
2037 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002038 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00002039 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 }
2041
Rusty Russell7e53bf92000-03-20 07:03:28 +00002042 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002043 /* it is no chain, and we can't load a plugin.
2044 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002045 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002046 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002047#ifdef IPT_F_GOTO
2048 if (fw.ip.flags & IPT_F_GOTO)
2049 exit_error(PARAMETER_PROBLEM,
2050 "goto '%s' is not a chain\n", jumpto);
2051#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002052 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002053 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002054 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002055 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002056 }
2057 }
2058
2059 switch (command) {
2060 case CMD_APPEND:
2061 ret = append_entry(chain, e,
2062 nsaddrs, saddrs, ndaddrs, daddrs,
2063 options&OPT_VERBOSE,
2064 handle);
2065 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002066 case CMD_DELETE:
2067 ret = delete_entry(chain, e,
2068 nsaddrs, saddrs, ndaddrs, daddrs,
2069 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00002070 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002071 break;
2072 case CMD_DELETE_NUM:
2073 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
2074 break;
2075 case CMD_REPLACE:
2076 ret = replace_entry(chain, e, rulenum - 1,
2077 saddrs, daddrs, options&OPT_VERBOSE,
2078 handle);
2079 break;
2080 case CMD_INSERT:
2081 ret = insert_entry(chain, e, rulenum - 1,
2082 nsaddrs, saddrs, ndaddrs, daddrs,
2083 options&OPT_VERBOSE,
2084 handle);
2085 break;
2086 case CMD_LIST:
2087 ret = list_entries(chain,
2088 options&OPT_VERBOSE,
2089 options&OPT_NUMERIC,
2090 options&OPT_EXPANDED,
2091 options&OPT_LINENUMBERS,
2092 handle);
2093 break;
2094 case CMD_FLUSH:
2095 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2096 break;
2097 case CMD_ZERO:
2098 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2099 break;
2100 case CMD_LIST|CMD_ZERO:
2101 ret = list_entries(chain,
2102 options&OPT_VERBOSE,
2103 options&OPT_NUMERIC,
2104 options&OPT_EXPANDED,
2105 options&OPT_LINENUMBERS,
2106 handle);
2107 if (ret)
2108 ret = zero_entries(chain,
2109 options&OPT_VERBOSE, handle);
2110 break;
2111 case CMD_NEW_CHAIN:
2112 ret = iptc_create_chain(chain, handle);
2113 break;
2114 case CMD_DELETE_CHAIN:
2115 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2116 break;
2117 case CMD_RENAME_CHAIN:
2118 ret = iptc_rename_chain(chain, newname, handle);
2119 break;
2120 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002121 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002122 break;
2123 default:
2124 /* We should never reach this... */
2125 exit_tryhelp(2);
2126 }
2127
2128 if (verbose > 1)
2129 dump_entries(*handle);
2130
Martin Josefsson78cafda2004-02-02 20:01:18 +00002131 clear_rule_matches(&matches);
2132
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002133 if (e != NULL) {
2134 free(e);
2135 e = NULL;
2136 }
2137
keso6997cdf2004-07-04 15:20:53 +00002138 free(saddrs);
2139 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002140 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002141
Marc Bouchere6869a82000-03-20 06:03:29 +00002142 return ret;
2143}