blob: f974fb15bd7617aab65a218648c0671668b99528 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +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 *
Rusty Russell5eed48a2000-06-02 20:12:24 +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>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Jan Engelhardtf89c1712009-06-12 20:48:52 +020046#include "xshared.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000047
48#ifndef TRUE
49#define TRUE 1
50#endif
51#ifndef FALSE
52#define FALSE 0
53#endif
54
Rusty Russell5eed48a2000-06-02 20:12:24 +000055#define FMT_NUMERIC 0x0001
56#define FMT_NOCOUNTS 0x0002
57#define FMT_KILOMEGAGIGA 0x0004
58#define FMT_OPTIONS 0x0008
59#define FMT_NOTABLE 0x0010
60#define FMT_NOTARGET 0x0020
61#define FMT_VIA 0x0040
62#define FMT_NONEWLINE 0x0080
63#define FMT_LINENUMBERS 0x0100
64
65#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
66 | FMT_NUMERIC | FMT_NOTABLE)
67#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
68
69
70#define CMD_NONE 0x0000U
71#define CMD_INSERT 0x0001U
72#define CMD_DELETE 0x0002U
73#define CMD_DELETE_NUM 0x0004U
74#define CMD_REPLACE 0x0008U
75#define CMD_APPEND 0x0010U
76#define CMD_LIST 0x0020U
77#define CMD_FLUSH 0x0040U
78#define CMD_ZERO 0x0080U
79#define CMD_NEW_CHAIN 0x0100U
80#define CMD_DELETE_CHAIN 0x0200U
81#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000082#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020083#define CMD_LIST_RULES 0x1000U
84#define NUMBER_OF_CMD 14
Rusty Russell5eed48a2000-06-02 20:12:24 +000085static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020086 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000087
Rusty Russell5eed48a2000-06-02 20:12:24 +000088#define OPT_NONE 0x00000U
89#define OPT_NUMERIC 0x00001U
90#define OPT_SOURCE 0x00002U
91#define OPT_DESTINATION 0x00004U
92#define OPT_PROTOCOL 0x00008U
93#define OPT_JUMP 0x00010U
94#define OPT_VERBOSE 0x00020U
95#define OPT_EXPANDED 0x00040U
96#define OPT_VIANAMEIN 0x00080U
97#define OPT_VIANAMEOUT 0x00100U
98#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000099#define OPT_COUNTERS 0x00400U
100#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000101static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000102= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000103
104static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100105 {.name = "append", .has_arg = 1, .val = 'A'},
106 {.name = "delete", .has_arg = 1, .val = 'D'},
107 {.name = "insert", .has_arg = 1, .val = 'I'},
108 {.name = "replace", .has_arg = 1, .val = 'R'},
109 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200110 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100111 {.name = "flush", .has_arg = 2, .val = 'F'},
112 {.name = "zero", .has_arg = 2, .val = 'Z'},
113 {.name = "new-chain", .has_arg = 1, .val = 'N'},
114 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
115 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
116 {.name = "policy", .has_arg = 1, .val = 'P'},
117 {.name = "source", .has_arg = 1, .val = 's'},
118 {.name = "destination", .has_arg = 1, .val = 'd'},
119 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
120 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
121 {.name = "protocol", .has_arg = 1, .val = 'p'},
122 {.name = "in-interface", .has_arg = 1, .val = 'i'},
123 {.name = "jump", .has_arg = 1, .val = 'j'},
124 {.name = "table", .has_arg = 1, .val = 't'},
125 {.name = "match", .has_arg = 1, .val = 'm'},
126 {.name = "numeric", .has_arg = 0, .val = 'n'},
127 {.name = "out-interface", .has_arg = 1, .val = 'o'},
128 {.name = "verbose", .has_arg = 0, .val = 'v'},
129 {.name = "exact", .has_arg = 0, .val = 'x'},
130 {.name = "version", .has_arg = 0, .val = 'V'},
131 {.name = "help", .has_arg = 2, .val = 'h'},
132 {.name = "line-numbers", .has_arg = 0, .val = '0'},
133 {.name = "modprobe", .has_arg = 1, .val = 'M'},
134 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200135 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100136 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000137};
138
Harald Weltea8658ca2003-03-05 07:46:15 +0000139/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
140 * current line of the input file, in order to give a more precise error
141 * message. ip6tables itself doesn't need this, so it is initialized to the
142 * magic number of -1 */
143int line = -1;
144
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100145void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100146struct xtables_globals ip6tables_globals = {
147 .option_offset = 0,
148 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100149 .opts = original_opts,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500150 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100151 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100152};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000153
154/* Table of legal combinations of commands and options. If any of the
155 * given commands make an option legal, that option is legal (applies to
156 * CMD_LIST and CMD_ZERO only).
157 * Key:
158 * + compulsory
159 * x illegal
160 * optional
161 */
162
163static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
164/* Well, it's better than "Re: Linux vs FreeBSD" */
165{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200166 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000167/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
168/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
169/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
170/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
171/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
172/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
173/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
175/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
176/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200177/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200178/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
179/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000180};
181
182static int inverse_for_options[NUMBER_OF_OPT] =
183{
184/* -n */ 0,
185/* -s */ IP6T_INV_SRCIP,
186/* -d */ IP6T_INV_DSTIP,
187/* -p */ IP6T_INV_PROTO,
188/* -j */ 0,
189/* -v */ 0,
190/* -x */ 0,
191/* -i */ IP6T_INV_VIA_IN,
192/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000193/*--line*/ 0,
194/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000195};
196
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100197#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500198#define prog_name ip6tables_globals.program_name
199#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000200/* A few hardcoded protocols for 'all' and in case the user has no
201 /etc/protocols */
202struct pprot {
203 char *name;
204 u_int8_t num;
205};
206
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100207static const char *
Rusty Russell5eed48a2000-06-02 20:12:24 +0000208proto_to_name(u_int8_t proto, int nolookup)
209{
210 unsigned int i;
211
212 if (proto && !nolookup) {
213 struct protoent *pent = getprotobynumber(proto);
214 if (pent)
215 return pent->p_name;
216 }
217
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100218 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
219 if (xtables_chain_protos[i].num == proto)
220 return xtables_chain_protos[i].name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000221
222 return NULL;
223}
224
Patrick McHardy0b639362007-09-08 16:00:01 +0000225static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226exit_tryhelp(int status)
227{
Harald Weltea8658ca2003-03-05 07:46:15 +0000228 if (line != -1)
229 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000230 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500231 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500232 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000233 exit(status);
234}
235
Patrick McHardy0b639362007-09-08 16:00:01 +0000236static void
Jan Engelhardt395e4412009-02-10 10:43:08 +0100237exit_printhelp(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000238{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000239 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000240"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100241" %s -I chain [rulenum] rule-specification [options]\n"
242" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000243" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200244" %s -[LS] [chain [rulenum]] [options]\n"
245" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000246" %s -[NX] chain\n"
247" %s -E old-chain-name new-chain-name\n"
248" %s -P chain target [options]\n"
249" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500250 prog_name, prog_vers, prog_name, prog_name,
251 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100252 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000253
254 printf(
255"Commands:\n"
256"Either long or short options are allowed.\n"
257" --append -A chain Append to chain\n"
258" --delete -D chain Delete matching rule from chain\n"
259" --delete -D chain rulenum\n"
260" Delete rule rulenum (1 = first) from chain\n"
261" --insert -I chain [rulenum]\n"
262" Insert in chain as rulenum (default 1=first)\n"
263" --replace -R chain rulenum\n"
264" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200265" --list -L [chain [rulenum]]\n"
266" List the rules in a chain or all chains\n"
267" --list-rules -S [chain [rulenum]]\n"
268" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000269" --flush -F [chain] Delete all rules in chain or all chains\n"
270" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271" --new -N chain Create a new user-defined chain\n"
272" --delete-chain\n"
273" -X [chain] Delete a user-defined chain\n"
274" --policy -P chain target\n"
275" Change policy on chain to target\n"
276" --rename-chain\n"
277" -E old-chain new-chain\n"
278" Change chain name, (moving any references)\n"
279
280"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200281"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100282"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100284"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200286"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287" network interface name ([+] for wildcard)\n"
288" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000289" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200290#ifdef IP6T_F_GOTO
291" --goto -g chain\n"
292" jump to chain with no return\n"
293#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000294" --match -m match\n"
295" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000296" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200297"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000298" network interface name ([+] for wildcard)\n"
299" --table -t table table to manipulate (default: `filter')\n"
300" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000301" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000302" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000303/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000304" --modprobe=<command> try to insert modules using this command\n"
305" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000306"[!] --version -V print package version.\n");
307
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200308 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000309 exit(0);
310}
311
Patrick McHardy0b639362007-09-08 16:00:01 +0000312void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100313ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000314{
315 va_list args;
316
317 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500318 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000319 vfprintf(stderr, msg, args);
320 va_end(args);
321 fprintf(stderr, "\n");
322 if (status == PARAMETER_PROBLEM)
323 exit_tryhelp(status);
324 if (status == VERSION_PROBLEM)
325 fprintf(stderr,
326 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
327 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500328 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000329 exit(status);
330}
331
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332static void
333generic_opt_check(int command, int options)
334{
335 int i, j, legal = 0;
336
337 /* Check that commands are valid with options. Complicated by the
338 * fact that if an option is legal with *any* command given, it is
339 * legal overall (ie. -z and -l).
340 */
341 for (i = 0; i < NUMBER_OF_OPT; i++) {
342 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
343
344 for (j = 0; j < NUMBER_OF_CMD; j++) {
345 if (!(command & (1<<j)))
346 continue;
347
348 if (!(options & (1<<i))) {
349 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100350 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000351 "You need to supply the `-%c' "
352 "option for this command\n",
353 optflags[i]);
354 } else {
355 if (commands_v_options[j][i] != 'x')
356 legal = 1;
357 else if (legal == 0)
358 legal = -1;
359 }
360 }
361 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100362 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000363 "Illegal option `-%c' with this command\n",
364 optflags[i]);
365 }
366}
367
368static char
369opt2char(int option)
370{
371 const char *ptr;
372 for (ptr = optflags; option > 1; option >>= 1, ptr++);
373
374 return *ptr;
375}
376
377static char
378cmd2char(int option)
379{
380 const char *ptr;
381 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
382
383 return *ptr;
384}
385
386static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000387add_command(unsigned int *cmd, const int newcmd, const int othercmds,
388 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000389{
390 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100391 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000392 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100393 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000394 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
395 *cmd |= newcmd;
396}
397
Rusty Russell5eed48a2000-06-02 20:12:24 +0000398/*
399 * All functions starting with "parse" should succeed, otherwise
400 * the program fails.
401 * Most routines return pointers to static data that may change
402 * between calls to the same or other routines with a few exceptions:
403 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
404 * return global static data.
405*/
406
Rusty Russell5eed48a2000-06-02 20:12:24 +0000407/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200408static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100409find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100410 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000411{
Harald Welteed498492001-07-23 01:24:22 +0000412 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000413
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100414 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100415 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000416
Harald Welte43ac1912002-03-03 09:43:07 +0000417 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100418 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000419 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100420 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000421
422 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000423}
424
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000425/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000426static int is_exthdr(u_int16_t proto)
427{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000428 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000429 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000430 proto == IPPROTO_AH ||
431 proto == IPPROTO_DSTOPTS);
432}
433
Rusty Russell5eed48a2000-06-02 20:12:24 +0000434/* Can't be zero. */
435static int
436parse_rulenumber(const char *rule)
437{
Harald Welte43ac1912002-03-03 09:43:07 +0000438 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000439
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100440 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100441 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000442 "Invalid rule number `%s'", rule);
443
444 return rulenum;
445}
446
447static const char *
448parse_target(const char *targetname)
449{
450 const char *ptr;
451
452 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100453 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000454 "Invalid target name (too short)");
455
456 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100457 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000458 "Invalid target name `%s' (%u chars max)",
459 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460
461 for (ptr = targetname; *ptr; ptr++)
462 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100463 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000464 "Invalid target name `%s'", targetname);
465 return targetname;
466}
467
Rusty Russell5eed48a2000-06-02 20:12:24 +0000468static void
469set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
470 int invert)
471{
472 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100473 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474 opt2char(option));
475 *options |= option;
476
477 if (invert) {
478 unsigned int i;
479 for (i = 0; 1 << i != option; i++);
480
481 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100482 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483 "cannot have ! before -%c",
484 opt2char(option));
485 *invflg |= inverse_for_options[i];
486 }
487}
488
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000489static void
490print_num(u_int64_t number, unsigned int format)
491{
Harald Welte43ac1912002-03-03 09:43:07 +0000492 if (format & FMT_KILOMEGAGIGA) {
493 if (number > 99999) {
494 number = (number + 500) / 1000;
495 if (number > 9999) {
496 number = (number + 500) / 1000;
497 if (number > 9999) {
498 number = (number + 500) / 1000;
499 if (number > 9999) {
500 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000501 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000502 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000503 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000504 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000505 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000506 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000507 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000508 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000509 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000510 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000511 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000512}
513
Harald Welte43ac1912002-03-03 09:43:07 +0000514
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100516print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000517{
518 struct ip6t_counters counters;
519 const char *pol = ip6tc_get_policy(chain, &counters, handle);
520 printf("Chain %s", chain);
521 if (pol) {
522 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000523 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000524 fputc(' ', stdout);
525 print_num(counters.pcnt, (format|FMT_NOTABLE));
526 fputs("packets, ", stdout);
527 print_num(counters.bcnt, (format|FMT_NOTABLE));
528 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000529 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000530 printf(")\n");
531 } else {
532 unsigned int refs;
533 if (!ip6tc_get_references(&refs, chain, handle))
534 printf(" (ERROR obtaining refs)\n");
535 else
536 printf(" (%u references)\n", refs);
537 }
538
539 if (format & FMT_LINENUMBERS)
540 printf(FMT("%-4s ", "%s "), "num");
541 if (!(format & FMT_NOCOUNTS)) {
542 if (format & FMT_KILOMEGAGIGA) {
543 printf(FMT("%5s ","%s "), "pkts");
544 printf(FMT("%5s ","%s "), "bytes");
545 } else {
546 printf(FMT("%8s ","%s "), "pkts");
547 printf(FMT("%10s ","%s "), "bytes");
548 }
549 }
550 if (!(format & FMT_NOTARGET))
551 printf(FMT("%-9s ","%s "), "target");
552 fputs(" prot ", stdout);
553 if (format & FMT_OPTIONS)
554 fputs("opt", stdout);
555 if (format & FMT_VIA) {
556 printf(FMT(" %-6s ","%s "), "in");
557 printf(FMT("%-6s ","%s "), "out");
558 }
559 printf(FMT(" %-19s ","%s "), "source");
560 printf(FMT(" %-19s "," %s "), "destination");
561 printf("\n");
562}
563
Harald Welte43ac1912002-03-03 09:43:07 +0000564
Rusty Russell5eed48a2000-06-02 20:12:24 +0000565static int
566print_match(const struct ip6t_entry_match *m,
567 const struct ip6t_ip6 *ip,
568 int numeric)
569{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100570 struct xtables_match *match =
571 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000572
573 if (match) {
574 if (match->print)
575 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000576 else
577 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578 } else {
579 if (m->u.user.name[0])
580 printf("UNKNOWN match `%s' ", m->u.user.name);
581 }
582 /* Don't stop iterating. */
583 return 0;
584}
585
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100586/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000587static void
588print_firewall(const struct ip6t_entry *fw,
589 const char *targname,
590 unsigned int num,
591 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100592 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000593{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200594 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000595 const struct ip6t_entry_target *t;
596 u_int8_t flags;
597 char buf[BUFSIZ];
598
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100600 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100602 target = xtables_find_target(IP6T_STANDARD_TARGET,
603 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000604
605 t = ip6t_get_target((struct ip6t_entry *)fw);
606 flags = fw->ipv6.flags;
607
608 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200609 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000610
611 if (!(format & FMT_NOCOUNTS)) {
612 print_num(fw->counters.pcnt, format);
613 print_num(fw->counters.bcnt, format);
614 }
615
616 if (!(format & FMT_NOTARGET))
617 printf(FMT("%-9s ", "%s "), targname);
618
619 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
620 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100621 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000622 if (pname)
623 printf(FMT("%-5s", "%s "), pname);
624 else
625 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
626 }
627
628 if (format & FMT_OPTIONS) {
629 if (format & FMT_NOTABLE)
630 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000631 fputc(' ', stdout); /* Invert flag of FRAG */
632 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000633 fputc(' ', stdout);
634 }
635
636 if (format & FMT_VIA) {
637 char iface[IFNAMSIZ+2];
638
639 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
640 iface[0] = '!';
641 iface[1] = '\0';
642 }
643 else iface[0] = '\0';
644
645 if (fw->ipv6.iniface[0] != '\0') {
646 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000647 }
648 else if (format & FMT_NUMERIC) strcat(iface, "*");
649 else strcat(iface, "any");
650 printf(FMT(" %-6s ","in %s "), iface);
651
652 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
653 iface[0] = '!';
654 iface[1] = '\0';
655 }
656 else iface[0] = '\0';
657
658 if (fw->ipv6.outiface[0] != '\0') {
659 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660 }
661 else if (format & FMT_NUMERIC) strcat(iface, "*");
662 else strcat(iface, "any");
663 printf(FMT("%-6s ","out %s "), iface);
664 }
665
666 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000667 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000668 && !(format & FMT_NUMERIC))
669 printf(FMT("%-19s ","%s "), "anywhere");
670 else {
671 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100672 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000673 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100674 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
675 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000676 printf(FMT("%-19s ","%s "), buf);
677 }
678
679 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
680 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
681 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200682 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000683 else {
684 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100685 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000686 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100687 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
688 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200689 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000690 }
691
692 if (format & FMT_NOTABLE)
693 fputs(" ", stdout);
694
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200695#ifdef IP6T_F_GOTO
696 if(fw->ipv6.flags & IP6T_F_GOTO)
697 printf("[goto] ");
698#endif
699
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
701
702 if (target) {
703 if (target->print)
704 /* Print the target information. */
705 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
706 } else if (t->u.target_size != sizeof(*t))
707 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000708 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709
710 if (!(format & FMT_NONEWLINE))
711 fputc('\n', stdout);
712}
713
714static void
715print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100716 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000717{
718 struct ip6t_entry_target *t;
719
720 t = ip6t_get_target((struct ip6t_entry *)fw);
721 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
722}
723
724static int
725append_entry(const ip6t_chainlabel chain,
726 struct ip6t_entry *fw,
727 unsigned int nsaddrs,
728 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100729 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730 unsigned int ndaddrs,
731 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100732 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000733 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100734 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735{
736 unsigned int i, j;
737 int ret = 1;
738
739 for (i = 0; i < nsaddrs; i++) {
740 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100741 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000742 for (j = 0; j < ndaddrs; j++) {
743 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100744 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000745 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100746 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000747 ret &= ip6tc_append_entry(chain, fw, handle);
748 }
749 }
750
751 return ret;
752}
753
754static int
755replace_entry(const ip6t_chainlabel chain,
756 struct ip6t_entry *fw,
757 unsigned int rulenum,
758 const struct in6_addr *saddr,
759 const struct in6_addr *daddr,
760 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100761 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000762{
763 fw->ipv6.src = *saddr;
764 fw->ipv6.dst = *daddr;
765
766 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100767 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000768 return ip6tc_replace_entry(chain, fw, rulenum, handle);
769}
770
771static int
772insert_entry(const ip6t_chainlabel chain,
773 struct ip6t_entry *fw,
774 unsigned int rulenum,
775 unsigned int nsaddrs,
776 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100777 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778 unsigned int ndaddrs,
779 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100780 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100782 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783{
784 unsigned int i, j;
785 int ret = 1;
786
787 for (i = 0; i < nsaddrs; i++) {
788 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100789 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790 for (j = 0; j < ndaddrs; j++) {
791 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100792 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000793 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100794 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
796 }
797 }
798
799 return ret;
800}
801
802static unsigned char *
Michael Granzow332e4ac2009-04-09 18:24:36 +0100803make_delete_mask(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000804{
805 /* Establish mask for comparison */
806 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100807 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808 unsigned char *mask, *mptr;
809
810 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000811 for (matchp = matches; matchp; matchp = matchp->next)
812 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813
Jan Engelhardt630ef482009-01-27 14:58:41 +0100814 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000815 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000816 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000817
818 memset(mask, 0xFF, sizeof(struct ip6t_entry));
819 mptr = mask + sizeof(struct ip6t_entry);
820
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000821 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000823 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000824 + matchp->match->userspacesize);
825 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826 }
827
Max Kellermann5b76f682008-01-29 13:42:48 +0000828 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000829 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000830 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831
832 return mask;
833}
834
835static int
836delete_entry(const ip6t_chainlabel chain,
837 struct ip6t_entry *fw,
838 unsigned int nsaddrs,
839 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100840 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841 unsigned int ndaddrs,
842 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100843 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100845 struct ip6tc_handle *handle,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100846 struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000847{
848 unsigned int i, j;
849 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850 unsigned char *mask;
851
Michael Granzow332e4ac2009-04-09 18:24:36 +0100852 mask = make_delete_mask(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000854 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100855 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000857 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100858 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000859 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100860 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000861 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862 }
863 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000864 free(mask);
865
Rusty Russell5eed48a2000-06-02 20:12:24 +0000866 return ret;
867}
868
András Kis-Szabó764316a2001-02-26 17:31:20 +0000869int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100870for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
871 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000872{
Max Kellermann5b76f682008-01-29 13:42:48 +0000873 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000874 const char *chain;
875 char *chains;
876 unsigned int i, chaincount = 0;
877
878 chain = ip6tc_first_chain(handle);
879 while (chain) {
880 chaincount++;
881 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000882 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000883
Jan Engelhardt630ef482009-01-27 14:58:41 +0100884 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000885 i = 0;
886 chain = ip6tc_first_chain(handle);
887 while (chain) {
888 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
889 i++;
890 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000891 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892
893 for (i = 0; i < chaincount; i++) {
894 if (!builtinstoo
895 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100896 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000898 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000899 }
900
901 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000902 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903}
904
András Kis-Szabó764316a2001-02-26 17:31:20 +0000905int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100907 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908{
909 if (!chain)
910 return for_each_chain(flush_entries, verbose, 1, handle);
911
912 if (verbose)
913 fprintf(stdout, "Flushing chain `%s'\n", chain);
914 return ip6tc_flush_entries(chain, handle);
915}
916
917static int
918zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100919 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000920{
921 if (!chain)
922 return for_each_chain(zero_entries, verbose, 1, handle);
923
924 if (verbose)
925 fprintf(stdout, "Zeroing chain `%s'\n", chain);
926 return ip6tc_zero_entries(chain, handle);
927}
928
András Kis-Szabó764316a2001-02-26 17:31:20 +0000929int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000930delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100931 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000932{
933 if (!chain)
934 return for_each_chain(delete_chain, verbose, 0, handle);
935
936 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000937 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938 return ip6tc_delete_chain(chain, handle);
939}
940
941static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200942list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100943 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000944{
945 int found = 0;
946 unsigned int format;
947 const char *this;
948
949 format = FMT_OPTIONS;
950 if (!verbose)
951 format |= FMT_NOCOUNTS;
952 else
953 format |= FMT_VIA;
954
955 if (numeric)
956 format |= FMT_NUMERIC;
957
958 if (!expanded)
959 format |= FMT_KILOMEGAGIGA;
960
961 if (linenumbers)
962 format |= FMT_LINENUMBERS;
963
964 for (this = ip6tc_first_chain(handle);
965 this;
966 this = ip6tc_next_chain(handle)) {
967 const struct ip6t_entry *i;
968 unsigned int num;
969
970 if (chain && strcmp(chain, this) != 0)
971 continue;
972
973 if (found) printf("\n");
974
Henrik Nordstrombb340822008-05-13 13:09:23 +0200975 if (!rulenum)
976 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977 i = ip6tc_first_rule(this, handle);
978
979 num = 0;
980 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200981 num++;
982 if (!rulenum || num == rulenum)
983 print_firewall(i,
984 ip6tc_get_target(i, handle),
985 num,
986 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100987 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000988 i = ip6tc_next_rule(i, handle);
989 }
990 found = 1;
991 }
992
993 errno = ENOENT;
994 return found;
995}
996
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200997/* This assumes that mask is contiguous, and byte-bounded. */
998static void
999print_iface(char letter, const char *iface, const unsigned char *mask,
1000 int invert)
1001{
1002 unsigned int i;
1003
1004 if (mask[0] == 0)
1005 return;
1006
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001007 printf("%s-%c ", invert ? "! " : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001008
1009 for (i = 0; i < IFNAMSIZ; i++) {
1010 if (mask[i] != 0) {
1011 if (iface[i] != '\0')
1012 printf("%c", iface[i]);
1013 } else {
1014 /* we can access iface[i-1] here, because
1015 * a few lines above we make sure that mask[0] != 0 */
1016 if (iface[i-1] != '\0')
1017 printf("+");
1018 break;
1019 }
1020 }
1021
1022 printf(" ");
1023}
1024
1025/* The ip6tables looks up the /etc/protocols. */
1026static void print_proto(u_int16_t proto, int invert)
1027{
1028 if (proto) {
1029 unsigned int i;
1030 const char *invertstr = invert ? "! " : "";
1031
1032 struct protoent *pent = getprotobynumber(proto);
1033 if (pent) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001034 printf("%s-p %s ",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001035 invertstr, pent->p_name);
1036 return;
1037 }
1038
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001039 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1040 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001041 printf("%s-p %s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001042 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001043 return;
1044 }
1045
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001046 printf("%s-p %u ", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001047 }
1048}
1049
1050static int print_match_save(const struct ip6t_entry_match *e,
1051 const struct ip6t_ip6 *ip)
1052{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001053 struct xtables_match *match =
1054 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001055
1056 if (match) {
1057 printf("-m %s ", e->u.user.name);
1058
1059 /* some matches don't provide a save function */
1060 if (match->save)
1061 match->save(ip, e);
1062 } else {
1063 if (e->u.match_size) {
1064 fprintf(stderr,
1065 "Can't find library for match `%s'\n",
1066 e->u.user.name);
1067 exit(1);
1068 }
1069 }
1070 return 0;
1071}
1072
1073/* print a given ip including mask if neccessary */
1074static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1075{
1076 char buf[51];
1077 int l = ipv6_prefix_length(mask);
1078
1079 if (l == 0 && !invert)
1080 return;
1081
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001082 printf("%s%s %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001083 invert ? "! " : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001084 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001085 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1086
1087 if (l == -1)
1088 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1089 else
1090 printf("/%d ", l);
1091}
1092
1093/* We want this to be readable, so only print out neccessary fields.
1094 * Because that's the kind of world I want to live in. */
1095void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001096 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001097{
1098 struct ip6t_entry_target *t;
1099 const char *target_name;
1100
1101 /* print counters for iptables-save */
1102 if (counters > 0)
1103 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1104
1105 /* print chain name */
1106 printf("-A %s ", chain);
1107
1108 /* Print IP part. */
1109 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1110 e->ipv6.invflags & IP6T_INV_SRCIP);
1111
1112 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1113 e->ipv6.invflags & IP6T_INV_DSTIP);
1114
1115 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1116 e->ipv6.invflags & IP6T_INV_VIA_IN);
1117
1118 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1119 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1120
1121 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1122
1123#if 0
1124 /* not definied in ipv6
1125 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1126 if (e->ipv6.flags & IPT_F_FRAG)
1127 printf("%s-f ",
1128 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1129#endif
1130
1131 if (e->ipv6.flags & IP6T_F_TOS)
1132 printf("%s-? %d ",
1133 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1134 e->ipv6.tos);
1135
1136 /* Print matchinfo part */
1137 if (e->target_offset) {
1138 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1139 }
1140
1141 /* print counters for iptables -R */
1142 if (counters < 0)
1143 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1144
1145 /* Print target name */
1146 target_name = ip6tc_get_target(e, h);
1147 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001148#ifdef IP6T_F_GOTO
1149 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1150#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001151 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001152#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001153
1154 /* Print targinfo part */
1155 t = ip6t_get_target((struct ip6t_entry *)e);
1156 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001157 struct xtables_target *target =
1158 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001159
1160 if (!target) {
1161 fprintf(stderr, "Can't find library for target `%s'\n",
1162 t->u.user.name);
1163 exit(1);
1164 }
1165
1166 if (target->save)
1167 target->save(&e->ipv6, t);
1168 else {
1169 /* If the target size is greater than ip6t_entry_target
1170 * there is something to be saved, we just don't know
1171 * how to print it */
1172 if (t->u.target_size !=
1173 sizeof(struct ip6t_entry_target)) {
1174 fprintf(stderr, "Target `%s' is missing "
1175 "save function\n",
1176 t->u.user.name);
1177 exit(1);
1178 }
1179 }
1180 }
1181 printf("\n");
1182}
1183
1184static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001185list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001186 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001187{
1188 const char *this = NULL;
1189 int found = 0;
1190
1191 if (counters)
1192 counters = -1; /* iptables -c format */
1193
1194 /* Dump out chain names first,
1195 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001196 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001197 this;
1198 this = ip6tc_next_chain(handle)) {
1199 if (chain && strcmp(this, chain) != 0)
1200 continue;
1201
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001202 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001203 struct ip6t_counters count;
1204 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1205 if (counters)
1206 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1207 printf("\n");
1208 } else {
1209 printf("-N %s\n", this);
1210 }
1211 }
1212
1213 for (this = ip6tc_first_chain(handle);
1214 this;
1215 this = ip6tc_next_chain(handle)) {
1216 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001217 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001218
1219 if (chain && strcmp(this, chain) != 0)
1220 continue;
1221
1222 /* Dump out rules */
1223 e = ip6tc_first_rule(this, handle);
1224 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001225 num++;
1226 if (!rulenum || num == rulenum)
1227 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001228 e = ip6tc_next_rule(e, handle);
1229 }
1230 found = 1;
1231 }
1232
1233 errno = ENOENT;
1234 return found;
1235}
1236
Rusty Russell5eed48a2000-06-02 20:12:24 +00001237static struct ip6t_entry *
1238generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001239 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 struct ip6t_entry_target *target)
1241{
1242 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001243 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001244 struct ip6t_entry *e;
1245
1246 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001247 for (matchp = matches; matchp; matchp = matchp->next)
1248 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001249
Jan Engelhardt630ef482009-01-27 14:58:41 +01001250 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001251 *e = *fw;
1252 e->target_offset = size;
1253 e->next_offset = size + target->u.target_size;
1254
1255 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001256 for (matchp = matches; matchp; matchp = matchp->next) {
1257 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1258 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001259 }
1260 memcpy(e->elems + size, target, target->u.target_size);
1261
1262 return e;
1263}
1264
Jan Engelhardt395e4412009-02-10 10:43:08 +01001265static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001266{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001267 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001268
1269 for (matchp = *matches; matchp;) {
1270 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001271 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001272 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001273 matchp->match->m = NULL;
1274 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001275 if (matchp->match == matchp->match->next) {
1276 free(matchp->match);
1277 matchp->match = NULL;
1278 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001279 free(matchp);
1280 matchp = tmp;
1281 }
1282
1283 *matches = NULL;
1284}
1285
Jan Engelhardtfd187312008-11-10 16:59:27 +01001286int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001287{
1288 struct ip6t_entry fw, *e = NULL;
1289 int invert = 0;
1290 unsigned int nsaddrs = 0, ndaddrs = 0;
1291 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001292 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293
1294 int c, verbose = 0;
1295 const char *chain = NULL;
1296 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1297 const char *policy = NULL, *newname = NULL;
1298 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001299 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001300 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001301 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001302 struct xtables_rule_match *matches = NULL;
1303 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001304 struct xtables_target *target = NULL;
1305 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001306 const char *jumpto = "";
1307 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001308 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001309 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001310
1311 memset(&fw, 0, sizeof(fw));
1312
Harald Welte43ac1912002-03-03 09:43:07 +00001313 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001314 * a second time */
1315 optind = 0;
1316
Harald Welte43ac1912002-03-03 09:43:07 +00001317 /* clear mflags in case do_command gets called a second time
1318 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001319 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001320 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001321
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001322 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001323 t->tflags = 0;
1324 t->used = 0;
1325 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001326
Rusty Russell5eed48a2000-06-02 20:12:24 +00001327 /* Suppress error messages: we may add new options if we
1328 demand-load a protocol. */
1329 opterr = 0;
1330
1331 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001332 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001333 opts, NULL)) != -1) {
1334 switch (c) {
1335 /*
1336 * Command selection
1337 */
1338 case 'A':
1339 add_command(&command, CMD_APPEND, CMD_NONE,
1340 invert);
1341 chain = optarg;
1342 break;
1343
1344 case 'D':
1345 add_command(&command, CMD_DELETE, CMD_NONE,
1346 invert);
1347 chain = optarg;
1348 if (optind < argc && argv[optind][0] != '-'
1349 && argv[optind][0] != '!') {
1350 rulenum = parse_rulenumber(argv[optind++]);
1351 command = CMD_DELETE_NUM;
1352 }
1353 break;
1354
Rusty Russell5eed48a2000-06-02 20:12:24 +00001355 case 'R':
1356 add_command(&command, CMD_REPLACE, CMD_NONE,
1357 invert);
1358 chain = optarg;
1359 if (optind < argc && argv[optind][0] != '-'
1360 && argv[optind][0] != '!')
1361 rulenum = parse_rulenumber(argv[optind++]);
1362 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001363 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 "-%c requires a rule number",
1365 cmd2char(CMD_REPLACE));
1366 break;
1367
1368 case 'I':
1369 add_command(&command, CMD_INSERT, CMD_NONE,
1370 invert);
1371 chain = optarg;
1372 if (optind < argc && argv[optind][0] != '-'
1373 && argv[optind][0] != '!')
1374 rulenum = parse_rulenumber(argv[optind++]);
1375 else rulenum = 1;
1376 break;
1377
1378 case 'L':
1379 add_command(&command, CMD_LIST, CMD_ZERO,
1380 invert);
1381 if (optarg) chain = optarg;
1382 else if (optind < argc && argv[optind][0] != '-'
1383 && argv[optind][0] != '!')
1384 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001385 if (optind < argc && argv[optind][0] != '-'
1386 && argv[optind][0] != '!')
1387 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001388 break;
1389
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001390 case 'S':
1391 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1392 invert);
1393 if (optarg) chain = optarg;
1394 else if (optind < argc && argv[optind][0] != '-'
1395 && argv[optind][0] != '!')
1396 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001397 if (optind < argc && argv[optind][0] != '-'
1398 && argv[optind][0] != '!')
1399 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001400 break;
1401
Rusty Russell5eed48a2000-06-02 20:12:24 +00001402 case 'F':
1403 add_command(&command, CMD_FLUSH, CMD_NONE,
1404 invert);
1405 if (optarg) chain = optarg;
1406 else if (optind < argc && argv[optind][0] != '-'
1407 && argv[optind][0] != '!')
1408 chain = argv[optind++];
1409 break;
1410
1411 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001412 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 invert);
1414 if (optarg) chain = optarg;
1415 else if (optind < argc && argv[optind][0] != '-'
1416 && argv[optind][0] != '!')
1417 chain = argv[optind++];
1418 break;
1419
1420 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001421 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001422 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001423 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001424 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001425 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001426 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001427 "chain name may not clash "
1428 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1430 invert);
1431 chain = optarg;
1432 break;
1433
1434 case 'X':
1435 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1436 invert);
1437 if (optarg) chain = optarg;
1438 else if (optind < argc && argv[optind][0] != '-'
1439 && argv[optind][0] != '!')
1440 chain = argv[optind++];
1441 break;
1442
1443 case 'E':
1444 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1445 invert);
1446 chain = optarg;
1447 if (optind < argc && argv[optind][0] != '-'
1448 && argv[optind][0] != '!')
1449 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001450 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001451 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001452 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001453 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001454 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001455 break;
1456
1457 case 'P':
1458 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1459 invert);
1460 chain = optarg;
1461 if (optind < argc && argv[optind][0] != '-'
1462 && argv[optind][0] != '!')
1463 policy = argv[optind++];
1464 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001465 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001466 "-%c requires a chain and a policy",
1467 cmd2char(CMD_SET_POLICY));
1468 break;
1469
1470 case 'h':
1471 if (!optarg)
1472 optarg = argv[optind];
1473
Jonas Berlin1b91e592005-04-01 06:58:38 +00001474 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001475 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001476 xtables_find_match(protocol, XTF_TRY_LOAD,
1477 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001478
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001479 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001480
1481 /*
1482 * Option selection
1483 */
1484 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001485 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001486 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1487 invert);
1488
1489 /* Canonicalize into lower case */
1490 for (protocol = argv[optind-1]; *protocol; protocol++)
1491 *protocol = tolower(*protocol);
1492
1493 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001494 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001495 fw.ipv6.flags |= IP6T_F_PROTO;
1496
1497 if (fw.ipv6.proto == 0
1498 && (fw.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001499 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001501
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001502 if (is_exthdr(fw.ipv6.proto)
1503 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001504 fprintf(stderr,
1505 "Warning: never matched protocol: %s. "
1506 "use extension match instead.\n",
1507 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001508 break;
1509
1510 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001511 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001512 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1513 invert);
1514 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001515 break;
1516
1517 case 'd':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001518 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001519 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1520 invert);
1521 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001522 break;
1523
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001524#ifdef IP6T_F_GOTO
1525 case 'g':
1526 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1527 invert);
1528 fw.ipv6.flags |= IP6T_F_GOTO;
1529 jumpto = parse_target(optarg);
1530 break;
1531#endif
1532
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533 case 'j':
1534 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1535 invert);
1536 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001537 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001538 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001539
1540 if (target) {
1541 size_t size;
1542
Harald Welte43ac1912002-03-03 09:43:07 +00001543 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1544 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001545
Jan Engelhardt630ef482009-01-27 14:58:41 +01001546 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 target->t->u.target_size = size;
1548 strcpy(target->t->u.user.name, jumpto);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001549 xtables_set_revision(target->t->u.user.name,
Patrick McHardy455559b2008-04-15 15:51:19 +02001550 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001551 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001552 target->init(target->t);
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001553 opts = xtables_merge_options(opts,
Patrick McHardy455559b2008-04-15 15:51:19 +02001554 target->extra_opts,
1555 &target->option_offset);
1556 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001557 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001558 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001559 }
1560 break;
1561
1562
1563 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001564 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001565 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1566 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001567 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001568 fw.ipv6.iniface,
1569 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570 break;
1571
1572 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001573 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1575 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001576 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001577 fw.ipv6.outiface,
1578 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001579 break;
1580
1581 case 'v':
1582 if (!verbose)
1583 set_option(&options, OPT_VERBOSE,
1584 &fw.ipv6.invflags, invert);
1585 verbose++;
1586 break;
1587
1588 case 'm': {
1589 size_t size;
1590
1591 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001592 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001593 "unexpected ! flag before --match");
1594
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001595 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1596 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001597 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1598 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001599 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001600 m->m->u.match_size = size;
1601 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001602 xtables_set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001603 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001604 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001605 if (m != m->next)
1606 /* Merge options for non-cloned matches */
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001607 opts = xtables_merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 }
1609 break;
1610
1611 case 'n':
1612 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1613 invert);
1614 break;
1615
1616 case 't':
1617 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001618 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001619 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001620 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001621 break;
1622
1623 case 'x':
1624 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1625 invert);
1626 break;
1627
1628 case 'V':
1629 if (invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001630 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001631 else
1632 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001633 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 exit(0);
1635
1636 case '0':
1637 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1638 invert);
1639 break;
1640
Harald Welte43ac1912002-03-03 09:43:07 +00001641 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001642 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001643 break;
1644
1645 case 'c':
1646
1647 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1648 invert);
1649 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001650 bcnt = strchr(pcnt + 1, ',');
1651 if (bcnt)
1652 bcnt++;
1653 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001654 && argv[optind][0] != '!')
1655 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001656 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001657 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001658 "-%c requires packet and byte counter",
1659 opt2char(OPT_COUNTERS));
1660
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001661 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001662 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001663 "-%c packet counter not numeric",
1664 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001665 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001666
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001667 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001668 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001669 "-%c byte counter not numeric",
1670 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001671 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001672 break;
1673
Rusty Russell5eed48a2000-06-02 20:12:24 +00001674 case 1: /* non option */
1675 if (optarg[0] == '!' && optarg[1] == '\0') {
1676 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001677 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001678 "multiple consecutive ! not"
1679 " allowed");
1680 invert = TRUE;
1681 optarg[0] = '\0';
1682 continue;
1683 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001684 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001685 exit_tryhelp(2);
1686
1687 default:
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001688 if (target == NULL || target->parse == NULL ||
1689 !target->parse(c - target->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001690 argv, invert,
1691 &target->tflags,
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001692 &fw, &target->t)) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001693 for (matchp = matches; matchp; matchp = matchp->next) {
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001694 if (matchp->completed ||
1695 matchp->match->parse == NULL)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001696 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001697 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001698 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001699 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001700 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001701 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001702 break;
1703 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001704 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001705
1706 /* If you listen carefully, you can
1707 actually hear this code suck. */
1708
1709 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001710 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001711 * parameter, that has not been parsed yet,
1712 * it's not an option of an explicitly loaded
1713 * match or a target. However, we support
1714 * implicit loading of the protocol match
1715 * extension. '-p tcp' means 'l4 proto 6' and
1716 * at the same time 'load tcp protocol match on
1717 * demand if we specify --dport'.
1718 *
1719 * To make this work, we need to make sure:
1720 * - the parameter has not been parsed by
1721 * a match (m above)
1722 * - a protocol has been specified
1723 * - the protocol extension has not been
1724 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001725 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001726 * - the protocol extension can be successively
1727 * loaded
1728 */
1729 if (m == NULL
1730 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001731 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001732 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001733 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001734 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001735 && (proto_used == 0))
1736 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001737 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001738 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001739 /* Try loading protocol */
1740 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001741
Harald Welte00f5a9c2002-08-26 14:37:35 +00001742 proto_used = 1;
1743
1744 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1745 + m->size;
1746
Jan Engelhardt630ef482009-01-27 14:58:41 +01001747 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001748 m->m->u.match_size = size;
1749 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001750 xtables_set_revision(m->m->u.user.name,
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001751 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001752 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001753 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001754
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001755 opts = xtables_merge_options(opts,
Harald Welte00f5a9c2002-08-26 14:37:35 +00001756 m->extra_opts, &m->option_offset);
1757
1758 optind--;
1759 continue;
1760 }
1761
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001762 if (!m) {
1763 if (c == '?') {
1764 if (optopt) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001765 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001766 PARAMETER_PROBLEM,
1767 "option `%s' "
1768 "requires an "
1769 "argument",
1770 argv[optind-1]);
1771 } else {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001772 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001773 PARAMETER_PROBLEM,
1774 "unknown option "
1775 "`%s'",
1776 argv[optind-1]);
1777 }
1778 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001779 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001780 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001781 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001782 }
1783 }
1784 invert = FALSE;
1785 }
1786
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001787 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001788 if (matchp->match->final_check != NULL)
1789 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001790
Jan Engelhardt830132a2007-10-04 16:24:50 +00001791 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001792 target->final_check(target->tflags);
1793
1794 /* Fix me: must put inverse options checking here --MN */
1795
1796 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001797 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001798 "unknown arguments found on commandline");
1799 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001800 xtables_error(PARAMETER_PROBLEM, "no command specified");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001801 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001802 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001803 "nothing appropriate following !");
1804
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001805 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001806 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001807 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001808 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001809 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810 }
1811
1812 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001813 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1814 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815
1816 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001817 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1818 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001819
1820 if ((nsaddrs > 1 || ndaddrs > 1) &&
1821 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001822 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001823 " source or destination IP addresses");
1824
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001826 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001827 "specify a unique address");
1828
1829 generic_opt_check(command, options);
1830
1831 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001832 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833 "chain name `%s' too long (must be under %i chars)",
1834 chain, IP6T_FUNCTION_MAXNAMELEN);
1835
Harald Welte43ac1912002-03-03 09:43:07 +00001836 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001837 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001838 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001839
Rusty Russell8beb0492004-12-22 00:37:10 +00001840 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001841 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001842 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001843
Harald Welte43ac1912002-03-03 09:43:07 +00001844 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001845 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001846 "can't initialize ip6tables table `%s': %s",
1847 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001848
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001849 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001850 || command == CMD_DELETE
1851 || command == CMD_INSERT
1852 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001853 if (strcmp(chain, "PREROUTING") == 0
1854 || strcmp(chain, "INPUT") == 0) {
1855 /* -o not valid with incoming packets. */
1856 if (options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001857 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001858 "Can't use -%c with %s\n",
1859 opt2char(OPT_VIANAMEOUT),
1860 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001861 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862
Harald Welte43ac1912002-03-03 09:43:07 +00001863 if (strcmp(chain, "POSTROUTING") == 0
1864 || strcmp(chain, "OUTPUT") == 0) {
1865 /* -i not valid with outgoing packets */
1866 if (options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001867 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001868 "Can't use -%c with %s\n",
1869 opt2char(OPT_VIANAMEIN),
1870 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001871 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001872
1873 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001874 fprintf(stderr,
1875 "Warning: using chain %s, not extension\n",
1876 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001877
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001878 if (target->t)
1879 free(target->t);
1880
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881 target = NULL;
1882 }
1883
1884 /* If they didn't specify a target, or it's a chain
1885 name, use standard. */
1886 if (!target
1887 && (strlen(jumpto) == 0
1888 || ip6tc_is_chain(jumpto, *handle))) {
1889 size_t size;
1890
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001891 target = xtables_find_target(IP6T_STANDARD_TARGET,
1892 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893
1894 size = sizeof(struct ip6t_entry_target)
1895 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001896 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897 target->t->u.target_size = size;
1898 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001899 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001900 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001901 }
1902
1903 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001904 /* it is no chain, and we can't load a plugin.
1905 * We cannot know if the plugin is corrupt, non
1906 * existant OR if the user just misspelled a
1907 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001908#ifdef IP6T_F_GOTO
1909 if (fw.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001910 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001911 "goto '%s' is not a chain\n", jumpto);
1912#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001913 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001914 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001915 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001916 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001917 }
1918 }
1919
1920 switch (command) {
1921 case CMD_APPEND:
1922 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001923 nsaddrs, saddrs, smasks,
1924 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001925 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001926 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928 case CMD_DELETE:
1929 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001930 nsaddrs, saddrs, smasks,
1931 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001933 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001934 break;
1935 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001936 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001937 break;
1938 case CMD_REPLACE:
1939 ret = replace_entry(chain, e, rulenum - 1,
1940 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001941 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 break;
1943 case CMD_INSERT:
1944 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001945 nsaddrs, saddrs, smasks,
1946 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001948 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001949 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001950 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001951 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 break;
1953 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001954 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001955 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001956 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 case CMD_LIST|CMD_ZERO:
1958 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001959 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 options&OPT_VERBOSE,
1961 options&OPT_NUMERIC,
1962 options&OPT_EXPANDED,
1963 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001964 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001965 if (ret && (command & CMD_ZERO))
1966 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001967 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001968 break;
1969 case CMD_LIST_RULES:
1970 case CMD_LIST_RULES|CMD_ZERO:
1971 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001972 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001973 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001974 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001975 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001976 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001977 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 break;
1979 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001980 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001981 break;
1982 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001983 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001984 break;
1985 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001986 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001987 break;
1988 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001989 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 break;
1991 default:
1992 /* We should never reach this... */
1993 exit_tryhelp(2);
1994 }
1995
1996 if (verbose > 1)
1997 dump_entries6(*handle);
1998
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001999 clear_rule_matches(&matches);
2000
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002001 if (e != NULL) {
2002 free(e);
2003 e = NULL;
2004 }
2005
Michael Granzow332e4ac2009-04-09 18:24:36 +01002006 free(saddrs);
2007 free(smasks);
2008 free(daddrs);
2009 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002010 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002011
Rusty Russell5eed48a2000-06-02 20:12:24 +00002012 return ret;
2013}