blob: 0300027cb111d12628f831bc690f999f48d4a57a [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;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000279 struct iptables_target *t = NULL;
280
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'. */
477static struct iptables_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
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000615void register_match(struct iptables_match *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000616{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617 me->family = PF_INET;
618 xtables_register_match(me);
Rusty Russell3aef54d2005-01-03 03:48:40 +0000619}
620
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000621void register_target(struct iptables_target *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000622{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623 me->family = PF_INET;
624 xtables_register_target(me);
Marc Bouchere6869a82000-03-20 06:03:29 +0000625}
626
627static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000628print_num(u_int64_t number, unsigned int format)
629{
630 if (format & FMT_KILOMEGAGIGA) {
631 if (number > 99999) {
632 number = (number + 500) / 1000;
633 if (number > 9999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000637 if (number > 9999) {
638 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000639 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000640 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000644 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000650}
651
652
653static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000654print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
655{
656 struct ipt_counters counters;
657 const char *pol = iptc_get_policy(chain, &counters, handle);
658 printf("Chain %s", chain);
659 if (pol) {
660 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000661 if (!(format & FMT_NOCOUNTS)) {
662 fputc(' ', stdout);
663 print_num(counters.pcnt, (format|FMT_NOTABLE));
664 fputs("packets, ", stdout);
665 print_num(counters.bcnt, (format|FMT_NOTABLE));
666 fputs("bytes", stdout);
667 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000668 printf(")\n");
669 } else {
670 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000671 if (!iptc_get_references(&refs, chain, handle))
672 printf(" (ERROR obtaining refs)\n");
673 else
674 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000675 }
676
677 if (format & FMT_LINENUMBERS)
678 printf(FMT("%-4s ", "%s "), "num");
679 if (!(format & FMT_NOCOUNTS)) {
680 if (format & FMT_KILOMEGAGIGA) {
681 printf(FMT("%5s ","%s "), "pkts");
682 printf(FMT("%5s ","%s "), "bytes");
683 } else {
684 printf(FMT("%8s ","%s "), "pkts");
685 printf(FMT("%10s ","%s "), "bytes");
686 }
687 }
688 if (!(format & FMT_NOTARGET))
689 printf(FMT("%-9s ","%s "), "target");
690 fputs(" prot ", stdout);
691 if (format & FMT_OPTIONS)
692 fputs("opt", stdout);
693 if (format & FMT_VIA) {
694 printf(FMT(" %-6s ","%s "), "in");
695 printf(FMT("%-6s ","%s "), "out");
696 }
697 printf(FMT(" %-19s ","%s "), "source");
698 printf(FMT(" %-19s "," %s "), "destination");
699 printf("\n");
700}
701
Marc Bouchere6869a82000-03-20 06:03:29 +0000702
703static int
704print_match(const struct ipt_entry_match *m,
705 const struct ipt_ip *ip,
706 int numeric)
707{
Martin Josefsson78cafda2004-02-02 20:01:18 +0000708 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000709
710 if (match) {
711 if (match->print)
712 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000713 else
Rusty Russellb039b022000-09-01 06:04:05 +0000714 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000715 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000716 if (m->u.user.name[0])
717 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000718 }
719 /* Don't stop iterating. */
720 return 0;
721}
722
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100723/* e is called `fw' here for historical reasons */
Marc Bouchere6869a82000-03-20 06:03:29 +0000724static void
725print_firewall(const struct ipt_entry *fw,
726 const char *targname,
727 unsigned int num,
728 unsigned int format,
729 const iptc_handle_t handle)
730{
731 struct iptables_target *target = NULL;
732 const struct ipt_entry_target *t;
733 u_int8_t flags;
734 char buf[BUFSIZ];
735
Marc Bouchere6869a82000-03-20 06:03:29 +0000736 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +0000737 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000738 else
Rusty Russell52a51492000-05-02 16:44:29 +0000739 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000740
741 t = ipt_get_target((struct ipt_entry *)fw);
742 flags = fw->ip.flags;
743
744 if (format & FMT_LINENUMBERS)
745 printf(FMT("%-4u ", "%u "), num+1);
746
747 if (!(format & FMT_NOCOUNTS)) {
748 print_num(fw->counters.pcnt, format);
749 print_num(fw->counters.bcnt, format);
750 }
751
752 if (!(format & FMT_NOTARGET))
753 printf(FMT("%-9s ", "%s "), targname);
754
755 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
756 {
Rusty Russell28381a42000-05-10 00:19:50 +0000757 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000758 if (pname)
759 printf(FMT("%-5s", "%s "), pname);
760 else
761 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
762 }
763
764 if (format & FMT_OPTIONS) {
765 if (format & FMT_NOTABLE)
766 fputs("opt ", stdout);
767 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
768 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
769 fputc(' ', stdout);
770 }
771
772 if (format & FMT_VIA) {
773 char iface[IFNAMSIZ+2];
774
775 if (fw->ip.invflags & IPT_INV_VIA_IN) {
776 iface[0] = '!';
777 iface[1] = '\0';
778 }
779 else iface[0] = '\0';
780
781 if (fw->ip.iniface[0] != '\0') {
782 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000783 }
784 else if (format & FMT_NUMERIC) strcat(iface, "*");
785 else strcat(iface, "any");
786 printf(FMT(" %-6s ","in %s "), iface);
787
788 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
789 iface[0] = '!';
790 iface[1] = '\0';
791 }
792 else iface[0] = '\0';
793
794 if (fw->ip.outiface[0] != '\0') {
795 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000796 }
797 else if (format & FMT_NUMERIC) strcat(iface, "*");
798 else strcat(iface, "any");
799 printf(FMT("%-6s ","out %s "), iface);
800 }
801
802 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
803 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
804 printf(FMT("%-19s ","%s "), "anywhere");
805 else {
806 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000807 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.src));
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.src));
810 strcat(buf, ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000811 printf(FMT("%-19s ","%s "), buf);
812 }
813
814 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
815 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000816 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000817 else {
818 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000819 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000820 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000821 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.dst));
822 strcat(buf, ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000823 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000824 }
825
826 if (format & FMT_NOTABLE)
827 fputs(" ", stdout);
828
Harald Welte72bd87e2005-11-24 17:04:05 +0000829#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000830 if(fw->ip.flags & IPT_F_GOTO)
831 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000832#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000833
Marc Bouchere6869a82000-03-20 06:03:29 +0000834 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
835
836 if (target) {
837 if (target->print)
838 /* Print the target information. */
839 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000840 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000841 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000842 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000843
844 if (!(format & FMT_NONEWLINE))
845 fputc('\n', stdout);
846}
847
848static void
849print_firewall_line(const struct ipt_entry *fw,
850 const iptc_handle_t h)
851{
852 struct ipt_entry_target *t;
853
854 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000855 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000856}
857
858static int
859append_entry(const ipt_chainlabel chain,
860 struct ipt_entry *fw,
861 unsigned int nsaddrs,
862 const struct in_addr saddrs[],
863 unsigned int ndaddrs,
864 const struct in_addr daddrs[],
865 int verbose,
866 iptc_handle_t *handle)
867{
868 unsigned int i, j;
869 int ret = 1;
870
871 for (i = 0; i < nsaddrs; i++) {
872 fw->ip.src.s_addr = saddrs[i].s_addr;
873 for (j = 0; j < ndaddrs; j++) {
874 fw->ip.dst.s_addr = daddrs[j].s_addr;
875 if (verbose)
876 print_firewall_line(fw, *handle);
877 ret &= iptc_append_entry(chain, fw, handle);
878 }
879 }
880
881 return ret;
882}
883
884static int
885replace_entry(const ipt_chainlabel chain,
886 struct ipt_entry *fw,
887 unsigned int rulenum,
888 const struct in_addr *saddr,
889 const struct in_addr *daddr,
890 int verbose,
891 iptc_handle_t *handle)
892{
893 fw->ip.src.s_addr = saddr->s_addr;
894 fw->ip.dst.s_addr = daddr->s_addr;
895
896 if (verbose)
897 print_firewall_line(fw, *handle);
898 return iptc_replace_entry(chain, fw, rulenum, handle);
899}
900
901static int
902insert_entry(const ipt_chainlabel chain,
903 struct ipt_entry *fw,
904 unsigned int rulenum,
905 unsigned int nsaddrs,
906 const struct in_addr saddrs[],
907 unsigned int ndaddrs,
908 const struct in_addr daddrs[],
909 int verbose,
910 iptc_handle_t *handle)
911{
912 unsigned int i, j;
913 int ret = 1;
914
915 for (i = 0; i < nsaddrs; i++) {
916 fw->ip.src.s_addr = saddrs[i].s_addr;
917 for (j = 0; j < ndaddrs; j++) {
918 fw->ip.dst.s_addr = daddrs[j].s_addr;
919 if (verbose)
920 print_firewall_line(fw, *handle);
921 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
922 }
923 }
924
925 return ret;
926}
927
Rusty Russell2e0a3212000-04-19 11:23:18 +0000928static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000929make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000930{
931 /* Establish mask for comparison */
932 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000933 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000934 unsigned char *mask, *mptr;
935
936 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000937 for (matchp = matches; matchp; matchp = matchp->next)
938 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000939
Rusty Russell9e1d2142000-04-23 09:11:12 +0000940 mask = fw_calloc(1, size
Rusty Russell73f72f52000-07-03 10:17:57 +0000941 + IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000942 + xtables_targets->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000943
Rusty Russell9e1d2142000-04-23 09:11:12 +0000944 memset(mask, 0xFF, sizeof(struct ipt_entry));
945 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000946
Martin Josefsson78cafda2004-02-02 20:01:18 +0000947 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000948 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000949 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000950 + matchp->match->userspacesize);
951 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000952 }
953
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000954 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000955 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000956 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000957
958 return mask;
959}
960
Marc Bouchere6869a82000-03-20 06:03:29 +0000961static int
962delete_entry(const ipt_chainlabel chain,
963 struct ipt_entry *fw,
964 unsigned int nsaddrs,
965 const struct in_addr saddrs[],
966 unsigned int ndaddrs,
967 const struct in_addr daddrs[],
968 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +0000969 iptc_handle_t *handle,
970 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000971{
972 unsigned int i, j;
973 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000974 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000975
Martin Josefsson78cafda2004-02-02 20:01:18 +0000976 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000977 for (i = 0; i < nsaddrs; i++) {
978 fw->ip.src.s_addr = saddrs[i].s_addr;
979 for (j = 0; j < ndaddrs; j++) {
980 fw->ip.dst.s_addr = daddrs[j].s_addr;
981 if (verbose)
982 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000983 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000984 }
985 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000986 free(mask);
987
Marc Bouchere6869a82000-03-20 06:03:29 +0000988 return ret;
989}
990
Harald Welteae1ff9f2000-12-01 14:26:20 +0000991int
Marc Bouchere6869a82000-03-20 06:03:29 +0000992for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +0000993 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000994{
995 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000996 const char *chain;
997 char *chains;
998 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +0000999
Rusty Russell9e1d2142000-04-23 09:11:12 +00001000 chain = iptc_first_chain(handle);
1001 while (chain) {
1002 chaincount++;
1003 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001004 }
1005
Rusty Russell9e1d2142000-04-23 09:11:12 +00001006 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1007 i = 0;
1008 chain = iptc_first_chain(handle);
1009 while (chain) {
1010 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1011 i++;
1012 chain = iptc_next_chain(handle);
1013 }
1014
1015 for (i = 0; i < chaincount; i++) {
1016 if (!builtinstoo
1017 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001018 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001019 continue;
1020 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1021 }
1022
1023 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001024 return ret;
1025}
1026
Harald Welteae1ff9f2000-12-01 14:26:20 +00001027int
Marc Bouchere6869a82000-03-20 06:03:29 +00001028flush_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(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001033
1034 if (verbose)
1035 fprintf(stdout, "Flushing chain `%s'\n", chain);
1036 return iptc_flush_entries(chain, handle);
1037}
Marc Bouchere6869a82000-03-20 06:03:29 +00001038
1039static int
1040zero_entries(const ipt_chainlabel chain, int verbose,
1041 iptc_handle_t *handle)
1042{
1043 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001044 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001045
Marc Bouchere6869a82000-03-20 06:03:29 +00001046 if (verbose)
1047 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1048 return iptc_zero_entries(chain, handle);
1049}
1050
Harald Welteae1ff9f2000-12-01 14:26:20 +00001051int
Marc Bouchere6869a82000-03-20 06:03:29 +00001052delete_chain(const ipt_chainlabel chain, int verbose,
1053 iptc_handle_t *handle)
1054{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001055 if (!chain)
1056 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001057
1058 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001059 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001060 return iptc_delete_chain(chain, handle);
1061}
1062
1063static int
1064list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1065 int expanded, int linenumbers, iptc_handle_t *handle)
1066{
1067 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001068 unsigned int format;
1069 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001070
1071 format = FMT_OPTIONS;
1072 if (!verbose)
1073 format |= FMT_NOCOUNTS;
1074 else
1075 format |= FMT_VIA;
1076
1077 if (numeric)
1078 format |= FMT_NUMERIC;
1079
1080 if (!expanded)
1081 format |= FMT_KILOMEGAGIGA;
1082
1083 if (linenumbers)
1084 format |= FMT_LINENUMBERS;
1085
Rusty Russell9e1d2142000-04-23 09:11:12 +00001086 for (this = iptc_first_chain(handle);
1087 this;
1088 this = iptc_next_chain(handle)) {
1089 const struct ipt_entry *i;
1090 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001091
Marc Bouchere6869a82000-03-20 06:03:29 +00001092 if (chain && strcmp(chain, this) != 0)
1093 continue;
1094
1095 if (found) printf("\n");
1096
1097 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001098 i = iptc_first_rule(this, handle);
1099
1100 num = 0;
1101 while (i) {
1102 print_firewall(i,
1103 iptc_get_target(i, handle),
1104 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001105 format,
1106 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001107 i = iptc_next_rule(i, handle);
1108 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001109 found = 1;
1110 }
1111
1112 errno = ENOENT;
1113 return found;
1114}
1115
1116static struct ipt_entry *
1117generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001118 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001119 struct ipt_entry_target *target)
1120{
1121 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001122 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001123 struct ipt_entry *e;
1124
1125 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001126 for (matchp = matches; matchp; matchp = matchp->next)
1127 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001128
Rusty Russell228e98d2000-04-27 10:28:06 +00001129 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001130 *e = *fw;
1131 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001132 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001133
1134 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001135 for (matchp = matches; matchp; matchp = matchp->next) {
1136 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1137 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001138 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001139 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001140
1141 return e;
1142}
1143
Jan Engelhardt33690a12008-02-11 00:54:00 +01001144static void clear_rule_matches(struct iptables_rule_match **matches)
Martin Josefsson78cafda2004-02-02 20:01:18 +00001145{
1146 struct iptables_rule_match *matchp, *tmp;
1147
1148 for (matchp = *matches; matchp;) {
1149 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001150 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001151 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001152 matchp->match->m = NULL;
1153 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001154 if (matchp->match == matchp->match->next) {
1155 free(matchp->match);
1156 matchp->match = NULL;
1157 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001158 free(matchp);
1159 matchp = tmp;
1160 }
1161
1162 *matches = NULL;
1163}
1164
Rusty Russell3aef54d2005-01-03 03:48:40 +00001165static void set_revision(char *name, u_int8_t revision)
1166{
1167 /* Old kernel sources don't have ".revision" field,
1168 but we stole a byte from name. */
1169 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1170 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1171}
1172
Phil Oester8cf65912005-09-19 15:00:33 +00001173void
1174get_kernel_version(void) {
1175 static struct utsname uts;
1176 int x = 0, y = 0, z = 0;
1177
1178 if (uname(&uts) == -1) {
1179 fprintf(stderr, "Unable to retrieve kernel version.\n");
1180 free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001181 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001182 }
1183
1184 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1185 kernel_version = LINUX_VERSION(x, y, z);
1186}
1187
Marc Bouchere6869a82000-03-20 06:03:29 +00001188int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1189{
1190 struct ipt_entry fw, *e = NULL;
1191 int invert = 0;
1192 unsigned int nsaddrs = 0, ndaddrs = 0;
1193 struct in_addr *saddrs = NULL, *daddrs = NULL;
1194
1195 int c, verbose = 0;
1196 const char *chain = NULL;
1197 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1198 const char *policy = NULL, *newname = NULL;
1199 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001200 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001201 int ret = 1;
1202 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001203 struct iptables_rule_match *matches = NULL;
1204 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001205 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001206 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001207 const char *jumpto = "";
1208 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001209 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001210 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001211
1212 memset(&fw, 0, sizeof(fw));
1213
Harald Welteae1ff9f2000-12-01 14:26:20 +00001214 /* re-set optind to 0 in case do_command gets called
1215 * a second time */
1216 optind = 0;
1217
1218 /* clear mflags in case do_command gets called a second time
1219 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001220 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001221 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001222
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001223 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001224 t->tflags = 0;
1225 t->used = 0;
1226 }
1227
Marc Bouchere6869a82000-03-20 06:03:29 +00001228 /* Suppress error messages: we may add new options if we
1229 demand-load a protocol. */
1230 opterr = 0;
1231
1232 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001233 "-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 +00001234 opts, NULL)) != -1) {
1235 switch (c) {
1236 /*
1237 * Command selection
1238 */
1239 case 'A':
1240 add_command(&command, CMD_APPEND, CMD_NONE,
1241 invert);
1242 chain = optarg;
1243 break;
1244
1245 case 'D':
1246 add_command(&command, CMD_DELETE, CMD_NONE,
1247 invert);
1248 chain = optarg;
1249 if (optind < argc && argv[optind][0] != '-'
1250 && argv[optind][0] != '!') {
1251 rulenum = parse_rulenumber(argv[optind++]);
1252 command = CMD_DELETE_NUM;
1253 }
1254 break;
1255
Marc Bouchere6869a82000-03-20 06:03:29 +00001256 case 'R':
1257 add_command(&command, CMD_REPLACE, CMD_NONE,
1258 invert);
1259 chain = optarg;
1260 if (optind < argc && argv[optind][0] != '-'
1261 && argv[optind][0] != '!')
1262 rulenum = parse_rulenumber(argv[optind++]);
1263 else
1264 exit_error(PARAMETER_PROBLEM,
1265 "-%c requires a rule number",
1266 cmd2char(CMD_REPLACE));
1267 break;
1268
1269 case 'I':
1270 add_command(&command, CMD_INSERT, CMD_NONE,
1271 invert);
1272 chain = optarg;
1273 if (optind < argc && argv[optind][0] != '-'
1274 && argv[optind][0] != '!')
1275 rulenum = parse_rulenumber(argv[optind++]);
1276 else rulenum = 1;
1277 break;
1278
1279 case 'L':
1280 add_command(&command, CMD_LIST, CMD_ZERO,
1281 invert);
1282 if (optarg) chain = optarg;
1283 else if (optind < argc && argv[optind][0] != '-'
1284 && argv[optind][0] != '!')
1285 chain = argv[optind++];
1286 break;
1287
1288 case 'F':
1289 add_command(&command, CMD_FLUSH, CMD_NONE,
1290 invert);
1291 if (optarg) chain = optarg;
1292 else if (optind < argc && argv[optind][0] != '-'
1293 && argv[optind][0] != '!')
1294 chain = argv[optind++];
1295 break;
1296
1297 case 'Z':
1298 add_command(&command, CMD_ZERO, CMD_LIST,
1299 invert);
1300 if (optarg) chain = optarg;
1301 else if (optind < argc && argv[optind][0] != '-'
1302 && argv[optind][0] != '!')
1303 chain = argv[optind++];
1304 break;
1305
1306 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001307 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001308 exit_error(PARAMETER_PROBLEM,
1309 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001310 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001311 if (find_target(optarg, TRY_LOAD))
1312 exit_error(PARAMETER_PROBLEM,
1313 "chain name may not clash "
1314 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001315 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1316 invert);
1317 chain = optarg;
1318 break;
1319
1320 case 'X':
1321 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1322 invert);
1323 if (optarg) chain = optarg;
1324 else if (optind < argc && argv[optind][0] != '-'
1325 && argv[optind][0] != '!')
1326 chain = argv[optind++];
1327 break;
1328
1329 case 'E':
1330 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1331 invert);
1332 chain = optarg;
1333 if (optind < argc && argv[optind][0] != '-'
1334 && argv[optind][0] != '!')
1335 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001336 else
1337 exit_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001338 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001339 "new-chain-name",
1340 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001341 break;
1342
1343 case 'P':
1344 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1345 invert);
1346 chain = optarg;
1347 if (optind < argc && argv[optind][0] != '-'
1348 && argv[optind][0] != '!')
1349 policy = argv[optind++];
1350 else
1351 exit_error(PARAMETER_PROBLEM,
1352 "-%c requires a chain and a policy",
1353 cmd2char(CMD_SET_POLICY));
1354 break;
1355
1356 case 'h':
1357 if (!optarg)
1358 optarg = argv[optind];
1359
Rusty Russell2e0a3212000-04-19 11:23:18 +00001360 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001361 if (!matches && protocol)
1362 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001363
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001364 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001365
1366 /*
1367 * Option selection
1368 */
1369 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001370 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001371 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1372 invert);
1373
1374 /* Canonicalize into lower case */
1375 for (protocol = argv[optind-1]; *protocol; protocol++)
1376 *protocol = tolower(*protocol);
1377
1378 protocol = argv[optind-1];
1379 fw.ip.proto = parse_protocol(protocol);
1380
1381 if (fw.ip.proto == 0
1382 && (fw.ip.invflags & IPT_INV_PROTO))
1383 exit_error(PARAMETER_PROBLEM,
1384 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001385 break;
1386
1387 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001388 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001389 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1390 invert);
1391 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001392 break;
1393
1394 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001395 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001396 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1397 invert);
1398 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001399 break;
1400
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001401#ifdef IPT_F_GOTO
1402 case 'g':
1403 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1404 invert);
1405 fw.ip.flags |= IPT_F_GOTO;
1406 jumpto = parse_target(optarg);
1407 break;
1408#endif
1409
Marc Bouchere6869a82000-03-20 06:03:29 +00001410 case 'j':
1411 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1412 invert);
1413 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001414 /* TRY_LOAD (may be chain name) */
1415 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001416
1417 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001418 size_t size;
1419
Rusty Russell73f72f52000-07-03 10:17:57 +00001420 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1421 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001422
Rusty Russell2e0a3212000-04-19 11:23:18 +00001423 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001424 target->t->u.target_size = size;
1425 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001426 set_revision(target->t->u.user.name,
1427 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001428 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001429 target->init(target->t);
Max Kellermann5b76f682008-01-29 13:42:48 +00001430 opts = merge_options(opts,
1431 target->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001432 &target->option_offset);
1433 if (opts == NULL)
1434 exit_error(OTHER_PROBLEM,
1435 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001436 }
1437 break;
1438
1439
1440 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001441 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001442 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1443 invert);
1444 parse_interface(argv[optind-1],
1445 fw.ip.iniface,
1446 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001447 break;
1448
1449 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001450 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001451 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1452 invert);
1453 parse_interface(argv[optind-1],
1454 fw.ip.outiface,
1455 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001456 break;
1457
1458 case 'f':
1459 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1460 invert);
1461 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001462 break;
1463
1464 case 'v':
1465 if (!verbose)
1466 set_option(&options, OPT_VERBOSE,
1467 &fw.ip.invflags, invert);
1468 verbose++;
1469 break;
1470
Rusty Russell52a51492000-05-02 16:44:29 +00001471 case 'm': {
1472 size_t size;
1473
Marc Bouchere6869a82000-03-20 06:03:29 +00001474 if (invert)
1475 exit_error(PARAMETER_PROBLEM,
1476 "unexpected ! flag before --match");
1477
Martin Josefsson78cafda2004-02-02 20:01:18 +00001478 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001479 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1480 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001481 m->m = fw_calloc(1, size);
1482 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001483 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001484 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001485 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001486 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001487 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001488 /* Merge options for non-cloned matches */
Max Kellermann5b76f682008-01-29 13:42:48 +00001489 opts = merge_options(opts,
1490 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001491 &m->option_offset);
1492 if (opts == NULL)
1493 exit_error(OTHER_PROBLEM,
1494 "can't alloc memory!");
1495 }
Rusty Russell52a51492000-05-02 16:44:29 +00001496 }
1497 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001498
1499 case 'n':
1500 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1501 invert);
1502 break;
1503
1504 case 't':
1505 if (invert)
1506 exit_error(PARAMETER_PROBLEM,
1507 "unexpected ! flag before --table");
1508 *table = argv[optind-1];
1509 break;
1510
1511 case 'x':
1512 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1513 invert);
1514 break;
1515
1516 case 'V':
1517 if (invert)
1518 printf("Not %s ;-)\n", program_version);
1519 else
1520 printf("%s v%s\n",
1521 program_name, program_version);
1522 exit(0);
1523
1524 case '0':
1525 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1526 invert);
1527 break;
1528
Harald Welte82dd2ec2000-12-19 05:18:15 +00001529 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001530 modprobe_program = optarg;
Harald Welte82dd2ec2000-12-19 05:18:15 +00001531 break;
1532
Harald Welteccd49e52001-01-23 22:54:34 +00001533 case 'c':
1534
1535 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1536 invert);
1537 pcnt = optarg;
1538 if (optind < argc && argv[optind][0] != '-'
1539 && argv[optind][0] != '!')
1540 bcnt = argv[optind++];
1541 else
1542 exit_error(PARAMETER_PROBLEM,
1543 "-%c requires packet and byte counter",
1544 opt2char(OPT_COUNTERS));
1545
Patrick McHardy875441e2007-10-17 08:48:58 +00001546 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001547 exit_error(PARAMETER_PROBLEM,
1548 "-%c packet counter not numeric",
1549 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001550 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001551
Patrick McHardy875441e2007-10-17 08:48:58 +00001552 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001553 exit_error(PARAMETER_PROBLEM,
1554 "-%c byte counter not numeric",
1555 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001556 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001557 break;
1558
1559
Marc Bouchere6869a82000-03-20 06:03:29 +00001560 case 1: /* non option */
1561 if (optarg[0] == '!' && optarg[1] == '\0') {
1562 if (invert)
1563 exit_error(PARAMETER_PROBLEM,
1564 "multiple consecutive ! not"
1565 " allowed");
1566 invert = TRUE;
1567 optarg[0] = '\0';
1568 continue;
1569 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001570 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001571 exit_tryhelp(2);
1572
1573 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001574 if (!target
1575 || !(target->parse(c - target->option_offset,
1576 argv, invert,
1577 &target->tflags,
1578 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001579 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001580 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001581 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001582 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001583 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001584 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001585 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001586 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001587 break;
1588 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001589 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001590
1591 /* If you listen carefully, you can
1592 actually hear this code suck. */
1593
1594 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001595 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001596 * parameter, that has not been parsed yet,
1597 * it's not an option of an explicitly loaded
1598 * match or a target. However, we support
1599 * implicit loading of the protocol match
1600 * extension. '-p tcp' means 'l4 proto 6' and
1601 * at the same time 'load tcp protocol match on
1602 * demand if we specify --dport'.
1603 *
1604 * To make this work, we need to make sure:
1605 * - the parameter has not been parsed by
1606 * a match (m above)
1607 * - a protocol has been specified
1608 * - the protocol extension has not been
1609 * loaded yet, or is loaded and unused
1610 * [think of iptables-restore!]
1611 * - the protocol extension can be successively
1612 * loaded
1613 */
1614 if (m == NULL
1615 && protocol
1616 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001617 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001618 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001619 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001620 && (proto_used == 0))
1621 )
1622 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001623 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001624 /* Try loading protocol */
1625 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001626
Harald Welte2d86b772002-08-26 12:21:44 +00001627 proto_used = 1;
1628
1629 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1630 + m->size;
1631
1632 m->m = fw_calloc(1, size);
1633 m->m->u.match_size = size;
1634 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001635 set_revision(m->m->u.user.name,
1636 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001637 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001638 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001639
1640 opts = merge_options(opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001641 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001642 &m->option_offset);
1643 if (opts == NULL)
1644 exit_error(OTHER_PROBLEM,
1645 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001646
1647 optind--;
1648 continue;
1649 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001650 if (!m)
1651 exit_error(PARAMETER_PROBLEM,
1652 "Unknown arg `%s'",
1653 argv[optind-1]);
1654 }
1655 }
1656 invert = FALSE;
1657 }
1658
Martin Josefsson78cafda2004-02-02 20:01:18 +00001659 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001660 if (matchp->match->final_check != NULL)
1661 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001662
Jan Engelhardt830132a2007-10-04 16:24:50 +00001663 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001664 target->final_check(target->tflags);
1665
1666 /* Fix me: must put inverse options checking here --MN */
1667
1668 if (optind < argc)
1669 exit_error(PARAMETER_PROBLEM,
1670 "unknown arguments found on commandline");
1671 if (!command)
1672 exit_error(PARAMETER_PROBLEM, "no command specified");
1673 if (invert)
1674 exit_error(PARAMETER_PROBLEM,
1675 "nothing appropriate following !");
1676
Harald Welte6336bfd2002-05-07 14:41:43 +00001677 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001678 if (!(options & OPT_DESTINATION))
1679 dhostnetworkmask = "0.0.0.0/0";
1680 if (!(options & OPT_SOURCE))
1681 shostnetworkmask = "0.0.0.0/0";
1682 }
1683
1684 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001685 ipparse_hostnetworkmask(shostnetworkmask, &saddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001686 &fw.ip.smsk, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001687
1688 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001689 ipparse_hostnetworkmask(dhostnetworkmask, &daddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001690 &fw.ip.dmsk, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001691
1692 if ((nsaddrs > 1 || ndaddrs > 1) &&
1693 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1694 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1695 " source or destination IP addresses");
1696
Marc Bouchere6869a82000-03-20 06:03:29 +00001697 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1698 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1699 "specify a unique address");
1700
1701 generic_opt_check(command, options);
1702
1703 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1704 exit_error(PARAMETER_PROBLEM,
1705 "chain name `%s' too long (must be under %i chars)",
1706 chain, IPT_FUNCTION_MAXNAMELEN);
1707
Harald Welteae1ff9f2000-12-01 14:26:20 +00001708 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001709 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001710 *handle = iptc_init(*table);
1711
Rusty Russell8beb0492004-12-22 00:37:10 +00001712 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001713 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001714 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001715
Marc Bouchere6869a82000-03-20 06:03:29 +00001716 if (!*handle)
1717 exit_error(VERSION_PROBLEM,
1718 "can't initialize iptables table `%s': %s",
1719 *table, iptc_strerror(errno));
1720
Harald Welte6336bfd2002-05-07 14:41:43 +00001721 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001722 || command == CMD_DELETE
1723 || command == CMD_INSERT
1724 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001725 if (strcmp(chain, "PREROUTING") == 0
1726 || strcmp(chain, "INPUT") == 0) {
1727 /* -o not valid with incoming packets. */
1728 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001729 exit_error(PARAMETER_PROBLEM,
1730 "Can't use -%c with %s\n",
1731 opt2char(OPT_VIANAMEOUT),
1732 chain);
1733 }
1734
Rusty Russella4860fd2000-06-17 16:13:02 +00001735 if (strcmp(chain, "POSTROUTING") == 0
1736 || strcmp(chain, "OUTPUT") == 0) {
1737 /* -i not valid with outgoing packets */
1738 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001739 exit_error(PARAMETER_PROBLEM,
1740 "Can't use -%c with %s\n",
1741 opt2char(OPT_VIANAMEIN),
1742 chain);
1743 }
1744
1745 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001746 fprintf(stderr,
1747 "Warning: using chain %s, not extension\n",
1748 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001749
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001750 if (target->t)
1751 free(target->t);
1752
Marc Bouchere6869a82000-03-20 06:03:29 +00001753 target = NULL;
1754 }
1755
1756 /* If they didn't specify a target, or it's a chain
1757 name, use standard. */
1758 if (!target
1759 && (strlen(jumpto) == 0
1760 || iptc_is_chain(jumpto, *handle))) {
1761 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001762
Rusty Russell52a51492000-05-02 16:44:29 +00001763 target = find_target(IPT_STANDARD_TARGET,
1764 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001765
1766 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001767 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001768 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001769 target->t->u.target_size = size;
1770 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00001771 if (!iptc_is_chain(jumpto, *handle))
1772 set_revision(target->t->u.user.name,
1773 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001774 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001775 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001776 }
1777
Rusty Russell7e53bf92000-03-20 07:03:28 +00001778 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001779 /* it is no chain, and we can't load a plugin.
1780 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001781 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001782 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001783#ifdef IPT_F_GOTO
1784 if (fw.ip.flags & IPT_F_GOTO)
1785 exit_error(PARAMETER_PROBLEM,
1786 "goto '%s' is not a chain\n", jumpto);
1787#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00001788 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001789 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001790 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001791 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001792 }
1793 }
1794
1795 switch (command) {
1796 case CMD_APPEND:
1797 ret = append_entry(chain, e,
1798 nsaddrs, saddrs, ndaddrs, daddrs,
1799 options&OPT_VERBOSE,
1800 handle);
1801 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001802 case CMD_DELETE:
1803 ret = delete_entry(chain, e,
1804 nsaddrs, saddrs, ndaddrs, daddrs,
1805 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001806 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001807 break;
1808 case CMD_DELETE_NUM:
1809 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
1810 break;
1811 case CMD_REPLACE:
1812 ret = replace_entry(chain, e, rulenum - 1,
1813 saddrs, daddrs, options&OPT_VERBOSE,
1814 handle);
1815 break;
1816 case CMD_INSERT:
1817 ret = insert_entry(chain, e, rulenum - 1,
1818 nsaddrs, saddrs, ndaddrs, daddrs,
1819 options&OPT_VERBOSE,
1820 handle);
1821 break;
1822 case CMD_LIST:
1823 ret = list_entries(chain,
1824 options&OPT_VERBOSE,
1825 options&OPT_NUMERIC,
1826 options&OPT_EXPANDED,
1827 options&OPT_LINENUMBERS,
1828 handle);
1829 break;
1830 case CMD_FLUSH:
1831 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1832 break;
1833 case CMD_ZERO:
1834 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1835 break;
1836 case CMD_LIST|CMD_ZERO:
1837 ret = list_entries(chain,
1838 options&OPT_VERBOSE,
1839 options&OPT_NUMERIC,
1840 options&OPT_EXPANDED,
1841 options&OPT_LINENUMBERS,
1842 handle);
1843 if (ret)
1844 ret = zero_entries(chain,
1845 options&OPT_VERBOSE, handle);
1846 break;
1847 case CMD_NEW_CHAIN:
1848 ret = iptc_create_chain(chain, handle);
1849 break;
1850 case CMD_DELETE_CHAIN:
1851 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1852 break;
1853 case CMD_RENAME_CHAIN:
1854 ret = iptc_rename_chain(chain, newname, handle);
1855 break;
1856 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001857 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001858 break;
1859 default:
1860 /* We should never reach this... */
1861 exit_tryhelp(2);
1862 }
1863
1864 if (verbose > 1)
1865 dump_entries(*handle);
1866
Martin Josefsson78cafda2004-02-02 20:01:18 +00001867 clear_rule_matches(&matches);
1868
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001869 if (e != NULL) {
1870 free(e);
1871 e = NULL;
1872 }
1873
keso6997cdf2004-07-04 15:20:53 +00001874 free(saddrs);
1875 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00001876 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001877
Marc Bouchere6869a82000-03-20 06:03:29 +00001878 return ret;
1879}