blob: fad7ef2d58c3b5a074555699553876d947a1c0c3 [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
87#define OPTION_OFFSET 256
88
89#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
Rusty Russell5eed48a2000-06-02 20:12:24 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100148void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100149struct xtables_globals ip6tables_globals = {
150 .option_offset = 0,
151 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100152 .opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100153 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100154};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000155
156/* Table of legal combinations of commands and options. If any of the
157 * given commands make an option legal, that option is legal (applies to
158 * CMD_LIST and CMD_ZERO only).
159 * Key:
160 * + compulsory
161 * x illegal
162 * optional
163 */
164
165static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
166/* Well, it's better than "Re: Linux vs FreeBSD" */
167{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200168 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000169/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
170/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
171/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
172/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
173/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
174/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
175/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
176/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
177/*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
199const char *program_version;
200const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000201
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
Pablo Neiradfdcd642005-05-29 19:05:23 +0000227static void free_opts(int reset_offset)
228{
229 if (opts != original_opts) {
230 free(opts);
231 opts = original_opts;
232 if (reset_offset)
233 global_option_offset = 0;
234 }
235}
236
Patrick McHardy0b639362007-09-08 16:00:01 +0000237static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000238exit_tryhelp(int status)
239{
Harald Weltea8658ca2003-03-05 07:46:15 +0000240 if (line != -1)
241 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000242 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
243 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000244 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000245 exit(status);
246}
247
Patrick McHardy0b639362007-09-08 16:00:01 +0000248static void
Jan Engelhardt395e4412009-02-10 10:43:08 +0100249exit_printhelp(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000250{
Jan Engelhardt395e4412009-02-10 10:43:08 +0100251 struct xtables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200252 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000253
254 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000255"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000256" %s -[RI] chain rulenum rule-specification [options]\n"
257" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200258" %s -[LS] [chain [rulenum]] [options]\n"
259" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000260" %s -[NX] chain\n"
261" %s -E old-chain-name new-chain-name\n"
262" %s -P chain target [options]\n"
263" %s -h (print this help information)\n\n",
264 program_name, program_version, program_name, program_name,
265 program_name, program_name, program_name, program_name,
Henrik Nordstrombb340822008-05-13 13:09:23 +0200266 program_name, program_name, program_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000267
268 printf(
269"Commands:\n"
270"Either long or short options are allowed.\n"
271" --append -A chain Append to chain\n"
272" --delete -D chain Delete matching rule from chain\n"
273" --delete -D chain rulenum\n"
274" Delete rule rulenum (1 = first) from chain\n"
275" --insert -I chain [rulenum]\n"
276" Insert in chain as rulenum (default 1=first)\n"
277" --replace -R chain rulenum\n"
278" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200279" --list -L [chain [rulenum]]\n"
280" List the rules in a chain or all chains\n"
281" --list-rules -S [chain [rulenum]]\n"
282" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283" --flush -F [chain] Delete all rules in chain or all chains\n"
284" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" --new -N chain Create a new user-defined chain\n"
286" --delete-chain\n"
287" -X [chain] Delete a user-defined chain\n"
288" --policy -P chain target\n"
289" Change policy on chain to target\n"
290" --rename-chain\n"
291" -E old-chain new-chain\n"
292" Change chain name, (moving any references)\n"
293
294"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200295"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
296"[!] --source -s address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000297" source specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200298"[!] --destination -d address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000299" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200300"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000301" network interface name ([+] for wildcard)\n"
302" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000303" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200304#ifdef IP6T_F_GOTO
305" --goto -g chain\n"
306" jump to chain with no return\n"
307#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000308" --match -m match\n"
309" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000310" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200311"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000312" network interface name ([+] for wildcard)\n"
313" --table -t table table to manipulate (default: `filter')\n"
314" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000315" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000316" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000317/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000318" --modprobe=<command> try to insert modules using this command\n"
319" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000320"[!] --version -V print package version.\n");
321
Max Kellermann5b76f682008-01-29 13:42:48 +0000322 /* Print out any special helps. A user might like to be able to add a --help
323 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000324 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000325 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000326 if (t->used) {
327 printf("\n");
328 t->help();
329 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000330 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000331 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000333 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334 }
335 exit(0);
336}
337
Patrick McHardy0b639362007-09-08 16:00:01 +0000338void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100339ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000340{
341 va_list args;
342
343 va_start(args, msg);
344 fprintf(stderr, "%s v%s: ", program_name, program_version);
345 vfprintf(stderr, msg, args);
346 va_end(args);
347 fprintf(stderr, "\n");
348 if (status == PARAMETER_PROBLEM)
349 exit_tryhelp(status);
350 if (status == VERSION_PROBLEM)
351 fprintf(stderr,
352 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
353 /* On error paths, make sure that we don't leak memory */
354 free_opts(1);
355 exit(status);
356}
357
Rusty Russell5eed48a2000-06-02 20:12:24 +0000358static void
359generic_opt_check(int command, int options)
360{
361 int i, j, legal = 0;
362
363 /* Check that commands are valid with options. Complicated by the
364 * fact that if an option is legal with *any* command given, it is
365 * legal overall (ie. -z and -l).
366 */
367 for (i = 0; i < NUMBER_OF_OPT; i++) {
368 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
369
370 for (j = 0; j < NUMBER_OF_CMD; j++) {
371 if (!(command & (1<<j)))
372 continue;
373
374 if (!(options & (1<<i))) {
375 if (commands_v_options[j][i] == '+')
376 exit_error(PARAMETER_PROBLEM,
377 "You need to supply the `-%c' "
378 "option for this command\n",
379 optflags[i]);
380 } else {
381 if (commands_v_options[j][i] != 'x')
382 legal = 1;
383 else if (legal == 0)
384 legal = -1;
385 }
386 }
387 if (legal == -1)
388 exit_error(PARAMETER_PROBLEM,
389 "Illegal option `-%c' with this command\n",
390 optflags[i]);
391 }
392}
393
394static char
395opt2char(int option)
396{
397 const char *ptr;
398 for (ptr = optflags; option > 1; option >>= 1, ptr++);
399
400 return *ptr;
401}
402
403static char
404cmd2char(int option)
405{
406 const char *ptr;
407 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
408
409 return *ptr;
410}
411
412static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000413add_command(unsigned int *cmd, const int newcmd, const int othercmds,
414 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000415{
416 if (invert)
417 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
418 if (*cmd & (~othercmds))
419 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
420 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
421 *cmd |= newcmd;
422}
423
Rusty Russell5eed48a2000-06-02 20:12:24 +0000424/*
425 * All functions starting with "parse" should succeed, otherwise
426 * the program fails.
427 * Most routines return pointers to static data that may change
428 * between calls to the same or other routines with a few exceptions:
429 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
430 * return global static data.
431*/
432
Rusty Russell5eed48a2000-06-02 20:12:24 +0000433/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200434static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100435find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100436 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437{
Harald Welteed498492001-07-23 01:24:22 +0000438 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000439
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100440 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100441 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000442
Harald Welte43ac1912002-03-03 09:43:07 +0000443 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100444 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000445 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100446 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000447
448 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000449}
450
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000451/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000452static int is_exthdr(u_int16_t proto)
453{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000454 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000455 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000456 proto == IPPROTO_AH ||
457 proto == IPPROTO_DSTOPTS);
458}
459
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460/* Can't be zero. */
461static int
462parse_rulenumber(const char *rule)
463{
Harald Welte43ac1912002-03-03 09:43:07 +0000464 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000465
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100466 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467 exit_error(PARAMETER_PROBLEM,
468 "Invalid rule number `%s'", rule);
469
470 return rulenum;
471}
472
473static const char *
474parse_target(const char *targetname)
475{
476 const char *ptr;
477
478 if (strlen(targetname) < 1)
479 exit_error(PARAMETER_PROBLEM,
480 "Invalid target name (too short)");
481
482 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
483 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000484 "Invalid target name `%s' (%u chars max)",
485 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486
487 for (ptr = targetname; *ptr; ptr++)
488 if (isspace(*ptr))
489 exit_error(PARAMETER_PROBLEM,
490 "Invalid target name `%s'", targetname);
491 return targetname;
492}
493
Rusty Russell5eed48a2000-06-02 20:12:24 +0000494static void
495set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
496 int invert)
497{
498 if (*options & option)
499 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
500 opt2char(option));
501 *options |= option;
502
503 if (invert) {
504 unsigned int i;
505 for (i = 0; 1 << i != option; i++);
506
507 if (!inverse_for_options[i])
508 exit_error(PARAMETER_PROBLEM,
509 "cannot have ! before -%c",
510 opt2char(option));
511 *invflg |= inverse_for_options[i];
512 }
513}
514
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000516merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000517 unsigned int *option_offset)
518{
519 unsigned int num_old, num_new, i;
520 struct option *merge;
521
Jan Engelhardtd0145402007-07-30 14:32:26 +0000522 if (newopts == NULL)
523 return oldopts;
524
Rusty Russell5eed48a2000-06-02 20:12:24 +0000525 for (num_old = 0; oldopts[num_old].name; num_old++);
526 for (num_new = 0; newopts[num_new].name; num_new++);
527
528 global_option_offset += OPTION_OFFSET;
529 *option_offset = global_option_offset;
530
531 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
532 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000533 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000534 for (i = 0; i < num_new; i++) {
535 merge[num_old + i] = newopts[i];
536 merge[num_old + i].val += *option_offset;
537 }
538 memset(merge + num_old + num_new, 0, sizeof(struct option));
539
540 return merge;
541}
542
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000543static void
544print_num(u_int64_t number, unsigned int format)
545{
Harald Welte43ac1912002-03-03 09:43:07 +0000546 if (format & FMT_KILOMEGAGIGA) {
547 if (number > 99999) {
548 number = (number + 500) / 1000;
549 if (number > 9999) {
550 number = (number + 500) / 1000;
551 if (number > 9999) {
552 number = (number + 500) / 1000;
553 if (number > 9999) {
554 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000555 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000556 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000557 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000558 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000559 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000560 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000561 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000562 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000563 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000564 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000565 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000566}
567
Harald Welte43ac1912002-03-03 09:43:07 +0000568
Rusty Russell5eed48a2000-06-02 20:12:24 +0000569static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100570print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000571{
572 struct ip6t_counters counters;
573 const char *pol = ip6tc_get_policy(chain, &counters, handle);
574 printf("Chain %s", chain);
575 if (pol) {
576 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000577 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000578 fputc(' ', stdout);
579 print_num(counters.pcnt, (format|FMT_NOTABLE));
580 fputs("packets, ", stdout);
581 print_num(counters.bcnt, (format|FMT_NOTABLE));
582 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000583 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000584 printf(")\n");
585 } else {
586 unsigned int refs;
587 if (!ip6tc_get_references(&refs, chain, handle))
588 printf(" (ERROR obtaining refs)\n");
589 else
590 printf(" (%u references)\n", refs);
591 }
592
593 if (format & FMT_LINENUMBERS)
594 printf(FMT("%-4s ", "%s "), "num");
595 if (!(format & FMT_NOCOUNTS)) {
596 if (format & FMT_KILOMEGAGIGA) {
597 printf(FMT("%5s ","%s "), "pkts");
598 printf(FMT("%5s ","%s "), "bytes");
599 } else {
600 printf(FMT("%8s ","%s "), "pkts");
601 printf(FMT("%10s ","%s "), "bytes");
602 }
603 }
604 if (!(format & FMT_NOTARGET))
605 printf(FMT("%-9s ","%s "), "target");
606 fputs(" prot ", stdout);
607 if (format & FMT_OPTIONS)
608 fputs("opt", stdout);
609 if (format & FMT_VIA) {
610 printf(FMT(" %-6s ","%s "), "in");
611 printf(FMT("%-6s ","%s "), "out");
612 }
613 printf(FMT(" %-19s ","%s "), "source");
614 printf(FMT(" %-19s "," %s "), "destination");
615 printf("\n");
616}
617
Harald Welte43ac1912002-03-03 09:43:07 +0000618
Rusty Russell5eed48a2000-06-02 20:12:24 +0000619static int
620print_match(const struct ip6t_entry_match *m,
621 const struct ip6t_ip6 *ip,
622 int numeric)
623{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100624 struct xtables_match *match =
625 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000626
627 if (match) {
628 if (match->print)
629 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000630 else
631 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000632 } else {
633 if (m->u.user.name[0])
634 printf("UNKNOWN match `%s' ", m->u.user.name);
635 }
636 /* Don't stop iterating. */
637 return 0;
638}
639
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100640/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000641static void
642print_firewall(const struct ip6t_entry *fw,
643 const char *targname,
644 unsigned int num,
645 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100646 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000647{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200648 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000649 const struct ip6t_entry_target *t;
650 u_int8_t flags;
651 char buf[BUFSIZ];
652
Rusty Russell5eed48a2000-06-02 20:12:24 +0000653 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100654 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100656 target = xtables_find_target(IP6T_STANDARD_TARGET,
657 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000658
659 t = ip6t_get_target((struct ip6t_entry *)fw);
660 flags = fw->ipv6.flags;
661
662 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200663 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000664
665 if (!(format & FMT_NOCOUNTS)) {
666 print_num(fw->counters.pcnt, format);
667 print_num(fw->counters.bcnt, format);
668 }
669
670 if (!(format & FMT_NOTARGET))
671 printf(FMT("%-9s ", "%s "), targname);
672
673 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
674 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100675 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000676 if (pname)
677 printf(FMT("%-5s", "%s "), pname);
678 else
679 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
680 }
681
682 if (format & FMT_OPTIONS) {
683 if (format & FMT_NOTABLE)
684 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000685 fputc(' ', stdout); /* Invert flag of FRAG */
686 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000687 fputc(' ', stdout);
688 }
689
690 if (format & FMT_VIA) {
691 char iface[IFNAMSIZ+2];
692
693 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
694 iface[0] = '!';
695 iface[1] = '\0';
696 }
697 else iface[0] = '\0';
698
699 if (fw->ipv6.iniface[0] != '\0') {
700 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701 }
702 else if (format & FMT_NUMERIC) strcat(iface, "*");
703 else strcat(iface, "any");
704 printf(FMT(" %-6s ","in %s "), iface);
705
706 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
707 iface[0] = '!';
708 iface[1] = '\0';
709 }
710 else iface[0] = '\0';
711
712 if (fw->ipv6.outiface[0] != '\0') {
713 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714 }
715 else if (format & FMT_NUMERIC) strcat(iface, "*");
716 else strcat(iface, "any");
717 printf(FMT("%-6s ","out %s "), iface);
718 }
719
720 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000721 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000722 && !(format & FMT_NUMERIC))
723 printf(FMT("%-19s ","%s "), "anywhere");
724 else {
725 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100726 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000727 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100728 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
729 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730 printf(FMT("%-19s ","%s "), buf);
731 }
732
733 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
734 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
735 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200736 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000737 else {
738 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100739 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000740 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100741 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
742 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200743 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000744 }
745
746 if (format & FMT_NOTABLE)
747 fputs(" ", stdout);
748
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200749#ifdef IP6T_F_GOTO
750 if(fw->ipv6.flags & IP6T_F_GOTO)
751 printf("[goto] ");
752#endif
753
Rusty Russell5eed48a2000-06-02 20:12:24 +0000754 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
755
756 if (target) {
757 if (target->print)
758 /* Print the target information. */
759 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
760 } else if (t->u.target_size != sizeof(*t))
761 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000762 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000763
764 if (!(format & FMT_NONEWLINE))
765 fputc('\n', stdout);
766}
767
768static void
769print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100770 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771{
772 struct ip6t_entry_target *t;
773
774 t = ip6t_get_target((struct ip6t_entry *)fw);
775 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
776}
777
778static int
779append_entry(const ip6t_chainlabel chain,
780 struct ip6t_entry *fw,
781 unsigned int nsaddrs,
782 const struct in6_addr saddrs[],
783 unsigned int ndaddrs,
784 const struct in6_addr daddrs[],
785 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100786 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000787{
788 unsigned int i, j;
789 int ret = 1;
790
791 for (i = 0; i < nsaddrs; i++) {
792 fw->ipv6.src = saddrs[i];
793 for (j = 0; j < ndaddrs; j++) {
794 fw->ipv6.dst = daddrs[j];
795 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100796 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797 ret &= ip6tc_append_entry(chain, fw, handle);
798 }
799 }
800
801 return ret;
802}
803
804static int
805replace_entry(const ip6t_chainlabel chain,
806 struct ip6t_entry *fw,
807 unsigned int rulenum,
808 const struct in6_addr *saddr,
809 const struct in6_addr *daddr,
810 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100811 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000812{
813 fw->ipv6.src = *saddr;
814 fw->ipv6.dst = *daddr;
815
816 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100817 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818 return ip6tc_replace_entry(chain, fw, rulenum, handle);
819}
820
821static int
822insert_entry(const ip6t_chainlabel chain,
823 struct ip6t_entry *fw,
824 unsigned int rulenum,
825 unsigned int nsaddrs,
826 const struct in6_addr saddrs[],
827 unsigned int ndaddrs,
828 const struct in6_addr daddrs[],
829 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100830 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831{
832 unsigned int i, j;
833 int ret = 1;
834
835 for (i = 0; i < nsaddrs; i++) {
836 fw->ipv6.src = saddrs[i];
837 for (j = 0; j < ndaddrs; j++) {
838 fw->ipv6.dst = daddrs[j];
839 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100840 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
842 }
843 }
844
845 return ret;
846}
847
848static unsigned char *
Jan Engelhardt395e4412009-02-10 10:43:08 +0100849make_delete_mask(struct ip6t_entry *fw, struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850{
851 /* Establish mask for comparison */
852 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100853 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000854 unsigned char *mask, *mptr;
855
856 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000857 for (matchp = matches; matchp; matchp = matchp->next)
858 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000859
Jan Engelhardt630ef482009-01-27 14:58:41 +0100860 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000861 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000862 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000863
864 memset(mask, 0xFF, sizeof(struct ip6t_entry));
865 mptr = mask + sizeof(struct ip6t_entry);
866
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000867 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000868 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000869 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000870 + matchp->match->userspacesize);
871 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000872 }
873
Max Kellermann5b76f682008-01-29 13:42:48 +0000874 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000875 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000876 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000877
878 return mask;
879}
880
881static int
882delete_entry(const ip6t_chainlabel chain,
883 struct ip6t_entry *fw,
884 unsigned int nsaddrs,
885 const struct in6_addr saddrs[],
886 unsigned int ndaddrs,
887 const struct in6_addr daddrs[],
888 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100889 struct ip6tc_handle *handle,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100890 struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000891{
892 unsigned int i, j;
893 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000894 unsigned char *mask;
895
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000896 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000898 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000899 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000900 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000901 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100902 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000903 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000904 }
905 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000906 free(mask);
907
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 return ret;
909}
910
András Kis-Szabó764316a2001-02-26 17:31:20 +0000911int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100912for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
913 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000914{
Max Kellermann5b76f682008-01-29 13:42:48 +0000915 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916 const char *chain;
917 char *chains;
918 unsigned int i, chaincount = 0;
919
920 chain = ip6tc_first_chain(handle);
921 while (chain) {
922 chaincount++;
923 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000924 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000925
Jan Engelhardt630ef482009-01-27 14:58:41 +0100926 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000927 i = 0;
928 chain = ip6tc_first_chain(handle);
929 while (chain) {
930 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
931 i++;
932 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000933 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000934
935 for (i = 0; i < chaincount; i++) {
936 if (!builtinstoo
937 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100938 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000939 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000940 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941 }
942
943 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000944 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000945}
946
András Kis-Szabó764316a2001-02-26 17:31:20 +0000947int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000948flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100949 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950{
951 if (!chain)
952 return for_each_chain(flush_entries, verbose, 1, handle);
953
954 if (verbose)
955 fprintf(stdout, "Flushing chain `%s'\n", chain);
956 return ip6tc_flush_entries(chain, handle);
957}
958
959static int
960zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100961 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000962{
963 if (!chain)
964 return for_each_chain(zero_entries, verbose, 1, handle);
965
966 if (verbose)
967 fprintf(stdout, "Zeroing chain `%s'\n", chain);
968 return ip6tc_zero_entries(chain, handle);
969}
970
András Kis-Szabó764316a2001-02-26 17:31:20 +0000971int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000972delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100973 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000974{
975 if (!chain)
976 return for_each_chain(delete_chain, verbose, 0, handle);
977
978 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000979 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 return ip6tc_delete_chain(chain, handle);
981}
982
983static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200984list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100985 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000986{
987 int found = 0;
988 unsigned int format;
989 const char *this;
990
991 format = FMT_OPTIONS;
992 if (!verbose)
993 format |= FMT_NOCOUNTS;
994 else
995 format |= FMT_VIA;
996
997 if (numeric)
998 format |= FMT_NUMERIC;
999
1000 if (!expanded)
1001 format |= FMT_KILOMEGAGIGA;
1002
1003 if (linenumbers)
1004 format |= FMT_LINENUMBERS;
1005
1006 for (this = ip6tc_first_chain(handle);
1007 this;
1008 this = ip6tc_next_chain(handle)) {
1009 const struct ip6t_entry *i;
1010 unsigned int num;
1011
1012 if (chain && strcmp(chain, this) != 0)
1013 continue;
1014
1015 if (found) printf("\n");
1016
Henrik Nordstrombb340822008-05-13 13:09:23 +02001017 if (!rulenum)
1018 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001019 i = ip6tc_first_rule(this, handle);
1020
1021 num = 0;
1022 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001023 num++;
1024 if (!rulenum || num == rulenum)
1025 print_firewall(i,
1026 ip6tc_get_target(i, handle),
1027 num,
1028 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001029 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030 i = ip6tc_next_rule(i, handle);
1031 }
1032 found = 1;
1033 }
1034
1035 errno = ENOENT;
1036 return found;
1037}
1038
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001039/* This assumes that mask is contiguous, and byte-bounded. */
1040static void
1041print_iface(char letter, const char *iface, const unsigned char *mask,
1042 int invert)
1043{
1044 unsigned int i;
1045
1046 if (mask[0] == 0)
1047 return;
1048
1049 printf("-%c %s", letter, invert ? "! " : "");
1050
1051 for (i = 0; i < IFNAMSIZ; i++) {
1052 if (mask[i] != 0) {
1053 if (iface[i] != '\0')
1054 printf("%c", iface[i]);
1055 } else {
1056 /* we can access iface[i-1] here, because
1057 * a few lines above we make sure that mask[0] != 0 */
1058 if (iface[i-1] != '\0')
1059 printf("+");
1060 break;
1061 }
1062 }
1063
1064 printf(" ");
1065}
1066
1067/* The ip6tables looks up the /etc/protocols. */
1068static void print_proto(u_int16_t proto, int invert)
1069{
1070 if (proto) {
1071 unsigned int i;
1072 const char *invertstr = invert ? "! " : "";
1073
1074 struct protoent *pent = getprotobynumber(proto);
1075 if (pent) {
1076 printf("-p %s%s ",
1077 invertstr, pent->p_name);
1078 return;
1079 }
1080
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001081 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1082 if (xtables_chain_protos[i].num == proto) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001083 printf("-p %s%s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001084 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001085 return;
1086 }
1087
1088 printf("-p %s%u ", invertstr, proto);
1089 }
1090}
1091
1092static int print_match_save(const struct ip6t_entry_match *e,
1093 const struct ip6t_ip6 *ip)
1094{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001095 struct xtables_match *match =
1096 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001097
1098 if (match) {
1099 printf("-m %s ", e->u.user.name);
1100
1101 /* some matches don't provide a save function */
1102 if (match->save)
1103 match->save(ip, e);
1104 } else {
1105 if (e->u.match_size) {
1106 fprintf(stderr,
1107 "Can't find library for match `%s'\n",
1108 e->u.user.name);
1109 exit(1);
1110 }
1111 }
1112 return 0;
1113}
1114
1115/* print a given ip including mask if neccessary */
1116static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1117{
1118 char buf[51];
1119 int l = ipv6_prefix_length(mask);
1120
1121 if (l == 0 && !invert)
1122 return;
1123
1124 printf("%s %s%s",
1125 prefix,
1126 invert ? "! " : "",
1127 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1128
1129 if (l == -1)
1130 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1131 else
1132 printf("/%d ", l);
1133}
1134
1135/* We want this to be readable, so only print out neccessary fields.
1136 * Because that's the kind of world I want to live in. */
1137void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001138 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001139{
1140 struct ip6t_entry_target *t;
1141 const char *target_name;
1142
1143 /* print counters for iptables-save */
1144 if (counters > 0)
1145 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1146
1147 /* print chain name */
1148 printf("-A %s ", chain);
1149
1150 /* Print IP part. */
1151 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1152 e->ipv6.invflags & IP6T_INV_SRCIP);
1153
1154 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1155 e->ipv6.invflags & IP6T_INV_DSTIP);
1156
1157 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1158 e->ipv6.invflags & IP6T_INV_VIA_IN);
1159
1160 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1161 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1162
1163 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1164
1165#if 0
1166 /* not definied in ipv6
1167 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1168 if (e->ipv6.flags & IPT_F_FRAG)
1169 printf("%s-f ",
1170 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1171#endif
1172
1173 if (e->ipv6.flags & IP6T_F_TOS)
1174 printf("%s-? %d ",
1175 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1176 e->ipv6.tos);
1177
1178 /* Print matchinfo part */
1179 if (e->target_offset) {
1180 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1181 }
1182
1183 /* print counters for iptables -R */
1184 if (counters < 0)
1185 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1186
1187 /* Print target name */
1188 target_name = ip6tc_get_target(e, h);
1189 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001190#ifdef IP6T_F_GOTO
1191 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1192#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001193 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001194#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001195
1196 /* Print targinfo part */
1197 t = ip6t_get_target((struct ip6t_entry *)e);
1198 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001199 struct xtables_target *target =
1200 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001201
1202 if (!target) {
1203 fprintf(stderr, "Can't find library for target `%s'\n",
1204 t->u.user.name);
1205 exit(1);
1206 }
1207
1208 if (target->save)
1209 target->save(&e->ipv6, t);
1210 else {
1211 /* If the target size is greater than ip6t_entry_target
1212 * there is something to be saved, we just don't know
1213 * how to print it */
1214 if (t->u.target_size !=
1215 sizeof(struct ip6t_entry_target)) {
1216 fprintf(stderr, "Target `%s' is missing "
1217 "save function\n",
1218 t->u.user.name);
1219 exit(1);
1220 }
1221 }
1222 }
1223 printf("\n");
1224}
1225
1226static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001227list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001228 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001229{
1230 const char *this = NULL;
1231 int found = 0;
1232
1233 if (counters)
1234 counters = -1; /* iptables -c format */
1235
1236 /* Dump out chain names first,
1237 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001238 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001239 this;
1240 this = ip6tc_next_chain(handle)) {
1241 if (chain && strcmp(this, chain) != 0)
1242 continue;
1243
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001244 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001245 struct ip6t_counters count;
1246 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1247 if (counters)
1248 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1249 printf("\n");
1250 } else {
1251 printf("-N %s\n", this);
1252 }
1253 }
1254
1255 for (this = ip6tc_first_chain(handle);
1256 this;
1257 this = ip6tc_next_chain(handle)) {
1258 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001259 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001260
1261 if (chain && strcmp(this, chain) != 0)
1262 continue;
1263
1264 /* Dump out rules */
1265 e = ip6tc_first_rule(this, handle);
1266 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001267 num++;
1268 if (!rulenum || num == rulenum)
1269 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001270 e = ip6tc_next_rule(e, handle);
1271 }
1272 found = 1;
1273 }
1274
1275 errno = ENOENT;
1276 return found;
1277}
1278
Rusty Russell5eed48a2000-06-02 20:12:24 +00001279static struct ip6t_entry *
1280generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001281 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001282 struct ip6t_entry_target *target)
1283{
1284 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001285 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001286 struct ip6t_entry *e;
1287
1288 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001289 for (matchp = matches; matchp; matchp = matchp->next)
1290 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001291
Jan Engelhardt630ef482009-01-27 14:58:41 +01001292 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293 *e = *fw;
1294 e->target_offset = size;
1295 e->next_offset = size + target->u.target_size;
1296
1297 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001298 for (matchp = matches; matchp; matchp = matchp->next) {
1299 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1300 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001301 }
1302 memcpy(e->elems + size, target, target->u.target_size);
1303
1304 return e;
1305}
1306
Jan Engelhardt395e4412009-02-10 10:43:08 +01001307static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001308{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001309 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001310
1311 for (matchp = *matches; matchp;) {
1312 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001313 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001314 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001315 matchp->match->m = NULL;
1316 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001317 if (matchp->match == matchp->match->next) {
1318 free(matchp->match);
1319 matchp->match = NULL;
1320 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001321 free(matchp);
1322 matchp = tmp;
1323 }
1324
1325 *matches = NULL;
1326}
1327
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001328static void set_revision(char *name, u_int8_t revision)
1329{
1330 /* Old kernel sources don't have ".revision" field,
1331 but we stole a byte from name. */
1332 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1333 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1334}
1335
Jan Engelhardtfd187312008-11-10 16:59:27 +01001336int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001337{
1338 struct ip6t_entry fw, *e = NULL;
1339 int invert = 0;
1340 unsigned int nsaddrs = 0, ndaddrs = 0;
1341 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1342
1343 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001344 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001345 const char *chain = NULL;
1346 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1347 const char *policy = NULL, *newname = NULL;
1348 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001349 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001350 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001351 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001352 struct xtables_rule_match *matches = NULL;
1353 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001354 struct xtables_target *target = NULL;
1355 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356 const char *jumpto = "";
1357 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001358 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001359 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001360
1361 memset(&fw, 0, sizeof(fw));
1362
Harald Welte43ac1912002-03-03 09:43:07 +00001363 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001364 * a second time */
1365 optind = 0;
1366
Harald Welte43ac1912002-03-03 09:43:07 +00001367 /* clear mflags in case do_command gets called a second time
1368 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001369 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001370 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001371
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001372 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001373 t->tflags = 0;
1374 t->used = 0;
1375 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001376
Rusty Russell5eed48a2000-06-02 20:12:24 +00001377 /* Suppress error messages: we may add new options if we
1378 demand-load a protocol. */
1379 opterr = 0;
1380
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +01001381 xtables_set_params(&ip6tables_globals);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001382 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001383 "-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 +00001384 opts, NULL)) != -1) {
1385 switch (c) {
1386 /*
1387 * Command selection
1388 */
1389 case 'A':
1390 add_command(&command, CMD_APPEND, CMD_NONE,
1391 invert);
1392 chain = optarg;
1393 break;
1394
1395 case 'D':
1396 add_command(&command, CMD_DELETE, CMD_NONE,
1397 invert);
1398 chain = optarg;
1399 if (optind < argc && argv[optind][0] != '-'
1400 && argv[optind][0] != '!') {
1401 rulenum = parse_rulenumber(argv[optind++]);
1402 command = CMD_DELETE_NUM;
1403 }
1404 break;
1405
Rusty Russell5eed48a2000-06-02 20:12:24 +00001406 case 'R':
1407 add_command(&command, CMD_REPLACE, CMD_NONE,
1408 invert);
1409 chain = optarg;
1410 if (optind < argc && argv[optind][0] != '-'
1411 && argv[optind][0] != '!')
1412 rulenum = parse_rulenumber(argv[optind++]);
1413 else
1414 exit_error(PARAMETER_PROBLEM,
1415 "-%c requires a rule number",
1416 cmd2char(CMD_REPLACE));
1417 break;
1418
1419 case 'I':
1420 add_command(&command, CMD_INSERT, CMD_NONE,
1421 invert);
1422 chain = optarg;
1423 if (optind < argc && argv[optind][0] != '-'
1424 && argv[optind][0] != '!')
1425 rulenum = parse_rulenumber(argv[optind++]);
1426 else rulenum = 1;
1427 break;
1428
1429 case 'L':
1430 add_command(&command, CMD_LIST, CMD_ZERO,
1431 invert);
1432 if (optarg) chain = optarg;
1433 else if (optind < argc && argv[optind][0] != '-'
1434 && argv[optind][0] != '!')
1435 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001436 if (optind < argc && argv[optind][0] != '-'
1437 && argv[optind][0] != '!')
1438 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001439 break;
1440
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001441 case 'S':
1442 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1443 invert);
1444 if (optarg) chain = optarg;
1445 else if (optind < argc && argv[optind][0] != '-'
1446 && argv[optind][0] != '!')
1447 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001448 if (optind < argc && argv[optind][0] != '-'
1449 && argv[optind][0] != '!')
1450 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001451 break;
1452
Rusty Russell5eed48a2000-06-02 20:12:24 +00001453 case 'F':
1454 add_command(&command, CMD_FLUSH, CMD_NONE,
1455 invert);
1456 if (optarg) chain = optarg;
1457 else if (optind < argc && argv[optind][0] != '-'
1458 && argv[optind][0] != '!')
1459 chain = argv[optind++];
1460 break;
1461
1462 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001463 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001464 invert);
1465 if (optarg) chain = optarg;
1466 else if (optind < argc && argv[optind][0] != '-'
1467 && argv[optind][0] != '!')
1468 chain = argv[optind++];
1469 break;
1470
1471 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001472 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001473 exit_error(PARAMETER_PROBLEM,
1474 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001475 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001476 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001477 exit_error(PARAMETER_PROBLEM,
1478 "chain name may not clash "
1479 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001480 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1481 invert);
1482 chain = optarg;
1483 break;
1484
1485 case 'X':
1486 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1487 invert);
1488 if (optarg) chain = optarg;
1489 else if (optind < argc && argv[optind][0] != '-'
1490 && argv[optind][0] != '!')
1491 chain = argv[optind++];
1492 break;
1493
1494 case 'E':
1495 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1496 invert);
1497 chain = optarg;
1498 if (optind < argc && argv[optind][0] != '-'
1499 && argv[optind][0] != '!')
1500 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001501 else
1502 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001503 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001504 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001505 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001506 break;
1507
1508 case 'P':
1509 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1510 invert);
1511 chain = optarg;
1512 if (optind < argc && argv[optind][0] != '-'
1513 && argv[optind][0] != '!')
1514 policy = argv[optind++];
1515 else
1516 exit_error(PARAMETER_PROBLEM,
1517 "-%c requires a chain and a policy",
1518 cmd2char(CMD_SET_POLICY));
1519 break;
1520
1521 case 'h':
1522 if (!optarg)
1523 optarg = argv[optind];
1524
Jonas Berlin1b91e592005-04-01 06:58:38 +00001525 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001526 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001527 xtables_find_match(protocol, XTF_TRY_LOAD,
1528 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001529
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001530 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001531
1532 /*
1533 * Option selection
1534 */
1535 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001536 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001537 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1538 invert);
1539
1540 /* Canonicalize into lower case */
1541 for (protocol = argv[optind-1]; *protocol; protocol++)
1542 *protocol = tolower(*protocol);
1543
1544 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001545 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546 fw.ipv6.flags |= IP6T_F_PROTO;
1547
1548 if (fw.ipv6.proto == 0
1549 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1550 exit_error(PARAMETER_PROBLEM,
1551 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001552
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001553 if (is_exthdr(fw.ipv6.proto)
1554 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001555 fprintf(stderr,
1556 "Warning: never matched protocol: %s. "
1557 "use extension match instead.\n",
1558 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001559 break;
1560
1561 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001562 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1564 invert);
1565 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001566 break;
1567
1568 case 'd':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001569 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1571 invert);
1572 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001573 break;
1574
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001575#ifdef IP6T_F_GOTO
1576 case 'g':
1577 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1578 invert);
1579 fw.ipv6.flags |= IP6T_F_GOTO;
1580 jumpto = parse_target(optarg);
1581 break;
1582#endif
1583
Rusty Russell5eed48a2000-06-02 20:12:24 +00001584 case 'j':
1585 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1586 invert);
1587 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001588 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001589 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001590
1591 if (target) {
1592 size_t size;
1593
Harald Welte43ac1912002-03-03 09:43:07 +00001594 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1595 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001596
Jan Engelhardt630ef482009-01-27 14:58:41 +01001597 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001598 target->t->u.target_size = size;
1599 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001600 set_revision(target->t->u.user.name,
1601 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001602 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001603 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001604 opts = merge_options(opts,
1605 target->extra_opts,
1606 &target->option_offset);
1607 if (opts == NULL)
1608 exit_error(OTHER_PROBLEM,
1609 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001610 }
1611 break;
1612
1613
1614 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001615 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001616 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1617 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001618 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001619 fw.ipv6.iniface,
1620 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001621 break;
1622
1623 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001624 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1626 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001627 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001628 fw.ipv6.outiface,
1629 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001630 break;
1631
1632 case 'v':
1633 if (!verbose)
1634 set_option(&options, OPT_VERBOSE,
1635 &fw.ipv6.invflags, invert);
1636 verbose++;
1637 break;
1638
1639 case 'm': {
1640 size_t size;
1641
1642 if (invert)
1643 exit_error(PARAMETER_PROBLEM,
1644 "unexpected ! flag before --match");
1645
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001646 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1647 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001648 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1649 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001650 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001651 m->m->u.match_size = size;
1652 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001653 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001654 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001655 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001656 if (m != m->next)
1657 /* Merge options for non-cloned matches */
1658 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001659 }
1660 break;
1661
1662 case 'n':
1663 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1664 invert);
1665 break;
1666
1667 case 't':
1668 if (invert)
1669 exit_error(PARAMETER_PROBLEM,
1670 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001671 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001672 break;
1673
1674 case 'x':
1675 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1676 invert);
1677 break;
1678
1679 case 'V':
1680 if (invert)
1681 printf("Not %s ;-)\n", program_version);
1682 else
1683 printf("%s v%s\n",
1684 program_name, program_version);
1685 exit(0);
1686
1687 case '0':
1688 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1689 invert);
1690 break;
1691
Harald Welte43ac1912002-03-03 09:43:07 +00001692 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001693 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001694 break;
1695
1696 case 'c':
1697
1698 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1699 invert);
1700 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001701 bcnt = strchr(pcnt + 1, ',');
1702 if (bcnt)
1703 bcnt++;
1704 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001705 && argv[optind][0] != '!')
1706 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001707 if (!bcnt)
Harald Welte43ac1912002-03-03 09:43:07 +00001708 exit_error(PARAMETER_PROBLEM,
1709 "-%c requires packet and byte counter",
1710 opt2char(OPT_COUNTERS));
1711
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001712 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001713 exit_error(PARAMETER_PROBLEM,
1714 "-%c packet counter not numeric",
1715 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001716 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001717
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001718 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001719 exit_error(PARAMETER_PROBLEM,
1720 "-%c byte counter not numeric",
1721 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001722 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001723 break;
1724
Rusty Russell5eed48a2000-06-02 20:12:24 +00001725 case 1: /* non option */
1726 if (optarg[0] == '!' && optarg[1] == '\0') {
1727 if (invert)
1728 exit_error(PARAMETER_PROBLEM,
1729 "multiple consecutive ! not"
1730 " allowed");
1731 invert = TRUE;
1732 optarg[0] = '\0';
1733 continue;
1734 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001735 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001736 exit_tryhelp(2);
1737
1738 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001739 if (!target
1740 || !(target->parse(c - target->option_offset,
1741 argv, invert,
1742 &target->tflags,
1743 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001744 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001745 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001746 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001747 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001748 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001749 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001750 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001751 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001752 break;
1753 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001754 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001755
1756 /* If you listen carefully, you can
1757 actually hear this code suck. */
1758
1759 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001760 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001761 * parameter, that has not been parsed yet,
1762 * it's not an option of an explicitly loaded
1763 * match or a target. However, we support
1764 * implicit loading of the protocol match
1765 * extension. '-p tcp' means 'l4 proto 6' and
1766 * at the same time 'load tcp protocol match on
1767 * demand if we specify --dport'.
1768 *
1769 * To make this work, we need to make sure:
1770 * - the parameter has not been parsed by
1771 * a match (m above)
1772 * - a protocol has been specified
1773 * - the protocol extension has not been
1774 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001775 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001776 * - the protocol extension can be successively
1777 * loaded
1778 */
1779 if (m == NULL
1780 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001781 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001782 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001783 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001784 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001785 && (proto_used == 0))
1786 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001787 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001788 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001789 /* Try loading protocol */
1790 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001791
Harald Welte00f5a9c2002-08-26 14:37:35 +00001792 proto_used = 1;
1793
1794 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1795 + m->size;
1796
Jan Engelhardt630ef482009-01-27 14:58:41 +01001797 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001798 m->m->u.match_size = size;
1799 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001800 set_revision(m->m->u.user.name,
1801 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001802 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001803 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001804
1805 opts = merge_options(opts,
1806 m->extra_opts, &m->option_offset);
1807
1808 optind--;
1809 continue;
1810 }
1811
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001812 if (!m) {
1813 if (c == '?') {
1814 if (optopt) {
1815 exit_error(
1816 PARAMETER_PROBLEM,
1817 "option `%s' "
1818 "requires an "
1819 "argument",
1820 argv[optind-1]);
1821 } else {
1822 exit_error(
1823 PARAMETER_PROBLEM,
1824 "unknown option "
1825 "`%s'",
1826 argv[optind-1]);
1827 }
1828 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001829 exit_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001830 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001831 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 }
1833 }
1834 invert = FALSE;
1835 }
1836
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001837 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001838 if (matchp->match->final_check != NULL)
1839 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001840
Jan Engelhardt830132a2007-10-04 16:24:50 +00001841 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001842 target->final_check(target->tflags);
1843
1844 /* Fix me: must put inverse options checking here --MN */
1845
1846 if (optind < argc)
1847 exit_error(PARAMETER_PROBLEM,
1848 "unknown arguments found on commandline");
1849 if (!command)
1850 exit_error(PARAMETER_PROBLEM, "no command specified");
1851 if (invert)
1852 exit_error(PARAMETER_PROBLEM,
1853 "nothing appropriate following !");
1854
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001855 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001856 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001857 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001859 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860 }
1861
1862 if (shostnetworkmask)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001863 xtables_ip6parse_any(shostnetworkmask, &saddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001864 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001865
1866 if (dhostnetworkmask)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001867 xtables_ip6parse_any(dhostnetworkmask, &daddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001868 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001869
1870 if ((nsaddrs > 1 || ndaddrs > 1) &&
1871 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1872 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1873 " source or destination IP addresses");
1874
Rusty Russell5eed48a2000-06-02 20:12:24 +00001875 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1876 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1877 "specify a unique address");
1878
1879 generic_opt_check(command, options);
1880
1881 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1882 exit_error(PARAMETER_PROBLEM,
1883 "chain name `%s' too long (must be under %i chars)",
1884 chain, IP6T_FUNCTION_MAXNAMELEN);
1885
Harald Welte43ac1912002-03-03 09:43:07 +00001886 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001887 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001888 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001889
Rusty Russell8beb0492004-12-22 00:37:10 +00001890 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001891 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001892 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001893
Harald Welte43ac1912002-03-03 09:43:07 +00001894 if (!*handle)
1895 exit_error(VERSION_PROBLEM,
1896 "can't initialize ip6tables table `%s': %s",
1897 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001899 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900 || command == CMD_DELETE
1901 || command == CMD_INSERT
1902 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001903 if (strcmp(chain, "PREROUTING") == 0
1904 || strcmp(chain, "INPUT") == 0) {
1905 /* -o not valid with incoming packets. */
1906 if (options & OPT_VIANAMEOUT)
1907 exit_error(PARAMETER_PROBLEM,
1908 "Can't use -%c with %s\n",
1909 opt2char(OPT_VIANAMEOUT),
1910 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001911 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912
Harald Welte43ac1912002-03-03 09:43:07 +00001913 if (strcmp(chain, "POSTROUTING") == 0
1914 || strcmp(chain, "OUTPUT") == 0) {
1915 /* -i not valid with outgoing packets */
1916 if (options & OPT_VIANAMEIN)
1917 exit_error(PARAMETER_PROBLEM,
1918 "Can't use -%c with %s\n",
1919 opt2char(OPT_VIANAMEIN),
1920 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001921 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922
1923 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001924 fprintf(stderr,
1925 "Warning: using chain %s, not extension\n",
1926 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001928 if (target->t)
1929 free(target->t);
1930
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 target = NULL;
1932 }
1933
1934 /* If they didn't specify a target, or it's a chain
1935 name, use standard. */
1936 if (!target
1937 && (strlen(jumpto) == 0
1938 || ip6tc_is_chain(jumpto, *handle))) {
1939 size_t size;
1940
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001941 target = xtables_find_target(IP6T_STANDARD_TARGET,
1942 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001943
1944 size = sizeof(struct ip6t_entry_target)
1945 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001946 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 target->t->u.target_size = size;
1948 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001949 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001950 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 }
1952
1953 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001954 /* it is no chain, and we can't load a plugin.
1955 * We cannot know if the plugin is corrupt, non
1956 * existant OR if the user just misspelled a
1957 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001958#ifdef IP6T_F_GOTO
1959 if (fw.ipv6.flags & IP6T_F_GOTO)
1960 exit_error(PARAMETER_PROBLEM,
1961 "goto '%s' is not a chain\n", jumpto);
1962#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001963 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001964 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001965 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001966 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001967 }
1968 }
1969
1970 switch (command) {
1971 case CMD_APPEND:
1972 ret = append_entry(chain, e,
1973 nsaddrs, saddrs, ndaddrs, daddrs,
1974 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001975 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001976 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001977 case CMD_DELETE:
1978 ret = delete_entry(chain, e,
1979 nsaddrs, saddrs, ndaddrs, daddrs,
1980 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001981 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 break;
1983 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001984 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985 break;
1986 case CMD_REPLACE:
1987 ret = replace_entry(chain, e, rulenum - 1,
1988 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001989 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 break;
1991 case CMD_INSERT:
1992 ret = insert_entry(chain, e, rulenum - 1,
1993 nsaddrs, saddrs, ndaddrs, daddrs,
1994 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001995 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001996 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001997 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001998 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001999 break;
2000 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002001 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002002 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002003 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002004 case CMD_LIST|CMD_ZERO:
2005 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002006 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007 options&OPT_VERBOSE,
2008 options&OPT_NUMERIC,
2009 options&OPT_EXPANDED,
2010 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002011 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002012 if (ret && (command & CMD_ZERO))
2013 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002014 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002015 break;
2016 case CMD_LIST_RULES:
2017 case CMD_LIST_RULES|CMD_ZERO:
2018 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002019 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002020 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002021 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002022 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002023 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002024 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002025 break;
2026 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002027 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002028 break;
2029 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002030 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002031 break;
2032 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002033 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002034 break;
2035 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002036 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002037 break;
2038 default:
2039 /* We should never reach this... */
2040 exit_tryhelp(2);
2041 }
2042
2043 if (verbose > 1)
2044 dump_entries6(*handle);
2045
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002046 clear_rule_matches(&matches);
2047
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002048 if (e != NULL) {
2049 free(e);
2050 e = NULL;
2051 }
2052
Max Kellermann9ee386a2008-01-29 13:48:05 +00002053 for (i = 0; i < nsaddrs; i++)
2054 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002055
Max Kellermann9ee386a2008-01-29 13:48:05 +00002056 for (i = 0; i < ndaddrs; i++)
2057 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002058
Pablo Neiradfdcd642005-05-29 19:05:23 +00002059 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002060
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 return ret;
2062}