blob: db97d08f7fa04c8356329abb9b6e2989ba656c8c [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[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100102 {.name = "append", .has_arg = 1, .val = 'A'},
103 {.name = "delete", .has_arg = 1, .val = 'D'},
104 {.name = "insert", .has_arg = 1, .val = 'I'},
105 {.name = "replace", .has_arg = 1, .val = 'R'},
106 {.name = "list", .has_arg = 2, .val = 'L'},
107 {.name = "flush", .has_arg = 2, .val = 'F'},
108 {.name = "zero", .has_arg = 2, .val = 'Z'},
109 {.name = "new-chain", .has_arg = 1, .val = 'N'},
110 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
111 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
112 {.name = "policy", .has_arg = 1, .val = 'P'},
113 {.name = "source", .has_arg = 1, .val = 's'},
114 {.name = "destination", .has_arg = 1, .val = 'd'},
115 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
116 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
117 {.name = "protocol", .has_arg = 1, .val = 'p'},
118 {.name = "in-interface", .has_arg = 1, .val = 'i'},
119 {.name = "jump", .has_arg = 1, .val = 'j'},
120 {.name = "table", .has_arg = 1, .val = 't'},
121 {.name = "match", .has_arg = 1, .val = 'm'},
122 {.name = "numeric", .has_arg = 0, .val = 'n'},
123 {.name = "out-interface", .has_arg = 1, .val = 'o'},
124 {.name = "verbose", .has_arg = 0, .val = 'v'},
125 {.name = "exact", .has_arg = 0, .val = 'x'},
126 {.name = "fragments", .has_arg = 0, .val = 'f'},
127 {.name = "version", .has_arg = 0, .val = 'V'},
128 {.name = "help", .has_arg = 2, .val = 'h'},
129 {.name = "line-numbers", .has_arg = 0, .val = '0'},
130 {.name = "modprobe", .has_arg = 1, .val = 'M'},
131 {.name = "set-counters", .has_arg = 1, .val = 'c'},
132 {.name = "goto", .has_arg = 1, .val = 'g'},
133 {NULL},
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 +0000193/* A few hardcoded protocols for 'all' and in case the user has no
194 /etc/protocols */
195struct pprot {
196 char *name;
197 u_int8_t num;
198};
199
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000200struct afinfo afinfo = {
201 .family = AF_INET,
202 .libprefix = "libipt_",
203 .ipproto = IPPROTO_IP,
204 .kmod = "ip_tables",
205 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
206 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
207};
208
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000209/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000210/* defined in netinet/in.h */
211#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000218#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000219
Marc Bouchere6869a82000-03-20 06:03:29 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000223 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000224 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000225 { "esp", IPPROTO_ESP },
226 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000227 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000228 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000229};
230
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000231static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000232proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000233{
234 unsigned int i;
235
Rusty Russell28381a42000-05-10 00:19:50 +0000236 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000237 struct protoent *pent = getprotobynumber(proto);
238 if (pent)
239 return pent->p_name;
240 }
241
242 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
243 if (chain_protos[i].num == proto)
244 return chain_protos[i].name;
245
246 return NULL;
247}
248
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000249enum {
250 IPT_DOTTED_ADDR = 0,
251 IPT_DOTTED_MASK
252};
253
Pablo Neiradfdcd642005-05-29 19:05:23 +0000254static void free_opts(int reset_offset)
255{
256 if (opts != original_opts) {
257 free(opts);
258 opts = original_opts;
259 if (reset_offset)
260 global_option_offset = 0;
261 }
262}
263
Patrick McHardy0b639362007-09-08 16:00:01 +0000264static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000265exit_tryhelp(int status)
266{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000267 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000268 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000269 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
270 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000271 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000272 exit(status);
273}
274
Patrick McHardy0b639362007-09-08 16:00:01 +0000275static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000276exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000277{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000278 struct iptables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200279 struct xtables_target *t = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000280
Marc Bouchere6869a82000-03-20 06:03:29 +0000281 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000282"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000283" %s -[RI] chain rulenum rule-specification [options]\n"
284" %s -D chain rulenum [options]\n"
285" %s -[LFZ] [chain] [options]\n"
286" %s -[NX] chain\n"
287" %s -E old-chain-name new-chain-name\n"
288" %s -P chain target [options]\n"
289" %s -h (print this help information)\n\n",
290 program_name, program_version, program_name, program_name,
291 program_name, program_name, program_name, program_name,
292 program_name, program_name);
293
294 printf(
295"Commands:\n"
296"Either long or short options are allowed.\n"
297" --append -A chain Append to chain\n"
298" --delete -D chain Delete matching rule from chain\n"
299" --delete -D chain rulenum\n"
300" Delete rule rulenum (1 = first) from chain\n"
301" --insert -I chain [rulenum]\n"
302" Insert in chain as rulenum (default 1=first)\n"
303" --replace -R chain rulenum\n"
304" Replace rule rulenum (1 = first) in chain\n"
305" --list -L [chain] List the rules in a chain or all chains\n"
306" --flush -F [chain] Delete all rules in chain or all chains\n"
307" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000308" --new -N chain Create a new user-defined chain\n"
309" --delete-chain\n"
310" -X [chain] Delete a user-defined chain\n"
311" --policy -P chain target\n"
312" Change policy on chain to target\n"
313" --rename-chain\n"
314" -E old-chain new-chain\n"
315" Change chain name, (moving any references)\n"
316
317"Options:\n"
318" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
319" --source -s [!] address[/mask]\n"
320" source specification\n"
321" --destination -d [!] address[/mask]\n"
322" destination specification\n"
323" --in-interface -i [!] input name[+]\n"
324" network interface name ([+] for wildcard)\n"
325" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000326" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000327#ifdef IPT_F_GOTO
328" --goto -g chain\n"
329" jump to chain with no return\n"
330#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000331" --match -m match\n"
332" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000333" --numeric -n numeric output of addresses and ports\n"
334" --out-interface -o [!] output name[+]\n"
335" network interface name ([+] for wildcard)\n"
336" --table -t table table to manipulate (default: `filter')\n"
337" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000338" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000339" --exact -x expand numbers (display exact values)\n"
340"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000341" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000342" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000343"[!] --version -V print package version.\n");
344
Rusty Russell363112d2000-08-11 13:49:26 +0000345 /* Print out any special helps. A user might like to be able
346 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000347 results. So we call help for all specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000348 for (t = xtables_targets; t ;t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000349 if (t->used) {
350 printf("\n");
351 t->help();
352 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000353 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000355 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000357 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000358 exit(0);
359}
360
Patrick McHardy0b639362007-09-08 16:00:01 +0000361void
362exit_error(enum exittype status, const char *msg, ...)
363{
364 va_list args;
365
366 va_start(args, msg);
367 fprintf(stderr, "%s v%s: ", program_name, program_version);
368 vfprintf(stderr, msg, args);
369 va_end(args);
370 fprintf(stderr, "\n");
371 if (status == PARAMETER_PROBLEM)
372 exit_tryhelp(status);
373 if (status == VERSION_PROBLEM)
374 fprintf(stderr,
375 "Perhaps iptables or your kernel needs to be upgraded.\n");
376 /* On error paths, make sure that we don't leak memory */
377 free_opts(1);
378 exit(status);
379}
380
Marc Bouchere6869a82000-03-20 06:03:29 +0000381static void
382generic_opt_check(int command, int options)
383{
384 int i, j, legal = 0;
385
386 /* Check that commands are valid with options. Complicated by the
387 * fact that if an option is legal with *any* command given, it is
388 * legal overall (ie. -z and -l).
389 */
390 for (i = 0; i < NUMBER_OF_OPT; i++) {
391 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
392
393 for (j = 0; j < NUMBER_OF_CMD; j++) {
394 if (!(command & (1<<j)))
395 continue;
396
397 if (!(options & (1<<i))) {
398 if (commands_v_options[j][i] == '+')
399 exit_error(PARAMETER_PROBLEM,
400 "You need to supply the `-%c' "
401 "option for this command\n",
402 optflags[i]);
403 } else {
404 if (commands_v_options[j][i] != 'x')
405 legal = 1;
406 else if (legal == 0)
407 legal = -1;
408 }
409 }
410 if (legal == -1)
411 exit_error(PARAMETER_PROBLEM,
412 "Illegal option `-%c' with this command\n",
413 optflags[i]);
414 }
415}
416
417static char
418opt2char(int option)
419{
420 const char *ptr;
421 for (ptr = optflags; option > 1; option >>= 1, ptr++);
422
423 return *ptr;
424}
425
426static char
427cmd2char(int option)
428{
429 const char *ptr;
430 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
431
432 return *ptr;
433}
434
435static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000436add_command(unsigned int *cmd, const int newcmd, const int othercmds,
437 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000438{
439 if (invert)
440 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
441 if (*cmd & (~othercmds))
442 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
443 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
444 *cmd |= newcmd;
445}
446
447int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100448check_inverse(const char option[], int *invert, int *my_optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000449{
450 if (option && strcmp(option, "!") == 0) {
451 if (*invert)
452 exit_error(PARAMETER_PROBLEM,
453 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000454 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100455 if (my_optind != NULL) {
456 ++*my_optind;
457 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000458 exit_error(PARAMETER_PROBLEM,
459 "no argument following `!'");
460 }
461
Marc Bouchere6869a82000-03-20 06:03:29 +0000462 return TRUE;
463 }
464 return FALSE;
465}
466
Marc Bouchere6869a82000-03-20 06:03:29 +0000467/*
468 * All functions starting with "parse" should succeed, otherwise
469 * the program fails.
470 * Most routines return pointers to static data that may change
471 * between calls to the same or other routines with a few exceptions:
472 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
473 * return global static data.
474*/
475
Rusty Russell28381a42000-05-10 00:19:50 +0000476/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200477static struct xtables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000478find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000479{
Harald Welteed498492001-07-23 01:24:22 +0000480 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000481
Harald Welte0b0013a2002-02-18 16:15:31 +0000482 if (string_to_number(pname, 0, 255, &proto) != -1) {
483 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000484
Harald Welte0b0013a2002-02-18 16:15:31 +0000485 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000486 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000487 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000488 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000489
490 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000491}
492
Marc Boucherb93c7982001-12-06 14:50:19 +0000493u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000494parse_protocol(const char *s)
495{
Harald Welteed498492001-07-23 01:24:22 +0000496 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000497
Harald Welteed498492001-07-23 01:24:22 +0000498 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000499 struct protoent *pent;
500
Harald Weltecbe1ec72006-02-11 09:50:11 +0000501 /* first deal with the special case of 'all' to prevent
502 * people from being able to redefine 'all' in nsswitch
503 * and/or provoke expensive [not working] ldap/nis/...
504 * lookups */
505 if (!strcmp(s, "all"))
506 return 0;
507
Marc Bouchere6869a82000-03-20 06:03:29 +0000508 if ((pent = getprotobyname(s)))
509 proto = pent->p_proto;
510 else {
511 unsigned int i;
512 for (i = 0;
513 i < sizeof(chain_protos)/sizeof(struct pprot);
514 i++) {
515 if (strcmp(s, chain_protos[i].name) == 0) {
516 proto = chain_protos[i].num;
517 break;
518 }
519 }
520 if (i == sizeof(chain_protos)/sizeof(struct pprot))
521 exit_error(PARAMETER_PROBLEM,
522 "unknown protocol `%s' specified",
523 s);
524 }
525 }
526
527 return (u_int16_t)proto;
528}
529
Marc Bouchere6869a82000-03-20 06:03:29 +0000530/* Can't be zero. */
531static int
532parse_rulenumber(const char *rule)
533{
Harald Welteed498492001-07-23 01:24:22 +0000534 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000535
Harald Welteed498492001-07-23 01:24:22 +0000536 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000537 exit_error(PARAMETER_PROBLEM,
538 "Invalid rule number `%s'", rule);
539
540 return rulenum;
541}
542
543static const char *
544parse_target(const char *targetname)
545{
546 const char *ptr;
547
548 if (strlen(targetname) < 1)
549 exit_error(PARAMETER_PROBLEM,
550 "Invalid target name (too short)");
551
552 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
553 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000554 "Invalid target name `%s' (%u chars max)",
555 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000556
557 for (ptr = targetname; *ptr; ptr++)
558 if (isspace(*ptr))
559 exit_error(PARAMETER_PROBLEM,
560 "Invalid target name `%s'", targetname);
561 return targetname;
562}
563
Marc Bouchere6869a82000-03-20 06:03:29 +0000564static void
565set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
566 int invert)
567{
568 if (*options & option)
569 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
570 opt2char(option));
571 *options |= option;
572
573 if (invert) {
574 unsigned int i;
575 for (i = 0; 1 << i != option; i++);
576
577 if (!inverse_for_options[i])
578 exit_error(PARAMETER_PROBLEM,
579 "cannot have ! before -%c",
580 opt2char(option));
581 *invflg |= inverse_for_options[i];
582 }
583}
584
Marc Bouchere6869a82000-03-20 06:03:29 +0000585static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000586merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000587 unsigned int *option_offset)
588{
589 unsigned int num_old, num_new, i;
590 struct option *merge;
591
Jan Engelhardtd0145402007-07-30 14:32:26 +0000592 if (newopts == NULL)
593 return oldopts;
594
Marc Bouchere6869a82000-03-20 06:03:29 +0000595 for (num_old = 0; oldopts[num_old].name; num_old++);
596 for (num_new = 0; newopts[num_new].name; num_new++);
597
598 global_option_offset += OPTION_OFFSET;
599 *option_offset = global_option_offset;
600
601 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +0000602 if (merge == NULL)
603 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000604 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000605 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000606 for (i = 0; i < num_new; i++) {
607 merge[num_old + i] = newopts[i];
608 merge[num_old + i].val += *option_offset;
609 }
610 memset(merge + num_old + num_new, 0, sizeof(struct option));
611
612 return merge;
613}
614
Marc Bouchere6869a82000-03-20 06:03:29 +0000615static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000616print_num(u_int64_t number, unsigned int format)
617{
618 if (format & FMT_KILOMEGAGIGA) {
619 if (number > 99999) {
620 number = (number + 500) / 1000;
621 if (number > 9999) {
622 number = (number + 500) / 1000;
623 if (number > 9999) {
624 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000625 if (number > 9999) {
626 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000627 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000628 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000629 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000630 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000631 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000632 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000633 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000634 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000635 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000636 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000637 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000638}
639
640
641static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000642print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
643{
644 struct ipt_counters counters;
645 const char *pol = iptc_get_policy(chain, &counters, handle);
646 printf("Chain %s", chain);
647 if (pol) {
648 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000649 if (!(format & FMT_NOCOUNTS)) {
650 fputc(' ', stdout);
651 print_num(counters.pcnt, (format|FMT_NOTABLE));
652 fputs("packets, ", stdout);
653 print_num(counters.bcnt, (format|FMT_NOTABLE));
654 fputs("bytes", stdout);
655 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000656 printf(")\n");
657 } else {
658 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000659 if (!iptc_get_references(&refs, chain, handle))
660 printf(" (ERROR obtaining refs)\n");
661 else
662 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000663 }
664
665 if (format & FMT_LINENUMBERS)
666 printf(FMT("%-4s ", "%s "), "num");
667 if (!(format & FMT_NOCOUNTS)) {
668 if (format & FMT_KILOMEGAGIGA) {
669 printf(FMT("%5s ","%s "), "pkts");
670 printf(FMT("%5s ","%s "), "bytes");
671 } else {
672 printf(FMT("%8s ","%s "), "pkts");
673 printf(FMT("%10s ","%s "), "bytes");
674 }
675 }
676 if (!(format & FMT_NOTARGET))
677 printf(FMT("%-9s ","%s "), "target");
678 fputs(" prot ", stdout);
679 if (format & FMT_OPTIONS)
680 fputs("opt", stdout);
681 if (format & FMT_VIA) {
682 printf(FMT(" %-6s ","%s "), "in");
683 printf(FMT("%-6s ","%s "), "out");
684 }
685 printf(FMT(" %-19s ","%s "), "source");
686 printf(FMT(" %-19s "," %s "), "destination");
687 printf("\n");
688}
689
Marc Bouchere6869a82000-03-20 06:03:29 +0000690
691static int
692print_match(const struct ipt_entry_match *m,
693 const struct ipt_ip *ip,
694 int numeric)
695{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200696 struct xtables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000697
698 if (match) {
699 if (match->print)
700 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000701 else
Rusty Russellb039b022000-09-01 06:04:05 +0000702 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000703 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000704 if (m->u.user.name[0])
705 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000706 }
707 /* Don't stop iterating. */
708 return 0;
709}
710
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100711/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000712static void
713print_firewall(const struct ipt_entry *fw,
714 const char *targname,
715 unsigned int num,
716 unsigned int format,
717 const iptc_handle_t handle)
718{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200719 struct xtables_target *target = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000720 const struct ipt_entry_target *t;
721 u_int8_t flags;
722 char buf[BUFSIZ];
723
Marc Bouchere6869a82000-03-20 06:03:29 +0000724 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +0000725 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000726 else
Rusty Russell52a51492000-05-02 16:44:29 +0000727 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000728
729 t = ipt_get_target((struct ipt_entry *)fw);
730 flags = fw->ip.flags;
731
732 if (format & FMT_LINENUMBERS)
733 printf(FMT("%-4u ", "%u "), num+1);
734
735 if (!(format & FMT_NOCOUNTS)) {
736 print_num(fw->counters.pcnt, format);
737 print_num(fw->counters.bcnt, format);
738 }
739
740 if (!(format & FMT_NOTARGET))
741 printf(FMT("%-9s ", "%s "), targname);
742
743 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
744 {
Rusty Russell28381a42000-05-10 00:19:50 +0000745 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000746 if (pname)
747 printf(FMT("%-5s", "%s "), pname);
748 else
749 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
750 }
751
752 if (format & FMT_OPTIONS) {
753 if (format & FMT_NOTABLE)
754 fputs("opt ", stdout);
755 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
756 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
757 fputc(' ', stdout);
758 }
759
760 if (format & FMT_VIA) {
761 char iface[IFNAMSIZ+2];
762
763 if (fw->ip.invflags & IPT_INV_VIA_IN) {
764 iface[0] = '!';
765 iface[1] = '\0';
766 }
767 else iface[0] = '\0';
768
769 if (fw->ip.iniface[0] != '\0') {
770 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000771 }
772 else if (format & FMT_NUMERIC) strcat(iface, "*");
773 else strcat(iface, "any");
774 printf(FMT(" %-6s ","in %s "), iface);
775
776 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
777 iface[0] = '!';
778 iface[1] = '\0';
779 }
780 else iface[0] = '\0';
781
782 if (fw->ip.outiface[0] != '\0') {
783 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000784 }
785 else if (format & FMT_NUMERIC) strcat(iface, "*");
786 else strcat(iface, "any");
787 printf(FMT("%-6s ","out %s "), iface);
788 }
789
790 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
791 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
792 printf(FMT("%-19s ","%s "), "anywhere");
793 else {
794 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000795 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000797 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.src));
798 strcat(buf, ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000799 printf(FMT("%-19s ","%s "), buf);
800 }
801
802 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
803 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000804 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000805 else {
806 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000807 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000808 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000809 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.dst));
810 strcat(buf, ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000811 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000812 }
813
814 if (format & FMT_NOTABLE)
815 fputs(" ", stdout);
816
Harald Welte72bd87e2005-11-24 17:04:05 +0000817#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000818 if(fw->ip.flags & IPT_F_GOTO)
819 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000820#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000821
Marc Bouchere6869a82000-03-20 06:03:29 +0000822 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
823
824 if (target) {
825 if (target->print)
826 /* Print the target information. */
827 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000828 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000829 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000830 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000831
832 if (!(format & FMT_NONEWLINE))
833 fputc('\n', stdout);
834}
835
836static void
837print_firewall_line(const struct ipt_entry *fw,
838 const iptc_handle_t h)
839{
840 struct ipt_entry_target *t;
841
842 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000843 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000844}
845
846static int
847append_entry(const ipt_chainlabel chain,
848 struct ipt_entry *fw,
849 unsigned int nsaddrs,
850 const struct in_addr saddrs[],
851 unsigned int ndaddrs,
852 const struct in_addr daddrs[],
853 int verbose,
854 iptc_handle_t *handle)
855{
856 unsigned int i, j;
857 int ret = 1;
858
859 for (i = 0; i < nsaddrs; i++) {
860 fw->ip.src.s_addr = saddrs[i].s_addr;
861 for (j = 0; j < ndaddrs; j++) {
862 fw->ip.dst.s_addr = daddrs[j].s_addr;
863 if (verbose)
864 print_firewall_line(fw, *handle);
865 ret &= iptc_append_entry(chain, fw, handle);
866 }
867 }
868
869 return ret;
870}
871
872static int
873replace_entry(const ipt_chainlabel chain,
874 struct ipt_entry *fw,
875 unsigned int rulenum,
876 const struct in_addr *saddr,
877 const struct in_addr *daddr,
878 int verbose,
879 iptc_handle_t *handle)
880{
881 fw->ip.src.s_addr = saddr->s_addr;
882 fw->ip.dst.s_addr = daddr->s_addr;
883
884 if (verbose)
885 print_firewall_line(fw, *handle);
886 return iptc_replace_entry(chain, fw, rulenum, handle);
887}
888
889static int
890insert_entry(const ipt_chainlabel chain,
891 struct ipt_entry *fw,
892 unsigned int rulenum,
893 unsigned int nsaddrs,
894 const struct in_addr saddrs[],
895 unsigned int ndaddrs,
896 const struct in_addr daddrs[],
897 int verbose,
898 iptc_handle_t *handle)
899{
900 unsigned int i, j;
901 int ret = 1;
902
903 for (i = 0; i < nsaddrs; i++) {
904 fw->ip.src.s_addr = saddrs[i].s_addr;
905 for (j = 0; j < ndaddrs; j++) {
906 fw->ip.dst.s_addr = daddrs[j].s_addr;
907 if (verbose)
908 print_firewall_line(fw, *handle);
909 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
910 }
911 }
912
913 return ret;
914}
915
Rusty Russell2e0a3212000-04-19 11:23:18 +0000916static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000917make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000918{
919 /* Establish mask for comparison */
920 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000921 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000922 unsigned char *mask, *mptr;
923
924 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000925 for (matchp = matches; matchp; matchp = matchp->next)
926 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000927
Rusty Russell9e1d2142000-04-23 09:11:12 +0000928 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000929 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000930 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000931
Rusty Russell9e1d2142000-04-23 09:11:12 +0000932 memset(mask, 0xFF, sizeof(struct ipt_entry));
933 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000934
Martin Josefsson78cafda2004-02-02 20:01:18 +0000935 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000936 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000937 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000938 + matchp->match->userspacesize);
939 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000940 }
941
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000942 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000943 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000944 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000945
946 return mask;
947}
948
Marc Bouchere6869a82000-03-20 06:03:29 +0000949static int
950delete_entry(const ipt_chainlabel chain,
951 struct ipt_entry *fw,
952 unsigned int nsaddrs,
953 const struct in_addr saddrs[],
954 unsigned int ndaddrs,
955 const struct in_addr daddrs[],
956 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +0000957 iptc_handle_t *handle,
958 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000959{
960 unsigned int i, j;
961 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000962 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000963
Martin Josefsson78cafda2004-02-02 20:01:18 +0000964 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000965 for (i = 0; i < nsaddrs; i++) {
966 fw->ip.src.s_addr = saddrs[i].s_addr;
967 for (j = 0; j < ndaddrs; j++) {
968 fw->ip.dst.s_addr = daddrs[j].s_addr;
969 if (verbose)
970 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000971 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000972 }
973 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000974 free(mask);
975
Marc Bouchere6869a82000-03-20 06:03:29 +0000976 return ret;
977}
978
Harald Welteae1ff9f2000-12-01 14:26:20 +0000979int
Marc Bouchere6869a82000-03-20 06:03:29 +0000980for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +0000981 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000982{
983 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000984 const char *chain;
985 char *chains;
986 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000987
Rusty Russell9e1d2142000-04-23 09:11:12 +0000988 chain = iptc_first_chain(handle);
989 while (chain) {
990 chaincount++;
991 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000992 }
993
Rusty Russell9e1d2142000-04-23 09:11:12 +0000994 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
995 i = 0;
996 chain = iptc_first_chain(handle);
997 while (chain) {
998 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
999 i++;
1000 chain = iptc_next_chain(handle);
1001 }
1002
1003 for (i = 0; i < chaincount; i++) {
1004 if (!builtinstoo
1005 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001006 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001007 continue;
1008 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1009 }
1010
1011 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001012 return ret;
1013}
1014
Harald Welteae1ff9f2000-12-01 14:26:20 +00001015int
Marc Bouchere6869a82000-03-20 06:03:29 +00001016flush_entries(const ipt_chainlabel chain, int verbose,
1017 iptc_handle_t *handle)
1018{
1019 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001020 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001021
1022 if (verbose)
1023 fprintf(stdout, "Flushing chain `%s'\n", chain);
1024 return iptc_flush_entries(chain, handle);
1025}
Marc Bouchere6869a82000-03-20 06:03:29 +00001026
1027static int
1028zero_entries(const ipt_chainlabel chain, int verbose,
1029 iptc_handle_t *handle)
1030{
1031 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001032 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001033
Marc Bouchere6869a82000-03-20 06:03:29 +00001034 if (verbose)
1035 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1036 return iptc_zero_entries(chain, handle);
1037}
1038
Harald Welteae1ff9f2000-12-01 14:26:20 +00001039int
Marc Bouchere6869a82000-03-20 06:03:29 +00001040delete_chain(const ipt_chainlabel chain, int verbose,
1041 iptc_handle_t *handle)
1042{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001043 if (!chain)
1044 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001045
1046 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001047 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001048 return iptc_delete_chain(chain, handle);
1049}
1050
1051static int
1052list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1053 int expanded, int linenumbers, iptc_handle_t *handle)
1054{
1055 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001056 unsigned int format;
1057 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001058
1059 format = FMT_OPTIONS;
1060 if (!verbose)
1061 format |= FMT_NOCOUNTS;
1062 else
1063 format |= FMT_VIA;
1064
1065 if (numeric)
1066 format |= FMT_NUMERIC;
1067
1068 if (!expanded)
1069 format |= FMT_KILOMEGAGIGA;
1070
1071 if (linenumbers)
1072 format |= FMT_LINENUMBERS;
1073
Rusty Russell9e1d2142000-04-23 09:11:12 +00001074 for (this = iptc_first_chain(handle);
1075 this;
1076 this = iptc_next_chain(handle)) {
1077 const struct ipt_entry *i;
1078 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001079
Marc Bouchere6869a82000-03-20 06:03:29 +00001080 if (chain && strcmp(chain, this) != 0)
1081 continue;
1082
1083 if (found) printf("\n");
1084
1085 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001086 i = iptc_first_rule(this, handle);
1087
1088 num = 0;
1089 while (i) {
1090 print_firewall(i,
1091 iptc_get_target(i, handle),
1092 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001093 format,
1094 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001095 i = iptc_next_rule(i, handle);
1096 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001097 found = 1;
1098 }
1099
1100 errno = ENOENT;
1101 return found;
1102}
1103
1104static struct ipt_entry *
1105generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001106 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001107 struct ipt_entry_target *target)
1108{
1109 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001110 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001111 struct ipt_entry *e;
1112
1113 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001114 for (matchp = matches; matchp; matchp = matchp->next)
1115 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001116
Rusty Russell228e98d2000-04-27 10:28:06 +00001117 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001118 *e = *fw;
1119 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001120 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001121
1122 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001123 for (matchp = matches; matchp; matchp = matchp->next) {
1124 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1125 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001126 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001127 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001128
1129 return e;
1130}
1131
Jan Engelhardt33690a12008-02-11 00:54:00 +01001132static void clear_rule_matches(struct iptables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001133{
1134 struct iptables_rule_match *matchp, *tmp;
1135
1136 for (matchp = *matches; matchp;) {
1137 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001138 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001139 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001140 matchp->match->m = NULL;
1141 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001142 if (matchp->match == matchp->match->next) {
1143 free(matchp->match);
1144 matchp->match = NULL;
1145 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001146 free(matchp);
1147 matchp = tmp;
1148 }
1149
1150 *matches = NULL;
1151}
1152
Rusty Russell3aef54d2005-01-03 03:48:40 +00001153static void set_revision(char *name, u_int8_t revision)
1154{
1155 /* Old kernel sources don't have ".revision" field,
1156 but we stole a byte from name. */
1157 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1158 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1159}
1160
Phil Oester8cf65912005-09-19 15:00:33 +00001161void
1162get_kernel_version(void) {
1163 static struct utsname uts;
1164 int x = 0, y = 0, z = 0;
1165
1166 if (uname(&uts) == -1) {
1167 fprintf(stderr, "Unable to retrieve kernel version.\n");
1168 free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001169 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001170 }
1171
1172 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1173 kernel_version = LINUX_VERSION(x, y, z);
1174}
1175
Marc Bouchere6869a82000-03-20 06:03:29 +00001176int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1177{
1178 struct ipt_entry fw, *e = NULL;
1179 int invert = 0;
1180 unsigned int nsaddrs = 0, ndaddrs = 0;
1181 struct in_addr *saddrs = NULL, *daddrs = NULL;
1182
1183 int c, verbose = 0;
1184 const char *chain = NULL;
1185 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1186 const char *policy = NULL, *newname = NULL;
1187 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001188 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001189 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001190 struct xtables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001191 struct iptables_rule_match *matches = NULL;
1192 struct iptables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001193 struct xtables_target *target = NULL;
1194 struct xtables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001195 const char *jumpto = "";
1196 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001197 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001198 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001199
1200 memset(&fw, 0, sizeof(fw));
1201
Harald Welteae1ff9f2000-12-01 14:26:20 +00001202 /* re-set optind to 0 in case do_command gets called
1203 * a second time */
1204 optind = 0;
1205
1206 /* clear mflags in case do_command gets called a second time
1207 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001208 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001209 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001210
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001211 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001212 t->tflags = 0;
1213 t->used = 0;
1214 }
1215
Marc Bouchere6869a82000-03-20 06:03:29 +00001216 /* Suppress error messages: we may add new options if we
1217 demand-load a protocol. */
1218 opterr = 0;
1219
1220 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001221 "-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 +00001222 opts, NULL)) != -1) {
1223 switch (c) {
1224 /*
1225 * Command selection
1226 */
1227 case 'A':
1228 add_command(&command, CMD_APPEND, CMD_NONE,
1229 invert);
1230 chain = optarg;
1231 break;
1232
1233 case 'D':
1234 add_command(&command, CMD_DELETE, CMD_NONE,
1235 invert);
1236 chain = optarg;
1237 if (optind < argc && argv[optind][0] != '-'
1238 && argv[optind][0] != '!') {
1239 rulenum = parse_rulenumber(argv[optind++]);
1240 command = CMD_DELETE_NUM;
1241 }
1242 break;
1243
Marc Bouchere6869a82000-03-20 06:03:29 +00001244 case 'R':
1245 add_command(&command, CMD_REPLACE, CMD_NONE,
1246 invert);
1247 chain = optarg;
1248 if (optind < argc && argv[optind][0] != '-'
1249 && argv[optind][0] != '!')
1250 rulenum = parse_rulenumber(argv[optind++]);
1251 else
1252 exit_error(PARAMETER_PROBLEM,
1253 "-%c requires a rule number",
1254 cmd2char(CMD_REPLACE));
1255 break;
1256
1257 case 'I':
1258 add_command(&command, CMD_INSERT, CMD_NONE,
1259 invert);
1260 chain = optarg;
1261 if (optind < argc && argv[optind][0] != '-'
1262 && argv[optind][0] != '!')
1263 rulenum = parse_rulenumber(argv[optind++]);
1264 else rulenum = 1;
1265 break;
1266
1267 case 'L':
1268 add_command(&command, CMD_LIST, CMD_ZERO,
1269 invert);
1270 if (optarg) chain = optarg;
1271 else if (optind < argc && argv[optind][0] != '-'
1272 && argv[optind][0] != '!')
1273 chain = argv[optind++];
1274 break;
1275
1276 case 'F':
1277 add_command(&command, CMD_FLUSH, CMD_NONE,
1278 invert);
1279 if (optarg) chain = optarg;
1280 else if (optind < argc && argv[optind][0] != '-'
1281 && argv[optind][0] != '!')
1282 chain = argv[optind++];
1283 break;
1284
1285 case 'Z':
1286 add_command(&command, CMD_ZERO, CMD_LIST,
1287 invert);
1288 if (optarg) chain = optarg;
1289 else if (optind < argc && argv[optind][0] != '-'
1290 && argv[optind][0] != '!')
1291 chain = argv[optind++];
1292 break;
1293
1294 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001295 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001296 exit_error(PARAMETER_PROBLEM,
1297 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001298 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001299 if (find_target(optarg, TRY_LOAD))
1300 exit_error(PARAMETER_PROBLEM,
1301 "chain name may not clash "
1302 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001303 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1304 invert);
1305 chain = optarg;
1306 break;
1307
1308 case 'X':
1309 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1310 invert);
1311 if (optarg) chain = optarg;
1312 else if (optind < argc && argv[optind][0] != '-'
1313 && argv[optind][0] != '!')
1314 chain = argv[optind++];
1315 break;
1316
1317 case 'E':
1318 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1319 invert);
1320 chain = optarg;
1321 if (optind < argc && argv[optind][0] != '-'
1322 && argv[optind][0] != '!')
1323 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001324 else
1325 exit_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001326 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001327 "new-chain-name",
1328 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001329 break;
1330
1331 case 'P':
1332 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1333 invert);
1334 chain = optarg;
1335 if (optind < argc && argv[optind][0] != '-'
1336 && argv[optind][0] != '!')
1337 policy = argv[optind++];
1338 else
1339 exit_error(PARAMETER_PROBLEM,
1340 "-%c requires a chain and a policy",
1341 cmd2char(CMD_SET_POLICY));
1342 break;
1343
1344 case 'h':
1345 if (!optarg)
1346 optarg = argv[optind];
1347
Rusty Russell2e0a3212000-04-19 11:23:18 +00001348 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001349 if (!matches && protocol)
1350 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001351
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001352 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001353
1354 /*
1355 * Option selection
1356 */
1357 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001358 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001359 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1360 invert);
1361
1362 /* Canonicalize into lower case */
1363 for (protocol = argv[optind-1]; *protocol; protocol++)
1364 *protocol = tolower(*protocol);
1365
1366 protocol = argv[optind-1];
1367 fw.ip.proto = parse_protocol(protocol);
1368
1369 if (fw.ip.proto == 0
1370 && (fw.ip.invflags & IPT_INV_PROTO))
1371 exit_error(PARAMETER_PROBLEM,
1372 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 break;
1374
1375 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001376 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001377 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1378 invert);
1379 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001380 break;
1381
1382 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001383 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001384 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1385 invert);
1386 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 break;
1388
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001389#ifdef IPT_F_GOTO
1390 case 'g':
1391 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1392 invert);
1393 fw.ip.flags |= IPT_F_GOTO;
1394 jumpto = parse_target(optarg);
1395 break;
1396#endif
1397
Marc Bouchere6869a82000-03-20 06:03:29 +00001398 case 'j':
1399 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1400 invert);
1401 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001402 /* TRY_LOAD (may be chain name) */
1403 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001404
1405 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001406 size_t size;
1407
Rusty Russell73f72f52000-07-03 10:17:57 +00001408 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1409 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001410
Rusty Russell2e0a3212000-04-19 11:23:18 +00001411 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001412 target->t->u.target_size = size;
1413 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001414 set_revision(target->t->u.user.name,
1415 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001416 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001417 target->init(target->t);
Max Kellermann5b76f682008-01-29 13:42:48 +00001418 opts = merge_options(opts,
1419 target->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001420 &target->option_offset);
1421 if (opts == NULL)
1422 exit_error(OTHER_PROBLEM,
1423 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001424 }
1425 break;
1426
1427
1428 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001429 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001430 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1431 invert);
1432 parse_interface(argv[optind-1],
1433 fw.ip.iniface,
1434 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001435 break;
1436
1437 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001438 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001439 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1440 invert);
1441 parse_interface(argv[optind-1],
1442 fw.ip.outiface,
1443 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001444 break;
1445
1446 case 'f':
1447 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1448 invert);
1449 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001450 break;
1451
1452 case 'v':
1453 if (!verbose)
1454 set_option(&options, OPT_VERBOSE,
1455 &fw.ip.invflags, invert);
1456 verbose++;
1457 break;
1458
Rusty Russell52a51492000-05-02 16:44:29 +00001459 case 'm': {
1460 size_t size;
1461
Marc Bouchere6869a82000-03-20 06:03:29 +00001462 if (invert)
1463 exit_error(PARAMETER_PROBLEM,
1464 "unexpected ! flag before --match");
1465
Martin Josefsson78cafda2004-02-02 20:01:18 +00001466 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001467 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1468 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001469 m->m = fw_calloc(1, size);
1470 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001471 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001472 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001473 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001474 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001475 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001476 /* Merge options for non-cloned matches */
Max Kellermann5b76f682008-01-29 13:42:48 +00001477 opts = merge_options(opts,
1478 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001479 &m->option_offset);
1480 if (opts == NULL)
1481 exit_error(OTHER_PROBLEM,
1482 "can't alloc memory!");
1483 }
Rusty Russell52a51492000-05-02 16:44:29 +00001484 }
1485 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001486
1487 case 'n':
1488 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1489 invert);
1490 break;
1491
1492 case 't':
1493 if (invert)
1494 exit_error(PARAMETER_PROBLEM,
1495 "unexpected ! flag before --table");
1496 *table = argv[optind-1];
1497 break;
1498
1499 case 'x':
1500 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1501 invert);
1502 break;
1503
1504 case 'V':
1505 if (invert)
1506 printf("Not %s ;-)\n", program_version);
1507 else
1508 printf("%s v%s\n",
1509 program_name, program_version);
1510 exit(0);
1511
1512 case '0':
1513 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1514 invert);
1515 break;
1516
Harald Welte82dd2ec2000-12-19 05:18:15 +00001517 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001518 modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001519 break;
1520
Harald Welteccd49e52001-01-23 22:54:34 +00001521 case 'c':
1522
1523 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1524 invert);
1525 pcnt = optarg;
1526 if (optind < argc && argv[optind][0] != '-'
1527 && argv[optind][0] != '!')
1528 bcnt = argv[optind++];
1529 else
1530 exit_error(PARAMETER_PROBLEM,
1531 "-%c requires packet and byte counter",
1532 opt2char(OPT_COUNTERS));
1533
Patrick McHardy875441e2007-10-17 08:48:58 +00001534 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001535 exit_error(PARAMETER_PROBLEM,
1536 "-%c packet counter not numeric",
1537 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001538 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001539
Patrick McHardy875441e2007-10-17 08:48:58 +00001540 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001541 exit_error(PARAMETER_PROBLEM,
1542 "-%c byte counter not numeric",
1543 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001544 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001545 break;
1546
1547
Marc Bouchere6869a82000-03-20 06:03:29 +00001548 case 1: /* non option */
1549 if (optarg[0] == '!' && optarg[1] == '\0') {
1550 if (invert)
1551 exit_error(PARAMETER_PROBLEM,
1552 "multiple consecutive ! not"
1553 " allowed");
1554 invert = TRUE;
1555 optarg[0] = '\0';
1556 continue;
1557 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001558 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001559 exit_tryhelp(2);
1560
1561 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001562 if (!target
1563 || !(target->parse(c - target->option_offset,
1564 argv, invert,
1565 &target->tflags,
1566 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001567 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001568 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001569 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001570 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001571 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001572 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001573 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001574 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001575 break;
1576 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001577 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001578
1579 /* If you listen carefully, you can
1580 actually hear this code suck. */
1581
1582 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001583 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001584 * parameter, that has not been parsed yet,
1585 * it's not an option of an explicitly loaded
1586 * match or a target. However, we support
1587 * implicit loading of the protocol match
1588 * extension. '-p tcp' means 'l4 proto 6' and
1589 * at the same time 'load tcp protocol match on
1590 * demand if we specify --dport'.
1591 *
1592 * To make this work, we need to make sure:
1593 * - the parameter has not been parsed by
1594 * a match (m above)
1595 * - a protocol has been specified
1596 * - the protocol extension has not been
1597 * loaded yet, or is loaded and unused
1598 * [think of iptables-restore!]
1599 * - the protocol extension can be successively
1600 * loaded
1601 */
1602 if (m == NULL
1603 && protocol
1604 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001605 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001606 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001607 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001608 && (proto_used == 0))
1609 )
1610 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001611 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001612 /* Try loading protocol */
1613 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001614
Harald Welte2d86b772002-08-26 12:21:44 +00001615 proto_used = 1;
1616
1617 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1618 + m->size;
1619
1620 m->m = fw_calloc(1, size);
1621 m->m->u.match_size = size;
1622 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001623 set_revision(m->m->u.user.name,
1624 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001625 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001626 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001627
1628 opts = merge_options(opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001629 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001630 &m->option_offset);
1631 if (opts == NULL)
1632 exit_error(OTHER_PROBLEM,
1633 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001634
1635 optind--;
1636 continue;
1637 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001638 if (!m)
1639 exit_error(PARAMETER_PROBLEM,
1640 "Unknown arg `%s'",
1641 argv[optind-1]);
1642 }
1643 }
1644 invert = FALSE;
1645 }
1646
Martin Josefsson78cafda2004-02-02 20:01:18 +00001647 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001648 if (matchp->match->final_check != NULL)
1649 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001650
Jan Engelhardt830132a2007-10-04 16:24:50 +00001651 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 target->final_check(target->tflags);
1653
1654 /* Fix me: must put inverse options checking here --MN */
1655
1656 if (optind < argc)
1657 exit_error(PARAMETER_PROBLEM,
1658 "unknown arguments found on commandline");
1659 if (!command)
1660 exit_error(PARAMETER_PROBLEM, "no command specified");
1661 if (invert)
1662 exit_error(PARAMETER_PROBLEM,
1663 "nothing appropriate following !");
1664
Harald Welte6336bfd2002-05-07 14:41:43 +00001665 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001666 if (!(options & OPT_DESTINATION))
1667 dhostnetworkmask = "0.0.0.0/0";
1668 if (!(options & OPT_SOURCE))
1669 shostnetworkmask = "0.0.0.0/0";
1670 }
1671
1672 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001673 ipparse_hostnetworkmask(shostnetworkmask, &saddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001674 &fw.ip.smsk, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001675
1676 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001677 ipparse_hostnetworkmask(dhostnetworkmask, &daddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001678 &fw.ip.dmsk, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001679
1680 if ((nsaddrs > 1 || ndaddrs > 1) &&
1681 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1682 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1683 " source or destination IP addresses");
1684
Marc Bouchere6869a82000-03-20 06:03:29 +00001685 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1686 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1687 "specify a unique address");
1688
1689 generic_opt_check(command, options);
1690
1691 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1692 exit_error(PARAMETER_PROBLEM,
1693 "chain name `%s' too long (must be under %i chars)",
1694 chain, IPT_FUNCTION_MAXNAMELEN);
1695
Harald Welteae1ff9f2000-12-01 14:26:20 +00001696 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001697 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001698 *handle = iptc_init(*table);
1699
Rusty Russell8beb0492004-12-22 00:37:10 +00001700 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001701 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001702 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001703
Marc Bouchere6869a82000-03-20 06:03:29 +00001704 if (!*handle)
1705 exit_error(VERSION_PROBLEM,
1706 "can't initialize iptables table `%s': %s",
1707 *table, iptc_strerror(errno));
1708
Harald Welte6336bfd2002-05-07 14:41:43 +00001709 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001710 || command == CMD_DELETE
1711 || command == CMD_INSERT
1712 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001713 if (strcmp(chain, "PREROUTING") == 0
1714 || strcmp(chain, "INPUT") == 0) {
1715 /* -o not valid with incoming packets. */
1716 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001717 exit_error(PARAMETER_PROBLEM,
1718 "Can't use -%c with %s\n",
1719 opt2char(OPT_VIANAMEOUT),
1720 chain);
1721 }
1722
Rusty Russella4860fd2000-06-17 16:13:02 +00001723 if (strcmp(chain, "POSTROUTING") == 0
1724 || strcmp(chain, "OUTPUT") == 0) {
1725 /* -i not valid with outgoing packets */
1726 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001727 exit_error(PARAMETER_PROBLEM,
1728 "Can't use -%c with %s\n",
1729 opt2char(OPT_VIANAMEIN),
1730 chain);
1731 }
1732
1733 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001734 fprintf(stderr,
1735 "Warning: using chain %s, not extension\n",
1736 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001737
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001738 if (target->t)
1739 free(target->t);
1740
Marc Bouchere6869a82000-03-20 06:03:29 +00001741 target = NULL;
1742 }
1743
1744 /* If they didn't specify a target, or it's a chain
1745 name, use standard. */
1746 if (!target
1747 && (strlen(jumpto) == 0
1748 || iptc_is_chain(jumpto, *handle))) {
1749 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001750
Rusty Russell52a51492000-05-02 16:44:29 +00001751 target = find_target(IPT_STANDARD_TARGET,
1752 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001753
1754 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001755 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001756 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001757 target->t->u.target_size = size;
1758 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00001759 if (!iptc_is_chain(jumpto, *handle))
1760 set_revision(target->t->u.user.name,
1761 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001762 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001763 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001764 }
1765
Rusty Russell7e53bf92000-03-20 07:03:28 +00001766 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001767 /* it is no chain, and we can't load a plugin.
1768 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001769 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001770 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001771#ifdef IPT_F_GOTO
1772 if (fw.ip.flags & IPT_F_GOTO)
1773 exit_error(PARAMETER_PROBLEM,
1774 "goto '%s' is not a chain\n", jumpto);
1775#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00001776 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001777 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001778 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001779 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001780 }
1781 }
1782
1783 switch (command) {
1784 case CMD_APPEND:
1785 ret = append_entry(chain, e,
1786 nsaddrs, saddrs, ndaddrs, daddrs,
1787 options&OPT_VERBOSE,
1788 handle);
1789 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001790 case CMD_DELETE:
1791 ret = delete_entry(chain, e,
1792 nsaddrs, saddrs, ndaddrs, daddrs,
1793 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001794 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001795 break;
1796 case CMD_DELETE_NUM:
1797 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
1798 break;
1799 case CMD_REPLACE:
1800 ret = replace_entry(chain, e, rulenum - 1,
1801 saddrs, daddrs, options&OPT_VERBOSE,
1802 handle);
1803 break;
1804 case CMD_INSERT:
1805 ret = insert_entry(chain, e, rulenum - 1,
1806 nsaddrs, saddrs, ndaddrs, daddrs,
1807 options&OPT_VERBOSE,
1808 handle);
1809 break;
1810 case CMD_LIST:
1811 ret = list_entries(chain,
1812 options&OPT_VERBOSE,
1813 options&OPT_NUMERIC,
1814 options&OPT_EXPANDED,
1815 options&OPT_LINENUMBERS,
1816 handle);
1817 break;
1818 case CMD_FLUSH:
1819 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1820 break;
1821 case CMD_ZERO:
1822 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1823 break;
1824 case CMD_LIST|CMD_ZERO:
1825 ret = list_entries(chain,
1826 options&OPT_VERBOSE,
1827 options&OPT_NUMERIC,
1828 options&OPT_EXPANDED,
1829 options&OPT_LINENUMBERS,
1830 handle);
1831 if (ret)
1832 ret = zero_entries(chain,
1833 options&OPT_VERBOSE, handle);
1834 break;
1835 case CMD_NEW_CHAIN:
1836 ret = iptc_create_chain(chain, handle);
1837 break;
1838 case CMD_DELETE_CHAIN:
1839 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1840 break;
1841 case CMD_RENAME_CHAIN:
1842 ret = iptc_rename_chain(chain, newname, handle);
1843 break;
1844 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001845 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001846 break;
1847 default:
1848 /* We should never reach this... */
1849 exit_tryhelp(2);
1850 }
1851
1852 if (verbose > 1)
1853 dump_entries(*handle);
1854
Martin Josefsson78cafda2004-02-02 20:01:18 +00001855 clear_rule_matches(&matches);
1856
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001857 if (e != NULL) {
1858 free(e);
1859 e = NULL;
1860 }
1861
keso6997cdf2004-07-04 15:20:53 +00001862 free(saddrs);
1863 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00001864 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001865
Marc Bouchere6869a82000-03-20 06:03:29 +00001866 return ret;
1867}