blob: 8f653e8e4a6c10f259b06bff5faafa0a7d9e6ab4 [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
Mohit Mehtab34199e2009-08-19 10:56:33 -070084#define CMD_ZERO_NUM 0x2000U
85#define NUMBER_OF_CMD 15
Rusty Russell5eed48a2000-06-02 20:12:24 +000086static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Mohit Mehtab34199e2009-08-19 10:56:33 -070087 'Z', 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
Rusty Russell5eed48a2000-06-02 20:12:24 +000089#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100106 {.name = "append", .has_arg = 1, .val = 'A'},
107 {.name = "delete", .has_arg = 1, .val = 'D'},
108 {.name = "insert", .has_arg = 1, .val = 'I'},
109 {.name = "replace", .has_arg = 1, .val = 'R'},
110 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200111 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100112 {.name = "flush", .has_arg = 2, .val = 'F'},
113 {.name = "zero", .has_arg = 2, .val = 'Z'},
114 {.name = "new-chain", .has_arg = 1, .val = 'N'},
115 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
116 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
117 {.name = "policy", .has_arg = 1, .val = 'P'},
118 {.name = "source", .has_arg = 1, .val = 's'},
119 {.name = "destination", .has_arg = 1, .val = 'd'},
120 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
121 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
122 {.name = "protocol", .has_arg = 1, .val = 'p'},
123 {.name = "in-interface", .has_arg = 1, .val = 'i'},
124 {.name = "jump", .has_arg = 1, .val = 'j'},
125 {.name = "table", .has_arg = 1, .val = 't'},
126 {.name = "match", .has_arg = 1, .val = 'm'},
127 {.name = "numeric", .has_arg = 0, .val = 'n'},
128 {.name = "out-interface", .has_arg = 1, .val = 'o'},
129 {.name = "verbose", .has_arg = 0, .val = 'v'},
130 {.name = "exact", .has_arg = 0, .val = 'x'},
131 {.name = "version", .has_arg = 0, .val = 'V'},
132 {.name = "help", .has_arg = 2, .val = 'h'},
133 {.name = "line-numbers", .has_arg = 0, .val = '0'},
134 {.name = "modprobe", .has_arg = 1, .val = 'M'},
135 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200136 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100137 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100146void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100147struct xtables_globals ip6tables_globals = {
148 .option_offset = 0,
149 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100150 .opts = original_opts,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500151 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100152 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100153};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000154
155/* Table of legal combinations of commands and options. If any of the
156 * given commands make an option legal, that option is legal (applies to
157 * CMD_LIST and CMD_ZERO only).
158 * Key:
159 * + compulsory
160 * x illegal
161 * optional
162 */
163
164static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
165/* Well, it's better than "Re: Linux vs FreeBSD" */
166{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200167 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000168/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
169/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
170/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
171/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
172/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
173/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
174/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
175/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Mohit Mehtab34199e2009-08-19 10:56:33 -0700176/*ZERO_NUM*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000177/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
178/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200179/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200180/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
181/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000182};
183
184static int inverse_for_options[NUMBER_OF_OPT] =
185{
186/* -n */ 0,
187/* -s */ IP6T_INV_SRCIP,
188/* -d */ IP6T_INV_DSTIP,
189/* -p */ IP6T_INV_PROTO,
190/* -j */ 0,
191/* -v */ 0,
192/* -x */ 0,
193/* -i */ IP6T_INV_VIA_IN,
194/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000195/*--line*/ 0,
196/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000197};
198
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100199#define opts ip6tables_globals.opts
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500200#define prog_name ip6tables_globals.program_name
201#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100209static const char *
Rusty Russell5eed48a2000-06-02 20:12:24 +0000210proto_to_name(u_int8_t proto, int nolookup)
211{
212 unsigned int i;
213
214 if (proto && !nolookup) {
215 struct protoent *pent = getprotobynumber(proto);
216 if (pent)
217 return pent->p_name;
218 }
219
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100220 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
221 if (xtables_chain_protos[i].num == proto)
222 return xtables_chain_protos[i].name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000223
224 return NULL;
225}
226
Patrick McHardy0b639362007-09-08 16:00:01 +0000227static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000228exit_tryhelp(int status)
229{
Harald Weltea8658ca2003-03-05 07:46:15 +0000230 if (line != -1)
231 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500233 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500234 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000235 exit(status);
236}
237
Patrick McHardy0b639362007-09-08 16:00:01 +0000238static void
Jan Engelhardt395e4412009-02-10 10:43:08 +0100239exit_printhelp(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240{
Rusty Russell5eed48a2000-06-02 20:12:24 +0000241 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000242"Usage: %s -[AD] chain rule-specification [options]\n"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100243" %s -I chain [rulenum] rule-specification [options]\n"
244" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000245" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200246" %s -[LS] [chain [rulenum]] [options]\n"
247" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000248" %s -[NX] chain\n"
249" %s -E old-chain-name new-chain-name\n"
250" %s -P chain target [options]\n"
251" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500252 prog_name, prog_vers, prog_name, prog_name,
253 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100254 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255
256 printf(
257"Commands:\n"
258"Either long or short options are allowed.\n"
259" --append -A chain Append to chain\n"
260" --delete -D chain Delete matching rule from chain\n"
261" --delete -D chain rulenum\n"
262" Delete rule rulenum (1 = first) from chain\n"
263" --insert -I chain [rulenum]\n"
264" Insert in chain as rulenum (default 1=first)\n"
265" --replace -R chain rulenum\n"
266" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200267" --list -L [chain [rulenum]]\n"
268" List the rules in a chain or all chains\n"
269" --list-rules -S [chain [rulenum]]\n"
270" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271" --flush -F [chain] Delete all rules in chain or all chains\n"
Mohit Mehtab34199e2009-08-19 10:56:33 -0700272" --zero -Z [chain [rulenum]]\n"
273" Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274" --new -N chain Create a new user-defined chain\n"
275" --delete-chain\n"
276" -X [chain] Delete a user-defined chain\n"
277" --policy -P chain target\n"
278" Change policy on chain to target\n"
279" --rename-chain\n"
280" -E old-chain new-chain\n"
281" Change chain name, (moving any references)\n"
282
283"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200284"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100285"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000286" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100287"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000288" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200289"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000290" network interface name ([+] for wildcard)\n"
291" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000292" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200293#ifdef IP6T_F_GOTO
294" --goto -g chain\n"
295" jump to chain with no return\n"
296#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000297" --match -m match\n"
298" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000299" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200300"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000301" network interface name ([+] for wildcard)\n"
302" --table -t table table to manipulate (default: `filter')\n"
303" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000304" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000305" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000306/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000307" --modprobe=<command> try to insert modules using this command\n"
308" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000309"[!] --version -V print package version.\n");
310
Jan Engelhardtf89c1712009-06-12 20:48:52 +0200311 print_extension_helps(xtables_targets, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000312 exit(0);
313}
314
Patrick McHardy0b639362007-09-08 16:00:01 +0000315void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100316ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000317{
318 va_list args;
319
320 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500321 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000322 vfprintf(stderr, msg, args);
323 va_end(args);
324 fprintf(stderr, "\n");
325 if (status == PARAMETER_PROBLEM)
326 exit_tryhelp(status);
327 if (status == VERSION_PROBLEM)
328 fprintf(stderr,
329 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
330 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500331 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000332 exit(status);
333}
334
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335static void
336generic_opt_check(int command, int options)
337{
338 int i, j, legal = 0;
339
340 /* Check that commands are valid with options. Complicated by the
341 * fact that if an option is legal with *any* command given, it is
342 * legal overall (ie. -z and -l).
343 */
344 for (i = 0; i < NUMBER_OF_OPT; i++) {
345 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
346
347 for (j = 0; j < NUMBER_OF_CMD; j++) {
348 if (!(command & (1<<j)))
349 continue;
350
351 if (!(options & (1<<i))) {
352 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100353 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000354 "You need to supply the `-%c' "
355 "option for this command\n",
356 optflags[i]);
357 } else {
358 if (commands_v_options[j][i] != 'x')
359 legal = 1;
360 else if (legal == 0)
361 legal = -1;
362 }
363 }
364 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100365 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000366 "Illegal option `-%c' with this command\n",
367 optflags[i]);
368 }
369}
370
371static char
372opt2char(int option)
373{
374 const char *ptr;
375 for (ptr = optflags; option > 1; option >>= 1, ptr++);
376
377 return *ptr;
378}
379
380static char
381cmd2char(int option)
382{
383 const char *ptr;
384 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
385
386 return *ptr;
387}
388
389static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000390add_command(unsigned int *cmd, const int newcmd, const int othercmds,
391 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000392{
393 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100394 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000395 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100396 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000397 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
398 *cmd |= newcmd;
399}
400
Rusty Russell5eed48a2000-06-02 20:12:24 +0000401/*
402 * All functions starting with "parse" should succeed, otherwise
403 * the program fails.
404 * Most routines return pointers to static data that may change
405 * between calls to the same or other routines with a few exceptions:
406 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
407 * return global static data.
408*/
409
Rusty Russell5eed48a2000-06-02 20:12:24 +0000410/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200411static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100412find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100413 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000414{
Harald Welteed498492001-07-23 01:24:22 +0000415 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000416
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100417 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100418 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000419
Harald Welte43ac1912002-03-03 09:43:07 +0000420 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100421 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000422 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100423 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000424
425 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426}
427
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000428/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000429static int is_exthdr(u_int16_t proto)
430{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000431 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000432 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000433 proto == IPPROTO_AH ||
434 proto == IPPROTO_DSTOPTS);
435}
436
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437/* Can't be zero. */
438static int
439parse_rulenumber(const char *rule)
440{
Harald Welte43ac1912002-03-03 09:43:07 +0000441 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000442
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100443 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100444 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000445 "Invalid rule number `%s'", rule);
446
447 return rulenum;
448}
449
450static const char *
451parse_target(const char *targetname)
452{
453 const char *ptr;
454
455 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100456 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000457 "Invalid target name (too short)");
458
459 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100460 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000461 "Invalid target name `%s' (%u chars max)",
462 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000463
464 for (ptr = targetname; *ptr; ptr++)
465 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100466 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467 "Invalid target name `%s'", targetname);
468 return targetname;
469}
470
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471static void
472set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
473 int invert)
474{
475 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100476 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477 opt2char(option));
478 *options |= option;
479
480 if (invert) {
481 unsigned int i;
482 for (i = 0; 1 << i != option; i++);
483
484 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100485 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486 "cannot have ! before -%c",
487 opt2char(option));
488 *invflg |= inverse_for_options[i];
489 }
490}
491
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000492static void
493print_num(u_int64_t number, unsigned int format)
494{
Harald Welte43ac1912002-03-03 09:43:07 +0000495 if (format & FMT_KILOMEGAGIGA) {
496 if (number > 99999) {
497 number = (number + 500) / 1000;
498 if (number > 9999) {
499 number = (number + 500) / 1000;
500 if (number > 9999) {
501 number = (number + 500) / 1000;
502 if (number > 9999) {
503 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000504 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000505 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000506 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000507 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000508 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000509 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000510 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000511 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000512 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000513 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000514 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515}
516
Harald Welte43ac1912002-03-03 09:43:07 +0000517
Rusty Russell5eed48a2000-06-02 20:12:24 +0000518static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100519print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000520{
521 struct ip6t_counters counters;
522 const char *pol = ip6tc_get_policy(chain, &counters, handle);
523 printf("Chain %s", chain);
524 if (pol) {
525 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000526 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000527 fputc(' ', stdout);
528 print_num(counters.pcnt, (format|FMT_NOTABLE));
529 fputs("packets, ", stdout);
530 print_num(counters.bcnt, (format|FMT_NOTABLE));
531 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000532 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000533 printf(")\n");
534 } else {
535 unsigned int refs;
536 if (!ip6tc_get_references(&refs, chain, handle))
537 printf(" (ERROR obtaining refs)\n");
538 else
539 printf(" (%u references)\n", refs);
540 }
541
542 if (format & FMT_LINENUMBERS)
543 printf(FMT("%-4s ", "%s "), "num");
544 if (!(format & FMT_NOCOUNTS)) {
545 if (format & FMT_KILOMEGAGIGA) {
546 printf(FMT("%5s ","%s "), "pkts");
547 printf(FMT("%5s ","%s "), "bytes");
548 } else {
549 printf(FMT("%8s ","%s "), "pkts");
550 printf(FMT("%10s ","%s "), "bytes");
551 }
552 }
553 if (!(format & FMT_NOTARGET))
554 printf(FMT("%-9s ","%s "), "target");
555 fputs(" prot ", stdout);
556 if (format & FMT_OPTIONS)
557 fputs("opt", stdout);
558 if (format & FMT_VIA) {
559 printf(FMT(" %-6s ","%s "), "in");
560 printf(FMT("%-6s ","%s "), "out");
561 }
562 printf(FMT(" %-19s ","%s "), "source");
563 printf(FMT(" %-19s "," %s "), "destination");
564 printf("\n");
565}
566
Harald Welte43ac1912002-03-03 09:43:07 +0000567
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568static int
569print_match(const struct ip6t_entry_match *m,
570 const struct ip6t_ip6 *ip,
571 int numeric)
572{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573 struct xtables_match *match =
574 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000575
576 if (match) {
577 if (match->print)
578 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000579 else
580 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000581 } else {
582 if (m->u.user.name[0])
583 printf("UNKNOWN match `%s' ", m->u.user.name);
584 }
585 /* Don't stop iterating. */
586 return 0;
587}
588
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100589/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000590static void
591print_firewall(const struct ip6t_entry *fw,
592 const char *targname,
593 unsigned int num,
594 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100595 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000596{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200597 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000598 const struct ip6t_entry_target *t;
599 u_int8_t flags;
600 char buf[BUFSIZ];
601
Rusty Russell5eed48a2000-06-02 20:12:24 +0000602 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100603 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000604 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100605 target = xtables_find_target(IP6T_STANDARD_TARGET,
606 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000607
608 t = ip6t_get_target((struct ip6t_entry *)fw);
609 flags = fw->ipv6.flags;
610
611 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200612 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000613
614 if (!(format & FMT_NOCOUNTS)) {
615 print_num(fw->counters.pcnt, format);
616 print_num(fw->counters.bcnt, format);
617 }
618
619 if (!(format & FMT_NOTARGET))
620 printf(FMT("%-9s ", "%s "), targname);
621
622 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
623 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100624 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000625 if (pname)
626 printf(FMT("%-5s", "%s "), pname);
627 else
628 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
629 }
630
631 if (format & FMT_OPTIONS) {
632 if (format & FMT_NOTABLE)
633 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000634 fputc(' ', stdout); /* Invert flag of FRAG */
635 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000636 fputc(' ', stdout);
637 }
638
639 if (format & FMT_VIA) {
640 char iface[IFNAMSIZ+2];
641
642 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
643 iface[0] = '!';
644 iface[1] = '\0';
645 }
646 else iface[0] = '\0';
647
648 if (fw->ipv6.iniface[0] != '\0') {
649 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000650 }
651 else if (format & FMT_NUMERIC) strcat(iface, "*");
652 else strcat(iface, "any");
653 printf(FMT(" %-6s ","in %s "), iface);
654
655 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
656 iface[0] = '!';
657 iface[1] = '\0';
658 }
659 else iface[0] = '\0';
660
661 if (fw->ipv6.outiface[0] != '\0') {
662 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000663 }
664 else if (format & FMT_NUMERIC) strcat(iface, "*");
665 else strcat(iface, "any");
666 printf(FMT("%-6s ","out %s "), iface);
667 }
668
669 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000670 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000671 && !(format & FMT_NUMERIC))
672 printf(FMT("%-19s ","%s "), "anywhere");
673 else {
674 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100675 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000676 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100677 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
678 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000679 printf(FMT("%-19s ","%s "), buf);
680 }
681
682 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
683 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
684 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200685 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000686 else {
687 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100688 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100690 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
691 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200692 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000693 }
694
695 if (format & FMT_NOTABLE)
696 fputs(" ", stdout);
697
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200698#ifdef IP6T_F_GOTO
699 if(fw->ipv6.flags & IP6T_F_GOTO)
700 printf("[goto] ");
701#endif
702
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
704
705 if (target) {
706 if (target->print)
707 /* Print the target information. */
708 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
709 } else if (t->u.target_size != sizeof(*t))
710 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000711 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000712
713 if (!(format & FMT_NONEWLINE))
714 fputc('\n', stdout);
715}
716
717static void
718print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100719 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720{
721 struct ip6t_entry_target *t;
722
723 t = ip6t_get_target((struct ip6t_entry *)fw);
724 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
725}
726
727static int
728append_entry(const ip6t_chainlabel chain,
729 struct ip6t_entry *fw,
730 unsigned int nsaddrs,
731 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100732 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000733 unsigned int ndaddrs,
734 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100735 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100737 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738{
739 unsigned int i, j;
740 int ret = 1;
741
742 for (i = 0; i < nsaddrs; i++) {
743 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100744 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000745 for (j = 0; j < ndaddrs; j++) {
746 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100747 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100749 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000750 ret &= ip6tc_append_entry(chain, fw, handle);
751 }
752 }
753
754 return ret;
755}
756
757static int
758replace_entry(const ip6t_chainlabel chain,
759 struct ip6t_entry *fw,
760 unsigned int rulenum,
761 const struct in6_addr *saddr,
762 const struct in6_addr *daddr,
763 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100764 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765{
766 fw->ipv6.src = *saddr;
767 fw->ipv6.dst = *daddr;
768
769 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100770 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771 return ip6tc_replace_entry(chain, fw, rulenum, handle);
772}
773
774static int
775insert_entry(const ip6t_chainlabel chain,
776 struct ip6t_entry *fw,
777 unsigned int rulenum,
778 unsigned int nsaddrs,
779 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100780 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781 unsigned int ndaddrs,
782 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100783 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100785 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000786{
787 unsigned int i, j;
788 int ret = 1;
789
790 for (i = 0; i < nsaddrs; i++) {
791 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100792 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000793 for (j = 0; j < ndaddrs; j++) {
794 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100795 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000796 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100797 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
799 }
800 }
801
802 return ret;
803}
804
805static unsigned char *
Michael Granzow332e4ac2009-04-09 18:24:36 +0100806make_delete_mask(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000807{
808 /* Establish mask for comparison */
809 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100810 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811 unsigned char *mask, *mptr;
812
813 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000814 for (matchp = matches; matchp; matchp = matchp->next)
815 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000816
Jan Engelhardt630ef482009-01-27 14:58:41 +0100817 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000818 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000819 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820
821 memset(mask, 0xFF, sizeof(struct ip6t_entry));
822 mptr = mask + sizeof(struct ip6t_entry);
823
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000824 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000825 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000826 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000827 + matchp->match->userspacesize);
828 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000829 }
830
Max Kellermann5b76f682008-01-29 13:42:48 +0000831 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000832 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000833 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000834
835 return mask;
836}
837
838static int
839delete_entry(const ip6t_chainlabel chain,
840 struct ip6t_entry *fw,
841 unsigned int nsaddrs,
842 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100843 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 unsigned int ndaddrs,
845 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100846 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000847 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100848 struct ip6tc_handle *handle,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100849 struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850{
851 unsigned int i, j;
852 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853 unsigned char *mask;
854
Michael Granzow332e4ac2009-04-09 18:24:36 +0100855 mask = make_delete_mask(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000857 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100858 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000859 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000860 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100861 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100863 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000864 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000865 }
866 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000867 free(mask);
868
Rusty Russell5eed48a2000-06-02 20:12:24 +0000869 return ret;
870}
871
András Kis-Szabó764316a2001-02-26 17:31:20 +0000872int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100873for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
874 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875{
Max Kellermann5b76f682008-01-29 13:42:48 +0000876 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000877 const char *chain;
878 char *chains;
879 unsigned int i, chaincount = 0;
880
881 chain = ip6tc_first_chain(handle);
882 while (chain) {
883 chaincount++;
884 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000885 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000886
Jan Engelhardt630ef482009-01-27 14:58:41 +0100887 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000888 i = 0;
889 chain = ip6tc_first_chain(handle);
890 while (chain) {
891 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
892 i++;
893 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000894 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000895
896 for (i = 0; i < chaincount; i++) {
897 if (!builtinstoo
898 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100899 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000900 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000901 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000902 }
903
904 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000905 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906}
907
András Kis-Szabó764316a2001-02-26 17:31:20 +0000908int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000909flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100910 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000911{
912 if (!chain)
913 return for_each_chain(flush_entries, verbose, 1, handle);
914
915 if (verbose)
916 fprintf(stdout, "Flushing chain `%s'\n", chain);
917 return ip6tc_flush_entries(chain, handle);
918}
919
920static int
921zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100922 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000923{
924 if (!chain)
925 return for_each_chain(zero_entries, verbose, 1, handle);
926
927 if (verbose)
928 fprintf(stdout, "Zeroing chain `%s'\n", chain);
929 return ip6tc_zero_entries(chain, handle);
930}
931
András Kis-Szabó764316a2001-02-26 17:31:20 +0000932int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100934 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935{
936 if (!chain)
937 return for_each_chain(delete_chain, verbose, 0, handle);
938
939 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000940 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941 return ip6tc_delete_chain(chain, handle);
942}
943
944static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200945list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100946 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947{
948 int found = 0;
949 unsigned int format;
950 const char *this;
951
952 format = FMT_OPTIONS;
953 if (!verbose)
954 format |= FMT_NOCOUNTS;
955 else
956 format |= FMT_VIA;
957
958 if (numeric)
959 format |= FMT_NUMERIC;
960
961 if (!expanded)
962 format |= FMT_KILOMEGAGIGA;
963
964 if (linenumbers)
965 format |= FMT_LINENUMBERS;
966
967 for (this = ip6tc_first_chain(handle);
968 this;
969 this = ip6tc_next_chain(handle)) {
970 const struct ip6t_entry *i;
971 unsigned int num;
972
973 if (chain && strcmp(chain, this) != 0)
974 continue;
975
976 if (found) printf("\n");
977
Henrik Nordstrombb340822008-05-13 13:09:23 +0200978 if (!rulenum)
979 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 i = ip6tc_first_rule(this, handle);
981
982 num = 0;
983 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200984 num++;
985 if (!rulenum || num == rulenum)
986 print_firewall(i,
987 ip6tc_get_target(i, handle),
988 num,
989 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100990 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000991 i = ip6tc_next_rule(i, handle);
992 }
993 found = 1;
994 }
995
996 errno = ENOENT;
997 return found;
998}
999
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001000/* This assumes that mask is contiguous, and byte-bounded. */
1001static void
1002print_iface(char letter, const char *iface, const unsigned char *mask,
1003 int invert)
1004{
1005 unsigned int i;
1006
1007 if (mask[0] == 0)
1008 return;
1009
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001010 printf("%s-%c ", invert ? "! " : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011
1012 for (i = 0; i < IFNAMSIZ; i++) {
1013 if (mask[i] != 0) {
1014 if (iface[i] != '\0')
1015 printf("%c", iface[i]);
1016 } else {
1017 /* we can access iface[i-1] here, because
1018 * a few lines above we make sure that mask[0] != 0 */
1019 if (iface[i-1] != '\0')
1020 printf("+");
1021 break;
1022 }
1023 }
1024
1025 printf(" ");
1026}
1027
1028/* The ip6tables looks up the /etc/protocols. */
1029static void print_proto(u_int16_t proto, int invert)
1030{
1031 if (proto) {
1032 unsigned int i;
1033 const char *invertstr = invert ? "! " : "";
1034
1035 struct protoent *pent = getprotobynumber(proto);
1036 if (pent) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001037 printf("%s-p %s ",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001038 invertstr, pent->p_name);
1039 return;
1040 }
1041
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001042 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1043 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001044 printf("%s-p %s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001045 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001046 return;
1047 }
1048
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001049 printf("%s-p %u ", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001050 }
1051}
1052
1053static int print_match_save(const struct ip6t_entry_match *e,
1054 const struct ip6t_ip6 *ip)
1055{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001056 struct xtables_match *match =
1057 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001058
1059 if (match) {
1060 printf("-m %s ", e->u.user.name);
1061
1062 /* some matches don't provide a save function */
1063 if (match->save)
1064 match->save(ip, e);
1065 } else {
1066 if (e->u.match_size) {
1067 fprintf(stderr,
1068 "Can't find library for match `%s'\n",
1069 e->u.user.name);
1070 exit(1);
1071 }
1072 }
1073 return 0;
1074}
1075
1076/* print a given ip including mask if neccessary */
1077static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1078{
1079 char buf[51];
1080 int l = ipv6_prefix_length(mask);
1081
1082 if (l == 0 && !invert)
1083 return;
1084
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001085 printf("%s%s %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001086 invert ? "! " : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001087 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001088 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1089
1090 if (l == -1)
1091 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1092 else
1093 printf("/%d ", l);
1094}
1095
1096/* We want this to be readable, so only print out neccessary fields.
1097 * Because that's the kind of world I want to live in. */
1098void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001099 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001100{
1101 struct ip6t_entry_target *t;
1102 const char *target_name;
1103
1104 /* print counters for iptables-save */
1105 if (counters > 0)
1106 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1107
1108 /* print chain name */
1109 printf("-A %s ", chain);
1110
1111 /* Print IP part. */
1112 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1113 e->ipv6.invflags & IP6T_INV_SRCIP);
1114
1115 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1116 e->ipv6.invflags & IP6T_INV_DSTIP);
1117
1118 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1119 e->ipv6.invflags & IP6T_INV_VIA_IN);
1120
1121 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1122 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1123
1124 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1125
1126#if 0
1127 /* not definied in ipv6
1128 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1129 if (e->ipv6.flags & IPT_F_FRAG)
1130 printf("%s-f ",
1131 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1132#endif
1133
1134 if (e->ipv6.flags & IP6T_F_TOS)
1135 printf("%s-? %d ",
1136 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1137 e->ipv6.tos);
1138
1139 /* Print matchinfo part */
1140 if (e->target_offset) {
1141 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1142 }
1143
1144 /* print counters for iptables -R */
1145 if (counters < 0)
1146 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1147
1148 /* Print target name */
1149 target_name = ip6tc_get_target(e, h);
1150 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001151#ifdef IP6T_F_GOTO
1152 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1153#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001154 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001155#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001156
1157 /* Print targinfo part */
1158 t = ip6t_get_target((struct ip6t_entry *)e);
1159 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001160 struct xtables_target *target =
1161 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001162
1163 if (!target) {
1164 fprintf(stderr, "Can't find library for target `%s'\n",
1165 t->u.user.name);
1166 exit(1);
1167 }
1168
1169 if (target->save)
1170 target->save(&e->ipv6, t);
1171 else {
1172 /* If the target size is greater than ip6t_entry_target
1173 * there is something to be saved, we just don't know
1174 * how to print it */
1175 if (t->u.target_size !=
1176 sizeof(struct ip6t_entry_target)) {
1177 fprintf(stderr, "Target `%s' is missing "
1178 "save function\n",
1179 t->u.user.name);
1180 exit(1);
1181 }
1182 }
1183 }
1184 printf("\n");
1185}
1186
1187static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001188list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001189 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001190{
1191 const char *this = NULL;
1192 int found = 0;
1193
1194 if (counters)
1195 counters = -1; /* iptables -c format */
1196
1197 /* Dump out chain names first,
1198 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001199 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001200 this;
1201 this = ip6tc_next_chain(handle)) {
1202 if (chain && strcmp(this, chain) != 0)
1203 continue;
1204
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001205 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001206 struct ip6t_counters count;
1207 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1208 if (counters)
1209 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1210 printf("\n");
1211 } else {
1212 printf("-N %s\n", this);
1213 }
1214 }
1215
1216 for (this = ip6tc_first_chain(handle);
1217 this;
1218 this = ip6tc_next_chain(handle)) {
1219 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001220 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001221
1222 if (chain && strcmp(this, chain) != 0)
1223 continue;
1224
1225 /* Dump out rules */
1226 e = ip6tc_first_rule(this, handle);
1227 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001228 num++;
1229 if (!rulenum || num == rulenum)
1230 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001231 e = ip6tc_next_rule(e, handle);
1232 }
1233 found = 1;
1234 }
1235
1236 errno = ENOENT;
1237 return found;
1238}
1239
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240static struct ip6t_entry *
1241generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001242 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001243 struct ip6t_entry_target *target)
1244{
1245 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001246 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001247 struct ip6t_entry *e;
1248
1249 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001250 for (matchp = matches; matchp; matchp = matchp->next)
1251 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001252
Jan Engelhardt630ef482009-01-27 14:58:41 +01001253 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001254 *e = *fw;
1255 e->target_offset = size;
1256 e->next_offset = size + target->u.target_size;
1257
1258 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001259 for (matchp = matches; matchp; matchp = matchp->next) {
1260 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1261 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001262 }
1263 memcpy(e->elems + size, target, target->u.target_size);
1264
1265 return e;
1266}
1267
Jan Engelhardt395e4412009-02-10 10:43:08 +01001268static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001269{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001270 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001271
1272 for (matchp = *matches; matchp;) {
1273 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001274 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001275 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001276 matchp->match->m = NULL;
1277 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001278 if (matchp->match == matchp->match->next) {
1279 free(matchp->match);
1280 matchp->match = NULL;
1281 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001282 free(matchp);
1283 matchp = tmp;
1284 }
1285
1286 *matches = NULL;
1287}
1288
Jan Engelhardtfd187312008-11-10 16:59:27 +01001289int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001290{
1291 struct ip6t_entry fw, *e = NULL;
1292 int invert = 0;
1293 unsigned int nsaddrs = 0, ndaddrs = 0;
1294 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001295 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001296
1297 int c, verbose = 0;
1298 const char *chain = NULL;
1299 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1300 const char *policy = NULL, *newname = NULL;
1301 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001302 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001303 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001304 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001305 struct xtables_rule_match *matches = NULL;
1306 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001307 struct xtables_target *target = NULL;
1308 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001309 const char *jumpto = "";
1310 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001311 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001312 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001313
1314 memset(&fw, 0, sizeof(fw));
1315
Harald Welte43ac1912002-03-03 09:43:07 +00001316 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001317 * a second time */
1318 optind = 0;
1319
Harald Welte43ac1912002-03-03 09:43:07 +00001320 /* clear mflags in case do_command gets called a second time
1321 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001322 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001323 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001324
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001325 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001326 t->tflags = 0;
1327 t->used = 0;
1328 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001329
Rusty Russell5eed48a2000-06-02 20:12:24 +00001330 /* Suppress error messages: we may add new options if we
1331 demand-load a protocol. */
1332 opterr = 0;
1333
1334 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001335 "-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 +00001336 opts, NULL)) != -1) {
1337 switch (c) {
1338 /*
1339 * Command selection
1340 */
1341 case 'A':
1342 add_command(&command, CMD_APPEND, CMD_NONE,
1343 invert);
1344 chain = optarg;
1345 break;
1346
1347 case 'D':
1348 add_command(&command, CMD_DELETE, CMD_NONE,
1349 invert);
1350 chain = optarg;
1351 if (optind < argc && argv[optind][0] != '-'
1352 && argv[optind][0] != '!') {
1353 rulenum = parse_rulenumber(argv[optind++]);
1354 command = CMD_DELETE_NUM;
1355 }
1356 break;
1357
Rusty Russell5eed48a2000-06-02 20:12:24 +00001358 case 'R':
1359 add_command(&command, CMD_REPLACE, CMD_NONE,
1360 invert);
1361 chain = optarg;
1362 if (optind < argc && argv[optind][0] != '-'
1363 && argv[optind][0] != '!')
1364 rulenum = parse_rulenumber(argv[optind++]);
1365 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001366 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001367 "-%c requires a rule number",
1368 cmd2char(CMD_REPLACE));
1369 break;
1370
1371 case 'I':
1372 add_command(&command, CMD_INSERT, CMD_NONE,
1373 invert);
1374 chain = optarg;
1375 if (optind < argc && argv[optind][0] != '-'
1376 && argv[optind][0] != '!')
1377 rulenum = parse_rulenumber(argv[optind++]);
1378 else rulenum = 1;
1379 break;
1380
1381 case 'L':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001382 add_command(&command, CMD_LIST,
1383 CMD_ZERO | CMD_ZERO_NUM, invert);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 if (optarg) chain = optarg;
1385 else if (optind < argc && argv[optind][0] != '-'
1386 && argv[optind][0] != '!')
1387 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001388 if (optind < argc && argv[optind][0] != '-'
1389 && argv[optind][0] != '!')
1390 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 break;
1392
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001393 case 'S':
Mohit Mehtab34199e2009-08-19 10:56:33 -07001394 add_command(&command, CMD_LIST_RULES,
1395 CMD_ZERO | CMD_ZERO_NUM, invert);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001396 if (optarg) chain = optarg;
1397 else if (optind < argc && argv[optind][0] != '-'
1398 && argv[optind][0] != '!')
1399 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001400 if (optind < argc && argv[optind][0] != '-'
1401 && argv[optind][0] != '!')
1402 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001403 break;
1404
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405 case 'F':
1406 add_command(&command, CMD_FLUSH, CMD_NONE,
1407 invert);
1408 if (optarg) chain = optarg;
1409 else if (optind < argc && argv[optind][0] != '-'
1410 && argv[optind][0] != '!')
1411 chain = argv[optind++];
1412 break;
1413
1414 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001415 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 invert);
1417 if (optarg) chain = optarg;
1418 else if (optind < argc && argv[optind][0] != '-'
1419 && argv[optind][0] != '!')
1420 chain = argv[optind++];
Mohit Mehtab34199e2009-08-19 10:56:33 -07001421 if (optind < argc && argv[optind][0] != '-'
1422 && argv[optind][0] != '!') {
1423 rulenum = parse_rulenumber(argv[optind++]);
1424 command = CMD_ZERO_NUM;
1425 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001426 break;
1427
1428 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001429 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001430 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001431 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001432 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001433 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001434 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001435 "chain name may not clash "
1436 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001437 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1438 invert);
1439 chain = optarg;
1440 break;
1441
1442 case 'X':
1443 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1444 invert);
1445 if (optarg) chain = optarg;
1446 else if (optind < argc && argv[optind][0] != '-'
1447 && argv[optind][0] != '!')
1448 chain = argv[optind++];
1449 break;
1450
1451 case 'E':
1452 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1453 invert);
1454 chain = optarg;
1455 if (optind < argc && argv[optind][0] != '-'
1456 && argv[optind][0] != '!')
1457 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001458 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001459 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001460 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001461 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001462 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001463 break;
1464
1465 case 'P':
1466 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1467 invert);
1468 chain = optarg;
1469 if (optind < argc && argv[optind][0] != '-'
1470 && argv[optind][0] != '!')
1471 policy = argv[optind++];
1472 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001473 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001474 "-%c requires a chain and a policy",
1475 cmd2char(CMD_SET_POLICY));
1476 break;
1477
1478 case 'h':
1479 if (!optarg)
1480 optarg = argv[optind];
1481
Jonas Berlin1b91e592005-04-01 06:58:38 +00001482 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001483 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001484 xtables_find_match(protocol, XTF_TRY_LOAD,
1485 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001486
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001487 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001488
1489 /*
1490 * Option selection
1491 */
1492 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001493 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1495 invert);
1496
1497 /* Canonicalize into lower case */
1498 for (protocol = argv[optind-1]; *protocol; protocol++)
1499 *protocol = tolower(*protocol);
1500
1501 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001502 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001503 fw.ipv6.flags |= IP6T_F_PROTO;
1504
1505 if (fw.ipv6.proto == 0
1506 && (fw.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001507 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001508 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001509
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001510 if (is_exthdr(fw.ipv6.proto)
1511 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001512 fprintf(stderr,
1513 "Warning: never matched protocol: %s. "
1514 "use extension match instead.\n",
1515 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516 break;
1517
1518 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001519 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001520 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1521 invert);
1522 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001523 break;
1524
1525 case 'd':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001526 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001527 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1528 invert);
1529 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530 break;
1531
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001532#ifdef IP6T_F_GOTO
1533 case 'g':
1534 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1535 invert);
1536 fw.ipv6.flags |= IP6T_F_GOTO;
1537 jumpto = parse_target(optarg);
1538 break;
1539#endif
1540
Rusty Russell5eed48a2000-06-02 20:12:24 +00001541 case 'j':
1542 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1543 invert);
1544 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001545 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001546 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547
1548 if (target) {
1549 size_t size;
1550
Harald Welte43ac1912002-03-03 09:43:07 +00001551 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1552 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001553
Jan Engelhardt630ef482009-01-27 14:58:41 +01001554 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001555 target->t->u.target_size = size;
1556 strcpy(target->t->u.user.name, jumpto);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001557 xtables_set_revision(target->t->u.user.name,
Patrick McHardy455559b2008-04-15 15:51:19 +02001558 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001559 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001560 target->init(target->t);
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001561 opts = xtables_merge_options(opts,
Patrick McHardy455559b2008-04-15 15:51:19 +02001562 target->extra_opts,
1563 &target->option_offset);
1564 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001565 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001566 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567 }
1568 break;
1569
1570
1571 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001572 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001573 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1574 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001575 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001576 fw.ipv6.iniface,
1577 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578 break;
1579
1580 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001581 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001582 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1583 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001584 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001585 fw.ipv6.outiface,
1586 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587 break;
1588
1589 case 'v':
1590 if (!verbose)
1591 set_option(&options, OPT_VERBOSE,
1592 &fw.ipv6.invflags, invert);
1593 verbose++;
1594 break;
1595
1596 case 'm': {
1597 size_t size;
1598
1599 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001600 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601 "unexpected ! flag before --match");
1602
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001603 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1604 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001605 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1606 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001607 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 m->m->u.match_size = size;
1609 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001610 xtables_set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001611 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001612 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001613 if (m != m->next)
1614 /* Merge options for non-cloned matches */
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001615 opts = xtables_merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001616 }
1617 break;
1618
1619 case 'n':
1620 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1621 invert);
1622 break;
1623
1624 case 't':
1625 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001626 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001627 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001628 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 break;
1630
1631 case 'x':
1632 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1633 invert);
1634 break;
1635
1636 case 'V':
1637 if (invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001638 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001639 else
1640 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001641 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001642 exit(0);
1643
1644 case '0':
1645 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1646 invert);
1647 break;
1648
Harald Welte43ac1912002-03-03 09:43:07 +00001649 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001650 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001651 break;
1652
1653 case 'c':
1654
1655 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1656 invert);
1657 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001658 bcnt = strchr(pcnt + 1, ',');
1659 if (bcnt)
1660 bcnt++;
1661 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001662 && argv[optind][0] != '!')
1663 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001664 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001665 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001666 "-%c requires packet and byte counter",
1667 opt2char(OPT_COUNTERS));
1668
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001669 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001670 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001671 "-%c packet counter not numeric",
1672 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001673 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001674
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001675 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001676 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001677 "-%c byte counter not numeric",
1678 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001679 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001680 break;
1681
Rusty Russell5eed48a2000-06-02 20:12:24 +00001682 case 1: /* non option */
1683 if (optarg[0] == '!' && optarg[1] == '\0') {
1684 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001685 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001686 "multiple consecutive ! not"
1687 " allowed");
1688 invert = TRUE;
1689 optarg[0] = '\0';
1690 continue;
1691 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001692 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693 exit_tryhelp(2);
1694
1695 default:
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001696 if (target == NULL || target->parse == NULL ||
1697 !target->parse(c - target->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001698 argv, invert,
1699 &target->tflags,
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001700 &fw, &target->t)) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001701 for (matchp = matches; matchp; matchp = matchp->next) {
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001702 if (matchp->completed ||
1703 matchp->match->parse == NULL)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001704 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001705 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001707 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001708 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001709 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 break;
1711 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001712 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001713
1714 /* If you listen carefully, you can
1715 actually hear this code suck. */
1716
1717 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001718 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001719 * parameter, that has not been parsed yet,
1720 * it's not an option of an explicitly loaded
1721 * match or a target. However, we support
1722 * implicit loading of the protocol match
1723 * extension. '-p tcp' means 'l4 proto 6' and
1724 * at the same time 'load tcp protocol match on
1725 * demand if we specify --dport'.
1726 *
1727 * To make this work, we need to make sure:
1728 * - the parameter has not been parsed by
1729 * a match (m above)
1730 * - a protocol has been specified
1731 * - the protocol extension has not been
1732 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001733 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001734 * - the protocol extension can be successively
1735 * loaded
1736 */
1737 if (m == NULL
1738 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001739 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001740 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001741 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001742 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001743 && (proto_used == 0))
1744 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001745 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001746 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001747 /* Try loading protocol */
1748 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001749
Harald Welte00f5a9c2002-08-26 14:37:35 +00001750 proto_used = 1;
1751
1752 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1753 + m->size;
1754
Jan Engelhardt630ef482009-01-27 14:58:41 +01001755 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001756 m->m->u.match_size = size;
1757 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001758 xtables_set_revision(m->m->u.user.name,
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001759 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001760 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001761 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001762
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001763 opts = xtables_merge_options(opts,
Harald Welte00f5a9c2002-08-26 14:37:35 +00001764 m->extra_opts, &m->option_offset);
1765
1766 optind--;
1767 continue;
1768 }
1769
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001770 if (!m) {
1771 if (c == '?') {
1772 if (optopt) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001773 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001774 PARAMETER_PROBLEM,
1775 "option `%s' "
1776 "requires an "
1777 "argument",
1778 argv[optind-1]);
1779 } else {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001780 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001781 PARAMETER_PROBLEM,
1782 "unknown option "
1783 "`%s'",
1784 argv[optind-1]);
1785 }
1786 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001787 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001788 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001789 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001790 }
1791 }
1792 invert = FALSE;
1793 }
1794
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001795 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001796 if (matchp->match->final_check != NULL)
1797 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001798
Jan Engelhardt830132a2007-10-04 16:24:50 +00001799 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001800 target->final_check(target->tflags);
1801
1802 /* Fix me: must put inverse options checking here --MN */
1803
1804 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001805 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001806 "unknown arguments found on commandline");
1807 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001808 xtables_error(PARAMETER_PROBLEM, "no command specified");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001809 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001810 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001811 "nothing appropriate following !");
1812
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001813 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001814 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001815 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001816 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001817 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001818 }
1819
1820 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001821 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1822 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001823
1824 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001825 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1826 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001827
1828 if ((nsaddrs > 1 || ndaddrs > 1) &&
1829 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001830 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001831 " source or destination IP addresses");
1832
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001834 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835 "specify a unique address");
1836
1837 generic_opt_check(command, options);
1838
1839 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001840 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001841 "chain name `%s' too long (must be under %i chars)",
1842 chain, IP6T_FUNCTION_MAXNAMELEN);
1843
Harald Welte43ac1912002-03-03 09:43:07 +00001844 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001845 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001846 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001847
Rusty Russell8beb0492004-12-22 00:37:10 +00001848 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001849 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001850 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001851
Harald Welte43ac1912002-03-03 09:43:07 +00001852 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001853 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001854 "can't initialize ip6tables table `%s': %s",
1855 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001856
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001857 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858 || command == CMD_DELETE
1859 || command == CMD_INSERT
1860 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001861 if (strcmp(chain, "PREROUTING") == 0
1862 || strcmp(chain, "INPUT") == 0) {
1863 /* -o not valid with incoming packets. */
1864 if (options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001865 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001866 "Can't use -%c with %s\n",
1867 opt2char(OPT_VIANAMEOUT),
1868 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001869 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001870
Harald Welte43ac1912002-03-03 09:43:07 +00001871 if (strcmp(chain, "POSTROUTING") == 0
1872 || strcmp(chain, "OUTPUT") == 0) {
1873 /* -i not valid with outgoing packets */
1874 if (options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001875 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001876 "Can't use -%c with %s\n",
1877 opt2char(OPT_VIANAMEIN),
1878 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001879 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001880
1881 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001882 fprintf(stderr,
1883 "Warning: using chain %s, not extension\n",
1884 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001885
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001886 if (target->t)
1887 free(target->t);
1888
Rusty Russell5eed48a2000-06-02 20:12:24 +00001889 target = NULL;
1890 }
1891
1892 /* If they didn't specify a target, or it's a chain
1893 name, use standard. */
1894 if (!target
1895 && (strlen(jumpto) == 0
1896 || ip6tc_is_chain(jumpto, *handle))) {
1897 size_t size;
1898
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001899 target = xtables_find_target(IP6T_STANDARD_TARGET,
1900 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001901
1902 size = sizeof(struct ip6t_entry_target)
1903 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001904 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905 target->t->u.target_size = size;
1906 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001907 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001908 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 }
1910
1911 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001912 /* it is no chain, and we can't load a plugin.
1913 * We cannot know if the plugin is corrupt, non
1914 * existant OR if the user just misspelled a
1915 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001916#ifdef IP6T_F_GOTO
1917 if (fw.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001918 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001919 "goto '%s' is not a chain\n", jumpto);
1920#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001921 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001923 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001924 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001925 }
1926 }
1927
1928 switch (command) {
1929 case CMD_APPEND:
1930 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001931 nsaddrs, saddrs, smasks,
1932 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001934 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001936 case CMD_DELETE:
1937 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001938 nsaddrs, saddrs, smasks,
1939 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001940 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001941 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 break;
1943 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001944 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001945 break;
1946 case CMD_REPLACE:
1947 ret = replace_entry(chain, e, rulenum - 1,
1948 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001949 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001950 break;
1951 case CMD_INSERT:
1952 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001953 nsaddrs, saddrs, smasks,
1954 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001955 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001956 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001958 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001959 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 break;
1961 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001962 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 break;
Mohit Mehtab34199e2009-08-19 10:56:33 -07001964 case CMD_ZERO_NUM:
1965 ret = ip6tc_zero_counter(chain, rulenum, *handle);
1966 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001967 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 case CMD_LIST|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001969 case CMD_LIST|CMD_ZERO_NUM:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001970 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001971 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001972 options&OPT_VERBOSE,
1973 options&OPT_NUMERIC,
1974 options&OPT_EXPANDED,
1975 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001976 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001977 if (ret && (command & CMD_ZERO))
1978 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001979 options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001980 if (ret && (command & CMD_ZERO_NUM))
1981 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001982 break;
1983 case CMD_LIST_RULES:
1984 case CMD_LIST_RULES|CMD_ZERO:
Mohit Mehtab34199e2009-08-19 10:56:33 -07001985 case CMD_LIST_RULES|CMD_ZERO_NUM:
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001986 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001987 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001988 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001989 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001990 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001991 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001992 options&OPT_VERBOSE, *handle);
Mohit Mehtab34199e2009-08-19 10:56:33 -07001993 if (ret && (command & CMD_ZERO_NUM))
1994 ret = ip6tc_zero_counter(chain, rulenum, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001995 break;
1996 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001997 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001998 break;
1999 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002000 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 break;
2002 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002003 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002004 break;
2005 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002006 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007 break;
2008 default:
2009 /* We should never reach this... */
2010 exit_tryhelp(2);
2011 }
2012
2013 if (verbose > 1)
2014 dump_entries6(*handle);
2015
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002016 clear_rule_matches(&matches);
2017
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002018 if (e != NULL) {
2019 free(e);
2020 e = NULL;
2021 }
2022
Michael Granzow332e4ac2009-04-09 18:24:36 +01002023 free(saddrs);
2024 free(smasks);
2025 free(daddrs);
2026 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002027 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002028
Rusty Russell5eed48a2000-06-02 20:12:24 +00002029 return ret;
2030}