blob: 43e36531c3e033a27b5ecf9505295cd5ad8b4dc6 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welted4ab5ad2002-08-07 09:07:24 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Marc Bouchere6869a82000-03-20 06:03:29 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000034#include <ctype.h>
35#include <stdarg.h>
36#include <limits.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000037#include <unistd.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000038#include <iptables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Harald Welte82dd2ec2000-12-19 05:18:15 +000040#include <fcntl.h>
Phil Oester8cf65912005-09-19 15:00:33 +000041#include <sys/utsname.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000042
43#ifndef TRUE
44#define TRUE 1
45#endif
46#ifndef FALSE
47#define FALSE 0
48#endif
49
Marc Bouchere6869a82000-03-20 06:03:29 +000050#define FMT_NUMERIC 0x0001
51#define FMT_NOCOUNTS 0x0002
52#define FMT_KILOMEGAGIGA 0x0004
53#define FMT_OPTIONS 0x0008
54#define FMT_NOTABLE 0x0010
55#define FMT_NOTARGET 0x0020
56#define FMT_VIA 0x0040
57#define FMT_NONEWLINE 0x0080
58#define FMT_LINENUMBERS 0x0100
59
60#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
61 | FMT_NUMERIC | FMT_NOTABLE)
62#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
63
64
65#define CMD_NONE 0x0000U
66#define CMD_INSERT 0x0001U
67#define CMD_DELETE 0x0002U
68#define CMD_DELETE_NUM 0x0004U
69#define CMD_REPLACE 0x0008U
70#define CMD_APPEND 0x0010U
71#define CMD_LIST 0x0020U
72#define CMD_FLUSH 0x0040U
73#define CMD_ZERO 0x0080U
74#define CMD_NEW_CHAIN 0x0100U
75#define CMD_DELETE_CHAIN 0x0200U
76#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000077#define CMD_RENAME_CHAIN 0x0800U
Marc Bouchere6869a82000-03-20 06:03:29 +000078#define NUMBER_OF_CMD 13
79static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Harald Welte6336bfd2002-05-07 14:41:43 +000080 'N', 'X', 'P', 'E' };
Marc Bouchere6869a82000-03-20 06:03:29 +000081
82#define OPTION_OFFSET 256
83
84#define OPT_NONE 0x00000U
85#define OPT_NUMERIC 0x00001U
86#define OPT_SOURCE 0x00002U
87#define OPT_DESTINATION 0x00004U
88#define OPT_PROTOCOL 0x00008U
89#define OPT_JUMP 0x00010U
90#define OPT_VERBOSE 0x00020U
91#define OPT_EXPANDED 0x00040U
92#define OPT_VIANAMEIN 0x00080U
93#define OPT_VIANAMEOUT 0x00100U
94#define OPT_FRAGMENT 0x00200U
95#define OPT_LINENUMBERS 0x00400U
Harald Welteccd49e52001-01-23 22:54:34 +000096#define OPT_COUNTERS 0x00800U
97#define NUMBER_OF_OPT 12
Marc Bouchere6869a82000-03-20 06:03:29 +000098static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +000099= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', 'f', '0', 'c'};
Marc Bouchere6869a82000-03-20 06:03:29 +0000100
101static struct option original_opts[] = {
Patrick McHardy0b639362007-09-08 16:00:01 +0000102 { "append", 1, NULL, 'A' },
103 { "delete", 1, NULL, 'D' },
104 { "insert", 1, NULL, 'I' },
105 { "replace", 1, NULL, 'R' },
106 { "list", 2, NULL, 'L' },
107 { "flush", 2, NULL, 'F' },
108 { "zero", 2, NULL, 'Z' },
109 { "new-chain", 1, NULL, 'N' },
110 { "delete-chain", 2, NULL, 'X' },
111 { "rename-chain", 1, NULL, 'E' },
112 { "policy", 1, NULL, 'P' },
113 { "source", 1, NULL, 's' },
114 { "destination", 1, NULL, 'd' },
115 { "src", 1, NULL, 's' }, /* synonym */
116 { "dst", 1, NULL, 'd' }, /* synonym */
117 { "protocol", 1, NULL, 'p' },
118 { "in-interface", 1, NULL, 'i' },
119 { "jump", 1, NULL, 'j' },
120 { "table", 1, NULL, 't' },
121 { "match", 1, NULL, 'm' },
122 { "numeric", 0, NULL, 'n' },
123 { "out-interface", 1, NULL, 'o' },
124 { "verbose", 0, NULL, 'v' },
125 { "exact", 0, NULL, 'x' },
126 { "fragments", 0, NULL, 'f' },
127 { "version", 0, NULL, 'V' },
128 { "help", 2, NULL, 'h' },
129 { "line-numbers", 0, NULL, '0' },
130 { "modprobe", 1, NULL, 'M' },
131 { "set-counters", 1, NULL, 'c' },
132 { "goto", 1, NULL, 'g' },
133 { }
Marc Bouchere6869a82000-03-20 06:03:29 +0000134};
135
Illes Marci63e90632003-03-03 08:08:37 +0000136/* we need this for iptables-restore. iptables-restore.c sets line to the
137 * current line of the input file, in order to give a more precise error
138 * message. iptables itself doesn't need this, so it is initialized to the
139 * magic number of -1 */
140int line = -1;
141
Marc Bouchere6869a82000-03-20 06:03:29 +0000142static struct option *opts = original_opts;
143static unsigned int global_option_offset = 0;
144
145/* Table of legal combinations of commands and options. If any of the
146 * given commands make an option legal, that option is legal (applies to
147 * CMD_LIST and CMD_ZERO only).
148 * Key:
149 * + compulsory
150 * x illegal
151 * optional
152 */
153
154static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
155/* Well, it's better than "Re: Linux vs FreeBSD" */
156{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157 /* -n -s -d -p -j -v -x -i -o -f --line -c */
158/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
159/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x','x'},
160/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
161/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
162/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ',' ','x',' '},
163/*LIST*/ {' ','x','x','x','x',' ',' ','x','x','x',' ','x'},
164/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
165/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'},
168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x','x'}
Marc Bouchere6869a82000-03-20 06:03:29 +0000170};
171
172static int inverse_for_options[NUMBER_OF_OPT] =
173{
174/* -n */ 0,
175/* -s */ IPT_INV_SRCIP,
176/* -d */ IPT_INV_DSTIP,
177/* -p */ IPT_INV_PROTO,
178/* -j */ 0,
179/* -v */ 0,
180/* -x */ 0,
181/* -i */ IPT_INV_VIA_IN,
182/* -o */ IPT_INV_VIA_OUT,
183/* -f */ IPT_INV_FRAG,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000184/*--line*/ 0,
185/* -c */ 0,
Marc Bouchere6869a82000-03-20 06:03:29 +0000186};
187
188const char *program_version;
189const char *program_name;
190
Phil Oester8cf65912005-09-19 15:00:33 +0000191int kernel_version;
192
Marc Bouchere6869a82000-03-20 06:03:29 +0000193extern void dump_entries(const iptc_handle_t handle);
194
195/* A few hardcoded protocols for 'all' and in case the user has no
196 /etc/protocols */
197struct pprot {
198 char *name;
199 u_int8_t num;
200};
201
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000202struct afinfo afinfo = {
203 .family = AF_INET,
204 .libprefix = "libipt_",
205 .ipproto = IPPROTO_IP,
206 .kmod = "ip_tables",
207 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
208 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
209};
210
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000211/* Primitive headers... */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000212/* defined in netinet/in.h */
213#if 0
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000214#ifndef IPPROTO_ESP
215#define IPPROTO_ESP 50
216#endif
217#ifndef IPPROTO_AH
218#define IPPROTO_AH 51
219#endif
András Kis-Szabó764316a2001-02-26 17:31:20 +0000220#endif
Rusty Russella3e6aaa2000-12-19 04:45:23 +0000221
Marc Bouchere6869a82000-03-20 06:03:29 +0000222static const struct pprot chain_protos[] = {
223 { "tcp", IPPROTO_TCP },
224 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000225 { "udplite", IPPROTO_UDPLITE },
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 { "icmp", IPPROTO_ICMP },
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000227 { "esp", IPPROTO_ESP },
228 { "ah", IPPROTO_AH },
Harald Welte12915232004-02-21 09:20:34 +0000229 { "sctp", IPPROTO_SCTP },
Phil Oesterb7408912007-04-30 00:01:39 +0000230 { "all", 0 },
Marc Bouchere6869a82000-03-20 06:03:29 +0000231};
232
Patrick McHardyJesper Brouerc1eae412006-07-25 01:50:48 +0000233static char *
Rusty Russell28381a42000-05-10 00:19:50 +0000234proto_to_name(u_int8_t proto, int nolookup)
Marc Bouchere6869a82000-03-20 06:03:29 +0000235{
236 unsigned int i;
237
Rusty Russell28381a42000-05-10 00:19:50 +0000238 if (proto && !nolookup) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
Pablo Neira Ayuso267a5702006-11-29 13:32:32 +0000251enum {
252 IPT_DOTTED_ADDR = 0,
253 IPT_DOTTED_MASK
254};
255
Pablo Neiradfdcd642005-05-29 19:05:23 +0000256static void free_opts(int reset_offset)
257{
258 if (opts != original_opts) {
259 free(opts);
260 opts = original_opts;
261 if (reset_offset)
262 global_option_offset = 0;
263 }
264}
265
Patrick McHardy0b639362007-09-08 16:00:01 +0000266static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000267exit_tryhelp(int status)
268{
Maciej Soltysiakedad9bb2003-03-31 12:11:55 +0000269 if (line != -1)
Harald Weltea5bb0a62003-05-03 18:56:19 +0000270 fprintf(stderr, "Error occurred at line: %d\n", line);
Marc Bouchere6869a82000-03-20 06:03:29 +0000271 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
272 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000273 free_opts(1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000274 exit(status);
275}
276
Patrick McHardy0b639362007-09-08 16:00:01 +0000277static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000278exit_printhelp(struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000279{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000280 struct iptables_rule_match *matchp = NULL;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000281 struct iptables_target *t = NULL;
282
Marc Bouchere6869a82000-03-20 06:03:29 +0000283 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000284"Usage: %s -[AD] chain rule-specification [options]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000285" %s -[RI] chain rulenum rule-specification [options]\n"
286" %s -D chain rulenum [options]\n"
287" %s -[LFZ] [chain] [options]\n"
288" %s -[NX] chain\n"
289" %s -E old-chain-name new-chain-name\n"
290" %s -P chain target [options]\n"
291" %s -h (print this help information)\n\n",
292 program_name, program_version, program_name, program_name,
293 program_name, program_name, program_name, program_name,
294 program_name, program_name);
295
296 printf(
297"Commands:\n"
298"Either long or short options are allowed.\n"
299" --append -A chain Append to chain\n"
300" --delete -D chain Delete matching rule from chain\n"
301" --delete -D chain rulenum\n"
302" Delete rule rulenum (1 = first) from chain\n"
303" --insert -I chain [rulenum]\n"
304" Insert in chain as rulenum (default 1=first)\n"
305" --replace -R chain rulenum\n"
306" Replace rule rulenum (1 = first) in chain\n"
307" --list -L [chain] List the rules in a chain or all chains\n"
308" --flush -F [chain] Delete all rules in chain or all chains\n"
309" --zero -Z [chain] Zero counters in chain or all chains\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000310" --new -N chain Create a new user-defined chain\n"
311" --delete-chain\n"
312" -X [chain] Delete a user-defined chain\n"
313" --policy -P chain target\n"
314" Change policy on chain to target\n"
315" --rename-chain\n"
316" -E old-chain new-chain\n"
317" Change chain name, (moving any references)\n"
318
319"Options:\n"
320" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
321" --source -s [!] address[/mask]\n"
322" source specification\n"
323" --destination -d [!] address[/mask]\n"
324" destination specification\n"
325" --in-interface -i [!] input name[+]\n"
326" network interface name ([+] for wildcard)\n"
327" --jump -j target\n"
Rusty Russell363112d2000-08-11 13:49:26 +0000328" target for rule (may load target extension)\n"
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000329#ifdef IPT_F_GOTO
330" --goto -g chain\n"
331" jump to chain with no return\n"
332#endif
Rusty Russell363112d2000-08-11 13:49:26 +0000333" --match -m match\n"
334" extended match (may load extension)\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000335" --numeric -n numeric output of addresses and ports\n"
336" --out-interface -o [!] output name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --table -t table table to manipulate (default: `filter')\n"
339" --verbose -v verbose mode\n"
Harald Welte82dd2ec2000-12-19 05:18:15 +0000340" --line-numbers print line numbers when listing\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000341" --exact -x expand numbers (display exact values)\n"
342"[!] --fragment -f match second or further fragments only\n"
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000343" --modprobe=<command> try to insert modules using this command\n"
Harald Welteccd49e52001-01-23 22:54:34 +0000344" --set-counters PKTS BYTES set the counter during insert/append\n"
Marc Bouchere6869a82000-03-20 06:03:29 +0000345"[!] --version -V print package version.\n");
346
Rusty Russell363112d2000-08-11 13:49:26 +0000347 /* Print out any special helps. A user might like to be able
348 to add a --help to the commandline, and see expected
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000349 results. So we call help for all specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000350 for (t = xtables_targets; t ;t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351 if (t->used) {
352 printf("\n");
353 t->help();
354 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000355 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 for (matchp = matches; matchp; matchp = matchp->next) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000357 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000358 matchp->match->help();
Marc Bouchere6869a82000-03-20 06:03:29 +0000359 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000360 exit(0);
361}
362
Patrick McHardy0b639362007-09-08 16:00:01 +0000363void
364exit_error(enum exittype status, const char *msg, ...)
365{
366 va_list args;
367
368 va_start(args, msg);
369 fprintf(stderr, "%s v%s: ", program_name, program_version);
370 vfprintf(stderr, msg, args);
371 va_end(args);
372 fprintf(stderr, "\n");
373 if (status == PARAMETER_PROBLEM)
374 exit_tryhelp(status);
375 if (status == VERSION_PROBLEM)
376 fprintf(stderr,
377 "Perhaps iptables or your kernel needs to be upgraded.\n");
378 /* On error paths, make sure that we don't leak memory */
379 free_opts(1);
380 exit(status);
381}
382
Marc Bouchere6869a82000-03-20 06:03:29 +0000383static void
384generic_opt_check(int command, int options)
385{
386 int i, j, legal = 0;
387
388 /* Check that commands are valid with options. Complicated by the
389 * fact that if an option is legal with *any* command given, it is
390 * legal overall (ie. -z and -l).
391 */
392 for (i = 0; i < NUMBER_OF_OPT; i++) {
393 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
394
395 for (j = 0; j < NUMBER_OF_CMD; j++) {
396 if (!(command & (1<<j)))
397 continue;
398
399 if (!(options & (1<<i))) {
400 if (commands_v_options[j][i] == '+')
401 exit_error(PARAMETER_PROBLEM,
402 "You need to supply the `-%c' "
403 "option for this command\n",
404 optflags[i]);
405 } else {
406 if (commands_v_options[j][i] != 'x')
407 legal = 1;
408 else if (legal == 0)
409 legal = -1;
410 }
411 }
412 if (legal == -1)
413 exit_error(PARAMETER_PROBLEM,
414 "Illegal option `-%c' with this command\n",
415 optflags[i]);
416 }
417}
418
419static char
420opt2char(int option)
421{
422 const char *ptr;
423 for (ptr = optflags; option > 1; option >>= 1, ptr++);
424
425 return *ptr;
426}
427
428static char
429cmd2char(int option)
430{
431 const char *ptr;
432 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
433
434 return *ptr;
435}
436
437static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000438add_command(unsigned int *cmd, const int newcmd, const int othercmds,
439 int invert)
Marc Bouchere6869a82000-03-20 06:03:29 +0000440{
441 if (invert)
442 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
443 if (*cmd & (~othercmds))
444 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
445 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
446 *cmd |= newcmd;
447}
448
449int
Harald Welteb77f1da2002-03-14 11:35:58 +0000450check_inverse(const char option[], int *invert, int *optind, int argc)
Marc Bouchere6869a82000-03-20 06:03:29 +0000451{
452 if (option && strcmp(option, "!") == 0) {
453 if (*invert)
454 exit_error(PARAMETER_PROBLEM,
455 "Multiple `!' flags not allowed");
Marc Bouchere6869a82000-03-20 06:03:29 +0000456 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000457 if (optind) {
458 *optind = *optind+1;
459 if (argc && *optind > argc)
460 exit_error(PARAMETER_PROBLEM,
461 "no argument following `!'");
462 }
463
Marc Bouchere6869a82000-03-20 06:03:29 +0000464 return TRUE;
465 }
466 return FALSE;
467}
468
Marc Bouchere6869a82000-03-20 06:03:29 +0000469/*
470 * All functions starting with "parse" should succeed, otherwise
471 * the program fails.
472 * Most routines return pointers to static data that may change
473 * between calls to the same or other routines with a few exceptions:
474 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
475 * return global static data.
476*/
477
Rusty Russell28381a42000-05-10 00:19:50 +0000478/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
479static struct iptables_match *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000480find_proto(const char *pname, enum ipt_tryload tryload, int nolookup, struct iptables_rule_match **matches)
Rusty Russell28381a42000-05-10 00:19:50 +0000481{
Harald Welteed498492001-07-23 01:24:22 +0000482 unsigned int proto;
Rusty Russell28381a42000-05-10 00:19:50 +0000483
Harald Welte0b0013a2002-02-18 16:15:31 +0000484 if (string_to_number(pname, 0, 255, &proto) != -1) {
485 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell28381a42000-05-10 00:19:50 +0000486
Harald Welte0b0013a2002-02-18 16:15:31 +0000487 if (protoname)
Martin Josefsson78cafda2004-02-02 20:01:18 +0000488 return find_match(protoname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000489 } else
Martin Josefsson78cafda2004-02-02 20:01:18 +0000490 return find_match(pname, tryload, matches);
Harald Welte0b0013a2002-02-18 16:15:31 +0000491
492 return NULL;
Rusty Russell28381a42000-05-10 00:19:50 +0000493}
494
Marc Boucherb93c7982001-12-06 14:50:19 +0000495u_int16_t
Marc Bouchere6869a82000-03-20 06:03:29 +0000496parse_protocol(const char *s)
497{
Harald Welteed498492001-07-23 01:24:22 +0000498 unsigned int proto;
Marc Bouchere6869a82000-03-20 06:03:29 +0000499
Harald Welteed498492001-07-23 01:24:22 +0000500 if (string_to_number(s, 0, 255, &proto) == -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000501 struct protoent *pent;
502
Harald Weltecbe1ec72006-02-11 09:50:11 +0000503 /* first deal with the special case of 'all' to prevent
504 * people from being able to redefine 'all' in nsswitch
505 * and/or provoke expensive [not working] ldap/nis/...
506 * lookups */
507 if (!strcmp(s, "all"))
508 return 0;
509
Marc Bouchere6869a82000-03-20 06:03:29 +0000510 if ((pent = getprotobyname(s)))
511 proto = pent->p_proto;
512 else {
513 unsigned int i;
514 for (i = 0;
515 i < sizeof(chain_protos)/sizeof(struct pprot);
516 i++) {
517 if (strcmp(s, chain_protos[i].name) == 0) {
518 proto = chain_protos[i].num;
519 break;
520 }
521 }
522 if (i == sizeof(chain_protos)/sizeof(struct pprot))
523 exit_error(PARAMETER_PROBLEM,
524 "unknown protocol `%s' specified",
525 s);
526 }
527 }
528
529 return (u_int16_t)proto;
530}
531
Marc Bouchere6869a82000-03-20 06:03:29 +0000532/* Can't be zero. */
533static int
534parse_rulenumber(const char *rule)
535{
Harald Welteed498492001-07-23 01:24:22 +0000536 unsigned int rulenum;
Marc Bouchere6869a82000-03-20 06:03:29 +0000537
Harald Welteed498492001-07-23 01:24:22 +0000538 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000539 exit_error(PARAMETER_PROBLEM,
540 "Invalid rule number `%s'", rule);
541
542 return rulenum;
543}
544
545static const char *
546parse_target(const char *targetname)
547{
548 const char *ptr;
549
550 if (strlen(targetname) < 1)
551 exit_error(PARAMETER_PROBLEM,
552 "Invalid target name (too short)");
553
554 if (strlen(targetname)+1 > sizeof(ipt_chainlabel))
555 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000556 "Invalid target name `%s' (%u chars max)",
557 targetname, (unsigned int)sizeof(ipt_chainlabel)-1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000558
559 for (ptr = targetname; *ptr; ptr++)
560 if (isspace(*ptr))
561 exit_error(PARAMETER_PROBLEM,
562 "Invalid target name `%s'", targetname);
563 return targetname;
564}
565
Marc Bouchere6869a82000-03-20 06:03:29 +0000566static void
567set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
568 int invert)
569{
570 if (*options & option)
571 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
572 opt2char(option));
573 *options |= option;
574
575 if (invert) {
576 unsigned int i;
577 for (i = 0; 1 << i != option; i++);
578
579 if (!inverse_for_options[i])
580 exit_error(PARAMETER_PROBLEM,
581 "cannot have ! before -%c",
582 opt2char(option));
583 *invflg |= inverse_for_options[i];
584 }
585}
586
Marc Bouchere6869a82000-03-20 06:03:29 +0000587static struct option *
Jan Echternach5a1041d2000-08-26 04:44:39 +0000588merge_options(struct option *oldopts, const struct option *newopts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000589 unsigned int *option_offset)
590{
591 unsigned int num_old, num_new, i;
592 struct option *merge;
593
Jan Engelhardtd0145402007-07-30 14:32:26 +0000594 if (newopts == NULL)
595 return oldopts;
596
Marc Bouchere6869a82000-03-20 06:03:29 +0000597 for (num_old = 0; oldopts[num_old].name; num_old++);
598 for (num_new = 0; newopts[num_new].name; num_new++);
599
600 global_option_offset += OPTION_OFFSET;
601 *option_offset = global_option_offset;
602
603 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +0000604 if (merge == NULL)
605 return NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000606 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000607 free_opts(0); /* Release previous options merged if any */
Marc Bouchere6869a82000-03-20 06:03:29 +0000608 for (i = 0; i < num_new; i++) {
609 merge[num_old + i] = newopts[i];
610 merge[num_old + i].val += *option_offset;
611 }
612 memset(merge + num_old + num_new, 0, sizeof(struct option));
613
614 return merge;
615}
616
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617void register_match(struct iptables_match *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000618{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000619 me->family = PF_INET;
620 xtables_register_match(me);
Rusty Russell3aef54d2005-01-03 03:48:40 +0000621}
622
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623void register_target(struct iptables_target *me)
Rusty Russell3aef54d2005-01-03 03:48:40 +0000624{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000625 me->family = PF_INET;
626 xtables_register_target(me);
Marc Bouchere6869a82000-03-20 06:03:29 +0000627}
628
629static void
Harald Weltea0b4f792001-03-25 19:55:04 +0000630print_num(u_int64_t number, unsigned int format)
631{
632 if (format & FMT_KILOMEGAGIGA) {
633 if (number > 99999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
637 if (number > 9999) {
638 number = (number + 500) / 1000;
Rusty Russell5a66fe42001-08-15 11:21:59 +0000639 if (number > 9999) {
640 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Rusty Russell5a66fe42001-08-15 11:21:59 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000644 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000650 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000651 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Harald Weltea0b4f792001-03-25 19:55:04 +0000652}
653
654
655static void
Marc Bouchere6869a82000-03-20 06:03:29 +0000656print_header(unsigned int format, const char *chain, iptc_handle_t *handle)
657{
658 struct ipt_counters counters;
659 const char *pol = iptc_get_policy(chain, &counters, handle);
660 printf("Chain %s", chain);
661 if (pol) {
662 printf(" (policy %s", pol);
Harald Weltea0b4f792001-03-25 19:55:04 +0000663 if (!(format & FMT_NOCOUNTS)) {
664 fputc(' ', stdout);
665 print_num(counters.pcnt, (format|FMT_NOTABLE));
666 fputs("packets, ", stdout);
667 print_num(counters.bcnt, (format|FMT_NOTABLE));
668 fputs("bytes", stdout);
669 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000670 printf(")\n");
671 } else {
672 unsigned int refs;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000673 if (!iptc_get_references(&refs, chain, handle))
674 printf(" (ERROR obtaining refs)\n");
675 else
676 printf(" (%u references)\n", refs);
Marc Bouchere6869a82000-03-20 06:03:29 +0000677 }
678
679 if (format & FMT_LINENUMBERS)
680 printf(FMT("%-4s ", "%s "), "num");
681 if (!(format & FMT_NOCOUNTS)) {
682 if (format & FMT_KILOMEGAGIGA) {
683 printf(FMT("%5s ","%s "), "pkts");
684 printf(FMT("%5s ","%s "), "bytes");
685 } else {
686 printf(FMT("%8s ","%s "), "pkts");
687 printf(FMT("%10s ","%s "), "bytes");
688 }
689 }
690 if (!(format & FMT_NOTARGET))
691 printf(FMT("%-9s ","%s "), "target");
692 fputs(" prot ", stdout);
693 if (format & FMT_OPTIONS)
694 fputs("opt", stdout);
695 if (format & FMT_VIA) {
696 printf(FMT(" %-6s ","%s "), "in");
697 printf(FMT("%-6s ","%s "), "out");
698 }
699 printf(FMT(" %-19s ","%s "), "source");
700 printf(FMT(" %-19s "," %s "), "destination");
701 printf("\n");
702}
703
Marc Bouchere6869a82000-03-20 06:03:29 +0000704
705static int
706print_match(const struct ipt_entry_match *m,
707 const struct ipt_ip *ip,
708 int numeric)
709{
Martin Josefsson78cafda2004-02-02 20:01:18 +0000710 struct iptables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Marc Bouchere6869a82000-03-20 06:03:29 +0000711
712 if (match) {
713 if (match->print)
714 match->print(ip, m, numeric);
Rusty Russell629149f2000-09-01 06:01:00 +0000715 else
Rusty Russellb039b022000-09-01 06:04:05 +0000716 printf("%s ", match->name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000717 } else {
Rusty Russell228e98d2000-04-27 10:28:06 +0000718 if (m->u.user.name[0])
719 printf("UNKNOWN match `%s' ", m->u.user.name);
Marc Bouchere6869a82000-03-20 06:03:29 +0000720 }
721 /* Don't stop iterating. */
722 return 0;
723}
724
725/* e is called `fw' here for hysterical raisins */
726static void
727print_firewall(const struct ipt_entry *fw,
728 const char *targname,
729 unsigned int num,
730 unsigned int format,
731 const iptc_handle_t handle)
732{
733 struct iptables_target *target = NULL;
734 const struct ipt_entry_target *t;
735 u_int8_t flags;
736 char buf[BUFSIZ];
737
Marc Bouchere6869a82000-03-20 06:03:29 +0000738 if (!iptc_is_chain(targname, handle))
Rusty Russell52a51492000-05-02 16:44:29 +0000739 target = find_target(targname, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +0000740 else
Rusty Russell52a51492000-05-02 16:44:29 +0000741 target = find_target(IPT_STANDARD_TARGET, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +0000742
743 t = ipt_get_target((struct ipt_entry *)fw);
744 flags = fw->ip.flags;
745
746 if (format & FMT_LINENUMBERS)
747 printf(FMT("%-4u ", "%u "), num+1);
748
749 if (!(format & FMT_NOCOUNTS)) {
750 print_num(fw->counters.pcnt, format);
751 print_num(fw->counters.bcnt, format);
752 }
753
754 if (!(format & FMT_NOTARGET))
755 printf(FMT("%-9s ", "%s "), targname);
756
757 fputc(fw->ip.invflags & IPT_INV_PROTO ? '!' : ' ', stdout);
758 {
Rusty Russell28381a42000-05-10 00:19:50 +0000759 char *pname = proto_to_name(fw->ip.proto, format&FMT_NUMERIC);
Marc Bouchere6869a82000-03-20 06:03:29 +0000760 if (pname)
761 printf(FMT("%-5s", "%s "), pname);
762 else
763 printf(FMT("%-5hu", "%hu "), fw->ip.proto);
764 }
765
766 if (format & FMT_OPTIONS) {
767 if (format & FMT_NOTABLE)
768 fputs("opt ", stdout);
769 fputc(fw->ip.invflags & IPT_INV_FRAG ? '!' : '-', stdout);
770 fputc(flags & IPT_F_FRAG ? 'f' : '-', stdout);
771 fputc(' ', stdout);
772 }
773
774 if (format & FMT_VIA) {
775 char iface[IFNAMSIZ+2];
776
777 if (fw->ip.invflags & IPT_INV_VIA_IN) {
778 iface[0] = '!';
779 iface[1] = '\0';
780 }
781 else iface[0] = '\0';
782
783 if (fw->ip.iniface[0] != '\0') {
784 strcat(iface, fw->ip.iniface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000785 }
786 else if (format & FMT_NUMERIC) strcat(iface, "*");
787 else strcat(iface, "any");
788 printf(FMT(" %-6s ","in %s "), iface);
789
790 if (fw->ip.invflags & IPT_INV_VIA_OUT) {
791 iface[0] = '!';
792 iface[1] = '\0';
793 }
794 else iface[0] = '\0';
795
796 if (fw->ip.outiface[0] != '\0') {
797 strcat(iface, fw->ip.outiface);
Marc Bouchere6869a82000-03-20 06:03:29 +0000798 }
799 else if (format & FMT_NUMERIC) strcat(iface, "*");
800 else strcat(iface, "any");
801 printf(FMT("%-6s ","out %s "), iface);
802 }
803
804 fputc(fw->ip.invflags & IPT_INV_SRCIP ? '!' : ' ', stdout);
805 if (fw->ip.smsk.s_addr == 0L && !(format & FMT_NUMERIC))
806 printf(FMT("%-19s ","%s "), "anywhere");
807 else {
808 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000809 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.src));
Marc Bouchere6869a82000-03-20 06:03:29 +0000810 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000811 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.src));
812 strcat(buf, ipmask_to_numeric(&fw->ip.smsk));
Marc Bouchere6869a82000-03-20 06:03:29 +0000813 printf(FMT("%-19s ","%s "), buf);
814 }
815
816 fputc(fw->ip.invflags & IPT_INV_DSTIP ? '!' : ' ', stdout);
817 if (fw->ip.dmsk.s_addr == 0L && !(format & FMT_NUMERIC))
Harald Welte25fc1d72003-05-31 21:30:33 +0000818 printf(FMT("%-19s ","-> %s"), "anywhere");
Marc Bouchere6869a82000-03-20 06:03:29 +0000819 else {
820 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000821 sprintf(buf, "%s", ipaddr_to_numeric(&fw->ip.dst));
Marc Bouchere6869a82000-03-20 06:03:29 +0000822 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000823 sprintf(buf, "%s", ipaddr_to_anyname(&fw->ip.dst));
824 strcat(buf, ipmask_to_numeric(&fw->ip.dmsk));
Harald Welte25fc1d72003-05-31 21:30:33 +0000825 printf(FMT("%-19s ","-> %s"), buf);
Marc Bouchere6869a82000-03-20 06:03:29 +0000826 }
827
828 if (format & FMT_NOTABLE)
829 fputs(" ", stdout);
830
Harald Welte72bd87e2005-11-24 17:04:05 +0000831#ifdef IPT_F_GOTO
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000832 if(fw->ip.flags & IPT_F_GOTO)
833 printf("[goto] ");
Harald Welte72bd87e2005-11-24 17:04:05 +0000834#endif
Henrik Nordstrom17fc1632005-11-05 09:26:40 +0000835
Marc Bouchere6869a82000-03-20 06:03:29 +0000836 IPT_MATCH_ITERATE(fw, print_match, &fw->ip, format & FMT_NUMERIC);
837
838 if (target) {
839 if (target->print)
840 /* Print the target information. */
841 target->print(&fw->ip, t, format & FMT_NUMERIC);
Rusty Russell228e98d2000-04-27 10:28:06 +0000842 } else if (t->u.target_size != sizeof(*t))
Marc Bouchere6869a82000-03-20 06:03:29 +0000843 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000844 (unsigned int)(t->u.target_size - sizeof(*t)));
Marc Bouchere6869a82000-03-20 06:03:29 +0000845
846 if (!(format & FMT_NONEWLINE))
847 fputc('\n', stdout);
848}
849
850static void
851print_firewall_line(const struct ipt_entry *fw,
852 const iptc_handle_t h)
853{
854 struct ipt_entry_target *t;
855
856 t = ipt_get_target((struct ipt_entry *)fw);
Rusty Russell228e98d2000-04-27 10:28:06 +0000857 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
Marc Bouchere6869a82000-03-20 06:03:29 +0000858}
859
860static int
861append_entry(const ipt_chainlabel chain,
862 struct ipt_entry *fw,
863 unsigned int nsaddrs,
864 const struct in_addr saddrs[],
865 unsigned int ndaddrs,
866 const struct in_addr daddrs[],
867 int verbose,
868 iptc_handle_t *handle)
869{
870 unsigned int i, j;
871 int ret = 1;
872
873 for (i = 0; i < nsaddrs; i++) {
874 fw->ip.src.s_addr = saddrs[i].s_addr;
875 for (j = 0; j < ndaddrs; j++) {
876 fw->ip.dst.s_addr = daddrs[j].s_addr;
877 if (verbose)
878 print_firewall_line(fw, *handle);
879 ret &= iptc_append_entry(chain, fw, handle);
880 }
881 }
882
883 return ret;
884}
885
886static int
887replace_entry(const ipt_chainlabel chain,
888 struct ipt_entry *fw,
889 unsigned int rulenum,
890 const struct in_addr *saddr,
891 const struct in_addr *daddr,
892 int verbose,
893 iptc_handle_t *handle)
894{
895 fw->ip.src.s_addr = saddr->s_addr;
896 fw->ip.dst.s_addr = daddr->s_addr;
897
898 if (verbose)
899 print_firewall_line(fw, *handle);
900 return iptc_replace_entry(chain, fw, rulenum, handle);
901}
902
903static int
904insert_entry(const ipt_chainlabel chain,
905 struct ipt_entry *fw,
906 unsigned int rulenum,
907 unsigned int nsaddrs,
908 const struct in_addr saddrs[],
909 unsigned int ndaddrs,
910 const struct in_addr daddrs[],
911 int verbose,
912 iptc_handle_t *handle)
913{
914 unsigned int i, j;
915 int ret = 1;
916
917 for (i = 0; i < nsaddrs; i++) {
918 fw->ip.src.s_addr = saddrs[i].s_addr;
919 for (j = 0; j < ndaddrs; j++) {
920 fw->ip.dst.s_addr = daddrs[j].s_addr;
921 if (verbose)
922 print_firewall_line(fw, *handle);
923 ret &= iptc_insert_entry(chain, fw, rulenum, handle);
924 }
925 }
926
927 return ret;
928}
929
Rusty Russell2e0a3212000-04-19 11:23:18 +0000930static unsigned char *
Martin Josefsson78cafda2004-02-02 20:01:18 +0000931make_delete_mask(struct ipt_entry *fw, struct iptables_rule_match *matches)
Rusty Russell2e0a3212000-04-19 11:23:18 +0000932{
933 /* Establish mask for comparison */
934 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +0000935 struct iptables_rule_match *matchp;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000936 unsigned char *mask, *mptr;
937
938 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +0000939 for (matchp = matches; matchp; matchp = matchp->next)
940 size += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000941
Rusty Russell9e1d2142000-04-23 09:11:12 +0000942 mask = fw_calloc(1, size
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->size);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000945
Rusty Russell9e1d2142000-04-23 09:11:12 +0000946 memset(mask, 0xFF, sizeof(struct ipt_entry));
947 mptr = mask + sizeof(struct ipt_entry);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000948
Martin Josefsson78cafda2004-02-02 20:01:18 +0000949 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell2e0a3212000-04-19 11:23:18 +0000950 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000951 IPT_ALIGN(sizeof(struct ipt_entry_match))
Martin Josefsson78cafda2004-02-02 20:01:18 +0000952 + matchp->match->userspacesize);
953 mptr += IPT_ALIGN(sizeof(struct ipt_entry_match)) + matchp->match->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000954 }
955
Rusty Russella4d3e1f2001-01-07 06:56:02 +0000956 memset(mptr, 0xFF,
Rusty Russell73f72f52000-07-03 10:17:57 +0000957 IPT_ALIGN(sizeof(struct ipt_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000958 + xtables_targets->userspacesize);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000959
960 return mask;
961}
962
Marc Bouchere6869a82000-03-20 06:03:29 +0000963static int
964delete_entry(const ipt_chainlabel chain,
965 struct ipt_entry *fw,
966 unsigned int nsaddrs,
967 const struct in_addr saddrs[],
968 unsigned int ndaddrs,
969 const struct in_addr daddrs[],
970 int verbose,
Martin Josefsson78cafda2004-02-02 20:01:18 +0000971 iptc_handle_t *handle,
972 struct iptables_rule_match *matches)
Marc Bouchere6869a82000-03-20 06:03:29 +0000973{
974 unsigned int i, j;
975 int ret = 1;
Rusty Russell2e0a3212000-04-19 11:23:18 +0000976 unsigned char *mask;
Marc Bouchere6869a82000-03-20 06:03:29 +0000977
Martin Josefsson78cafda2004-02-02 20:01:18 +0000978 mask = make_delete_mask(fw, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +0000979 for (i = 0; i < nsaddrs; i++) {
980 fw->ip.src.s_addr = saddrs[i].s_addr;
981 for (j = 0; j < ndaddrs; j++) {
982 fw->ip.dst.s_addr = daddrs[j].s_addr;
983 if (verbose)
984 print_firewall_line(fw, *handle);
Rusty Russell2e0a3212000-04-19 11:23:18 +0000985 ret &= iptc_delete_entry(chain, fw, mask, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +0000986 }
987 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000988 free(mask);
989
Marc Bouchere6869a82000-03-20 06:03:29 +0000990 return ret;
991}
992
Harald Welteae1ff9f2000-12-01 14:26:20 +0000993int
Marc Bouchere6869a82000-03-20 06:03:29 +0000994for_each_chain(int (*fn)(const ipt_chainlabel, int, iptc_handle_t *),
Rusty Russell9e1d2142000-04-23 09:11:12 +0000995 int verbose, int builtinstoo, iptc_handle_t *handle)
Marc Bouchere6869a82000-03-20 06:03:29 +0000996{
997 int ret = 1;
Rusty Russell9e1d2142000-04-23 09:11:12 +0000998 const char *chain;
999 char *chains;
1000 unsigned int i, chaincount = 0;
Marc Bouchere6869a82000-03-20 06:03:29 +00001001
Rusty Russell9e1d2142000-04-23 09:11:12 +00001002 chain = iptc_first_chain(handle);
1003 while (chain) {
1004 chaincount++;
1005 chain = iptc_next_chain(handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001006 }
1007
Rusty Russell9e1d2142000-04-23 09:11:12 +00001008 chains = fw_malloc(sizeof(ipt_chainlabel) * chaincount);
1009 i = 0;
1010 chain = iptc_first_chain(handle);
1011 while (chain) {
1012 strcpy(chains + i*sizeof(ipt_chainlabel), chain);
1013 i++;
1014 chain = iptc_next_chain(handle);
1015 }
1016
1017 for (i = 0; i < chaincount; i++) {
1018 if (!builtinstoo
1019 && iptc_builtin(chains + i*sizeof(ipt_chainlabel),
Harald Welte3a506ac2004-08-30 16:00:09 +00001020 *handle) == 1)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001021 continue;
1022 ret &= fn(chains + i*sizeof(ipt_chainlabel), verbose, handle);
1023 }
1024
1025 free(chains);
Marc Bouchere6869a82000-03-20 06:03:29 +00001026 return ret;
1027}
1028
Harald Welteae1ff9f2000-12-01 14:26:20 +00001029int
Marc Bouchere6869a82000-03-20 06:03:29 +00001030flush_entries(const ipt_chainlabel chain, int verbose,
1031 iptc_handle_t *handle)
1032{
1033 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001034 return for_each_chain(flush_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001035
1036 if (verbose)
1037 fprintf(stdout, "Flushing chain `%s'\n", chain);
1038 return iptc_flush_entries(chain, handle);
1039}
Marc Bouchere6869a82000-03-20 06:03:29 +00001040
1041static int
1042zero_entries(const ipt_chainlabel chain, int verbose,
1043 iptc_handle_t *handle)
1044{
1045 if (!chain)
Rusty Russell9e1d2142000-04-23 09:11:12 +00001046 return for_each_chain(zero_entries, verbose, 1, handle);
Rusty Russell7e53bf92000-03-20 07:03:28 +00001047
Marc Bouchere6869a82000-03-20 06:03:29 +00001048 if (verbose)
1049 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1050 return iptc_zero_entries(chain, handle);
1051}
1052
Harald Welteae1ff9f2000-12-01 14:26:20 +00001053int
Marc Bouchere6869a82000-03-20 06:03:29 +00001054delete_chain(const ipt_chainlabel chain, int verbose,
1055 iptc_handle_t *handle)
1056{
Rusty Russell9e1d2142000-04-23 09:11:12 +00001057 if (!chain)
1058 return for_each_chain(delete_chain, verbose, 0, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001059
1060 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001061 fprintf(stdout, "Deleting chain `%s'\n", chain);
Marc Bouchere6869a82000-03-20 06:03:29 +00001062 return iptc_delete_chain(chain, handle);
1063}
1064
1065static int
1066list_entries(const ipt_chainlabel chain, int verbose, int numeric,
1067 int expanded, int linenumbers, iptc_handle_t *handle)
1068{
1069 int found = 0;
Rusty Russell9e1d2142000-04-23 09:11:12 +00001070 unsigned int format;
1071 const char *this;
Marc Bouchere6869a82000-03-20 06:03:29 +00001072
1073 format = FMT_OPTIONS;
1074 if (!verbose)
1075 format |= FMT_NOCOUNTS;
1076 else
1077 format |= FMT_VIA;
1078
1079 if (numeric)
1080 format |= FMT_NUMERIC;
1081
1082 if (!expanded)
1083 format |= FMT_KILOMEGAGIGA;
1084
1085 if (linenumbers)
1086 format |= FMT_LINENUMBERS;
1087
Rusty Russell9e1d2142000-04-23 09:11:12 +00001088 for (this = iptc_first_chain(handle);
1089 this;
1090 this = iptc_next_chain(handle)) {
1091 const struct ipt_entry *i;
1092 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +00001093
Marc Bouchere6869a82000-03-20 06:03:29 +00001094 if (chain && strcmp(chain, this) != 0)
1095 continue;
1096
1097 if (found) printf("\n");
1098
1099 print_header(format, this, handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001100 i = iptc_first_rule(this, handle);
1101
1102 num = 0;
1103 while (i) {
1104 print_firewall(i,
1105 iptc_get_target(i, handle),
1106 num++,
Marc Bouchere6869a82000-03-20 06:03:29 +00001107 format,
1108 *handle);
Rusty Russell9e1d2142000-04-23 09:11:12 +00001109 i = iptc_next_rule(i, handle);
1110 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001111 found = 1;
1112 }
1113
1114 errno = ENOENT;
1115 return found;
1116}
1117
1118static struct ipt_entry *
1119generate_entry(const struct ipt_entry *fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001120 struct iptables_rule_match *matches,
Marc Bouchere6869a82000-03-20 06:03:29 +00001121 struct ipt_entry_target *target)
1122{
1123 unsigned int size;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001124 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001125 struct ipt_entry *e;
1126
1127 size = sizeof(struct ipt_entry);
Martin Josefsson78cafda2004-02-02 20:01:18 +00001128 for (matchp = matches; matchp; matchp = matchp->next)
1129 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001130
Rusty Russell228e98d2000-04-27 10:28:06 +00001131 e = fw_malloc(size + target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001132 *e = *fw;
1133 e->target_offset = size;
Rusty Russell228e98d2000-04-27 10:28:06 +00001134 e->next_offset = size + target->u.target_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001135
1136 size = 0;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001137 for (matchp = matches; matchp; matchp = matchp->next) {
1138 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1139 size += matchp->match->m->u.match_size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001140 }
Rusty Russell228e98d2000-04-27 10:28:06 +00001141 memcpy(e->elems + size, target, target->u.target_size);
Marc Bouchere6869a82000-03-20 06:03:29 +00001142
1143 return e;
1144}
1145
Martin Josefsson78cafda2004-02-02 20:01:18 +00001146void clear_rule_matches(struct iptables_rule_match **matches)
1147{
1148 struct iptables_rule_match *matchp, *tmp;
1149
1150 for (matchp = *matches; matchp;) {
1151 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001152 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001153 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001154 matchp->match->m = NULL;
1155 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001156 if (matchp->match == matchp->match->next) {
1157 free(matchp->match);
1158 matchp->match = NULL;
1159 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001160 free(matchp);
1161 matchp = tmp;
1162 }
1163
1164 *matches = NULL;
1165}
1166
Rusty Russell3aef54d2005-01-03 03:48:40 +00001167static void set_revision(char *name, u_int8_t revision)
1168{
1169 /* Old kernel sources don't have ".revision" field,
1170 but we stole a byte from name. */
1171 name[IPT_FUNCTION_MAXNAMELEN - 2] = '\0';
1172 name[IPT_FUNCTION_MAXNAMELEN - 1] = revision;
1173}
1174
Phil Oester8cf65912005-09-19 15:00:33 +00001175void
1176get_kernel_version(void) {
1177 static struct utsname uts;
1178 int x = 0, y = 0, z = 0;
1179
1180 if (uname(&uts) == -1) {
1181 fprintf(stderr, "Unable to retrieve kernel version.\n");
1182 free_opts(1);
Max Kellermann5b76f682008-01-29 13:42:48 +00001183 exit(1);
Phil Oester8cf65912005-09-19 15:00:33 +00001184 }
1185
1186 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1187 kernel_version = LINUX_VERSION(x, y, z);
1188}
1189
Marc Bouchere6869a82000-03-20 06:03:29 +00001190int do_command(int argc, char *argv[], char **table, iptc_handle_t *handle)
1191{
1192 struct ipt_entry fw, *e = NULL;
1193 int invert = 0;
1194 unsigned int nsaddrs = 0, ndaddrs = 0;
1195 struct in_addr *saddrs = NULL, *daddrs = NULL;
1196
1197 int c, verbose = 0;
1198 const char *chain = NULL;
1199 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1200 const char *policy = NULL, *newname = NULL;
1201 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welteccd49e52001-01-23 22:54:34 +00001202 const char *pcnt = NULL, *bcnt = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +00001203 int ret = 1;
1204 struct iptables_match *m;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001205 struct iptables_rule_match *matches = NULL;
1206 struct iptables_rule_match *matchp;
Marc Bouchere6869a82000-03-20 06:03:29 +00001207 struct iptables_target *target = NULL;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001208 struct iptables_target *t;
Marc Bouchere6869a82000-03-20 06:03:29 +00001209 const char *jumpto = "";
1210 char *protocol = NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001211 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001212 unsigned long long cnt;
Marc Bouchere6869a82000-03-20 06:03:29 +00001213
1214 memset(&fw, 0, sizeof(fw));
1215
Harald Welteae1ff9f2000-12-01 14:26:20 +00001216 /* re-set optind to 0 in case do_command gets called
1217 * a second time */
1218 optind = 0;
1219
1220 /* clear mflags in case do_command gets called a second time
1221 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001222 for (m = xtables_matches; m; m = m->next)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001223 m->mflags = 0;
Harald Welteae1ff9f2000-12-01 14:26:20 +00001224
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001225 for (t = xtables_targets; t; t = t->next) {
Harald Welteae1ff9f2000-12-01 14:26:20 +00001226 t->tflags = 0;
1227 t->used = 0;
1228 }
1229
Marc Bouchere6869a82000-03-20 06:03:29 +00001230 /* Suppress error messages: we may add new options if we
1231 demand-load a protocol. */
1232 opterr = 0;
1233
1234 while ((c = getopt_long(argc, argv,
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001235 "-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 +00001236 opts, NULL)) != -1) {
1237 switch (c) {
1238 /*
1239 * Command selection
1240 */
1241 case 'A':
1242 add_command(&command, CMD_APPEND, CMD_NONE,
1243 invert);
1244 chain = optarg;
1245 break;
1246
1247 case 'D':
1248 add_command(&command, CMD_DELETE, CMD_NONE,
1249 invert);
1250 chain = optarg;
1251 if (optind < argc && argv[optind][0] != '-'
1252 && argv[optind][0] != '!') {
1253 rulenum = parse_rulenumber(argv[optind++]);
1254 command = CMD_DELETE_NUM;
1255 }
1256 break;
1257
Marc Bouchere6869a82000-03-20 06:03:29 +00001258 case 'R':
1259 add_command(&command, CMD_REPLACE, CMD_NONE,
1260 invert);
1261 chain = optarg;
1262 if (optind < argc && argv[optind][0] != '-'
1263 && argv[optind][0] != '!')
1264 rulenum = parse_rulenumber(argv[optind++]);
1265 else
1266 exit_error(PARAMETER_PROBLEM,
1267 "-%c requires a rule number",
1268 cmd2char(CMD_REPLACE));
1269 break;
1270
1271 case 'I':
1272 add_command(&command, CMD_INSERT, CMD_NONE,
1273 invert);
1274 chain = optarg;
1275 if (optind < argc && argv[optind][0] != '-'
1276 && argv[optind][0] != '!')
1277 rulenum = parse_rulenumber(argv[optind++]);
1278 else rulenum = 1;
1279 break;
1280
1281 case 'L':
1282 add_command(&command, CMD_LIST, CMD_ZERO,
1283 invert);
1284 if (optarg) chain = optarg;
1285 else if (optind < argc && argv[optind][0] != '-'
1286 && argv[optind][0] != '!')
1287 chain = argv[optind++];
1288 break;
1289
1290 case 'F':
1291 add_command(&command, CMD_FLUSH, CMD_NONE,
1292 invert);
1293 if (optarg) chain = optarg;
1294 else if (optind < argc && argv[optind][0] != '-'
1295 && argv[optind][0] != '!')
1296 chain = argv[optind++];
1297 break;
1298
1299 case 'Z':
1300 add_command(&command, CMD_ZERO, CMD_LIST,
1301 invert);
1302 if (optarg) chain = optarg;
1303 else if (optind < argc && argv[optind][0] != '-'
1304 && argv[optind][0] != '!')
1305 chain = argv[optind++];
1306 break;
1307
1308 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001309 if (optarg && (*optarg == '-' || *optarg == '!'))
Harald Welte6336bfd2002-05-07 14:41:43 +00001310 exit_error(PARAMETER_PROBLEM,
1311 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001312 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001313 if (find_target(optarg, TRY_LOAD))
1314 exit_error(PARAMETER_PROBLEM,
1315 "chain name may not clash "
1316 "with target name\n");
Marc Bouchere6869a82000-03-20 06:03:29 +00001317 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1318 invert);
1319 chain = optarg;
1320 break;
1321
1322 case 'X':
1323 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1324 invert);
1325 if (optarg) chain = optarg;
1326 else if (optind < argc && argv[optind][0] != '-'
1327 && argv[optind][0] != '!')
1328 chain = argv[optind++];
1329 break;
1330
1331 case 'E':
1332 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1333 invert);
1334 chain = optarg;
1335 if (optind < argc && argv[optind][0] != '-'
1336 && argv[optind][0] != '!')
1337 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001338 else
1339 exit_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +00001340 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001341 "new-chain-name",
1342 cmd2char(CMD_RENAME_CHAIN));
Marc Bouchere6869a82000-03-20 06:03:29 +00001343 break;
1344
1345 case 'P':
1346 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1347 invert);
1348 chain = optarg;
1349 if (optind < argc && argv[optind][0] != '-'
1350 && argv[optind][0] != '!')
1351 policy = argv[optind++];
1352 else
1353 exit_error(PARAMETER_PROBLEM,
1354 "-%c requires a chain and a policy",
1355 cmd2char(CMD_SET_POLICY));
1356 break;
1357
1358 case 'h':
1359 if (!optarg)
1360 optarg = argv[optind];
1361
Rusty Russell2e0a3212000-04-19 11:23:18 +00001362 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001363 if (!matches && protocol)
1364 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell2e0a3212000-04-19 11:23:18 +00001365
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001366 exit_printhelp(matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001367
1368 /*
1369 * Option selection
1370 */
1371 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001372 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001373 set_option(&options, OPT_PROTOCOL, &fw.ip.invflags,
1374 invert);
1375
1376 /* Canonicalize into lower case */
1377 for (protocol = argv[optind-1]; *protocol; protocol++)
1378 *protocol = tolower(*protocol);
1379
1380 protocol = argv[optind-1];
1381 fw.ip.proto = parse_protocol(protocol);
1382
1383 if (fw.ip.proto == 0
1384 && (fw.ip.invflags & IPT_INV_PROTO))
1385 exit_error(PARAMETER_PROBLEM,
1386 "rule would never match protocol");
Marc Bouchere6869a82000-03-20 06:03:29 +00001387 break;
1388
1389 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001390 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001391 set_option(&options, OPT_SOURCE, &fw.ip.invflags,
1392 invert);
1393 shostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001394 break;
1395
1396 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001397 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001398 set_option(&options, OPT_DESTINATION, &fw.ip.invflags,
1399 invert);
1400 dhostnetworkmask = argv[optind-1];
Marc Bouchere6869a82000-03-20 06:03:29 +00001401 break;
1402
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001403#ifdef IPT_F_GOTO
1404 case 'g':
1405 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1406 invert);
1407 fw.ip.flags |= IPT_F_GOTO;
1408 jumpto = parse_target(optarg);
1409 break;
1410#endif
1411
Marc Bouchere6869a82000-03-20 06:03:29 +00001412 case 'j':
1413 set_option(&options, OPT_JUMP, &fw.ip.invflags,
1414 invert);
1415 jumpto = parse_target(optarg);
Rusty Russell859f7262000-08-24 06:00:33 +00001416 /* TRY_LOAD (may be chain name) */
1417 target = find_target(jumpto, TRY_LOAD);
Marc Bouchere6869a82000-03-20 06:03:29 +00001418
1419 if (target) {
Rusty Russell228e98d2000-04-27 10:28:06 +00001420 size_t size;
1421
Rusty Russell73f72f52000-07-03 10:17:57 +00001422 size = IPT_ALIGN(sizeof(struct ipt_entry_target))
1423 + target->size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001424
Rusty Russell2e0a3212000-04-19 11:23:18 +00001425 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001426 target->t->u.target_size = size;
1427 strcpy(target->t->u.user.name, jumpto);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001428 set_revision(target->t->u.user.name,
1429 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001430 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001431 target->init(target->t);
Max Kellermann5b76f682008-01-29 13:42:48 +00001432 opts = merge_options(opts,
1433 target->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001434 &target->option_offset);
1435 if (opts == NULL)
1436 exit_error(OTHER_PROBLEM,
1437 "can't alloc memory!");
Marc Bouchere6869a82000-03-20 06:03:29 +00001438 }
1439 break;
1440
1441
1442 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001443 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001444 set_option(&options, OPT_VIANAMEIN, &fw.ip.invflags,
1445 invert);
1446 parse_interface(argv[optind-1],
1447 fw.ip.iniface,
1448 fw.ip.iniface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001449 break;
1450
1451 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001452 check_inverse(optarg, &invert, &optind, argc);
Marc Bouchere6869a82000-03-20 06:03:29 +00001453 set_option(&options, OPT_VIANAMEOUT, &fw.ip.invflags,
1454 invert);
1455 parse_interface(argv[optind-1],
1456 fw.ip.outiface,
1457 fw.ip.outiface_mask);
Marc Bouchere6869a82000-03-20 06:03:29 +00001458 break;
1459
1460 case 'f':
1461 set_option(&options, OPT_FRAGMENT, &fw.ip.invflags,
1462 invert);
1463 fw.ip.flags |= IPT_F_FRAG;
Marc Bouchere6869a82000-03-20 06:03:29 +00001464 break;
1465
1466 case 'v':
1467 if (!verbose)
1468 set_option(&options, OPT_VERBOSE,
1469 &fw.ip.invflags, invert);
1470 verbose++;
1471 break;
1472
Rusty Russell52a51492000-05-02 16:44:29 +00001473 case 'm': {
1474 size_t size;
1475
Marc Bouchere6869a82000-03-20 06:03:29 +00001476 if (invert)
1477 exit_error(PARAMETER_PROBLEM,
1478 "unexpected ! flag before --match");
1479
Martin Josefsson78cafda2004-02-02 20:01:18 +00001480 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Rusty Russell73f72f52000-07-03 10:17:57 +00001481 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1482 + m->size;
Rusty Russell52a51492000-05-02 16:44:29 +00001483 m->m = fw_calloc(1, size);
1484 m->m->u.match_size = size;
Rusty Russell27ff3472000-05-12 14:04:50 +00001485 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001486 set_revision(m->m->u.user.name, m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001487 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001488 m->init(m->m);
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001489 if (m != m->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001490 /* Merge options for non-cloned matches */
Max Kellermann5b76f682008-01-29 13:42:48 +00001491 opts = merge_options(opts,
1492 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001493 &m->option_offset);
1494 if (opts == NULL)
1495 exit_error(OTHER_PROBLEM,
1496 "can't alloc memory!");
1497 }
Rusty Russell52a51492000-05-02 16:44:29 +00001498 }
1499 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001500
1501 case 'n':
1502 set_option(&options, OPT_NUMERIC, &fw.ip.invflags,
1503 invert);
1504 break;
1505
1506 case 't':
1507 if (invert)
1508 exit_error(PARAMETER_PROBLEM,
1509 "unexpected ! flag before --table");
1510 *table = argv[optind-1];
1511 break;
1512
1513 case 'x':
1514 set_option(&options, OPT_EXPANDED, &fw.ip.invflags,
1515 invert);
1516 break;
1517
1518 case 'V':
1519 if (invert)
1520 printf("Not %s ;-)\n", program_version);
1521 else
1522 printf("%s v%s\n",
1523 program_name, program_version);
1524 exit(0);
1525
1526 case '0':
1527 set_option(&options, OPT_LINENUMBERS, &fw.ip.invflags,
1528 invert);
1529 break;
1530
Harald Welte82dd2ec2000-12-19 05:18:15 +00001531 case 'M':
1532 modprobe = optarg;
1533 break;
1534
Harald Welteccd49e52001-01-23 22:54:34 +00001535 case 'c':
1536
1537 set_option(&options, OPT_COUNTERS, &fw.ip.invflags,
1538 invert);
1539 pcnt = optarg;
1540 if (optind < argc && argv[optind][0] != '-'
1541 && argv[optind][0] != '!')
1542 bcnt = argv[optind++];
1543 else
1544 exit_error(PARAMETER_PROBLEM,
1545 "-%c requires packet and byte counter",
1546 opt2char(OPT_COUNTERS));
1547
Patrick McHardy875441e2007-10-17 08:48:58 +00001548 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001549 exit_error(PARAMETER_PROBLEM,
1550 "-%c packet counter not numeric",
1551 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001552 fw.counters.pcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001553
Patrick McHardy875441e2007-10-17 08:48:58 +00001554 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welteccd49e52001-01-23 22:54:34 +00001555 exit_error(PARAMETER_PROBLEM,
1556 "-%c byte counter not numeric",
1557 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001558 fw.counters.bcnt = cnt;
Harald Welteccd49e52001-01-23 22:54:34 +00001559 break;
1560
1561
Marc Bouchere6869a82000-03-20 06:03:29 +00001562 case 1: /* non option */
1563 if (optarg[0] == '!' && optarg[1] == '\0') {
1564 if (invert)
1565 exit_error(PARAMETER_PROBLEM,
1566 "multiple consecutive ! not"
1567 " allowed");
1568 invert = TRUE;
1569 optarg[0] = '\0';
1570 continue;
1571 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001572 fprintf(stderr, "Bad argument `%s'\n", optarg);
Marc Bouchere6869a82000-03-20 06:03:29 +00001573 exit_tryhelp(2);
1574
1575 default:
Marc Bouchere6869a82000-03-20 06:03:29 +00001576 if (!target
1577 || !(target->parse(c - target->option_offset,
1578 argv, invert,
1579 &target->tflags,
1580 &fw, &target->t))) {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001581 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001582 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001583 continue;
Martin Josefsson78cafda2004-02-02 20:01:18 +00001584 if (matchp->match->parse(c - matchp->match->option_offset,
Marc Bouchere6869a82000-03-20 06:03:29 +00001585 argv, invert,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001586 &matchp->match->mflags,
Marc Bouchere6869a82000-03-20 06:03:29 +00001587 &fw,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001588 &matchp->match->m))
Marc Bouchere6869a82000-03-20 06:03:29 +00001589 break;
1590 }
Martin Josefsson78cafda2004-02-02 20:01:18 +00001591 m = matchp ? matchp->match : NULL;
Harald Welte2d86b772002-08-26 12:21:44 +00001592
1593 /* If you listen carefully, you can
1594 actually hear this code suck. */
1595
1596 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001597 * in 3 different releases): If we encounter a
Harald Welte2d86b772002-08-26 12:21:44 +00001598 * parameter, that has not been parsed yet,
1599 * it's not an option of an explicitly loaded
1600 * match or a target. However, we support
1601 * implicit loading of the protocol match
1602 * extension. '-p tcp' means 'l4 proto 6' and
1603 * at the same time 'load tcp protocol match on
1604 * demand if we specify --dport'.
1605 *
1606 * To make this work, we need to make sure:
1607 * - the parameter has not been parsed by
1608 * a match (m above)
1609 * - a protocol has been specified
1610 * - the protocol extension has not been
1611 * loaded yet, or is loaded and unused
1612 * [think of iptables-restore!]
1613 * - the protocol extension can be successively
1614 * loaded
1615 */
1616 if (m == NULL
1617 && protocol
1618 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001619 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001620 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001621 options&OPT_NUMERIC, NULL)
Harald Welte2d86b772002-08-26 12:21:44 +00001622 && (proto_used == 0))
1623 )
1624 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001625 options&OPT_NUMERIC, &matches))) {
Harald Welte2d86b772002-08-26 12:21:44 +00001626 /* Try loading protocol */
1627 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001628
Harald Welte2d86b772002-08-26 12:21:44 +00001629 proto_used = 1;
1630
1631 size = IPT_ALIGN(sizeof(struct ipt_entry_match))
1632 + m->size;
1633
1634 m->m = fw_calloc(1, size);
1635 m->m->u.match_size = size;
1636 strcpy(m->m->u.user.name, m->name);
Rusty Russell3aef54d2005-01-03 03:48:40 +00001637 set_revision(m->m->u.user.name,
1638 m->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001639 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001640 m->init(m->m);
Harald Welte2d86b772002-08-26 12:21:44 +00001641
1642 opts = merge_options(opts,
Max Kellermann5b76f682008-01-29 13:42:48 +00001643 m->extra_opts,
Pablo Neira Ayuso8e707d72008-01-17 17:30:27 +00001644 &m->option_offset);
1645 if (opts == NULL)
1646 exit_error(OTHER_PROBLEM,
1647 "can't alloc memory!");
Harald Welte2d86b772002-08-26 12:21:44 +00001648
1649 optind--;
1650 continue;
1651 }
Marc Bouchere6869a82000-03-20 06:03:29 +00001652 if (!m)
1653 exit_error(PARAMETER_PROBLEM,
1654 "Unknown arg `%s'",
1655 argv[optind-1]);
1656 }
1657 }
1658 invert = FALSE;
1659 }
1660
Martin Josefsson78cafda2004-02-02 20:01:18 +00001661 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001662 if (matchp->match->final_check != NULL)
1663 matchp->match->final_check(matchp->match->mflags);
Sven Kochfb1279a2001-02-27 12:25:12 +00001664
Jan Engelhardt830132a2007-10-04 16:24:50 +00001665 if (target != NULL && target->final_check != NULL)
Marc Bouchere6869a82000-03-20 06:03:29 +00001666 target->final_check(target->tflags);
1667
1668 /* Fix me: must put inverse options checking here --MN */
1669
1670 if (optind < argc)
1671 exit_error(PARAMETER_PROBLEM,
1672 "unknown arguments found on commandline");
1673 if (!command)
1674 exit_error(PARAMETER_PROBLEM, "no command specified");
1675 if (invert)
1676 exit_error(PARAMETER_PROBLEM,
1677 "nothing appropriate following !");
1678
Harald Welte6336bfd2002-05-07 14:41:43 +00001679 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Marc Bouchere6869a82000-03-20 06:03:29 +00001680 if (!(options & OPT_DESTINATION))
1681 dhostnetworkmask = "0.0.0.0/0";
1682 if (!(options & OPT_SOURCE))
1683 shostnetworkmask = "0.0.0.0/0";
1684 }
1685
1686 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001687 ipparse_hostnetworkmask(shostnetworkmask, &saddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001688 &fw.ip.smsk, &nsaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001689
1690 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001691 ipparse_hostnetworkmask(dhostnetworkmask, &daddrs,
Max Kellermann5b76f682008-01-29 13:42:48 +00001692 &fw.ip.dmsk, &ndaddrs);
Marc Bouchere6869a82000-03-20 06:03:29 +00001693
1694 if ((nsaddrs > 1 || ndaddrs > 1) &&
1695 (fw.ip.invflags & (IPT_INV_SRCIP | IPT_INV_DSTIP)))
1696 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1697 " source or destination IP addresses");
1698
Marc Bouchere6869a82000-03-20 06:03:29 +00001699 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1700 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1701 "specify a unique address");
1702
1703 generic_opt_check(command, options);
1704
1705 if (chain && strlen(chain) > IPT_FUNCTION_MAXNAMELEN)
1706 exit_error(PARAMETER_PROBLEM,
1707 "chain name `%s' too long (must be under %i chars)",
1708 chain, IPT_FUNCTION_MAXNAMELEN);
1709
Harald Welteae1ff9f2000-12-01 14:26:20 +00001710 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001711 if (!*handle)
Harald Welteae1ff9f2000-12-01 14:26:20 +00001712 *handle = iptc_init(*table);
1713
Rusty Russell8beb0492004-12-22 00:37:10 +00001714 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001715 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte82dd2ec2000-12-19 05:18:15 +00001716 *handle = iptc_init(*table);
Harald Welte82dd2ec2000-12-19 05:18:15 +00001717
Marc Bouchere6869a82000-03-20 06:03:29 +00001718 if (!*handle)
1719 exit_error(VERSION_PROBLEM,
1720 "can't initialize iptables table `%s': %s",
1721 *table, iptc_strerror(errno));
1722
Harald Welte6336bfd2002-05-07 14:41:43 +00001723 if (command == CMD_APPEND
Marc Bouchere6869a82000-03-20 06:03:29 +00001724 || command == CMD_DELETE
1725 || command == CMD_INSERT
1726 || command == CMD_REPLACE) {
Rusty Russella4860fd2000-06-17 16:13:02 +00001727 if (strcmp(chain, "PREROUTING") == 0
1728 || strcmp(chain, "INPUT") == 0) {
1729 /* -o not valid with incoming packets. */
1730 if (options & OPT_VIANAMEOUT)
Marc Bouchere6869a82000-03-20 06:03:29 +00001731 exit_error(PARAMETER_PROBLEM,
1732 "Can't use -%c with %s\n",
1733 opt2char(OPT_VIANAMEOUT),
1734 chain);
1735 }
1736
Rusty Russella4860fd2000-06-17 16:13:02 +00001737 if (strcmp(chain, "POSTROUTING") == 0
1738 || strcmp(chain, "OUTPUT") == 0) {
1739 /* -i not valid with outgoing packets */
1740 if (options & OPT_VIANAMEIN)
Marc Bouchere6869a82000-03-20 06:03:29 +00001741 exit_error(PARAMETER_PROBLEM,
1742 "Can't use -%c with %s\n",
1743 opt2char(OPT_VIANAMEIN),
1744 chain);
1745 }
1746
1747 if (target && iptc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001748 fprintf(stderr,
1749 "Warning: using chain %s, not extension\n",
1750 jumpto);
Marc Bouchere6869a82000-03-20 06:03:29 +00001751
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001752 if (target->t)
1753 free(target->t);
1754
Marc Bouchere6869a82000-03-20 06:03:29 +00001755 target = NULL;
1756 }
1757
1758 /* If they didn't specify a target, or it's a chain
1759 name, use standard. */
1760 if (!target
1761 && (strlen(jumpto) == 0
1762 || iptc_is_chain(jumpto, *handle))) {
1763 size_t size;
Marc Bouchere6869a82000-03-20 06:03:29 +00001764
Rusty Russell52a51492000-05-02 16:44:29 +00001765 target = find_target(IPT_STANDARD_TARGET,
1766 LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001767
1768 size = sizeof(struct ipt_entry_target)
Rusty Russell228e98d2000-04-27 10:28:06 +00001769 + target->size;
Rusty Russell2e0a3212000-04-19 11:23:18 +00001770 target->t = fw_calloc(1, size);
Rusty Russell228e98d2000-04-27 10:28:06 +00001771 target->t->u.target_size = size;
1772 strcpy(target->t->u.user.name, jumpto);
Pablo Neira0b905642005-11-17 13:04:49 +00001773 if (!iptc_is_chain(jumpto, *handle))
1774 set_revision(target->t->u.user.name,
1775 target->revision);
Pablo Neira8115e542005-02-14 13:13:04 +00001776 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001777 target->init(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001778 }
1779
Rusty Russell7e53bf92000-03-20 07:03:28 +00001780 if (!target) {
Harald Weltef2a24bd2000-08-30 02:11:18 +00001781 /* it is no chain, and we can't load a plugin.
1782 * We cannot know if the plugin is corrupt, non
Rusty Russella4d3e1f2001-01-07 06:56:02 +00001783 * existant OR if the user just misspelled a
Harald Weltef2a24bd2000-08-30 02:11:18 +00001784 * chain. */
Henrik Nordstrom17fc1632005-11-05 09:26:40 +00001785#ifdef IPT_F_GOTO
1786 if (fw.ip.flags & IPT_F_GOTO)
1787 exit_error(PARAMETER_PROBLEM,
1788 "goto '%s' is not a chain\n", jumpto);
1789#endif
Harald Weltef2a24bd2000-08-30 02:11:18 +00001790 find_target(jumpto, LOAD_MUST_SUCCEED);
Marc Bouchere6869a82000-03-20 06:03:29 +00001791 } else {
Martin Josefsson78cafda2004-02-02 20:01:18 +00001792 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001793 free(target->t);
Marc Bouchere6869a82000-03-20 06:03:29 +00001794 }
1795 }
1796
1797 switch (command) {
1798 case CMD_APPEND:
1799 ret = append_entry(chain, e,
1800 nsaddrs, saddrs, ndaddrs, daddrs,
1801 options&OPT_VERBOSE,
1802 handle);
1803 break;
Marc Bouchere6869a82000-03-20 06:03:29 +00001804 case CMD_DELETE:
1805 ret = delete_entry(chain, e,
1806 nsaddrs, saddrs, ndaddrs, daddrs,
1807 options&OPT_VERBOSE,
Martin Josefsson78cafda2004-02-02 20:01:18 +00001808 handle, matches);
Marc Bouchere6869a82000-03-20 06:03:29 +00001809 break;
1810 case CMD_DELETE_NUM:
1811 ret = iptc_delete_num_entry(chain, rulenum - 1, handle);
1812 break;
1813 case CMD_REPLACE:
1814 ret = replace_entry(chain, e, rulenum - 1,
1815 saddrs, daddrs, options&OPT_VERBOSE,
1816 handle);
1817 break;
1818 case CMD_INSERT:
1819 ret = insert_entry(chain, e, rulenum - 1,
1820 nsaddrs, saddrs, ndaddrs, daddrs,
1821 options&OPT_VERBOSE,
1822 handle);
1823 break;
1824 case CMD_LIST:
1825 ret = list_entries(chain,
1826 options&OPT_VERBOSE,
1827 options&OPT_NUMERIC,
1828 options&OPT_EXPANDED,
1829 options&OPT_LINENUMBERS,
1830 handle);
1831 break;
1832 case CMD_FLUSH:
1833 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1834 break;
1835 case CMD_ZERO:
1836 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1837 break;
1838 case CMD_LIST|CMD_ZERO:
1839 ret = list_entries(chain,
1840 options&OPT_VERBOSE,
1841 options&OPT_NUMERIC,
1842 options&OPT_EXPANDED,
1843 options&OPT_LINENUMBERS,
1844 handle);
1845 if (ret)
1846 ret = zero_entries(chain,
1847 options&OPT_VERBOSE, handle);
1848 break;
1849 case CMD_NEW_CHAIN:
1850 ret = iptc_create_chain(chain, handle);
1851 break;
1852 case CMD_DELETE_CHAIN:
1853 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1854 break;
1855 case CMD_RENAME_CHAIN:
1856 ret = iptc_rename_chain(chain, newname, handle);
1857 break;
1858 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001859 ret = iptc_set_policy(chain, policy, NULL, handle);
Marc Bouchere6869a82000-03-20 06:03:29 +00001860 break;
1861 default:
1862 /* We should never reach this... */
1863 exit_tryhelp(2);
1864 }
1865
1866 if (verbose > 1)
1867 dump_entries(*handle);
1868
Martin Josefsson78cafda2004-02-02 20:01:18 +00001869 clear_rule_matches(&matches);
1870
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001871 if (e != NULL) {
1872 free(e);
1873 e = NULL;
1874 }
1875
keso6997cdf2004-07-04 15:20:53 +00001876 free(saddrs);
1877 free(daddrs);
Pablo Neiradfdcd642005-05-29 19:05:23 +00001878 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001879
Marc Bouchere6869a82000-03-20 06:03:29 +00001880 return ret;
1881}