blob: 06c0a608e6223630b26b1407e4e277f904f66331 [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"
Rusty Russell5eed48a2000-06-02 20:12:24 +000046
47#ifndef TRUE
48#define TRUE 1
49#endif
50#ifndef FALSE
51#define FALSE 0
52#endif
53
Rusty Russell5eed48a2000-06-02 20:12:24 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000081#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020082#define CMD_LIST_RULES 0x1000U
83#define NUMBER_OF_CMD 14
Rusty Russell5eed48a2000-06-02 20:12:24 +000084static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020085 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000086
Rusty Russell5eed48a2000-06-02 20:12:24 +000087#define OPT_NONE 0x00000U
88#define OPT_NUMERIC 0x00001U
89#define OPT_SOURCE 0x00002U
90#define OPT_DESTINATION 0x00004U
91#define OPT_PROTOCOL 0x00008U
92#define OPT_JUMP 0x00010U
93#define OPT_VERBOSE 0x00020U
94#define OPT_EXPANDED 0x00040U
95#define OPT_VIANAMEIN 0x00080U
96#define OPT_VIANAMEOUT 0x00100U
97#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000098#define OPT_COUNTERS 0x00400U
99#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000100static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102
103static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100104 {.name = "append", .has_arg = 1, .val = 'A'},
105 {.name = "delete", .has_arg = 1, .val = 'D'},
106 {.name = "insert", .has_arg = 1, .val = 'I'},
107 {.name = "replace", .has_arg = 1, .val = 'R'},
108 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200109 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100110 {.name = "flush", .has_arg = 2, .val = 'F'},
111 {.name = "zero", .has_arg = 2, .val = 'Z'},
112 {.name = "new-chain", .has_arg = 1, .val = 'N'},
113 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
114 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
115 {.name = "policy", .has_arg = 1, .val = 'P'},
116 {.name = "source", .has_arg = 1, .val = 's'},
117 {.name = "destination", .has_arg = 1, .val = 'd'},
118 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
119 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
120 {.name = "protocol", .has_arg = 1, .val = 'p'},
121 {.name = "in-interface", .has_arg = 1, .val = 'i'},
122 {.name = "jump", .has_arg = 1, .val = 'j'},
123 {.name = "table", .has_arg = 1, .val = 't'},
124 {.name = "match", .has_arg = 1, .val = 'm'},
125 {.name = "numeric", .has_arg = 0, .val = 'n'},
126 {.name = "out-interface", .has_arg = 1, .val = 'o'},
127 {.name = "verbose", .has_arg = 0, .val = 'v'},
128 {.name = "exact", .has_arg = 0, .val = 'x'},
129 {.name = "version", .has_arg = 0, .val = 'V'},
130 {.name = "help", .has_arg = 2, .val = 'h'},
131 {.name = "line-numbers", .has_arg = 0, .val = '0'},
132 {.name = "modprobe", .has_arg = 1, .val = 'M'},
133 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200134 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100135 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000136};
137
Harald Weltea8658ca2003-03-05 07:46:15 +0000138/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
139 * current line of the input file, in order to give a more precise error
140 * message. ip6tables itself doesn't need this, so it is initialized to the
141 * magic number of -1 */
142int line = -1;
143
Rusty Russell5eed48a2000-06-02 20:12:24 +0000144static struct option *opts = original_opts;
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
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500197#define prog_name ip6tables_globals.program_name
198#define prog_vers ip6tables_globals.program_version
Rusty Russell5eed48a2000-06-02 20:12:24 +0000199/* A few hardcoded protocols for 'all' and in case the user has no
200 /etc/protocols */
201struct pprot {
202 char *name;
203 u_int8_t num;
204};
205
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100206static const char *
Rusty Russell5eed48a2000-06-02 20:12:24 +0000207proto_to_name(u_int8_t proto, int nolookup)
208{
209 unsigned int i;
210
211 if (proto && !nolookup) {
212 struct protoent *pent = getprotobynumber(proto);
213 if (pent)
214 return pent->p_name;
215 }
216
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100217 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
218 if (xtables_chain_protos[i].num == proto)
219 return xtables_chain_protos[i].name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000220
221 return NULL;
222}
223
Patrick McHardy0b639362007-09-08 16:00:01 +0000224static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000225exit_tryhelp(int status)
226{
Harald Weltea8658ca2003-03-05 07:46:15 +0000227 if (line != -1)
228 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000229 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500230 prog_name, prog_name);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500231 xtables_free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232 exit(status);
233}
234
Patrick McHardy0b639362007-09-08 16:00:01 +0000235static void
Jan Engelhardt395e4412009-02-10 10:43:08 +0100236exit_printhelp(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000237{
Jan Engelhardt395e4412009-02-10 10:43:08 +0100238 struct xtables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200239 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240
241 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"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000243" %s -[RI] chain rulenum rule-specification [options]\n"
244" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200245" %s -[LS] [chain [rulenum]] [options]\n"
246" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000247" %s -[NX] chain\n"
248" %s -E old-chain-name new-chain-name\n"
249" %s -P chain target [options]\n"
250" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500251 prog_name, prog_vers, prog_name, prog_name,
252 prog_name, prog_name, prog_name, prog_name,
253 prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000254
255 printf(
256"Commands:\n"
257"Either long or short options are allowed.\n"
258" --append -A chain Append to chain\n"
259" --delete -D chain Delete matching rule from chain\n"
260" --delete -D chain rulenum\n"
261" Delete rule rulenum (1 = first) from chain\n"
262" --insert -I chain [rulenum]\n"
263" Insert in chain as rulenum (default 1=first)\n"
264" --replace -R chain rulenum\n"
265" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200266" --list -L [chain [rulenum]]\n"
267" List the rules in a chain or all chains\n"
268" --list-rules -S [chain [rulenum]]\n"
269" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270" --flush -F [chain] Delete all rules in chain or all chains\n"
271" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272" --new -N chain Create a new user-defined chain\n"
273" --delete-chain\n"
274" -X [chain] Delete a user-defined chain\n"
275" --policy -P chain target\n"
276" Change policy on chain to target\n"
277" --rename-chain\n"
278" -E old-chain new-chain\n"
279" Change chain name, (moving any references)\n"
280
281"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200282"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
283"[!] --source -s address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000284" source specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200285"[!] --destination -d address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000286" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200287"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000288" network interface name ([+] for wildcard)\n"
289" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000290" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200291#ifdef IP6T_F_GOTO
292" --goto -g chain\n"
293" jump to chain with no return\n"
294#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000295" --match -m match\n"
296" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000297" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200298"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000299" network interface name ([+] for wildcard)\n"
300" --table -t table table to manipulate (default: `filter')\n"
301" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000302" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000303" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000304/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000305" --modprobe=<command> try to insert modules using this command\n"
306" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000307"[!] --version -V print package version.\n");
308
Max Kellermann5b76f682008-01-29 13:42:48 +0000309 /* Print out any special helps. A user might like to be able to add a --help
310 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000311 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000312 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000313 if (t->used) {
314 printf("\n");
315 t->help();
316 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000317 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000318 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000319 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000320 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000321 }
322 exit(0);
323}
324
Patrick McHardy0b639362007-09-08 16:00:01 +0000325void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100326ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000327{
328 va_list args;
329
330 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500331 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000332 vfprintf(stderr, msg, args);
333 va_end(args);
334 fprintf(stderr, "\n");
335 if (status == PARAMETER_PROBLEM)
336 exit_tryhelp(status);
337 if (status == VERSION_PROBLEM)
338 fprintf(stderr,
339 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
340 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500341 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000342 exit(status);
343}
344
Rusty Russell5eed48a2000-06-02 20:12:24 +0000345static void
346generic_opt_check(int command, int options)
347{
348 int i, j, legal = 0;
349
350 /* Check that commands are valid with options. Complicated by the
351 * fact that if an option is legal with *any* command given, it is
352 * legal overall (ie. -z and -l).
353 */
354 for (i = 0; i < NUMBER_OF_OPT; i++) {
355 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
356
357 for (j = 0; j < NUMBER_OF_CMD; j++) {
358 if (!(command & (1<<j)))
359 continue;
360
361 if (!(options & (1<<i))) {
362 if (commands_v_options[j][i] == '+')
363 exit_error(PARAMETER_PROBLEM,
364 "You need to supply the `-%c' "
365 "option for this command\n",
366 optflags[i]);
367 } else {
368 if (commands_v_options[j][i] != 'x')
369 legal = 1;
370 else if (legal == 0)
371 legal = -1;
372 }
373 }
374 if (legal == -1)
375 exit_error(PARAMETER_PROBLEM,
376 "Illegal option `-%c' with this command\n",
377 optflags[i]);
378 }
379}
380
381static char
382opt2char(int option)
383{
384 const char *ptr;
385 for (ptr = optflags; option > 1; option >>= 1, ptr++);
386
387 return *ptr;
388}
389
390static char
391cmd2char(int option)
392{
393 const char *ptr;
394 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
395
396 return *ptr;
397}
398
399static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000400add_command(unsigned int *cmd, const int newcmd, const int othercmds,
401 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000402{
403 if (invert)
404 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
405 if (*cmd & (~othercmds))
406 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
407 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
408 *cmd |= newcmd;
409}
410
Rusty Russell5eed48a2000-06-02 20:12:24 +0000411/*
412 * All functions starting with "parse" should succeed, otherwise
413 * the program fails.
414 * Most routines return pointers to static data that may change
415 * between calls to the same or other routines with a few exceptions:
416 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
417 * return global static data.
418*/
419
Rusty Russell5eed48a2000-06-02 20:12:24 +0000420/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200421static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100422find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100423 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000424{
Harald Welteed498492001-07-23 01:24:22 +0000425 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100427 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100428 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000429
Harald Welte43ac1912002-03-03 09:43:07 +0000430 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100431 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000432 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100433 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000434
435 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000436}
437
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000438/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000439static int is_exthdr(u_int16_t proto)
440{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000441 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000442 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000443 proto == IPPROTO_AH ||
444 proto == IPPROTO_DSTOPTS);
445}
446
Rusty Russell5eed48a2000-06-02 20:12:24 +0000447/* Can't be zero. */
448static int
449parse_rulenumber(const char *rule)
450{
Harald Welte43ac1912002-03-03 09:43:07 +0000451 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000452
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100453 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000454 exit_error(PARAMETER_PROBLEM,
455 "Invalid rule number `%s'", rule);
456
457 return rulenum;
458}
459
460static const char *
461parse_target(const char *targetname)
462{
463 const char *ptr;
464
465 if (strlen(targetname) < 1)
466 exit_error(PARAMETER_PROBLEM,
467 "Invalid target name (too short)");
468
469 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
470 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000471 "Invalid target name `%s' (%u chars max)",
472 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000473
474 for (ptr = targetname; *ptr; ptr++)
475 if (isspace(*ptr))
476 exit_error(PARAMETER_PROBLEM,
477 "Invalid target name `%s'", targetname);
478 return targetname;
479}
480
Rusty Russell5eed48a2000-06-02 20:12:24 +0000481static void
482set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
483 int invert)
484{
485 if (*options & option)
486 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
487 opt2char(option));
488 *options |= option;
489
490 if (invert) {
491 unsigned int i;
492 for (i = 0; 1 << i != option; i++);
493
494 if (!inverse_for_options[i])
495 exit_error(PARAMETER_PROBLEM,
496 "cannot have ! before -%c",
497 opt2char(option));
498 *invflg |= inverse_for_options[i];
499 }
500}
501
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000502static void
503print_num(u_int64_t number, unsigned int format)
504{
Harald Welte43ac1912002-03-03 09:43:07 +0000505 if (format & FMT_KILOMEGAGIGA) {
506 if (number > 99999) {
507 number = (number + 500) / 1000;
508 if (number > 9999) {
509 number = (number + 500) / 1000;
510 if (number > 9999) {
511 number = (number + 500) / 1000;
512 if (number > 9999) {
513 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000514 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000515 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000516 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000517 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000518 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000519 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000520 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000521 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000522 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000523 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000524 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000525}
526
Harald Welte43ac1912002-03-03 09:43:07 +0000527
Rusty Russell5eed48a2000-06-02 20:12:24 +0000528static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100529print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000530{
531 struct ip6t_counters counters;
532 const char *pol = ip6tc_get_policy(chain, &counters, handle);
533 printf("Chain %s", chain);
534 if (pol) {
535 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000536 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000537 fputc(' ', stdout);
538 print_num(counters.pcnt, (format|FMT_NOTABLE));
539 fputs("packets, ", stdout);
540 print_num(counters.bcnt, (format|FMT_NOTABLE));
541 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000542 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000543 printf(")\n");
544 } else {
545 unsigned int refs;
546 if (!ip6tc_get_references(&refs, chain, handle))
547 printf(" (ERROR obtaining refs)\n");
548 else
549 printf(" (%u references)\n", refs);
550 }
551
552 if (format & FMT_LINENUMBERS)
553 printf(FMT("%-4s ", "%s "), "num");
554 if (!(format & FMT_NOCOUNTS)) {
555 if (format & FMT_KILOMEGAGIGA) {
556 printf(FMT("%5s ","%s "), "pkts");
557 printf(FMT("%5s ","%s "), "bytes");
558 } else {
559 printf(FMT("%8s ","%s "), "pkts");
560 printf(FMT("%10s ","%s "), "bytes");
561 }
562 }
563 if (!(format & FMT_NOTARGET))
564 printf(FMT("%-9s ","%s "), "target");
565 fputs(" prot ", stdout);
566 if (format & FMT_OPTIONS)
567 fputs("opt", stdout);
568 if (format & FMT_VIA) {
569 printf(FMT(" %-6s ","%s "), "in");
570 printf(FMT("%-6s ","%s "), "out");
571 }
572 printf(FMT(" %-19s ","%s "), "source");
573 printf(FMT(" %-19s "," %s "), "destination");
574 printf("\n");
575}
576
Harald Welte43ac1912002-03-03 09:43:07 +0000577
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578static int
579print_match(const struct ip6t_entry_match *m,
580 const struct ip6t_ip6 *ip,
581 int numeric)
582{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100583 struct xtables_match *match =
584 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000585
586 if (match) {
587 if (match->print)
588 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000589 else
590 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000591 } else {
592 if (m->u.user.name[0])
593 printf("UNKNOWN match `%s' ", m->u.user.name);
594 }
595 /* Don't stop iterating. */
596 return 0;
597}
598
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100599/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000600static void
601print_firewall(const struct ip6t_entry *fw,
602 const char *targname,
603 unsigned int num,
604 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100605 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000606{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200607 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000608 const struct ip6t_entry_target *t;
609 u_int8_t flags;
610 char buf[BUFSIZ];
611
Rusty Russell5eed48a2000-06-02 20:12:24 +0000612 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100613 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100615 target = xtables_find_target(IP6T_STANDARD_TARGET,
616 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000617
618 t = ip6t_get_target((struct ip6t_entry *)fw);
619 flags = fw->ipv6.flags;
620
621 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200622 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000623
624 if (!(format & FMT_NOCOUNTS)) {
625 print_num(fw->counters.pcnt, format);
626 print_num(fw->counters.bcnt, format);
627 }
628
629 if (!(format & FMT_NOTARGET))
630 printf(FMT("%-9s ", "%s "), targname);
631
632 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
633 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100634 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000635 if (pname)
636 printf(FMT("%-5s", "%s "), pname);
637 else
638 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
639 }
640
641 if (format & FMT_OPTIONS) {
642 if (format & FMT_NOTABLE)
643 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000644 fputc(' ', stdout); /* Invert flag of FRAG */
645 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000646 fputc(' ', stdout);
647 }
648
649 if (format & FMT_VIA) {
650 char iface[IFNAMSIZ+2];
651
652 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
653 iface[0] = '!';
654 iface[1] = '\0';
655 }
656 else iface[0] = '\0';
657
658 if (fw->ipv6.iniface[0] != '\0') {
659 strcat(iface, fw->ipv6.iniface);
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 ","in %s "), iface);
664
665 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
666 iface[0] = '!';
667 iface[1] = '\0';
668 }
669 else iface[0] = '\0';
670
671 if (fw->ipv6.outiface[0] != '\0') {
672 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000673 }
674 else if (format & FMT_NUMERIC) strcat(iface, "*");
675 else strcat(iface, "any");
676 printf(FMT("%-6s ","out %s "), iface);
677 }
678
679 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000680 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000681 && !(format & FMT_NUMERIC))
682 printf(FMT("%-19s ","%s "), "anywhere");
683 else {
684 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100685 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
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.src));
688 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 printf(FMT("%-19s ","%s "), buf);
690 }
691
692 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
693 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
694 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200695 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696 else {
697 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100698 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000699 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100700 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
701 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200702 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 }
704
705 if (format & FMT_NOTABLE)
706 fputs(" ", stdout);
707
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200708#ifdef IP6T_F_GOTO
709 if(fw->ipv6.flags & IP6T_F_GOTO)
710 printf("[goto] ");
711#endif
712
Rusty Russell5eed48a2000-06-02 20:12:24 +0000713 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
714
715 if (target) {
716 if (target->print)
717 /* Print the target information. */
718 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
719 } else if (t->u.target_size != sizeof(*t))
720 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000721 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000722
723 if (!(format & FMT_NONEWLINE))
724 fputc('\n', stdout);
725}
726
727static void
728print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100729 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730{
731 struct ip6t_entry_target *t;
732
733 t = ip6t_get_target((struct ip6t_entry *)fw);
734 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
735}
736
737static int
738append_entry(const ip6t_chainlabel chain,
739 struct ip6t_entry *fw,
740 unsigned int nsaddrs,
741 const struct in6_addr saddrs[],
742 unsigned int ndaddrs,
743 const struct in6_addr daddrs[],
744 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100745 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746{
747 unsigned int i, j;
748 int ret = 1;
749
750 for (i = 0; i < nsaddrs; i++) {
751 fw->ipv6.src = saddrs[i];
752 for (j = 0; j < ndaddrs; j++) {
753 fw->ipv6.dst = daddrs[j];
754 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100755 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756 ret &= ip6tc_append_entry(chain, fw, handle);
757 }
758 }
759
760 return ret;
761}
762
763static int
764replace_entry(const ip6t_chainlabel chain,
765 struct ip6t_entry *fw,
766 unsigned int rulenum,
767 const struct in6_addr *saddr,
768 const struct in6_addr *daddr,
769 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100770 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771{
772 fw->ipv6.src = *saddr;
773 fw->ipv6.dst = *daddr;
774
775 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100776 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777 return ip6tc_replace_entry(chain, fw, rulenum, handle);
778}
779
780static int
781insert_entry(const ip6t_chainlabel chain,
782 struct ip6t_entry *fw,
783 unsigned int rulenum,
784 unsigned int nsaddrs,
785 const struct in6_addr saddrs[],
786 unsigned int ndaddrs,
787 const struct in6_addr daddrs[],
788 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100789 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790{
791 unsigned int i, j;
792 int ret = 1;
793
794 for (i = 0; i < nsaddrs; i++) {
795 fw->ipv6.src = saddrs[i];
796 for (j = 0; j < ndaddrs; j++) {
797 fw->ipv6.dst = daddrs[j];
798 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100799 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
801 }
802 }
803
804 return ret;
805}
806
807static unsigned char *
Jan Engelhardt395e4412009-02-10 10:43:08 +0100808make_delete_mask(struct ip6t_entry *fw, struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809{
810 /* Establish mask for comparison */
811 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100812 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 unsigned char *mask, *mptr;
814
815 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000816 for (matchp = matches; matchp; matchp = matchp->next)
817 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818
Jan Engelhardt630ef482009-01-27 14:58:41 +0100819 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000820 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000821 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822
823 memset(mask, 0xFF, sizeof(struct ip6t_entry));
824 mptr = mask + sizeof(struct ip6t_entry);
825
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000826 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000828 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000829 + matchp->match->userspacesize);
830 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831 }
832
Max Kellermann5b76f682008-01-29 13:42:48 +0000833 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000834 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000835 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836
837 return mask;
838}
839
840static int
841delete_entry(const ip6t_chainlabel chain,
842 struct ip6t_entry *fw,
843 unsigned int nsaddrs,
844 const struct in6_addr saddrs[],
845 unsigned int ndaddrs,
846 const struct in6_addr daddrs[],
847 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
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000855 mask = make_delete_mask(fw, 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];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000858 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000859 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000860 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100861 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000862 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000863 }
864 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000865 free(mask);
866
Rusty Russell5eed48a2000-06-02 20:12:24 +0000867 return ret;
868}
869
András Kis-Szabó764316a2001-02-26 17:31:20 +0000870int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100871for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
872 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873{
Max Kellermann5b76f682008-01-29 13:42:48 +0000874 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875 const char *chain;
876 char *chains;
877 unsigned int i, chaincount = 0;
878
879 chain = ip6tc_first_chain(handle);
880 while (chain) {
881 chaincount++;
882 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000883 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000884
Jan Engelhardt630ef482009-01-27 14:58:41 +0100885 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000886 i = 0;
887 chain = ip6tc_first_chain(handle);
888 while (chain) {
889 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
890 i++;
891 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000892 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000893
894 for (i = 0; i < chaincount; i++) {
895 if (!builtinstoo
896 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100897 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000899 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000900 }
901
902 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000903 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000904}
905
András Kis-Szabó764316a2001-02-26 17:31:20 +0000906int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000907flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100908 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000909{
910 if (!chain)
911 return for_each_chain(flush_entries, verbose, 1, handle);
912
913 if (verbose)
914 fprintf(stdout, "Flushing chain `%s'\n", chain);
915 return ip6tc_flush_entries(chain, handle);
916}
917
918static int
919zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100920 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000921{
922 if (!chain)
923 return for_each_chain(zero_entries, verbose, 1, handle);
924
925 if (verbose)
926 fprintf(stdout, "Zeroing chain `%s'\n", chain);
927 return ip6tc_zero_entries(chain, handle);
928}
929
András Kis-Szabó764316a2001-02-26 17:31:20 +0000930int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000931delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100932 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933{
934 if (!chain)
935 return for_each_chain(delete_chain, verbose, 0, handle);
936
937 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000938 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000939 return ip6tc_delete_chain(chain, handle);
940}
941
942static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200943list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100944 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000945{
946 int found = 0;
947 unsigned int format;
948 const char *this;
949
950 format = FMT_OPTIONS;
951 if (!verbose)
952 format |= FMT_NOCOUNTS;
953 else
954 format |= FMT_VIA;
955
956 if (numeric)
957 format |= FMT_NUMERIC;
958
959 if (!expanded)
960 format |= FMT_KILOMEGAGIGA;
961
962 if (linenumbers)
963 format |= FMT_LINENUMBERS;
964
965 for (this = ip6tc_first_chain(handle);
966 this;
967 this = ip6tc_next_chain(handle)) {
968 const struct ip6t_entry *i;
969 unsigned int num;
970
971 if (chain && strcmp(chain, this) != 0)
972 continue;
973
974 if (found) printf("\n");
975
Henrik Nordstrombb340822008-05-13 13:09:23 +0200976 if (!rulenum)
977 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 i = ip6tc_first_rule(this, handle);
979
980 num = 0;
981 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200982 num++;
983 if (!rulenum || num == rulenum)
984 print_firewall(i,
985 ip6tc_get_target(i, handle),
986 num,
987 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100988 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000989 i = ip6tc_next_rule(i, handle);
990 }
991 found = 1;
992 }
993
994 errno = ENOENT;
995 return found;
996}
997
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200998/* This assumes that mask is contiguous, and byte-bounded. */
999static void
1000print_iface(char letter, const char *iface, const unsigned char *mask,
1001 int invert)
1002{
1003 unsigned int i;
1004
1005 if (mask[0] == 0)
1006 return;
1007
1008 printf("-%c %s", letter, invert ? "! " : "");
1009
1010 for (i = 0; i < IFNAMSIZ; i++) {
1011 if (mask[i] != 0) {
1012 if (iface[i] != '\0')
1013 printf("%c", iface[i]);
1014 } else {
1015 /* we can access iface[i-1] here, because
1016 * a few lines above we make sure that mask[0] != 0 */
1017 if (iface[i-1] != '\0')
1018 printf("+");
1019 break;
1020 }
1021 }
1022
1023 printf(" ");
1024}
1025
1026/* The ip6tables looks up the /etc/protocols. */
1027static void print_proto(u_int16_t proto, int invert)
1028{
1029 if (proto) {
1030 unsigned int i;
1031 const char *invertstr = invert ? "! " : "";
1032
1033 struct protoent *pent = getprotobynumber(proto);
1034 if (pent) {
1035 printf("-p %s%s ",
1036 invertstr, pent->p_name);
1037 return;
1038 }
1039
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001040 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1041 if (xtables_chain_protos[i].num == proto) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001042 printf("-p %s%s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001043 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001044 return;
1045 }
1046
1047 printf("-p %s%u ", invertstr, proto);
1048 }
1049}
1050
1051static int print_match_save(const struct ip6t_entry_match *e,
1052 const struct ip6t_ip6 *ip)
1053{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001054 struct xtables_match *match =
1055 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001056
1057 if (match) {
1058 printf("-m %s ", e->u.user.name);
1059
1060 /* some matches don't provide a save function */
1061 if (match->save)
1062 match->save(ip, e);
1063 } else {
1064 if (e->u.match_size) {
1065 fprintf(stderr,
1066 "Can't find library for match `%s'\n",
1067 e->u.user.name);
1068 exit(1);
1069 }
1070 }
1071 return 0;
1072}
1073
1074/* print a given ip including mask if neccessary */
1075static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1076{
1077 char buf[51];
1078 int l = ipv6_prefix_length(mask);
1079
1080 if (l == 0 && !invert)
1081 return;
1082
1083 printf("%s %s%s",
1084 prefix,
1085 invert ? "! " : "",
1086 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1087
1088 if (l == -1)
1089 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1090 else
1091 printf("/%d ", l);
1092}
1093
1094/* We want this to be readable, so only print out neccessary fields.
1095 * Because that's the kind of world I want to live in. */
1096void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001097 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001098{
1099 struct ip6t_entry_target *t;
1100 const char *target_name;
1101
1102 /* print counters for iptables-save */
1103 if (counters > 0)
1104 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1105
1106 /* print chain name */
1107 printf("-A %s ", chain);
1108
1109 /* Print IP part. */
1110 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1111 e->ipv6.invflags & IP6T_INV_SRCIP);
1112
1113 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1114 e->ipv6.invflags & IP6T_INV_DSTIP);
1115
1116 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1117 e->ipv6.invflags & IP6T_INV_VIA_IN);
1118
1119 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1120 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1121
1122 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1123
1124#if 0
1125 /* not definied in ipv6
1126 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1127 if (e->ipv6.flags & IPT_F_FRAG)
1128 printf("%s-f ",
1129 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1130#endif
1131
1132 if (e->ipv6.flags & IP6T_F_TOS)
1133 printf("%s-? %d ",
1134 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1135 e->ipv6.tos);
1136
1137 /* Print matchinfo part */
1138 if (e->target_offset) {
1139 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1140 }
1141
1142 /* print counters for iptables -R */
1143 if (counters < 0)
1144 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1145
1146 /* Print target name */
1147 target_name = ip6tc_get_target(e, h);
1148 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001149#ifdef IP6T_F_GOTO
1150 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1151#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001152 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001153#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001154
1155 /* Print targinfo part */
1156 t = ip6t_get_target((struct ip6t_entry *)e);
1157 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001158 struct xtables_target *target =
1159 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001160
1161 if (!target) {
1162 fprintf(stderr, "Can't find library for target `%s'\n",
1163 t->u.user.name);
1164 exit(1);
1165 }
1166
1167 if (target->save)
1168 target->save(&e->ipv6, t);
1169 else {
1170 /* If the target size is greater than ip6t_entry_target
1171 * there is something to be saved, we just don't know
1172 * how to print it */
1173 if (t->u.target_size !=
1174 sizeof(struct ip6t_entry_target)) {
1175 fprintf(stderr, "Target `%s' is missing "
1176 "save function\n",
1177 t->u.user.name);
1178 exit(1);
1179 }
1180 }
1181 }
1182 printf("\n");
1183}
1184
1185static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001186list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001187 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001188{
1189 const char *this = NULL;
1190 int found = 0;
1191
1192 if (counters)
1193 counters = -1; /* iptables -c format */
1194
1195 /* Dump out chain names first,
1196 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001197 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001198 this;
1199 this = ip6tc_next_chain(handle)) {
1200 if (chain && strcmp(this, chain) != 0)
1201 continue;
1202
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001203 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001204 struct ip6t_counters count;
1205 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1206 if (counters)
1207 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1208 printf("\n");
1209 } else {
1210 printf("-N %s\n", this);
1211 }
1212 }
1213
1214 for (this = ip6tc_first_chain(handle);
1215 this;
1216 this = ip6tc_next_chain(handle)) {
1217 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001218 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001219
1220 if (chain && strcmp(this, chain) != 0)
1221 continue;
1222
1223 /* Dump out rules */
1224 e = ip6tc_first_rule(this, handle);
1225 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001226 num++;
1227 if (!rulenum || num == rulenum)
1228 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001229 e = ip6tc_next_rule(e, handle);
1230 }
1231 found = 1;
1232 }
1233
1234 errno = ENOENT;
1235 return found;
1236}
1237
Rusty Russell5eed48a2000-06-02 20:12:24 +00001238static struct ip6t_entry *
1239generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001240 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241 struct ip6t_entry_target *target)
1242{
1243 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001244 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001245 struct ip6t_entry *e;
1246
1247 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001248 for (matchp = matches; matchp; matchp = matchp->next)
1249 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001250
Jan Engelhardt630ef482009-01-27 14:58:41 +01001251 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001252 *e = *fw;
1253 e->target_offset = size;
1254 e->next_offset = size + target->u.target_size;
1255
1256 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001257 for (matchp = matches; matchp; matchp = matchp->next) {
1258 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1259 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001260 }
1261 memcpy(e->elems + size, target, target->u.target_size);
1262
1263 return e;
1264}
1265
Jan Engelhardt395e4412009-02-10 10:43:08 +01001266static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001267{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001268 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001269
1270 for (matchp = *matches; matchp;) {
1271 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001272 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001273 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001274 matchp->match->m = NULL;
1275 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001276 if (matchp->match == matchp->match->next) {
1277 free(matchp->match);
1278 matchp->match = NULL;
1279 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001280 free(matchp);
1281 matchp = tmp;
1282 }
1283
1284 *matches = NULL;
1285}
1286
Jan Engelhardtfd187312008-11-10 16:59:27 +01001287int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001288{
1289 struct ip6t_entry fw, *e = NULL;
1290 int invert = 0;
1291 unsigned int nsaddrs = 0, ndaddrs = 0;
1292 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1293
1294 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001295 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001296 const char *chain = NULL;
1297 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1298 const char *policy = NULL, *newname = NULL;
1299 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001300 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001301 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001302 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001303 struct xtables_rule_match *matches = NULL;
1304 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001305 struct xtables_target *target = NULL;
1306 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001307 const char *jumpto = "";
1308 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001309 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001310 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001311
1312 memset(&fw, 0, sizeof(fw));
1313
Harald Welte43ac1912002-03-03 09:43:07 +00001314 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001315 * a second time */
1316 optind = 0;
1317
Harald Welte43ac1912002-03-03 09:43:07 +00001318 /* clear mflags in case do_command gets called a second time
1319 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001320 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001321 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001322
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001323 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001324 t->tflags = 0;
1325 t->used = 0;
1326 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001327
Rusty Russell5eed48a2000-06-02 20:12:24 +00001328 /* Suppress error messages: we may add new options if we
1329 demand-load a protocol. */
1330 opterr = 0;
1331
1332 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001333 "-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 +00001334 opts, NULL)) != -1) {
1335 switch (c) {
1336 /*
1337 * Command selection
1338 */
1339 case 'A':
1340 add_command(&command, CMD_APPEND, CMD_NONE,
1341 invert);
1342 chain = optarg;
1343 break;
1344
1345 case 'D':
1346 add_command(&command, CMD_DELETE, CMD_NONE,
1347 invert);
1348 chain = optarg;
1349 if (optind < argc && argv[optind][0] != '-'
1350 && argv[optind][0] != '!') {
1351 rulenum = parse_rulenumber(argv[optind++]);
1352 command = CMD_DELETE_NUM;
1353 }
1354 break;
1355
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356 case 'R':
1357 add_command(&command, CMD_REPLACE, CMD_NONE,
1358 invert);
1359 chain = optarg;
1360 if (optind < argc && argv[optind][0] != '-'
1361 && argv[optind][0] != '!')
1362 rulenum = parse_rulenumber(argv[optind++]);
1363 else
1364 exit_error(PARAMETER_PROBLEM,
1365 "-%c requires a rule number",
1366 cmd2char(CMD_REPLACE));
1367 break;
1368
1369 case 'I':
1370 add_command(&command, CMD_INSERT, CMD_NONE,
1371 invert);
1372 chain = optarg;
1373 if (optind < argc && argv[optind][0] != '-'
1374 && argv[optind][0] != '!')
1375 rulenum = parse_rulenumber(argv[optind++]);
1376 else rulenum = 1;
1377 break;
1378
1379 case 'L':
1380 add_command(&command, CMD_LIST, CMD_ZERO,
1381 invert);
1382 if (optarg) chain = optarg;
1383 else if (optind < argc && argv[optind][0] != '-'
1384 && argv[optind][0] != '!')
1385 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001386 if (optind < argc && argv[optind][0] != '-'
1387 && argv[optind][0] != '!')
1388 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001389 break;
1390
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001391 case 'S':
1392 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1393 invert);
1394 if (optarg) chain = optarg;
1395 else if (optind < argc && argv[optind][0] != '-'
1396 && argv[optind][0] != '!')
1397 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001398 if (optind < argc && argv[optind][0] != '-'
1399 && argv[optind][0] != '!')
1400 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001401 break;
1402
Rusty Russell5eed48a2000-06-02 20:12:24 +00001403 case 'F':
1404 add_command(&command, CMD_FLUSH, CMD_NONE,
1405 invert);
1406 if (optarg) chain = optarg;
1407 else if (optind < argc && argv[optind][0] != '-'
1408 && argv[optind][0] != '!')
1409 chain = argv[optind++];
1410 break;
1411
1412 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001413 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 invert);
1415 if (optarg) chain = optarg;
1416 else if (optind < argc && argv[optind][0] != '-'
1417 && argv[optind][0] != '!')
1418 chain = argv[optind++];
1419 break;
1420
1421 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001422 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001423 exit_error(PARAMETER_PROBLEM,
1424 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001425 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001426 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001427 exit_error(PARAMETER_PROBLEM,
1428 "chain name may not clash "
1429 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001430 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1431 invert);
1432 chain = optarg;
1433 break;
1434
1435 case 'X':
1436 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1437 invert);
1438 if (optarg) chain = optarg;
1439 else if (optind < argc && argv[optind][0] != '-'
1440 && argv[optind][0] != '!')
1441 chain = argv[optind++];
1442 break;
1443
1444 case 'E':
1445 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1446 invert);
1447 chain = optarg;
1448 if (optind < argc && argv[optind][0] != '-'
1449 && argv[optind][0] != '!')
1450 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001451 else
1452 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001453 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001454 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001455 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001456 break;
1457
1458 case 'P':
1459 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1460 invert);
1461 chain = optarg;
1462 if (optind < argc && argv[optind][0] != '-'
1463 && argv[optind][0] != '!')
1464 policy = argv[optind++];
1465 else
1466 exit_error(PARAMETER_PROBLEM,
1467 "-%c requires a chain and a policy",
1468 cmd2char(CMD_SET_POLICY));
1469 break;
1470
1471 case 'h':
1472 if (!optarg)
1473 optarg = argv[optind];
1474
Jonas Berlin1b91e592005-04-01 06:58:38 +00001475 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001476 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001477 xtables_find_match(protocol, XTF_TRY_LOAD,
1478 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001479
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001480 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001481
1482 /*
1483 * Option selection
1484 */
1485 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001486 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001487 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1488 invert);
1489
1490 /* Canonicalize into lower case */
1491 for (protocol = argv[optind-1]; *protocol; protocol++)
1492 *protocol = tolower(*protocol);
1493
1494 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001495 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001496 fw.ipv6.flags |= IP6T_F_PROTO;
1497
1498 if (fw.ipv6.proto == 0
1499 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1500 exit_error(PARAMETER_PROBLEM,
1501 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001502
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001503 if (is_exthdr(fw.ipv6.proto)
1504 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001505 fprintf(stderr,
1506 "Warning: never matched protocol: %s. "
1507 "use extension match instead.\n",
1508 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 break;
1510
1511 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001512 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001513 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1514 invert);
1515 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516 break;
1517
1518 case 'd':
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_DESTINATION, &fw.ipv6.invflags,
1521 invert);
1522 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001523 break;
1524
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001525#ifdef IP6T_F_GOTO
1526 case 'g':
1527 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1528 invert);
1529 fw.ipv6.flags |= IP6T_F_GOTO;
1530 jumpto = parse_target(optarg);
1531 break;
1532#endif
1533
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 case 'j':
1535 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1536 invert);
1537 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001538 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001539 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001540
1541 if (target) {
1542 size_t size;
1543
Harald Welte43ac1912002-03-03 09:43:07 +00001544 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1545 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546
Jan Engelhardt630ef482009-01-27 14:58:41 +01001547 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001548 target->t->u.target_size = size;
1549 strcpy(target->t->u.user.name, jumpto);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001550 xtables_set_revision(target->t->u.user.name,
Patrick McHardy455559b2008-04-15 15:51:19 +02001551 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001552 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001553 target->init(target->t);
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001554 opts = xtables_merge_options(opts,
Patrick McHardy455559b2008-04-15 15:51:19 +02001555 target->extra_opts,
1556 &target->option_offset);
1557 if (opts == NULL)
1558 exit_error(OTHER_PROBLEM,
1559 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001560 }
1561 break;
1562
1563
1564 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001565 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001566 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1567 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001568 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001569 fw.ipv6.iniface,
1570 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001571 break;
1572
1573 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001574 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001575 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1576 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001577 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578 fw.ipv6.outiface,
1579 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001580 break;
1581
1582 case 'v':
1583 if (!verbose)
1584 set_option(&options, OPT_VERBOSE,
1585 &fw.ipv6.invflags, invert);
1586 verbose++;
1587 break;
1588
1589 case 'm': {
1590 size_t size;
1591
1592 if (invert)
1593 exit_error(PARAMETER_PROBLEM,
1594 "unexpected ! flag before --match");
1595
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001596 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1597 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001598 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1599 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001600 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601 m->m->u.match_size = size;
1602 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001603 xtables_set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001604 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001605 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001606 if (m != m->next)
1607 /* Merge options for non-cloned matches */
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001608 opts = xtables_merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001609 }
1610 break;
1611
1612 case 'n':
1613 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1614 invert);
1615 break;
1616
1617 case 't':
1618 if (invert)
1619 exit_error(PARAMETER_PROBLEM,
1620 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001621 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 break;
1623
1624 case 'x':
1625 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1626 invert);
1627 break;
1628
1629 case 'V':
1630 if (invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001631 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632 else
1633 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001634 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635 exit(0);
1636
1637 case '0':
1638 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1639 invert);
1640 break;
1641
Harald Welte43ac1912002-03-03 09:43:07 +00001642 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001643 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001644 break;
1645
1646 case 'c':
1647
1648 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1649 invert);
1650 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001651 bcnt = strchr(pcnt + 1, ',');
1652 if (bcnt)
1653 bcnt++;
1654 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001655 && argv[optind][0] != '!')
1656 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001657 if (!bcnt)
Harald Welte43ac1912002-03-03 09:43:07 +00001658 exit_error(PARAMETER_PROBLEM,
1659 "-%c requires packet and byte counter",
1660 opt2char(OPT_COUNTERS));
1661
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001662 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001663 exit_error(PARAMETER_PROBLEM,
1664 "-%c packet counter not numeric",
1665 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001666 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001667
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001668 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001669 exit_error(PARAMETER_PROBLEM,
1670 "-%c byte counter not numeric",
1671 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001672 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001673 break;
1674
Rusty Russell5eed48a2000-06-02 20:12:24 +00001675 case 1: /* non option */
1676 if (optarg[0] == '!' && optarg[1] == '\0') {
1677 if (invert)
1678 exit_error(PARAMETER_PROBLEM,
1679 "multiple consecutive ! not"
1680 " allowed");
1681 invert = TRUE;
1682 optarg[0] = '\0';
1683 continue;
1684 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001685 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001686 exit_tryhelp(2);
1687
1688 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001689 if (!target
1690 || !(target->parse(c - target->option_offset,
1691 argv, invert,
1692 &target->tflags,
1693 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001694 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001695 if (matchp->completed)
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) {
1765 exit_error(
1766 PARAMETER_PROBLEM,
1767 "option `%s' "
1768 "requires an "
1769 "argument",
1770 argv[optind-1]);
1771 } else {
1772 exit_error(
1773 PARAMETER_PROBLEM,
1774 "unknown option "
1775 "`%s'",
1776 argv[optind-1]);
1777 }
1778 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001779 exit_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)
1797 exit_error(PARAMETER_PROBLEM,
1798 "unknown arguments found on commandline");
1799 if (!command)
1800 exit_error(PARAMETER_PROBLEM, "no command specified");
1801 if (invert)
1802 exit_error(PARAMETER_PROBLEM,
1803 "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)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001813 xtables_ip6parse_any(shostnetworkmask, &saddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001814 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815
1816 if (dhostnetworkmask)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001817 xtables_ip6parse_any(dhostnetworkmask, &daddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001818 &fw.ipv6.dmsk, &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)))
1822 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1823 " source or destination IP addresses");
1824
Rusty Russell5eed48a2000-06-02 20:12:24 +00001825 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1826 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1827 "specify a unique address");
1828
1829 generic_opt_check(command, options);
1830
1831 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1832 exit_error(PARAMETER_PROBLEM,
1833 "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)
1845 exit_error(VERSION_PROBLEM,
1846 "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)
1857 exit_error(PARAMETER_PROBLEM,
1858 "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)
1867 exit_error(PARAMETER_PROBLEM,
1868 "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)
1910 exit_error(PARAMETER_PROBLEM,
1911 "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,
1923 nsaddrs, saddrs, ndaddrs, daddrs,
1924 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001925 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001926 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 case CMD_DELETE:
1928 ret = delete_entry(chain, e,
1929 nsaddrs, saddrs, ndaddrs, daddrs,
1930 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001931 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932 break;
1933 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001934 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935 break;
1936 case CMD_REPLACE:
1937 ret = replace_entry(chain, e, rulenum - 1,
1938 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001939 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001940 break;
1941 case CMD_INSERT:
1942 ret = insert_entry(chain, e, rulenum - 1,
1943 nsaddrs, saddrs, ndaddrs, daddrs,
1944 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001945 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001946 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001948 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001949 break;
1950 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001951 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001953 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001954 case CMD_LIST|CMD_ZERO:
1955 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001956 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 options&OPT_VERBOSE,
1958 options&OPT_NUMERIC,
1959 options&OPT_EXPANDED,
1960 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001961 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001962 if (ret && (command & CMD_ZERO))
1963 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001964 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001965 break;
1966 case CMD_LIST_RULES:
1967 case CMD_LIST_RULES|CMD_ZERO:
1968 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001969 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001970 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001971 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001972 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001973 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001974 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975 break;
1976 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001977 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 break;
1979 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001980 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001981 break;
1982 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001983 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001984 break;
1985 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001986 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001987 break;
1988 default:
1989 /* We should never reach this... */
1990 exit_tryhelp(2);
1991 }
1992
1993 if (verbose > 1)
1994 dump_entries6(*handle);
1995
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001996 clear_rule_matches(&matches);
1997
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001998 if (e != NULL) {
1999 free(e);
2000 e = NULL;
2001 }
2002
Max Kellermann9ee386a2008-01-29 13:48:05 +00002003 for (i = 0; i < nsaddrs; i++)
2004 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002005
Max Kellermann9ee386a2008-01-29 13:48:05 +00002006 for (i = 0; i < ndaddrs; i++)
2007 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002008
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002009 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002010
Rusty Russell5eed48a2000-06-02 20:12:24 +00002011 return ret;
2012}