blob: 75fe583171c9197929796a16c72b06576f866aad [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>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010032#include <stdbool.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000033#include <stdio.h>
34#include <stdlib.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000035#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000038#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000039#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000040#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000041#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000042#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000043
44#ifndef TRUE
45#define TRUE 1
46#endif
47#ifndef FALSE
48#define FALSE 0
49#endif
50
Marc Bouchere6869a82000-03-20 06:03:29 +000051#define FMT_NUMERIC 0x0001
52#define FMT_NOCOUNTS 0x0002
53#define FMT_KILOMEGAGIGA 0x0004
54#define FMT_OPTIONS 0x0008
55#define FMT_NOTABLE 0x0010
56#define FMT_NOTARGET 0x0020
57#define FMT_VIA 0x0040
58#define FMT_NONEWLINE 0x0080
59#define FMT_LINENUMBERS 0x0100
60
61#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
62 | FMT_NUMERIC | FMT_NOTABLE)
63#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
64
65
66#define CMD_NONE 0x0000U
67#define CMD_INSERT 0x0001U
68#define CMD_DELETE 0x0002U
69#define CMD_DELETE_NUM 0x0004U
70#define CMD_REPLACE 0x0008U
71#define CMD_APPEND 0x0010U
72#define CMD_LIST 0x0020U
73#define CMD_FLUSH 0x0040U
74#define CMD_ZERO 0x0080U
75#define CMD_NEW_CHAIN 0x0100U
76#define CMD_DELETE_CHAIN 0x0200U
77#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000078#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020079#define CMD_LIST_RULES 0x1000U
80#define NUMBER_OF_CMD 14
Marc Bouchere6869a82000-03-20 06:03:29 +000081static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020082 'N', 'X', 'P', 'E', 'S' };
Marc Bouchere6869a82000-03-20 06:03:29 +000083
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_FRAGMENT 0x00200U
97#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000098#define OPT_COUNTERS 0x00800U
99#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +0000100static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000102
103static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100104 {.name = "append", .has_arg = 1, .val = 'A'},
105 {.name = "delete", .has_arg = 1, .val = 'D'},
106 {.name = "insert", .has_arg = 1, .val = 'I'},
107 {.name = "replace", .has_arg = 1, .val = 'R'},
108 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200109 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100110 {.name = "flush", .has_arg = 2, .val = 'F'},
111 {.name = "zero", .has_arg = 2, .val = 'Z'},
112 {.name = "new-chain", .has_arg = 1, .val = 'N'},
113 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
114 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
115 {.name = "policy", .has_arg = 1, .val = 'P'},
116 {.name = "source", .has_arg = 1, .val = 's'},
117 {.name = "destination", .has_arg = 1, .val = 'd'},
118 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
119 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
120 {.name = "protocol", .has_arg = 1, .val = 'p'},
121 {.name = "in-interface", .has_arg = 1, .val = 'i'},
122 {.name = "jump", .has_arg = 1, .val = 'j'},
123 {.name = "table", .has_arg = 1, .val = 't'},
124 {.name = "match", .has_arg = 1, .val = 'm'},
125 {.name = "numeric", .has_arg = 0, .val = 'n'},
126 {.name = "out-interface", .has_arg = 1, .val = 'o'},
127 {.name = "verbose", .has_arg = 0, .val = 'v'},
128 {.name = "exact", .has_arg = 0, .val = 'x'},
129 {.name = "fragments", .has_arg = 0, .val = 'f'},
130 {.name = "version", .has_arg = 0, .val = 'V'},
131 {.name = "help", .has_arg = 2, .val = 'h'},
132 {.name = "line-numbers", .has_arg = 0, .val = '0'},
133 {.name = "modprobe", .has_arg = 1, .val = 'M'},
134 {.name = "set-counters", .has_arg = 1, .val = 'c'},
135 {.name = "goto", .has_arg = 1, .val = 'g'},
136 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +0000137};
138
Illes Marci63e90632003-03-03 08:08:37 +0000139/* we need this for iptables-restore. iptables-restore.c sets line to the
140 * current line of the input file, in order to give a more precise error
141 * message. iptables itself doesn't need this, so it is initialized to the
142 * magic number of -1 */
143int line = -1;
144
Marc Bouchere6869a82000-03-20 06:03:29 +0000145static struct option *opts = original_opts;
146static unsigned int global_option_offset = 0;
147
148/* Table of legal combinations of commands and options. If any of the
149 * given commands make an option legal, that option is legal (applies to
150 * CMD_LIST and CMD_ZERO only).
151 * Key:
152 * + compulsory
153 * x illegal
154 * optional
155 */
156
157static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
158/* Well, it's better than "Re: Linux vs FreeBSD" */
159{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200160 /* -n -s -d -p -j -v -x -i -o -f --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
162/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
163/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
164/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
165/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
166/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
167/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
168/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
169/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
170/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200171/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200172/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
173/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000174};
175
176static int inverse_for_options[NUMBER_OF_OPT] =
177{
178/* -n */ 0,
179/* -s */ IPT_INV_SRCIP,
180/* -d */ IPT_INV_DSTIP,
181/* -p */ IPT_INV_PROTO,
182/* -j */ 0,
183/* -v */ 0,
184/* -x */ 0,
185/* -i */ IPT_INV_VIA_IN,
186/* -o */ IPT_INV_VIA_OUT,
187/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000190};
191
192const char *program_version;
193const char *program_name;
194
Phil Oester8cf65912005-09-19 15:00:33 +0000195int kernel_version;
196
Marc Bouchere6869a82000-03-20 06:03:29 +0000197/* A few hardcoded protocols for 'all' and in case the user has no
198 /etc/protocols */
199struct pprot {
200 char *name;
201 u_int8_t num;
202};
203
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000204struct afinfo afinfo = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100205 .family = NFPROTO_IPV4,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000206 .libprefix = "libipt_",
207 .ipproto = IPPROTO_IP,
208 .kmod = "ip_tables",
209 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
210 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
211};
212
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000213/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000214/* defined in netinet/in.h */
215#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000216#ifndef IPPROTO_ESP
217#define IPPROTO_ESP 50
218#endif
219#ifndef IPPROTO_AH
220#define IPPROTO_AH 51
221#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000222#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000223
Marc Bouchere6869a82000-03-20 06:03:29 +0000224static const struct pprot chain_protos[] = {
225 { "tcp", IPPROTO_TCP },
226 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000227 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000228 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000231 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000232 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000233};
234
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000235static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000236proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000237{
238 unsigned int i;
239
Rusty Russell28381a42000-05-10 00:19:50 +0000240 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000253enum {
254 IPT_DOTTED_ADDR = 0,
255 IPT_DOTTED_MASK
256};
257
Pablo Neiradfdcd642005-05-29 19:05:23 +0000258static void free_opts(int reset_offset)
259{
260 if (opts != original_opts) {
261 free(opts);
262 opts = original_opts;
263 if (reset_offset)
264 global_option_offset = 0;
265 }
266}
267
Patrick McHardy0b639362007-09-08 16:00:01 +0000268static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000269exit_tryhelp(int status)
270{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000271 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000272 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000273 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
274 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000275 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000276 exit(status);
277}
278
Patrick McHardy0b639362007-09-08 16:00:01 +0000279static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000280exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000281{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000282 struct iptables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200283 struct xtables_target *t = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000284
Marc Bouchere6869a82000-03-20 06:03:29 +0000285 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000286"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000287" %s -[RI] chain rulenum rule-specification [options]\n"
288" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200289" %s -[LS] [chain [rulenum]] [options]\n"
290" %s -[FZ] [chain] [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000291" %s -[NX] chain\n"
292" %s -E old-chain-name new-chain-name\n"
293" %s -P chain target [options]\n"
294" %s -h (print this help information)\n\n",
295 program_name, program_version, program_name, program_name,
296 program_name, program_name, program_name, program_name,
Henrik Nordstrombb340822008-05-13 13:09:23 +0200297 program_name, program_name, program_name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000298
299 printf(
300"Commands:\n"
301"Either long or short options are allowed.\n"
302" --append -A chain Append to chain\n"
303" --delete -D chain Delete matching rule from chain\n"
304" --delete -D chain rulenum\n"
305" Delete rule rulenum (1 = first) from chain\n"
306" --insert -I chain [rulenum]\n"
307" Insert in chain as rulenum (default 1=first)\n"
308" --replace -R chain rulenum\n"
309" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200310" --list -L [chain [rulenum]]\n"
311" List the rules in a chain or all chains\n"
312" --list-rules -S [chain [rulenum]]\n"
313" Print the rules in a chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000314" --flush -F [chain] Delete all rules in chain or all chains\n"
315" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000316" --new -N chain Create a new user-defined chain\n"
317" --delete-chain\n"
318" -X [chain] Delete a user-defined chain\n"
319" --policy -P chain target\n"
320" Change policy on chain to target\n"
321" --rename-chain\n"
322" -E old-chain new-chain\n"
323" Change chain name, (moving any references)\n"
324
325"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200326"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
327"[!] --source -s address[/mask]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000328" source specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200329"[!] --destination -d address[/mask]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000330" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200331"[!] --in-interface -i input name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000332" network interface name ([+] for wildcard)\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200333" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000334" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000335#ifdef IPT_F_GOTO
336" --goto -g chain\n"
337" jump to chain with no return\n"
338#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000339" --match -m match\n"
340" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000341" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200342"[!] --out-interface -o output name[+]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000343" network interface name ([+] for wildcard)\n"
344" --table -t table table to manipulate (default: `filter')\n"
345" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000346" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000347" --exact -x expand numbers (display exact values)\n"
348"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000349" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000350" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000351"[!] --version -V print package version.\n");
352
Rusty Russell363112d2000-08-11 13:49:26 +0000353 /* Print out any special helps. A user might like to be able
354 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000355 results. So we call help for all specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000356 for (t = xtables_targets; t ;t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000357 if (t->used) {
358 printf("\n");
359 t->help();
360 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000361 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000362 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000363 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000364 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000365 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000366 exit(0);
367}
368
Patrick McHardy0b639362007-09-08 16:00:01 +0000369void
370exit_error(enum exittype status, const char *msg, ...)
371{
372 va_list args;
373
374 va_start(args, msg);
375 fprintf(stderr, "%s v%s: ", program_name, program_version);
376 vfprintf(stderr, msg, args);
377 va_end(args);
378 fprintf(stderr, "\n");
379 if (status == PARAMETER_PROBLEM)
380 exit_tryhelp(status);
381 if (status == VERSION_PROBLEM)
382 fprintf(stderr,
383 "Perhaps iptables or your kernel needs to be upgraded.\n");
384 /* On error paths, make sure that we don't leak memory */
385 free_opts(1);
386 exit(status);
387}
388
Marc Bouchere6869a82000-03-20 06:03:29 +0000389static void
390generic_opt_check(int command, int options)
391{
392 int i, j, legal = 0;
393
394 /* Check that commands are valid with options. Complicated by the
395 * fact that if an option is legal with *any* command given, it is
396 * legal overall (ie. -z and -l).
397 */
398 for (i = 0; i < NUMBER_OF_OPT; i++) {
399 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
400
401 for (j = 0; j < NUMBER_OF_CMD; j++) {
402 if (!(command & (1<<j)))
403 continue;
404
405 if (!(options & (1<<i))) {
406 if (commands_v_options[j][i] == '+')
407 exit_error(PARAMETER_PROBLEM,
408 "You need to supply the `-%c' "
409 "option for this command\n",
410 optflags[i]);
411 } else {
412 if (commands_v_options[j][i] != 'x')
413 legal = 1;
414 else if (legal == 0)
415 legal = -1;
416 }
417 }
418 if (legal == -1)
419 exit_error(PARAMETER_PROBLEM,
420 "Illegal option `-%c' with this command\n",
421 optflags[i]);
422 }
423}
424
425static char
426opt2char(int option)
427{
428 const char *ptr;
429 for (ptr = optflags; option > 1; option >>= 1, ptr++);
430
431 return *ptr;
432}
433
434static char
435cmd2char(int option)
436{
437 const char *ptr;
438 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
439
440 return *ptr;
441}
442
443static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000444add_command(unsigned int *cmd, const int newcmd, const int othercmds,
445 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000446{
447 if (invert)
448 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
449 if (*cmd & (~othercmds))
450 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
451 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
452 *cmd |= newcmd;
453}
454
455int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100456check_inverse(const char option[], int *invert, int *my_optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000457{
458 if (option && strcmp(option, "!") == 0) {
459 if (*invert)
460 exit_error(PARAMETER_PROBLEM,
461 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000462 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100463 if (my_optind != NULL) {
464 ++*my_optind;
465 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000466 exit_error(PARAMETER_PROBLEM,
467 "no argument following `!'");
468 }
469
Marc Bouchere6869a82000-03-20 06:03:29 +0000470 return TRUE;
471 }
472 return FALSE;
473}
474
Marc Bouchere6869a82000-03-20 06:03:29 +0000475/*
476 * All functions starting with "parse" should succeed, otherwise
477 * the program fails.
478 * Most routines return pointers to static data that may change
479 * between calls to the same or other routines with a few exceptions:
480 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
481 * return global static data.
482*/
483
Rusty Russell28381a42000-05-10 00:19:50 +0000484/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200485static struct xtables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000486find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000487{
Harald Welteed498492001-07-23 01:24:22 +0000488 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000489
Jan Engelhardte917bca2009-01-27 18:14:21 +0100490 if (string_to_number(pname, 0, UINT8_MAX, &proto) != -1) {
Harald Welte0b0013a2002-02-18 16:15:31 +0000491 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000492
Harald Welte0b0013a2002-02-18 16:15:31 +0000493 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000494 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000495 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000496 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000497
498 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000499}
500
Marc Boucherb93c7982001-12-06 14:50:19 +0000501u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000502parse_protocol(const char *s)
503{
Harald Welteed498492001-07-23 01:24:22 +0000504 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000505
Jan Engelhardte917bca2009-01-27 18:14:21 +0100506 if (string_to_number(s, 0, UINT8_MAX, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000507 struct protoent *pent;
508
Harald Weltecbe1ec72006-02-11 09:50:11 +0000509 /* first deal with the special case of 'all' to prevent
510 * people from being able to redefine 'all' in nsswitch
511 * and/or provoke expensive [not working] ldap/nis/...
512 * lookups */
513 if (!strcmp(s, "all"))
514 return 0;
515
Marc Bouchere6869a82000-03-20 06:03:29 +0000516 if ((pent = getprotobyname(s)))
517 proto = pent->p_proto;
518 else {
519 unsigned int i;
520 for (i = 0;
521 i < sizeof(chain_protos)/sizeof(struct pprot);
522 i++) {
523 if (strcmp(s, chain_protos[i].name) == 0) {
524 proto = chain_protos[i].num;
525 break;
526 }
527 }
528 if (i == sizeof(chain_protos)/sizeof(struct pprot))
529 exit_error(PARAMETER_PROBLEM,
530 "unknown protocol `%s' specified",
531 s);
532 }
533 }
534
535 return (u_int16_t)proto;
536}
537
Marc Bouchere6869a82000-03-20 06:03:29 +0000538/* Can't be zero. */
539static int
540parse_rulenumber(const char *rule)
541{
Harald Welteed498492001-07-23 01:24:22 +0000542 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000543
Harald Welteed498492001-07-23 01:24:22 +0000544 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000545 exit_error(PARAMETER_PROBLEM,
546 "Invalid rule number `%s'", rule);
547
548 return rulenum;
549}
550
551static const char *
552parse_target(const char *targetname)
553{
554 const char *ptr;
555
556 if (strlen(targetname) < 1)
557 exit_error(PARAMETER_PROBLEM,
558 "Invalid target name (too short)");
559
560 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
561 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000562 "Invalid target name `%s' (%u chars max)",
563 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000564
565 for (ptr = targetname; *ptr; ptr++)
566 if (isspace(*ptr))
567 exit_error(PARAMETER_PROBLEM,
568 "Invalid target name `%s'", targetname);
569 return targetname;
570}
571
Marc Bouchere6869a82000-03-20 06:03:29 +0000572static void
573set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
574 int invert)
575{
576 if (*options & option)
577 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
578 opt2char(option));
579 *options |= option;
580
581 if (invert) {
582 unsigned int i;
583 for (i = 0; 1 << i != option; i++);
584
585 if (!inverse_for_options[i])
586 exit_error(PARAMETER_PROBLEM,
587 "cannot have ! before -%c",
588 opt2char(option));
589 *invflg |= inverse_for_options[i];
590 }
591}
592
Marc Bouchere6869a82000-03-20 06:03:29 +0000593static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000594merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000595 unsigned int *option_offset)
596{
597 unsigned int num_old, num_new, i;
598 struct option *merge;
599
Jan Engelhardtd0145402007-07-30 14:32:26 +0000600 if (newopts == NULL)
601 return oldopts;
602
Marc Bouchere6869a82000-03-20 06:03:29 +0000603 for (num_old = 0; oldopts[num_old].name; num_old++);
604 for (num_new = 0; newopts[num_new].name; num_new++);
605
606 global_option_offset += OPTION_OFFSET;
607 *option_offset = global_option_offset;
608
609 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +0000610 if (merge == NULL)
611 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000612 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000613 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000614 for (i = 0; i < num_new; i++) {
615 merge[num_old + i] = newopts[i];
616 merge[num_old + i].val += *option_offset;
617 }
618 memset(merge + num_old + num_new, 0, sizeof(struct option));
619
620 return merge;
621}
622
Marc Bouchere6869a82000-03-20 06:03:29 +0000623static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000624print_num(u_int64_t number, unsigned int format)
625{
626 if (format & FMT_KILOMEGAGIGA) {
627 if (number > 99999) {
628 number = (number + 500) / 1000;
629 if (number > 9999) {
630 number = (number + 500) / 1000;
631 if (number > 9999) {
632 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000633 if (number > 9999) {
634 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000635 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000636 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000637 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000638 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000639 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000640 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000642 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000644 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000646}
647
648
649static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100650print_header(unsigned int format, const char *chain, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000651{
652 struct ipt_counters counters;
653 const char *pol = iptc_get_policy(chain, &counters, handle);
654 printf("Chain %s", chain);
655 if (pol) {
656 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000657 if (!(format & FMT_NOCOUNTS)) {
658 fputc(' ', stdout);
659 print_num(counters.pcnt, (format|FMT_NOTABLE));
660 fputs("packets, ", stdout);
661 print_num(counters.bcnt, (format|FMT_NOTABLE));
662 fputs("bytes", stdout);
663 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000664 printf(")\n");
665 } else {
666 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000667 if (!iptc_get_references(&refs, chain, handle))
668 printf(" (ERROR obtaining refs)\n");
669 else
670 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000671 }
672
673 if (format & FMT_LINENUMBERS)
674 printf(FMT("%-4s ", "%s "), "num");
675 if (!(format & FMT_NOCOUNTS)) {
676 if (format & FMT_KILOMEGAGIGA) {
677 printf(FMT("%5s ","%s "), "pkts");
678 printf(FMT("%5s ","%s "), "bytes");
679 } else {
680 printf(FMT("%8s ","%s "), "pkts");
681 printf(FMT("%10s ","%s "), "bytes");
682 }
683 }
684 if (!(format & FMT_NOTARGET))
685 printf(FMT("%-9s ","%s "), "target");
686 fputs(" prot ", stdout);
687 if (format & FMT_OPTIONS)
688 fputs("opt", stdout);
689 if (format & FMT_VIA) {
690 printf(FMT(" %-6s ","%s "), "in");
691 printf(FMT("%-6s ","%s "), "out");
692 }
693 printf(FMT(" %-19s ","%s "), "source");
694 printf(FMT(" %-19s "," %s "), "destination");
695 printf("\n");
696}
697
Marc Bouchere6869a82000-03-20 06:03:29 +0000698
699static int
700print_match(const struct ipt_entry_match *m,
701 const struct ipt_ip *ip,
702 int numeric)
703{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200704 struct xtables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000705
706 if (match) {
707 if (match->print)
708 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000709 else
Rusty Russellb039b022000-09-01 06:04:05 +0000710 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000711 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000712 if (m->u.user.name[0])
713 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000714 }
715 /* Don't stop iterating. */
716 return 0;
717}
718
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100719/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000720static void
721print_firewall(const struct ipt_entry *fw,
722 const char *targname,
723 unsigned int num,
724 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100725 struct iptc_handle *const handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000726{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200727 struct xtables_target *target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000728 const struct ipt_entry_target *t;
729 u_int8_t flags;
730 char buf[BUFSIZ];
731
Marc Bouchere6869a82000-03-20 06:03:29 +0000732 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +0000733 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000734 else
Rusty Russell52a51492000-05-02 16:44:29 +0000735 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000736
737 t = ipt_get_target((struct ipt_entry *)fw);
738 flags = fw->ip.flags;
739
740 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200741 printf(FMT("%-4u ", "%u "), num);
Marc Bouchere6869a82000-03-20 06:03:29 +0000742
743 if (!(format & FMT_NOCOUNTS)) {
744 print_num(fw->counters.pcnt, format);
745 print_num(fw->counters.bcnt, format);
746 }
747
748 if (!(format & FMT_NOTARGET))
749 printf(FMT("%-9s ", "%s "), targname);
750
751 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
752 {
Rusty Russell28381a42000-05-10 00:19:50 +0000753 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000754 if (pname)
755 printf(FMT("%-5s", "%s "), pname);
756 else
757 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
758 }
759
760 if (format & FMT_OPTIONS) {
761 if (format & FMT_NOTABLE)
762 fputs("opt ", stdout);
763 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
764 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
765 fputc(' ', stdout);
766 }
767
768 if (format & FMT_VIA) {
769 char iface[IFNAMSIZ+2];
770
771 if (fw->ip.invflags & IPT_INV_VIA_IN) {
772 iface[0] = '!';
773 iface[1] = '\0';
774 }
775 else iface[0] = '\0';
776
777 if (fw->ip.iniface[0] != '\0') {
778 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000779 }
780 else if (format & FMT_NUMERIC) strcat(iface, "*");
781 else strcat(iface, "any");
782 printf(FMT(" %-6s ","in %s "), iface);
783
784 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
785 iface[0] = '!';
786 iface[1] = '\0';
787 }
788 else iface[0] = '\0';
789
790 if (fw->ip.outiface[0] != '\0') {
791 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000792 }
793 else if (format & FMT_NUMERIC) strcat(iface, "*");
794 else strcat(iface, "any");
795 printf(FMT("%-6s ","out %s "), iface);
796 }
797
798 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
799 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
800 printf(FMT("%-19s ","%s "), "anywhere");
801 else {
802 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000803 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000804 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000805 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.src));
806 strcat(buf, ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000807 printf(FMT("%-19s ","%s "), buf);
808 }
809
810 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
811 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000812 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000813 else {
814 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000815 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000816 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000817 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.dst));
818 strcat(buf, ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000819 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 }
821
822 if (format & FMT_NOTABLE)
823 fputs(" ", stdout);
824
Harald Welte72bd87e2005-11-24 17:04:05 +0000825#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000826 if(fw->ip.flags & IPT_F_GOTO)
827 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000828#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000829
Marc Bouchere6869a82000-03-20 06:03:29 +0000830 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
831
832 if (target) {
833 if (target->print)
834 /* Print the target information. */
835 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000836 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000837 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000838 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000839
840 if (!(format & FMT_NONEWLINE))
841 fputc('\n', stdout);
842}
843
844static void
845print_firewall_line(const struct ipt_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100846 struct iptc_handle *const h)
Marc Bouchere6869a82000-03-20 06:03:29 +0000847{
848 struct ipt_entry_target *t;
849
850 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000851 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000852}
853
854static int
855append_entry(const ipt_chainlabel chain,
856 struct ipt_entry *fw,
857 unsigned int nsaddrs,
858 const struct in_addr saddrs[],
859 unsigned int ndaddrs,
860 const struct in_addr daddrs[],
861 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100862 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000863{
864 unsigned int i, j;
865 int ret = 1;
866
867 for (i = 0; i < nsaddrs; i++) {
868 fw->ip.src.s_addr = saddrs[i].s_addr;
869 for (j = 0; j < ndaddrs; j++) {
870 fw->ip.dst.s_addr = daddrs[j].s_addr;
871 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100872 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000873 ret &= iptc_append_entry(chain, fw, handle);
874 }
875 }
876
877 return ret;
878}
879
880static int
881replace_entry(const ipt_chainlabel chain,
882 struct ipt_entry *fw,
883 unsigned int rulenum,
884 const struct in_addr *saddr,
885 const struct in_addr *daddr,
886 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100887 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000888{
889 fw->ip.src.s_addr = saddr->s_addr;
890 fw->ip.dst.s_addr = daddr->s_addr;
891
892 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100893 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000894 return iptc_replace_entry(chain, fw, rulenum, handle);
895}
896
897static int
898insert_entry(const ipt_chainlabel chain,
899 struct ipt_entry *fw,
900 unsigned int rulenum,
901 unsigned int nsaddrs,
902 const struct in_addr saddrs[],
903 unsigned int ndaddrs,
904 const struct in_addr daddrs[],
905 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100906 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000907{
908 unsigned int i, j;
909 int ret = 1;
910
911 for (i = 0; i < nsaddrs; i++) {
912 fw->ip.src.s_addr = saddrs[i].s_addr;
913 for (j = 0; j < ndaddrs; j++) {
914 fw->ip.dst.s_addr = daddrs[j].s_addr;
915 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100916 print_firewall_line(fw, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000917 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
918 }
919 }
920
921 return ret;
922}
923
Rusty Russell2e0a3212000-04-19 11:23:18 +0000924static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000925make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000926{
927 /* Establish mask for comparison */
928 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000929 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000930 unsigned char *mask, *mptr;
931
932 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000933 for (matchp = matches; matchp; matchp = matchp->next)
934 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000935
Jan Engelhardt630ef482009-01-27 14:58:41 +0100936 mask = xtables_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000937 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000938 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000939
Rusty Russell9e1d2142000-04-23 09:11:12 +0000940 memset(mask, 0xFF, sizeof(struct ipt_entry));
941 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000942
Martin Josefsson78cafda2004-02-02 20:01:18 +0000943 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000944 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000945 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000946 + matchp->match->userspacesize);
947 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000948 }
949
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000950 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000951 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000952 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000953
954 return mask;
955}
956
Marc Bouchere6869a82000-03-20 06:03:29 +0000957static int
958delete_entry(const ipt_chainlabel chain,
959 struct ipt_entry *fw,
960 unsigned int nsaddrs,
961 const struct in_addr saddrs[],
962 unsigned int ndaddrs,
963 const struct in_addr daddrs[],
964 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100965 struct iptc_handle *handle,
Martin Josefsson78cafda2004-02-02 20:01:18 +0000966 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000967{
968 unsigned int i, j;
969 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000970 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000971
Martin Josefsson78cafda2004-02-02 20:01:18 +0000972 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000973 for (i = 0; i < nsaddrs; i++) {
974 fw->ip.src.s_addr = saddrs[i].s_addr;
975 for (j = 0; j < ndaddrs; j++) {
976 fw->ip.dst.s_addr = daddrs[j].s_addr;
977 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100978 print_firewall_line(fw, handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000979 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000980 }
981 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000982 free(mask);
983
Marc Bouchere6869a82000-03-20 06:03:29 +0000984 return ret;
985}
986
Harald Welteae1ff9f2000-12-01 14:26:20 +0000987int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100988for_each_chain(int (*fn)(const ipt_chainlabel, int, struct iptc_handle *),
989 int verbose, int builtinstoo, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000990{
991 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000992 const char *chain;
993 char *chains;
994 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000995
Rusty Russell9e1d2142000-04-23 09:11:12 +0000996 chain = iptc_first_chain(handle);
997 while (chain) {
998 chaincount++;
999 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001000 }
1001
Jan Engelhardt630ef482009-01-27 14:58:41 +01001002 chains = xtables_malloc(sizeof(ipt_chainlabel) * chaincount);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001003 i = 0;
1004 chain = iptc_first_chain(handle);
1005 while (chain) {
1006 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1007 i++;
1008 chain = iptc_next_chain(handle);
1009 }
1010
1011 for (i = 0; i < chaincount; i++) {
1012 if (!builtinstoo
1013 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001014 handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001015 continue;
1016 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1017 }
1018
1019 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001020 return ret;
1021}
1022
Harald Welteae1ff9f2000-12-01 14:26:20 +00001023int
Marc Bouchere6869a82000-03-20 06:03:29 +00001024flush_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001025 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001026{
1027 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001028 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001029
1030 if (verbose)
1031 fprintf(stdout, "Flushing chain `%s'\n", chain);
1032 return iptc_flush_entries(chain, handle);
1033}
Marc Bouchere6869a82000-03-20 06:03:29 +00001034
1035static int
1036zero_entries(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001037 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001038{
1039 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001040 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001041
Marc Bouchere6869a82000-03-20 06:03:29 +00001042 if (verbose)
1043 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1044 return iptc_zero_entries(chain, handle);
1045}
1046
Harald Welteae1ff9f2000-12-01 14:26:20 +00001047int
Marc Bouchere6869a82000-03-20 06:03:29 +00001048delete_chain(const ipt_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001049 struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001050{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001051 if (!chain)
1052 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001053
1054 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001055 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001056 return iptc_delete_chain(chain, handle);
1057}
1058
1059static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001060list_entries(const ipt_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001061 int expanded, int linenumbers, struct iptc_handle *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001062{
1063 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001064 unsigned int format;
1065 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001066
1067 format = FMT_OPTIONS;
1068 if (!verbose)
1069 format |= FMT_NOCOUNTS;
1070 else
1071 format |= FMT_VIA;
1072
1073 if (numeric)
1074 format |= FMT_NUMERIC;
1075
1076 if (!expanded)
1077 format |= FMT_KILOMEGAGIGA;
1078
1079 if (linenumbers)
1080 format |= FMT_LINENUMBERS;
1081
Rusty Russell9e1d2142000-04-23 09:11:12 +00001082 for (this = iptc_first_chain(handle);
1083 this;
1084 this = iptc_next_chain(handle)) {
1085 const struct ipt_entry *i;
1086 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001087
Marc Bouchere6869a82000-03-20 06:03:29 +00001088 if (chain && strcmp(chain, this) != 0)
1089 continue;
1090
1091 if (found) printf("\n");
1092
Henrik Nordstrombb340822008-05-13 13:09:23 +02001093 if (!rulenum)
1094 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001095 i = iptc_first_rule(this, handle);
1096
1097 num = 0;
1098 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001099 num++;
1100 if (!rulenum || num == rulenum)
1101 print_firewall(i,
1102 iptc_get_target(i, handle),
1103 num,
1104 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001105 handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001106 i = iptc_next_rule(i, handle);
1107 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001108 found = 1;
1109 }
1110
1111 errno = ENOENT;
1112 return found;
1113}
1114
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001115static void print_proto(u_int16_t proto, int invert)
1116{
1117 if (proto) {
1118 unsigned int i;
1119 const char *invertstr = invert ? "! " : "";
1120
1121 struct protoent *pent = getprotobynumber(proto);
1122 if (pent) {
1123 printf("-p %s%s ", invertstr, pent->p_name);
1124 return;
1125 }
1126
1127 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
1128 if (chain_protos[i].num == proto) {
1129 printf("-p %s%s ",
1130 invertstr, chain_protos[i].name);
1131 return;
1132 }
1133
1134 printf("-p %s%u ", invertstr, proto);
1135 }
1136}
1137
1138#define IP_PARTS_NATIVE(n) \
1139(unsigned int)((n)>>24)&0xFF, \
1140(unsigned int)((n)>>16)&0xFF, \
1141(unsigned int)((n)>>8)&0xFF, \
1142(unsigned int)((n)&0xFF)
1143
1144#define IP_PARTS(n) IP_PARTS_NATIVE(ntohl(n))
1145
1146/* This assumes that mask is contiguous, and byte-bounded. */
1147static void
1148print_iface(char letter, const char *iface, const unsigned char *mask,
1149 int invert)
1150{
1151 unsigned int i;
1152
1153 if (mask[0] == 0)
1154 return;
1155
1156 printf("-%c %s", letter, invert ? "! " : "");
1157
1158 for (i = 0; i < IFNAMSIZ; i++) {
1159 if (mask[i] != 0) {
1160 if (iface[i] != '\0')
1161 printf("%c", iface[i]);
1162 } else {
1163 /* we can access iface[i-1] here, because
1164 * a few lines above we make sure that mask[0] != 0 */
1165 if (iface[i-1] != '\0')
1166 printf("+");
1167 break;
1168 }
1169 }
1170
1171 printf(" ");
1172}
1173
1174static int print_match_save(const struct ipt_entry_match *e,
1175 const struct ipt_ip *ip)
1176{
1177 struct xtables_match *match
1178 = find_match(e->u.user.name, TRY_LOAD, NULL);
1179
1180 if (match) {
1181 printf("-m %s ", e->u.user.name);
1182
1183 /* some matches don't provide a save function */
1184 if (match->save)
1185 match->save(ip, e);
1186 } else {
1187 if (e->u.match_size) {
1188 fprintf(stderr,
1189 "Can't find library for match `%s'\n",
1190 e->u.user.name);
1191 exit(1);
1192 }
1193 }
1194 return 0;
1195}
1196
1197/* print a given ip including mask if neccessary */
1198static void print_ip(char *prefix, u_int32_t ip, u_int32_t mask, int invert)
1199{
1200 u_int32_t bits, hmask = ntohl(mask);
1201 int i;
1202
1203 if (!mask && !ip && !invert)
1204 return;
1205
1206 printf("%s %s%u.%u.%u.%u",
1207 prefix,
1208 invert ? "! " : "",
1209 IP_PARTS(ip));
1210
1211 if (mask == 0xFFFFFFFFU) {
1212 printf("/32 ");
1213 return;
1214 }
1215
1216 i = 32;
1217 bits = 0xFFFFFFFEU;
1218 while (--i >= 0 && hmask != bits)
1219 bits <<= 1;
1220 if (i >= 0)
1221 printf("/%u ", i);
1222 else
1223 printf("/%u.%u.%u.%u ", IP_PARTS(mask));
1224}
1225
1226/* We want this to be readable, so only print out neccessary fields.
1227 * Because that's the kind of world I want to live in. */
1228void print_rule(const struct ipt_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001229 struct iptc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001230{
1231 struct ipt_entry_target *t;
1232 const char *target_name;
1233
1234 /* print counters for iptables-save */
1235 if (counters > 0)
1236 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1237
1238 /* print chain name */
1239 printf("-A %s ", chain);
1240
1241 /* Print IP part. */
1242 print_ip("-s", e->ip.src.s_addr,e->ip.smsk.s_addr,
1243 e->ip.invflags & IPT_INV_SRCIP);
1244
1245 print_ip("-d", e->ip.dst.s_addr, e->ip.dmsk.s_addr,
1246 e->ip.invflags & IPT_INV_DSTIP);
1247
1248 print_iface('i', e->ip.iniface, e->ip.iniface_mask,
1249 e->ip.invflags & IPT_INV_VIA_IN);
1250
1251 print_iface('o', e->ip.outiface, e->ip.outiface_mask,
1252 e->ip.invflags & IPT_INV_VIA_OUT);
1253
1254 print_proto(e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
1255
1256 if (e->ip.flags & IPT_F_FRAG)
1257 printf("%s-f ",
1258 e->ip.invflags & IPT_INV_FRAG ? "! " : "");
1259
1260 /* Print matchinfo part */
1261 if (e->target_offset) {
1262 IPT_MATCH_ITERATE(e, print_match_save, &e->ip);
1263 }
1264
1265 /* print counters for iptables -R */
1266 if (counters < 0)
1267 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1268
1269 /* Print target name */
1270 target_name = iptc_get_target(e, h);
1271 if (target_name && (*target_name != '\0'))
1272#ifdef IPT_F_GOTO
1273 printf("-%c %s ", e->ip.flags & IPT_F_GOTO ? 'g' : 'j', target_name);
1274#else
1275 printf("-j %s ", target_name);
1276#endif
1277
1278 /* Print targinfo part */
1279 t = ipt_get_target((struct ipt_entry *)e);
1280 if (t->u.user.name[0]) {
1281 struct xtables_target *target
1282 = find_target(t->u.user.name, TRY_LOAD);
1283
1284 if (!target) {
1285 fprintf(stderr, "Can't find library for target `%s'\n",
1286 t->u.user.name);
1287 exit(1);
1288 }
1289
1290 if (target->save)
1291 target->save(&e->ip, t);
1292 else {
1293 /* If the target size is greater than ipt_entry_target
1294 * there is something to be saved, we just don't know
1295 * how to print it */
1296 if (t->u.target_size !=
1297 sizeof(struct ipt_entry_target)) {
1298 fprintf(stderr, "Target `%s' is missing "
1299 "save function\n",
1300 t->u.user.name);
1301 exit(1);
1302 }
1303 }
1304 }
1305 printf("\n");
1306}
1307
1308static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001309list_rules(const ipt_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001310 struct iptc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001311{
1312 const char *this = NULL;
1313 int found = 0;
1314
1315 if (counters)
1316 counters = -1; /* iptables -c format */
1317
1318 /* Dump out chain names first,
1319 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001320 if (!rulenum) for (this = iptc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001321 this;
1322 this = iptc_next_chain(handle)) {
1323 if (chain && strcmp(this, chain) != 0)
1324 continue;
1325
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001326 if (iptc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001327 struct ipt_counters count;
1328 printf("-P %s %s", this, iptc_get_policy(this, &count, handle));
1329 if (counters)
1330 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1331 printf("\n");
1332 } else {
1333 printf("-N %s\n", this);
1334 }
1335 }
1336
1337 for (this = iptc_first_chain(handle);
1338 this;
1339 this = iptc_next_chain(handle)) {
1340 const struct ipt_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001341 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001342
1343 if (chain && strcmp(this, chain) != 0)
1344 continue;
1345
1346 /* Dump out rules */
1347 e = iptc_first_rule(this, handle);
1348 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001349 num++;
1350 if (!rulenum || num == rulenum)
1351 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001352 e = iptc_next_rule(e, handle);
1353 }
1354 found = 1;
1355 }
1356
1357 errno = ENOENT;
1358 return found;
1359}
1360
Marc Bouchere6869a82000-03-20 06:03:29 +00001361static struct ipt_entry *
1362generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001363 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001364 struct ipt_entry_target *target)
1365{
1366 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001367 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001368 struct ipt_entry *e;
1369
1370 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001371 for (matchp = matches; matchp; matchp = matchp->next)
1372 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001373
Jan Engelhardt630ef482009-01-27 14:58:41 +01001374 e = xtables_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001375 *e = *fw;
1376 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001377 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001378
1379 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001380 for (matchp = matches; matchp; matchp = matchp->next) {
1381 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1382 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001383 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001384 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001385
1386 return e;
1387}
1388
Jan Engelhardt33690a12008-02-11 00:54:00 +01001389static void clear_rule_matches(struct iptables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001390{
1391 struct iptables_rule_match *matchp, *tmp;
1392
1393 for (matchp = *matches; matchp;) {
1394 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001395 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001396 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001397 matchp->match->m = NULL;
1398 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001399 if (matchp->match == matchp->match->next) {
1400 free(matchp->match);
1401 matchp->match = NULL;
1402 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001403 free(matchp);
1404 matchp = tmp;
1405 }
1406
1407 *matches = NULL;
1408}
1409
Rusty Russell3aef54d2005-01-03 03:48:40 +00001410static void set_revision(char *name, u_int8_t revision)
1411{
1412 /* Old kernel sources don't have ".revision" field,
1413 but we stole a byte from name. */
1414 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1415 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1416}
1417
Phil Oester8cf65912005-09-19 15:00:33 +00001418void
1419get_kernel_version(void) {
1420 static struct utsname uts;
1421 int x = 0, y = 0, z = 0;
1422
1423 if (uname(&uts) == -1) {
1424 fprintf(stderr, "Unable to retrieve kernel version.\n");
1425 free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001426 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001427 }
1428
1429 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1430 kernel_version = LINUX_VERSION(x, y, z);
1431}
1432
Jan Engelhardtfd187312008-11-10 16:59:27 +01001433int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)
Marc Bouchere6869a82000-03-20 06:03:29 +00001434{
1435 struct ipt_entry fw, *e = NULL;
1436 int invert = 0;
1437 unsigned int nsaddrs = 0, ndaddrs = 0;
1438 struct in_addr *saddrs = NULL, *daddrs = NULL;
1439
1440 int c, verbose = 0;
1441 const char *chain = NULL;
1442 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1443 const char *policy = NULL, *newname = NULL;
1444 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001445 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001446 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001447 struct xtables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001448 struct iptables_rule_match *matches = NULL;
1449 struct iptables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001450 struct xtables_target *target = NULL;
1451 struct xtables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001452 const char *jumpto = "";
1453 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001454 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001455 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001456
1457 memset(&fw, 0, sizeof(fw));
1458
Harald Welteae1ff9f2000-12-01 14:26:20 +00001459 /* re-set optind to 0 in case do_command gets called
1460 * a second time */
1461 optind = 0;
1462
1463 /* clear mflags in case do_command gets called a second time
1464 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001465 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001466 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001467
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001468 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001469 t->tflags = 0;
1470 t->used = 0;
1471 }
1472
Marc Bouchere6869a82000-03-20 06:03:29 +00001473 /* Suppress error messages: we may add new options if we
1474 demand-load a protocol. */
1475 opterr = 0;
1476
1477 while ((c = getopt_long(argc, argv,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001478 "-A:D:R:I:L::S::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 +00001479 opts, NULL)) != -1) {
1480 switch (c) {
1481 /*
1482 * Command selection
1483 */
1484 case 'A':
1485 add_command(&command, CMD_APPEND, CMD_NONE,
1486 invert);
1487 chain = optarg;
1488 break;
1489
1490 case 'D':
1491 add_command(&command, CMD_DELETE, CMD_NONE,
1492 invert);
1493 chain = optarg;
1494 if (optind < argc && argv[optind][0] != '-'
1495 && argv[optind][0] != '!') {
1496 rulenum = parse_rulenumber(argv[optind++]);
1497 command = CMD_DELETE_NUM;
1498 }
1499 break;
1500
Marc Bouchere6869a82000-03-20 06:03:29 +00001501 case 'R':
1502 add_command(&command, CMD_REPLACE, CMD_NONE,
1503 invert);
1504 chain = optarg;
1505 if (optind < argc && argv[optind][0] != '-'
1506 && argv[optind][0] != '!')
1507 rulenum = parse_rulenumber(argv[optind++]);
1508 else
1509 exit_error(PARAMETER_PROBLEM,
1510 "-%c requires a rule number",
1511 cmd2char(CMD_REPLACE));
1512 break;
1513
1514 case 'I':
1515 add_command(&command, CMD_INSERT, CMD_NONE,
1516 invert);
1517 chain = optarg;
1518 if (optind < argc && argv[optind][0] != '-'
1519 && argv[optind][0] != '!')
1520 rulenum = parse_rulenumber(argv[optind++]);
1521 else rulenum = 1;
1522 break;
1523
1524 case 'L':
1525 add_command(&command, CMD_LIST, CMD_ZERO,
1526 invert);
1527 if (optarg) chain = optarg;
1528 else if (optind < argc && argv[optind][0] != '-'
1529 && argv[optind][0] != '!')
1530 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001531 if (optind < argc && argv[optind][0] != '-'
1532 && argv[optind][0] != '!')
1533 rulenum = parse_rulenumber(argv[optind++]);
Marc Bouchere6869a82000-03-20 06:03:29 +00001534 break;
1535
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001536 case 'S':
1537 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1538 invert);
1539 if (optarg) chain = optarg;
1540 else if (optind < argc && argv[optind][0] != '-'
1541 && argv[optind][0] != '!')
1542 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001543 if (optind < argc && argv[optind][0] != '-'
1544 && argv[optind][0] != '!')
1545 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001546 break;
1547
Marc Bouchere6869a82000-03-20 06:03:29 +00001548 case 'F':
1549 add_command(&command, CMD_FLUSH, CMD_NONE,
1550 invert);
1551 if (optarg) chain = optarg;
1552 else if (optind < argc && argv[optind][0] != '-'
1553 && argv[optind][0] != '!')
1554 chain = argv[optind++];
1555 break;
1556
1557 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001558 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Marc Bouchere6869a82000-03-20 06:03:29 +00001559 invert);
1560 if (optarg) chain = optarg;
1561 else if (optind < argc && argv[optind][0] != '-'
1562 && argv[optind][0] != '!')
1563 chain = argv[optind++];
1564 break;
1565
1566 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001567 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001568 exit_error(PARAMETER_PROBLEM,
1569 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001570 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001571 if (find_target(optarg, TRY_LOAD))
1572 exit_error(PARAMETER_PROBLEM,
1573 "chain name may not clash "
1574 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001575 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1576 invert);
1577 chain = optarg;
1578 break;
1579
1580 case 'X':
1581 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1582 invert);
1583 if (optarg) chain = optarg;
1584 else if (optind < argc && argv[optind][0] != '-'
1585 && argv[optind][0] != '!')
1586 chain = argv[optind++];
1587 break;
1588
1589 case 'E':
1590 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1591 invert);
1592 chain = optarg;
1593 if (optind < argc && argv[optind][0] != '-'
1594 && argv[optind][0] != '!')
1595 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001596 else
1597 exit_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001598 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001599 "new-chain-name",
1600 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001601 break;
1602
1603 case 'P':
1604 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1605 invert);
1606 chain = optarg;
1607 if (optind < argc && argv[optind][0] != '-'
1608 && argv[optind][0] != '!')
1609 policy = argv[optind++];
1610 else
1611 exit_error(PARAMETER_PROBLEM,
1612 "-%c requires a chain and a policy",
1613 cmd2char(CMD_SET_POLICY));
1614 break;
1615
1616 case 'h':
1617 if (!optarg)
1618 optarg = argv[optind];
1619
Rusty Russell2e0a3212000-04-19 11:23:18 +00001620 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001621 if (!matches && protocol)
1622 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001623
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001624 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001625
1626 /*
1627 * Option selection
1628 */
1629 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001630 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001631 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1632 invert);
1633
1634 /* Canonicalize into lower case */
1635 for (protocol = argv[optind-1]; *protocol; protocol++)
1636 *protocol = tolower(*protocol);
1637
1638 protocol = argv[optind-1];
1639 fw.ip.proto = parse_protocol(protocol);
1640
1641 if (fw.ip.proto == 0
1642 && (fw.ip.invflags & IPT_INV_PROTO))
1643 exit_error(PARAMETER_PROBLEM,
1644 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001645 break;
1646
1647 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001648 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001649 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1650 invert);
1651 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 break;
1653
1654 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001655 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001656 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1657 invert);
1658 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001659 break;
1660
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001661#ifdef IPT_F_GOTO
1662 case 'g':
1663 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1664 invert);
1665 fw.ip.flags |= IPT_F_GOTO;
1666 jumpto = parse_target(optarg);
1667 break;
1668#endif
1669
Marc Bouchere6869a82000-03-20 06:03:29 +00001670 case 'j':
1671 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1672 invert);
1673 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001674 /* TRY_LOAD (may be chain name) */
1675 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001676
1677 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001678 size_t size;
1679
Rusty Russell73f72f52000-07-03 10:17:57 +00001680 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1681 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001682
Jan Engelhardt630ef482009-01-27 14:58:41 +01001683 target->t = xtables_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001684 target->t->u.target_size = size;
1685 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001686 set_revision(target->t->u.user.name,
1687 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001688 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001689 target->init(target->t);
Max Kellermann5b76f682008-01-29 13:42:48 +00001690 opts = merge_options(opts,
1691 target->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001692 &target->option_offset);
1693 if (opts == NULL)
1694 exit_error(OTHER_PROBLEM,
1695 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001696 }
1697 break;
1698
1699
1700 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001701 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001702 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1703 invert);
1704 parse_interface(argv[optind-1],
1705 fw.ip.iniface,
1706 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001707 break;
1708
1709 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001710 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001711 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1712 invert);
1713 parse_interface(argv[optind-1],
1714 fw.ip.outiface,
1715 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001716 break;
1717
1718 case 'f':
1719 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1720 invert);
1721 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001722 break;
1723
1724 case 'v':
1725 if (!verbose)
1726 set_option(&options, OPT_VERBOSE,
1727 &fw.ip.invflags, invert);
1728 verbose++;
1729 break;
1730
Rusty Russell52a51492000-05-02 16:44:29 +00001731 case 'm': {
1732 size_t size;
1733
Marc Bouchere6869a82000-03-20 06:03:29 +00001734 if (invert)
1735 exit_error(PARAMETER_PROBLEM,
1736 "unexpected ! flag before --match");
1737
Martin Josefsson78cafda2004-02-02 20:01:18 +00001738 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001739 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1740 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001741 m->m = xtables_calloc(1, size);
Rusty Russell52a51492000-05-02 16:44:29 +00001742 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001743 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001744 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001745 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001746 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001747 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001748 /* Merge options for non-cloned matches */
Max Kellermann5b76f682008-01-29 13:42:48 +00001749 opts = merge_options(opts,
1750 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001751 &m->option_offset);
1752 if (opts == NULL)
1753 exit_error(OTHER_PROBLEM,
1754 "can't alloc memory!");
1755 }
Rusty Russell52a51492000-05-02 16:44:29 +00001756 }
1757 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001758
1759 case 'n':
1760 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1761 invert);
1762 break;
1763
1764 case 't':
1765 if (invert)
1766 exit_error(PARAMETER_PROBLEM,
1767 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001768 *table = optarg;
Marc Bouchere6869a82000-03-20 06:03:29 +00001769 break;
1770
1771 case 'x':
1772 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1773 invert);
1774 break;
1775
1776 case 'V':
1777 if (invert)
1778 printf("Not %s ;-)\n", program_version);
1779 else
1780 printf("%s v%s\n",
1781 program_name, program_version);
1782 exit(0);
1783
1784 case '0':
1785 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1786 invert);
1787 break;
1788
Harald Welte82dd2ec2000-12-19 05:18:15 +00001789 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001790 xtables_modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001791 break;
1792
Harald Welteccd49e52001-01-23 22:54:34 +00001793 case 'c':
1794
1795 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1796 invert);
1797 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001798 bcnt = strchr(pcnt + 1, ',');
1799 if (bcnt)
1800 bcnt++;
1801 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welteccd49e52001-01-23 22:54:34 +00001802 && argv[optind][0] != '!')
1803 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001804 if (!bcnt)
Harald Welteccd49e52001-01-23 22:54:34 +00001805 exit_error(PARAMETER_PROBLEM,
1806 "-%c requires packet and byte counter",
1807 opt2char(OPT_COUNTERS));
1808
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001809 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001810 exit_error(PARAMETER_PROBLEM,
1811 "-%c packet counter not numeric",
1812 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001813 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001814
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001815 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001816 exit_error(PARAMETER_PROBLEM,
1817 "-%c byte counter not numeric",
1818 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001819 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001820 break;
1821
1822
Marc Bouchere6869a82000-03-20 06:03:29 +00001823 case 1: /* non option */
1824 if (optarg[0] == '!' && optarg[1] == '\0') {
1825 if (invert)
1826 exit_error(PARAMETER_PROBLEM,
1827 "multiple consecutive ! not"
1828 " allowed");
1829 invert = TRUE;
1830 optarg[0] = '\0';
1831 continue;
1832 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001833 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001834 exit_tryhelp(2);
1835
1836 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001837 if (!target
1838 || !(target->parse(c - target->option_offset,
1839 argv, invert,
1840 &target->tflags,
1841 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001842 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001843 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001844 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001845 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001846 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001847 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001848 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001849 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001850 break;
1851 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001852 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001853
1854 /* If you listen carefully, you can
1855 actually hear this code suck. */
1856
1857 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001858 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001859 * parameter, that has not been parsed yet,
1860 * it's not an option of an explicitly loaded
1861 * match or a target. However, we support
1862 * implicit loading of the protocol match
1863 * extension. '-p tcp' means 'l4 proto 6' and
1864 * at the same time 'load tcp protocol match on
1865 * demand if we specify --dport'.
1866 *
1867 * To make this work, we need to make sure:
1868 * - the parameter has not been parsed by
1869 * a match (m above)
1870 * - a protocol has been specified
1871 * - the protocol extension has not been
1872 * loaded yet, or is loaded and unused
1873 * [think of iptables-restore!]
1874 * - the protocol extension can be successively
1875 * loaded
1876 */
1877 if (m == NULL
1878 && protocol
1879 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001880 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001881 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001882 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001883 && (proto_used == 0))
1884 )
1885 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001886 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001887 /* Try loading protocol */
1888 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001889
Harald Welte2d86b772002-08-26 12:21:44 +00001890 proto_used = 1;
1891
1892 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1893 + m->size;
1894
Jan Engelhardt630ef482009-01-27 14:58:41 +01001895 m->m = xtables_calloc(1, size);
Harald Welte2d86b772002-08-26 12:21:44 +00001896 m->m->u.match_size = size;
1897 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001898 set_revision(m->m->u.user.name,
1899 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001900 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001901 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001902
1903 opts = merge_options(opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001904 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001905 &m->option_offset);
1906 if (opts == NULL)
1907 exit_error(OTHER_PROBLEM,
1908 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001909
1910 optind--;
1911 continue;
1912 }
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001913 if (!m) {
1914 if (c == '?') {
1915 if (optopt) {
1916 exit_error(
1917 PARAMETER_PROBLEM,
1918 "option `%s' "
1919 "requires an "
1920 "argument",
1921 argv[optind-1]);
1922 } else {
1923 exit_error(
1924 PARAMETER_PROBLEM,
1925 "unknown option "
1926 "`%s'",
1927 argv[optind-1]);
1928 }
1929 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001930 exit_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001931 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001932 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001933 }
1934 }
1935 invert = FALSE;
1936 }
1937
Jan Engelhardt1eada722008-08-13 14:41:32 +02001938 if (strcmp(*table, "nat") == 0 &&
1939 ((policy != NULL && strcmp(policy, "DROP") == 0) ||
1940 (jumpto != NULL && strcmp(jumpto, "DROP") == 0)))
1941 fprintf(stderr, "\nThe \"nat\" table is not intended for "
1942 "filtering, hence the use of DROP is deprecated and "
1943 "will permanently be disabled in the next iptables "
1944 "release. Please adjust your scripts.\n\n");
1945
Martin Josefsson78cafda2004-02-02 20:01:18 +00001946 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001947 if (matchp->match->final_check != NULL)
1948 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001949
Jan Engelhardt830132a2007-10-04 16:24:50 +00001950 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001951 target->final_check(target->tflags);
1952
1953 /* Fix me: must put inverse options checking here --MN */
1954
1955 if (optind < argc)
1956 exit_error(PARAMETER_PROBLEM,
1957 "unknown arguments found on commandline");
1958 if (!command)
1959 exit_error(PARAMETER_PROBLEM, "no command specified");
1960 if (invert)
1961 exit_error(PARAMETER_PROBLEM,
1962 "nothing appropriate following !");
1963
Harald Welte6336bfd2002-05-07 14:41:43 +00001964 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001965 if (!(options & OPT_DESTINATION))
1966 dhostnetworkmask = "0.0.0.0/0";
1967 if (!(options & OPT_SOURCE))
1968 shostnetworkmask = "0.0.0.0/0";
1969 }
1970
1971 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001972 ipparse_hostnetworkmask(shostnetworkmask, &saddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001973 &fw.ip.smsk, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001974
1975 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001976 ipparse_hostnetworkmask(dhostnetworkmask, &daddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001977 &fw.ip.dmsk, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001978
1979 if ((nsaddrs > 1 || ndaddrs > 1) &&
1980 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1981 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1982 " source or destination IP addresses");
1983
Marc Bouchere6869a82000-03-20 06:03:29 +00001984 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1985 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1986 "specify a unique address");
1987
1988 generic_opt_check(command, options);
1989
1990 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1991 exit_error(PARAMETER_PROBLEM,
1992 "chain name `%s' too long (must be under %i chars)",
1993 chain, IPT_FUNCTION_MAXNAMELEN);
1994
Harald Welteae1ff9f2000-12-01 14:26:20 +00001995 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001996 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001997 *handle = iptc_init(*table);
1998
Rusty Russell8beb0492004-12-22 00:37:10 +00001999 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01002000 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00002001 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00002002
Marc Bouchere6869a82000-03-20 06:03:29 +00002003 if (!*handle)
2004 exit_error(VERSION_PROBLEM,
2005 "can't initialize iptables table `%s': %s",
2006 *table, iptc_strerror(errno));
2007
Harald Welte6336bfd2002-05-07 14:41:43 +00002008 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00002009 || command == CMD_DELETE
2010 || command == CMD_INSERT
2011 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00002012 if (strcmp(chain, "PREROUTING") == 0
2013 || strcmp(chain, "INPUT") == 0) {
2014 /* -o not valid with incoming packets. */
2015 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00002016 exit_error(PARAMETER_PROBLEM,
2017 "Can't use -%c with %s\n",
2018 opt2char(OPT_VIANAMEOUT),
2019 chain);
2020 }
2021
Rusty Russella4860fd2000-06-17 16:13:02 +00002022 if (strcmp(chain, "POSTROUTING") == 0
2023 || strcmp(chain, "OUTPUT") == 0) {
2024 /* -i not valid with outgoing packets */
2025 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00002026 exit_error(PARAMETER_PROBLEM,
2027 "Can't use -%c with %s\n",
2028 opt2char(OPT_VIANAMEIN),
2029 chain);
2030 }
2031
2032 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00002033 fprintf(stderr,
2034 "Warning: using chain %s, not extension\n",
2035 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00002036
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002037 if (target->t)
2038 free(target->t);
2039
Marc Bouchere6869a82000-03-20 06:03:29 +00002040 target = NULL;
2041 }
2042
2043 /* If they didn't specify a target, or it's a chain
2044 name, use standard. */
2045 if (!target
2046 && (strlen(jumpto) == 0
2047 || iptc_is_chain(jumpto, *handle))) {
2048 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00002049
Rusty Russell52a51492000-05-02 16:44:29 +00002050 target = find_target(IPT_STANDARD_TARGET,
2051 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002052
2053 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00002054 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01002055 target->t = xtables_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00002056 target->t->u.target_size = size;
2057 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00002058 if (!iptc_is_chain(jumpto, *handle))
2059 set_revision(target->t->u.user.name,
2060 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00002061 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00002062 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002063 }
2064
Rusty Russell7e53bf92000-03-20 07:03:28 +00002065 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00002066 /* it is no chain, and we can't load a plugin.
2067 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00002068 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00002069 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00002070#ifdef IPT_F_GOTO
2071 if (fw.ip.flags & IPT_F_GOTO)
2072 exit_error(PARAMETER_PROBLEM,
2073 "goto '%s' is not a chain\n", jumpto);
2074#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00002075 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00002076 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00002077 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002078 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00002079 }
2080 }
2081
2082 switch (command) {
2083 case CMD_APPEND:
2084 ret = append_entry(chain, e,
2085 nsaddrs, saddrs, ndaddrs, daddrs,
2086 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002087 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002088 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002089 case CMD_DELETE:
2090 ret = delete_entry(chain, e,
2091 nsaddrs, saddrs, ndaddrs, daddrs,
2092 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002093 *handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00002094 break;
2095 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002096 ret = iptc_delete_num_entry(chain, rulenum - 1, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002097 break;
2098 case CMD_REPLACE:
2099 ret = replace_entry(chain, e, rulenum - 1,
2100 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002101 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002102 break;
2103 case CMD_INSERT:
2104 ret = insert_entry(chain, e, rulenum - 1,
2105 nsaddrs, saddrs, ndaddrs, daddrs,
2106 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002107 *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002108 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00002109 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002110 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002111 break;
2112 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002113 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002114 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002115 case CMD_LIST:
Marc Bouchere6869a82000-03-20 06:03:29 +00002116 case CMD_LIST|CMD_ZERO:
2117 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002118 rulenum,
Marc Bouchere6869a82000-03-20 06:03:29 +00002119 options&OPT_VERBOSE,
2120 options&OPT_NUMERIC,
2121 options&OPT_EXPANDED,
2122 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002123 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002124 if (ret && (command & CMD_ZERO))
2125 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002126 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002127 break;
2128 case CMD_LIST_RULES:
2129 case CMD_LIST_RULES|CMD_ZERO:
2130 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002131 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002132 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002133 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002134 if (ret && (command & CMD_ZERO))
Marc Bouchere6869a82000-03-20 06:03:29 +00002135 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002136 options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002137 break;
2138 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002139 ret = iptc_create_chain(chain, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002140 break;
2141 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002142 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002143 break;
2144 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002145 ret = iptc_rename_chain(chain, newname, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002146 break;
2147 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002148 ret = iptc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00002149 break;
2150 default:
2151 /* We should never reach this... */
2152 exit_tryhelp(2);
2153 }
2154
2155 if (verbose > 1)
2156 dump_entries(*handle);
2157
Martin Josefsson78cafda2004-02-02 20:01:18 +00002158 clear_rule_matches(&matches);
2159
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002160 if (e != NULL) {
2161 free(e);
2162 e = NULL;
2163 }
2164
keso6997cdf2004-07-04 15:20:53 +00002165 free(saddrs);
2166 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00002167 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002168
Marc Bouchere6869a82000-03-20 06:03:29 +00002169 return ret;
2170}