blob: 71ff46f5bc5b095719f9c26a933a31789b1c0b73 [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
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100144void ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100145struct xtables_globals ip6tables_globals = {
146 .option_offset = 0,
147 .program_version = IPTABLES_VERSION,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100148 .opts = original_opts,
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500149 .orig_opts = original_opts,
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100150 .exit_err = ip6tables_exit_error,
Jamal Hadi Salim4dcdc9b2009-02-11 13:03:34 +0100151};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000152
153/* Table of legal combinations of commands and options. If any of the
154 * given commands make an option legal, that option is legal (applies to
155 * CMD_LIST and CMD_ZERO only).
156 * Key:
157 * + compulsory
158 * x illegal
159 * optional
160 */
161
162static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
163/* Well, it's better than "Re: Linux vs FreeBSD" */
164{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200165 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000166/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
168/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
169/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
170/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
171/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
172/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
175/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200176/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200177/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
178/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000179};
180
181static int inverse_for_options[NUMBER_OF_OPT] =
182{
183/* -n */ 0,
184/* -s */ IP6T_INV_SRCIP,
185/* -d */ IP6T_INV_DSTIP,
186/* -p */ IP6T_INV_PROTO,
187/* -j */ 0,
188/* -v */ 0,
189/* -x */ 0,
190/* -i */ IP6T_INV_VIA_IN,
191/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000192/*--line*/ 0,
193/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194};
195
Pablo Neira Ayusof503cb82009-03-03 17:46:17 +0100196#define opts ip6tables_globals.opts
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"
Jan Engelhardt1791a452009-02-20 16:39:54 +0100243" %s -I chain [rulenum] rule-specification [options]\n"
244" %s -R chain rulenum rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000245" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200246" %s -[LS] [chain [rulenum]] [options]\n"
247" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000248" %s -[NX] chain\n"
249" %s -E old-chain-name new-chain-name\n"
250" %s -P chain target [options]\n"
251" %s -h (print this help information)\n\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500252 prog_name, prog_vers, prog_name, prog_name,
253 prog_name, prog_name, prog_name, prog_name,
Jan Engelhardt1791a452009-02-20 16:39:54 +0100254 prog_name, prog_name, prog_name, prog_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255
256 printf(
257"Commands:\n"
258"Either long or short options are allowed.\n"
259" --append -A chain Append to chain\n"
260" --delete -D chain Delete matching rule from chain\n"
261" --delete -D chain rulenum\n"
262" Delete rule rulenum (1 = first) from chain\n"
263" --insert -I chain [rulenum]\n"
264" Insert in chain as rulenum (default 1=first)\n"
265" --replace -R chain rulenum\n"
266" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200267" --list -L [chain [rulenum]]\n"
268" List the rules in a chain or all chains\n"
269" --list-rules -S [chain [rulenum]]\n"
270" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271" --flush -F [chain] Delete all rules in chain or all chains\n"
272" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000273" --new -N chain Create a new user-defined chain\n"
274" --delete-chain\n"
275" -X [chain] Delete a user-defined chain\n"
276" --policy -P chain target\n"
277" Change policy on chain to target\n"
278" --rename-chain\n"
279" -E old-chain new-chain\n"
280" Change chain name, (moving any references)\n"
281
282"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200283"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100284"[!] --source -s address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" source specification\n"
Michael Granzow332e4ac2009-04-09 18:24:36 +0100286"[!] --destination -d address[/mask][,...]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200288"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289" network interface name ([+] for wildcard)\n"
290" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000291" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200292#ifdef IP6T_F_GOTO
293" --goto -g chain\n"
294" jump to chain with no return\n"
295#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000296" --match -m match\n"
297" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000298" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200299"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000300" network interface name ([+] for wildcard)\n"
301" --table -t table table to manipulate (default: `filter')\n"
302" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000303" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000304" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000305/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000306" --modprobe=<command> try to insert modules using this command\n"
307" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000308"[!] --version -V print package version.\n");
309
Max Kellermann5b76f682008-01-29 13:42:48 +0000310 /* Print out any special helps. A user might like to be able to add a --help
311 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000312 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000313 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000314 if (t->used) {
315 printf("\n");
316 t->help();
317 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000318 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000319 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000320 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000321 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322 }
323 exit(0);
324}
325
Patrick McHardy0b639362007-09-08 16:00:01 +0000326void
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100327ip6tables_exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000328{
329 va_list args;
330
331 va_start(args, msg);
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500332 fprintf(stderr, "%s v%s: ", prog_name, prog_vers);
Patrick McHardy0b639362007-09-08 16:00:01 +0000333 vfprintf(stderr, msg, args);
334 va_end(args);
335 fprintf(stderr, "\n");
336 if (status == PARAMETER_PROBLEM)
337 exit_tryhelp(status);
338 if (status == VERSION_PROBLEM)
339 fprintf(stderr,
340 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
341 /* On error paths, make sure that we don't leak memory */
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -0500342 xtables_free_opts(1);
Patrick McHardy0b639362007-09-08 16:00:01 +0000343 exit(status);
344}
345
Rusty Russell5eed48a2000-06-02 20:12:24 +0000346static void
347generic_opt_check(int command, int options)
348{
349 int i, j, legal = 0;
350
351 /* Check that commands are valid with options. Complicated by the
352 * fact that if an option is legal with *any* command given, it is
353 * legal overall (ie. -z and -l).
354 */
355 for (i = 0; i < NUMBER_OF_OPT; i++) {
356 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
357
358 for (j = 0; j < NUMBER_OF_CMD; j++) {
359 if (!(command & (1<<j)))
360 continue;
361
362 if (!(options & (1<<i))) {
363 if (commands_v_options[j][i] == '+')
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100364 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000365 "You need to supply the `-%c' "
366 "option for this command\n",
367 optflags[i]);
368 } else {
369 if (commands_v_options[j][i] != 'x')
370 legal = 1;
371 else if (legal == 0)
372 legal = -1;
373 }
374 }
375 if (legal == -1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100376 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000377 "Illegal option `-%c' with this command\n",
378 optflags[i]);
379 }
380}
381
382static char
383opt2char(int option)
384{
385 const char *ptr;
386 for (ptr = optflags; option > 1; option >>= 1, ptr++);
387
388 return *ptr;
389}
390
391static char
392cmd2char(int option)
393{
394 const char *ptr;
395 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
396
397 return *ptr;
398}
399
400static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000401add_command(unsigned int *cmd, const int newcmd, const int othercmds,
402 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000403{
404 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100405 xtables_error(PARAMETER_PROBLEM, "unexpected '!' flag");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000406 if (*cmd & (~othercmds))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100407 xtables_error(PARAMETER_PROBLEM, "Cannot use -%c with -%c\n",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000408 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
409 *cmd |= newcmd;
410}
411
Rusty Russell5eed48a2000-06-02 20:12:24 +0000412/*
413 * All functions starting with "parse" should succeed, otherwise
414 * the program fails.
415 * Most routines return pointers to static data that may change
416 * between calls to the same or other routines with a few exceptions:
417 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
418 * return global static data.
419*/
420
Rusty Russell5eed48a2000-06-02 20:12:24 +0000421/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200422static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100423find_proto(const char *pname, enum xtables_tryload tryload,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100424 int nolookup, struct xtables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000425{
Harald Welteed498492001-07-23 01:24:22 +0000426 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000427
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100428 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100429 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000430
Harald Welte43ac1912002-03-03 09:43:07 +0000431 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100432 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000433 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100434 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000435
436 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000437}
438
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000439/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000440static int is_exthdr(u_int16_t proto)
441{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000442 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000443 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000444 proto == IPPROTO_AH ||
445 proto == IPPROTO_DSTOPTS);
446}
447
Rusty Russell5eed48a2000-06-02 20:12:24 +0000448/* Can't be zero. */
449static int
450parse_rulenumber(const char *rule)
451{
Harald Welte43ac1912002-03-03 09:43:07 +0000452 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000453
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100454 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100455 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000456 "Invalid rule number `%s'", rule);
457
458 return rulenum;
459}
460
461static const char *
462parse_target(const char *targetname)
463{
464 const char *ptr;
465
466 if (strlen(targetname) < 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100467 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000468 "Invalid target name (too short)");
469
470 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100471 xtables_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000472 "Invalid target name `%s' (%u chars max)",
473 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474
475 for (ptr = targetname; *ptr; ptr++)
476 if (isspace(*ptr))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100477 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478 "Invalid target name `%s'", targetname);
479 return targetname;
480}
481
Rusty Russell5eed48a2000-06-02 20:12:24 +0000482static void
483set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
484 int invert)
485{
486 if (*options & option)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100487 xtables_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488 opt2char(option));
489 *options |= option;
490
491 if (invert) {
492 unsigned int i;
493 for (i = 0; 1 << i != option; i++);
494
495 if (!inverse_for_options[i])
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100496 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000497 "cannot have ! before -%c",
498 opt2char(option));
499 *invflg |= inverse_for_options[i];
500 }
501}
502
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000503static void
504print_num(u_int64_t number, unsigned int format)
505{
Harald Welte43ac1912002-03-03 09:43:07 +0000506 if (format & FMT_KILOMEGAGIGA) {
507 if (number > 99999) {
508 number = (number + 500) / 1000;
509 if (number > 9999) {
510 number = (number + 500) / 1000;
511 if (number > 9999) {
512 number = (number + 500) / 1000;
513 if (number > 9999) {
514 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000515 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000516 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000517 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000518 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000519 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000520 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000521 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000522 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000523 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000524 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000525 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000526}
527
Harald Welte43ac1912002-03-03 09:43:07 +0000528
Rusty Russell5eed48a2000-06-02 20:12:24 +0000529static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100530print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000531{
532 struct ip6t_counters counters;
533 const char *pol = ip6tc_get_policy(chain, &counters, handle);
534 printf("Chain %s", chain);
535 if (pol) {
536 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000537 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000538 fputc(' ', stdout);
539 print_num(counters.pcnt, (format|FMT_NOTABLE));
540 fputs("packets, ", stdout);
541 print_num(counters.bcnt, (format|FMT_NOTABLE));
542 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000543 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000544 printf(")\n");
545 } else {
546 unsigned int refs;
547 if (!ip6tc_get_references(&refs, chain, handle))
548 printf(" (ERROR obtaining refs)\n");
549 else
550 printf(" (%u references)\n", refs);
551 }
552
553 if (format & FMT_LINENUMBERS)
554 printf(FMT("%-4s ", "%s "), "num");
555 if (!(format & FMT_NOCOUNTS)) {
556 if (format & FMT_KILOMEGAGIGA) {
557 printf(FMT("%5s ","%s "), "pkts");
558 printf(FMT("%5s ","%s "), "bytes");
559 } else {
560 printf(FMT("%8s ","%s "), "pkts");
561 printf(FMT("%10s ","%s "), "bytes");
562 }
563 }
564 if (!(format & FMT_NOTARGET))
565 printf(FMT("%-9s ","%s "), "target");
566 fputs(" prot ", stdout);
567 if (format & FMT_OPTIONS)
568 fputs("opt", stdout);
569 if (format & FMT_VIA) {
570 printf(FMT(" %-6s ","%s "), "in");
571 printf(FMT("%-6s ","%s "), "out");
572 }
573 printf(FMT(" %-19s ","%s "), "source");
574 printf(FMT(" %-19s "," %s "), "destination");
575 printf("\n");
576}
577
Harald Welte43ac1912002-03-03 09:43:07 +0000578
Rusty Russell5eed48a2000-06-02 20:12:24 +0000579static int
580print_match(const struct ip6t_entry_match *m,
581 const struct ip6t_ip6 *ip,
582 int numeric)
583{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100584 struct xtables_match *match =
585 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000586
587 if (match) {
588 if (match->print)
589 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000590 else
591 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000592 } else {
593 if (m->u.user.name[0])
594 printf("UNKNOWN match `%s' ", m->u.user.name);
595 }
596 /* Don't stop iterating. */
597 return 0;
598}
599
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100600/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601static void
602print_firewall(const struct ip6t_entry *fw,
603 const char *targname,
604 unsigned int num,
605 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100606 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000607{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200608 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000609 const struct ip6t_entry_target *t;
610 u_int8_t flags;
611 char buf[BUFSIZ];
612
Rusty Russell5eed48a2000-06-02 20:12:24 +0000613 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100614 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000615 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100616 target = xtables_find_target(IP6T_STANDARD_TARGET,
617 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000618
619 t = ip6t_get_target((struct ip6t_entry *)fw);
620 flags = fw->ipv6.flags;
621
622 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200623 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000624
625 if (!(format & FMT_NOCOUNTS)) {
626 print_num(fw->counters.pcnt, format);
627 print_num(fw->counters.bcnt, format);
628 }
629
630 if (!(format & FMT_NOTARGET))
631 printf(FMT("%-9s ", "%s "), targname);
632
633 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
634 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100635 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000636 if (pname)
637 printf(FMT("%-5s", "%s "), pname);
638 else
639 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
640 }
641
642 if (format & FMT_OPTIONS) {
643 if (format & FMT_NOTABLE)
644 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000645 fputc(' ', stdout); /* Invert flag of FRAG */
646 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000647 fputc(' ', stdout);
648 }
649
650 if (format & FMT_VIA) {
651 char iface[IFNAMSIZ+2];
652
653 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
654 iface[0] = '!';
655 iface[1] = '\0';
656 }
657 else iface[0] = '\0';
658
659 if (fw->ipv6.iniface[0] != '\0') {
660 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000661 }
662 else if (format & FMT_NUMERIC) strcat(iface, "*");
663 else strcat(iface, "any");
664 printf(FMT(" %-6s ","in %s "), iface);
665
666 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
667 iface[0] = '!';
668 iface[1] = '\0';
669 }
670 else iface[0] = '\0';
671
672 if (fw->ipv6.outiface[0] != '\0') {
673 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000674 }
675 else if (format & FMT_NUMERIC) strcat(iface, "*");
676 else strcat(iface, "any");
677 printf(FMT("%-6s ","out %s "), iface);
678 }
679
680 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000681 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000682 && !(format & FMT_NUMERIC))
683 printf(FMT("%-19s ","%s "), "anywhere");
684 else {
685 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100686 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000687 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100688 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
689 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000690 printf(FMT("%-19s ","%s "), buf);
691 }
692
693 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
694 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
695 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200696 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000697 else {
698 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100699 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100701 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
702 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200703 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000704 }
705
706 if (format & FMT_NOTABLE)
707 fputs(" ", stdout);
708
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200709#ifdef IP6T_F_GOTO
710 if(fw->ipv6.flags & IP6T_F_GOTO)
711 printf("[goto] ");
712#endif
713
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
715
716 if (target) {
717 if (target->print)
718 /* Print the target information. */
719 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
720 } else if (t->u.target_size != sizeof(*t))
721 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000722 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000723
724 if (!(format & FMT_NONEWLINE))
725 fputc('\n', stdout);
726}
727
728static void
729print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100730 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000731{
732 struct ip6t_entry_target *t;
733
734 t = ip6t_get_target((struct ip6t_entry *)fw);
735 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
736}
737
738static int
739append_entry(const ip6t_chainlabel chain,
740 struct ip6t_entry *fw,
741 unsigned int nsaddrs,
742 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100743 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000744 unsigned int ndaddrs,
745 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100746 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000747 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100748 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749{
750 unsigned int i, j;
751 int ret = 1;
752
753 for (i = 0; i < nsaddrs; i++) {
754 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100755 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756 for (j = 0; j < ndaddrs; j++) {
757 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100758 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000759 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100760 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761 ret &= ip6tc_append_entry(chain, fw, handle);
762 }
763 }
764
765 return ret;
766}
767
768static int
769replace_entry(const ip6t_chainlabel chain,
770 struct ip6t_entry *fw,
771 unsigned int rulenum,
772 const struct in6_addr *saddr,
773 const struct in6_addr *daddr,
774 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100775 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000776{
777 fw->ipv6.src = *saddr;
778 fw->ipv6.dst = *daddr;
779
780 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100781 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782 return ip6tc_replace_entry(chain, fw, rulenum, handle);
783}
784
785static int
786insert_entry(const ip6t_chainlabel chain,
787 struct ip6t_entry *fw,
788 unsigned int rulenum,
789 unsigned int nsaddrs,
790 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100791 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792 unsigned int ndaddrs,
793 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100794 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100796 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797{
798 unsigned int i, j;
799 int ret = 1;
800
801 for (i = 0; i < nsaddrs; i++) {
802 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100803 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000804 for (j = 0; j < ndaddrs; j++) {
805 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100806 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000807 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100808 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
810 }
811 }
812
813 return ret;
814}
815
816static unsigned char *
Michael Granzow332e4ac2009-04-09 18:24:36 +0100817make_delete_mask(struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818{
819 /* Establish mask for comparison */
820 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +0100821 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 unsigned char *mask, *mptr;
823
824 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000825 for (matchp = matches; matchp; matchp = matchp->next)
826 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827
Jan Engelhardt630ef482009-01-27 14:58:41 +0100828 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000829 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000830 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831
832 memset(mask, 0xFF, sizeof(struct ip6t_entry));
833 mptr = mask + sizeof(struct ip6t_entry);
834
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000835 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000837 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000838 + matchp->match->userspacesize);
839 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840 }
841
Max Kellermann5b76f682008-01-29 13:42:48 +0000842 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000843 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000844 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000845
846 return mask;
847}
848
849static int
850delete_entry(const ip6t_chainlabel chain,
851 struct ip6t_entry *fw,
852 unsigned int nsaddrs,
853 const struct in6_addr saddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100854 const struct in6_addr smasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 unsigned int ndaddrs,
856 const struct in6_addr daddrs[],
Michael Granzow332e4ac2009-04-09 18:24:36 +0100857 const struct in6_addr dmasks[],
Rusty Russell5eed48a2000-06-02 20:12:24 +0000858 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100859 struct ip6tc_handle *handle,
Jan Engelhardt395e4412009-02-10 10:43:08 +0100860 struct xtables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000861{
862 unsigned int i, j;
863 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000864 unsigned char *mask;
865
Michael Granzow332e4ac2009-04-09 18:24:36 +0100866 mask = make_delete_mask(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000867 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000868 fw->ipv6.src = saddrs[i];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100869 fw->ipv6.smsk = smasks[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000870 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000871 fw->ipv6.dst = daddrs[j];
Michael Granzow332e4ac2009-04-09 18:24:36 +0100872 fw->ipv6.dmsk = dmasks[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100874 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000875 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000876 }
877 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000878 free(mask);
879
Rusty Russell5eed48a2000-06-02 20:12:24 +0000880 return ret;
881}
882
András Kis-Szabó764316a2001-02-26 17:31:20 +0000883int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100884for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
885 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000886{
Max Kellermann5b76f682008-01-29 13:42:48 +0000887 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000888 const char *chain;
889 char *chains;
890 unsigned int i, chaincount = 0;
891
892 chain = ip6tc_first_chain(handle);
893 while (chain) {
894 chaincount++;
895 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000896 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897
Jan Engelhardt630ef482009-01-27 14:58:41 +0100898 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000899 i = 0;
900 chain = ip6tc_first_chain(handle);
901 while (chain) {
902 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
903 i++;
904 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000905 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906
907 for (i = 0; i < chaincount; i++) {
908 if (!builtinstoo
909 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100910 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000911 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000912 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000913 }
914
915 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000916 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000917}
918
András Kis-Szabó764316a2001-02-26 17:31:20 +0000919int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000920flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100921 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000922{
923 if (!chain)
924 return for_each_chain(flush_entries, verbose, 1, handle);
925
926 if (verbose)
927 fprintf(stdout, "Flushing chain `%s'\n", chain);
928 return ip6tc_flush_entries(chain, handle);
929}
930
931static int
932zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100933 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000934{
935 if (!chain)
936 return for_each_chain(zero_entries, verbose, 1, handle);
937
938 if (verbose)
939 fprintf(stdout, "Zeroing chain `%s'\n", chain);
940 return ip6tc_zero_entries(chain, handle);
941}
942
András Kis-Szabó764316a2001-02-26 17:31:20 +0000943int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000944delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100945 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000946{
947 if (!chain)
948 return for_each_chain(delete_chain, verbose, 0, handle);
949
950 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000951 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000952 return ip6tc_delete_chain(chain, handle);
953}
954
955static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200956list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100957 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000958{
959 int found = 0;
960 unsigned int format;
961 const char *this;
962
963 format = FMT_OPTIONS;
964 if (!verbose)
965 format |= FMT_NOCOUNTS;
966 else
967 format |= FMT_VIA;
968
969 if (numeric)
970 format |= FMT_NUMERIC;
971
972 if (!expanded)
973 format |= FMT_KILOMEGAGIGA;
974
975 if (linenumbers)
976 format |= FMT_LINENUMBERS;
977
978 for (this = ip6tc_first_chain(handle);
979 this;
980 this = ip6tc_next_chain(handle)) {
981 const struct ip6t_entry *i;
982 unsigned int num;
983
984 if (chain && strcmp(chain, this) != 0)
985 continue;
986
987 if (found) printf("\n");
988
Henrik Nordstrombb340822008-05-13 13:09:23 +0200989 if (!rulenum)
990 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000991 i = ip6tc_first_rule(this, handle);
992
993 num = 0;
994 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +0200995 num++;
996 if (!rulenum || num == rulenum)
997 print_firewall(i,
998 ip6tc_get_target(i, handle),
999 num,
1000 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001001 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001002 i = ip6tc_next_rule(i, handle);
1003 }
1004 found = 1;
1005 }
1006
1007 errno = ENOENT;
1008 return found;
1009}
1010
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001011/* This assumes that mask is contiguous, and byte-bounded. */
1012static void
1013print_iface(char letter, const char *iface, const unsigned char *mask,
1014 int invert)
1015{
1016 unsigned int i;
1017
1018 if (mask[0] == 0)
1019 return;
1020
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001021 printf("%s-%c ", invert ? "! " : "", letter);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001022
1023 for (i = 0; i < IFNAMSIZ; i++) {
1024 if (mask[i] != 0) {
1025 if (iface[i] != '\0')
1026 printf("%c", iface[i]);
1027 } else {
1028 /* we can access iface[i-1] here, because
1029 * a few lines above we make sure that mask[0] != 0 */
1030 if (iface[i-1] != '\0')
1031 printf("+");
1032 break;
1033 }
1034 }
1035
1036 printf(" ");
1037}
1038
1039/* The ip6tables looks up the /etc/protocols. */
1040static void print_proto(u_int16_t proto, int invert)
1041{
1042 if (proto) {
1043 unsigned int i;
1044 const char *invertstr = invert ? "! " : "";
1045
1046 struct protoent *pent = getprotobynumber(proto);
1047 if (pent) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001048 printf("%s-p %s ",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001049 invertstr, pent->p_name);
1050 return;
1051 }
1052
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001053 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1054 if (xtables_chain_protos[i].num == proto) {
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001055 printf("%s-p %s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001056 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001057 return;
1058 }
1059
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001060 printf("%s-p %u ", invertstr, proto);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001061 }
1062}
1063
1064static int print_match_save(const struct ip6t_entry_match *e,
1065 const struct ip6t_ip6 *ip)
1066{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001067 struct xtables_match *match =
1068 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001069
1070 if (match) {
1071 printf("-m %s ", e->u.user.name);
1072
1073 /* some matches don't provide a save function */
1074 if (match->save)
1075 match->save(ip, e);
1076 } else {
1077 if (e->u.match_size) {
1078 fprintf(stderr,
1079 "Can't find library for match `%s'\n",
1080 e->u.user.name);
1081 exit(1);
1082 }
1083 }
1084 return 0;
1085}
1086
1087/* print a given ip including mask if neccessary */
1088static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1089{
1090 char buf[51];
1091 int l = ipv6_prefix_length(mask);
1092
1093 if (l == 0 && !invert)
1094 return;
1095
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001096 printf("%s%s %s",
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001097 invert ? "! " : "",
Jan Engelhardtb1d968c2009-04-04 13:28:40 +02001098 prefix,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001099 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1100
1101 if (l == -1)
1102 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1103 else
1104 printf("/%d ", l);
1105}
1106
1107/* We want this to be readable, so only print out neccessary fields.
1108 * Because that's the kind of world I want to live in. */
1109void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001110 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001111{
1112 struct ip6t_entry_target *t;
1113 const char *target_name;
1114
1115 /* print counters for iptables-save */
1116 if (counters > 0)
1117 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1118
1119 /* print chain name */
1120 printf("-A %s ", chain);
1121
1122 /* Print IP part. */
1123 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1124 e->ipv6.invflags & IP6T_INV_SRCIP);
1125
1126 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1127 e->ipv6.invflags & IP6T_INV_DSTIP);
1128
1129 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1130 e->ipv6.invflags & IP6T_INV_VIA_IN);
1131
1132 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1133 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1134
1135 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1136
1137#if 0
1138 /* not definied in ipv6
1139 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1140 if (e->ipv6.flags & IPT_F_FRAG)
1141 printf("%s-f ",
1142 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1143#endif
1144
1145 if (e->ipv6.flags & IP6T_F_TOS)
1146 printf("%s-? %d ",
1147 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1148 e->ipv6.tos);
1149
1150 /* Print matchinfo part */
1151 if (e->target_offset) {
1152 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1153 }
1154
1155 /* print counters for iptables -R */
1156 if (counters < 0)
1157 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1158
1159 /* Print target name */
1160 target_name = ip6tc_get_target(e, h);
1161 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001162#ifdef IP6T_F_GOTO
1163 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1164#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001165 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001166#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001167
1168 /* Print targinfo part */
1169 t = ip6t_get_target((struct ip6t_entry *)e);
1170 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001171 struct xtables_target *target =
1172 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001173
1174 if (!target) {
1175 fprintf(stderr, "Can't find library for target `%s'\n",
1176 t->u.user.name);
1177 exit(1);
1178 }
1179
1180 if (target->save)
1181 target->save(&e->ipv6, t);
1182 else {
1183 /* If the target size is greater than ip6t_entry_target
1184 * there is something to be saved, we just don't know
1185 * how to print it */
1186 if (t->u.target_size !=
1187 sizeof(struct ip6t_entry_target)) {
1188 fprintf(stderr, "Target `%s' is missing "
1189 "save function\n",
1190 t->u.user.name);
1191 exit(1);
1192 }
1193 }
1194 }
1195 printf("\n");
1196}
1197
1198static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001199list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001200 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001201{
1202 const char *this = NULL;
1203 int found = 0;
1204
1205 if (counters)
1206 counters = -1; /* iptables -c format */
1207
1208 /* Dump out chain names first,
1209 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001210 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001211 this;
1212 this = ip6tc_next_chain(handle)) {
1213 if (chain && strcmp(this, chain) != 0)
1214 continue;
1215
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001216 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001217 struct ip6t_counters count;
1218 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1219 if (counters)
1220 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1221 printf("\n");
1222 } else {
1223 printf("-N %s\n", this);
1224 }
1225 }
1226
1227 for (this = ip6tc_first_chain(handle);
1228 this;
1229 this = ip6tc_next_chain(handle)) {
1230 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001231 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001232
1233 if (chain && strcmp(this, chain) != 0)
1234 continue;
1235
1236 /* Dump out rules */
1237 e = ip6tc_first_rule(this, handle);
1238 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001239 num++;
1240 if (!rulenum || num == rulenum)
1241 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001242 e = ip6tc_next_rule(e, handle);
1243 }
1244 found = 1;
1245 }
1246
1247 errno = ENOENT;
1248 return found;
1249}
1250
Rusty Russell5eed48a2000-06-02 20:12:24 +00001251static struct ip6t_entry *
1252generate_entry(const struct ip6t_entry *fw,
Jan Engelhardt395e4412009-02-10 10:43:08 +01001253 struct xtables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001254 struct ip6t_entry_target *target)
1255{
1256 unsigned int size;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001257 struct xtables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001258 struct ip6t_entry *e;
1259
1260 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001261 for (matchp = matches; matchp; matchp = matchp->next)
1262 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001263
Jan Engelhardt630ef482009-01-27 14:58:41 +01001264 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001265 *e = *fw;
1266 e->target_offset = size;
1267 e->next_offset = size + target->u.target_size;
1268
1269 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001270 for (matchp = matches; matchp; matchp = matchp->next) {
1271 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1272 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001273 }
1274 memcpy(e->elems + size, target, target->u.target_size);
1275
1276 return e;
1277}
1278
Jan Engelhardt395e4412009-02-10 10:43:08 +01001279static void clear_rule_matches(struct xtables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001280{
Jan Engelhardt395e4412009-02-10 10:43:08 +01001281 struct xtables_rule_match *matchp, *tmp;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001282
1283 for (matchp = *matches; matchp;) {
1284 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001285 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001286 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001287 matchp->match->m = NULL;
1288 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001289 if (matchp->match == matchp->match->next) {
1290 free(matchp->match);
1291 matchp->match = NULL;
1292 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001293 free(matchp);
1294 matchp = tmp;
1295 }
1296
1297 *matches = NULL;
1298}
1299
Jan Engelhardtfd187312008-11-10 16:59:27 +01001300int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001301{
1302 struct ip6t_entry fw, *e = NULL;
1303 int invert = 0;
1304 unsigned int nsaddrs = 0, ndaddrs = 0;
1305 struct in6_addr *saddrs = NULL, *daddrs = NULL;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001306 struct in6_addr *smasks = NULL, *dmasks = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001307
1308 int c, verbose = 0;
1309 const char *chain = NULL;
1310 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1311 const char *policy = NULL, *newname = NULL;
1312 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001313 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001314 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001315 struct xtables_match *m;
Jan Engelhardt395e4412009-02-10 10:43:08 +01001316 struct xtables_rule_match *matches = NULL;
1317 struct xtables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001318 struct xtables_target *target = NULL;
1319 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001320 const char *jumpto = "";
1321 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001322 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001323 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001324
1325 memset(&fw, 0, sizeof(fw));
1326
Harald Welte43ac1912002-03-03 09:43:07 +00001327 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001328 * a second time */
1329 optind = 0;
1330
Harald Welte43ac1912002-03-03 09:43:07 +00001331 /* clear mflags in case do_command gets called a second time
1332 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001333 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001334 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001335
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001336 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001337 t->tflags = 0;
1338 t->used = 0;
1339 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001340
Rusty Russell5eed48a2000-06-02 20:12:24 +00001341 /* Suppress error messages: we may add new options if we
1342 demand-load a protocol. */
1343 opterr = 0;
1344
1345 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001346 "-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 +00001347 opts, NULL)) != -1) {
1348 switch (c) {
1349 /*
1350 * Command selection
1351 */
1352 case 'A':
1353 add_command(&command, CMD_APPEND, CMD_NONE,
1354 invert);
1355 chain = optarg;
1356 break;
1357
1358 case 'D':
1359 add_command(&command, CMD_DELETE, CMD_NONE,
1360 invert);
1361 chain = optarg;
1362 if (optind < argc && argv[optind][0] != '-'
1363 && argv[optind][0] != '!') {
1364 rulenum = parse_rulenumber(argv[optind++]);
1365 command = CMD_DELETE_NUM;
1366 }
1367 break;
1368
Rusty Russell5eed48a2000-06-02 20:12:24 +00001369 case 'R':
1370 add_command(&command, CMD_REPLACE, 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
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001377 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378 "-%c requires a rule number",
1379 cmd2char(CMD_REPLACE));
1380 break;
1381
1382 case 'I':
1383 add_command(&command, CMD_INSERT, CMD_NONE,
1384 invert);
1385 chain = optarg;
1386 if (optind < argc && argv[optind][0] != '-'
1387 && argv[optind][0] != '!')
1388 rulenum = parse_rulenumber(argv[optind++]);
1389 else rulenum = 1;
1390 break;
1391
1392 case 'L':
1393 add_command(&command, CMD_LIST, CMD_ZERO,
1394 invert);
1395 if (optarg) chain = optarg;
1396 else if (optind < argc && argv[optind][0] != '-'
1397 && argv[optind][0] != '!')
1398 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001399 if (optind < argc && argv[optind][0] != '-'
1400 && argv[optind][0] != '!')
1401 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001402 break;
1403
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001404 case 'S':
1405 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1406 invert);
1407 if (optarg) chain = optarg;
1408 else if (optind < argc && argv[optind][0] != '-'
1409 && argv[optind][0] != '!')
1410 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001411 if (optind < argc && argv[optind][0] != '-'
1412 && argv[optind][0] != '!')
1413 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001414 break;
1415
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 case 'F':
1417 add_command(&command, CMD_FLUSH, CMD_NONE,
1418 invert);
1419 if (optarg) chain = optarg;
1420 else if (optind < argc && argv[optind][0] != '-'
1421 && argv[optind][0] != '!')
1422 chain = argv[optind++];
1423 break;
1424
1425 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001426 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 invert);
1428 if (optarg) chain = optarg;
1429 else if (optind < argc && argv[optind][0] != '-'
1430 && argv[optind][0] != '!')
1431 chain = argv[optind++];
1432 break;
1433
1434 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001435 if (optarg && (*optarg == '-' || *optarg == '!'))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001436 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001437 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001438 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001439 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001440 xtables_error(PARAMETER_PROBLEM,
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001441 "chain name may not clash "
1442 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1444 invert);
1445 chain = optarg;
1446 break;
1447
1448 case 'X':
1449 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1450 invert);
1451 if (optarg) chain = optarg;
1452 else if (optind < argc && argv[optind][0] != '-'
1453 && argv[optind][0] != '!')
1454 chain = argv[optind++];
1455 break;
1456
1457 case 'E':
1458 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1459 invert);
1460 chain = optarg;
1461 if (optind < argc && argv[optind][0] != '-'
1462 && argv[optind][0] != '!')
1463 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001464 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001465 xtables_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001466 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001467 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001468 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469 break;
1470
1471 case 'P':
1472 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1473 invert);
1474 chain = optarg;
1475 if (optind < argc && argv[optind][0] != '-'
1476 && argv[optind][0] != '!')
1477 policy = argv[optind++];
1478 else
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001479 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001480 "-%c requires a chain and a policy",
1481 cmd2char(CMD_SET_POLICY));
1482 break;
1483
1484 case 'h':
1485 if (!optarg)
1486 optarg = argv[optind];
1487
Jonas Berlin1b91e592005-04-01 06:58:38 +00001488 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001489 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001490 xtables_find_match(protocol, XTF_TRY_LOAD,
1491 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001492
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001493 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494
1495 /*
1496 * Option selection
1497 */
1498 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001499 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1501 invert);
1502
1503 /* Canonicalize into lower case */
1504 for (protocol = argv[optind-1]; *protocol; protocol++)
1505 *protocol = tolower(*protocol);
1506
1507 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001508 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 fw.ipv6.flags |= IP6T_F_PROTO;
1510
1511 if (fw.ipv6.proto == 0
1512 && (fw.ipv6.invflags & IP6T_INV_PROTO))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001513 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001515
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001516 if (is_exthdr(fw.ipv6.proto)
1517 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001518 fprintf(stderr,
1519 "Warning: never matched protocol: %s. "
1520 "use extension match instead.\n",
1521 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001522 break;
1523
1524 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001525 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001526 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1527 invert);
1528 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001529 break;
1530
1531 case 'd':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001532 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1534 invert);
1535 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001536 break;
1537
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001538#ifdef IP6T_F_GOTO
1539 case 'g':
1540 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1541 invert);
1542 fw.ipv6.flags |= IP6T_F_GOTO;
1543 jumpto = parse_target(optarg);
1544 break;
1545#endif
1546
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 case 'j':
1548 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1549 invert);
1550 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001551 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001552 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001553
1554 if (target) {
1555 size_t size;
1556
Harald Welte43ac1912002-03-03 09:43:07 +00001557 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1558 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001559
Jan Engelhardt630ef482009-01-27 14:58:41 +01001560 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561 target->t->u.target_size = size;
1562 strcpy(target->t->u.user.name, jumpto);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001563 xtables_set_revision(target->t->u.user.name,
Patrick McHardy455559b2008-04-15 15:51:19 +02001564 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001565 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001566 target->init(target->t);
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001567 opts = xtables_merge_options(opts,
Patrick McHardy455559b2008-04-15 15:51:19 +02001568 target->extra_opts,
1569 &target->option_offset);
1570 if (opts == NULL)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001571 xtables_error(OTHER_PROBLEM,
Patrick McHardy455559b2008-04-15 15:51:19 +02001572 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001573 }
1574 break;
1575
1576
1577 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001578 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001579 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1580 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001581 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001582 fw.ipv6.iniface,
1583 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001584 break;
1585
1586 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001587 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001588 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1589 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001590 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001591 fw.ipv6.outiface,
1592 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001593 break;
1594
1595 case 'v':
1596 if (!verbose)
1597 set_option(&options, OPT_VERBOSE,
1598 &fw.ipv6.invflags, invert);
1599 verbose++;
1600 break;
1601
1602 case 'm': {
1603 size_t size;
1604
1605 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001606 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001607 "unexpected ! flag before --match");
1608
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001609 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1610 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001611 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1612 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001613 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614 m->m->u.match_size = size;
1615 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001616 xtables_set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001617 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001618 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001619 if (m != m->next)
1620 /* Merge options for non-cloned matches */
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001621 opts = xtables_merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 }
1623 break;
1624
1625 case 'n':
1626 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1627 invert);
1628 break;
1629
1630 case 't':
1631 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001632 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001633 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001634 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635 break;
1636
1637 case 'x':
1638 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1639 invert);
1640 break;
1641
1642 case 'V':
1643 if (invert)
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001644 printf("Not %s ;-)\n", prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001645 else
1646 printf("%s v%s\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -05001647 prog_name, prog_vers);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648 exit(0);
1649
1650 case '0':
1651 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1652 invert);
1653 break;
1654
Harald Welte43ac1912002-03-03 09:43:07 +00001655 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001656 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001657 break;
1658
1659 case 'c':
1660
1661 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1662 invert);
1663 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001664 bcnt = strchr(pcnt + 1, ',');
1665 if (bcnt)
1666 bcnt++;
1667 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001668 && argv[optind][0] != '!')
1669 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001670 if (!bcnt)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001671 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001672 "-%c requires packet and byte counter",
1673 opt2char(OPT_COUNTERS));
1674
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001675 if (sscanf(pcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001676 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001677 "-%c packet counter not numeric",
1678 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001679 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001680
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001681 if (sscanf(bcnt, "%llu", &cnt) != 1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001682 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001683 "-%c byte counter not numeric",
1684 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001685 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001686 break;
1687
Rusty Russell5eed48a2000-06-02 20:12:24 +00001688 case 1: /* non option */
1689 if (optarg[0] == '!' && optarg[1] == '\0') {
1690 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001691 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001692 "multiple consecutive ! not"
1693 " allowed");
1694 invert = TRUE;
1695 optarg[0] = '\0';
1696 continue;
1697 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001698 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699 exit_tryhelp(2);
1700
1701 default:
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001702 if (target == NULL || target->parse == NULL ||
1703 !target->parse(c - target->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704 argv, invert,
1705 &target->tflags,
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001706 &fw, &target->t)) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001707 for (matchp = matches; matchp; matchp = matchp->next) {
Jan Engelhardt92edcb02009-06-12 20:35:42 +02001708 if (matchp->completed ||
1709 matchp->match->parse == NULL)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001710 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001711 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001713 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001714 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001715 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001716 break;
1717 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001718 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001719
1720 /* If you listen carefully, you can
1721 actually hear this code suck. */
1722
1723 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001724 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001725 * parameter, that has not been parsed yet,
1726 * it's not an option of an explicitly loaded
1727 * match or a target. However, we support
1728 * implicit loading of the protocol match
1729 * extension. '-p tcp' means 'l4 proto 6' and
1730 * at the same time 'load tcp protocol match on
1731 * demand if we specify --dport'.
1732 *
1733 * To make this work, we need to make sure:
1734 * - the parameter has not been parsed by
1735 * a match (m above)
1736 * - a protocol has been specified
1737 * - the protocol extension has not been
1738 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001739 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001740 * - the protocol extension can be successively
1741 * loaded
1742 */
1743 if (m == NULL
1744 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001745 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001746 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001747 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001748 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001749 && (proto_used == 0))
1750 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001751 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001752 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001753 /* Try loading protocol */
1754 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001755
Harald Welte00f5a9c2002-08-26 14:37:35 +00001756 proto_used = 1;
1757
1758 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1759 + m->size;
1760
Jan Engelhardt630ef482009-01-27 14:58:41 +01001761 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001762 m->m->u.match_size = size;
1763 strcpy(m->m->u.user.name, m->name);
Jamal Hadi Salim85332212009-02-12 09:33:59 -05001764 xtables_set_revision(m->m->u.user.name,
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001765 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001766 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001767 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001768
Jamal Hadi Salim70581922009-02-13 08:36:44 -05001769 opts = xtables_merge_options(opts,
Harald Welte00f5a9c2002-08-26 14:37:35 +00001770 m->extra_opts, &m->option_offset);
1771
1772 optind--;
1773 continue;
1774 }
1775
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001776 if (!m) {
1777 if (c == '?') {
1778 if (optopt) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001779 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001780 PARAMETER_PROBLEM,
1781 "option `%s' "
1782 "requires an "
1783 "argument",
1784 argv[optind-1]);
1785 } else {
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001786 xtables_error(
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001787 PARAMETER_PROBLEM,
1788 "unknown option "
1789 "`%s'",
1790 argv[optind-1]);
1791 }
1792 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001793 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001794 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001795 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001796 }
1797 }
1798 invert = FALSE;
1799 }
1800
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001801 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001802 if (matchp->match->final_check != NULL)
1803 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001804
Jan Engelhardt830132a2007-10-04 16:24:50 +00001805 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001806 target->final_check(target->tflags);
1807
1808 /* Fix me: must put inverse options checking here --MN */
1809
1810 if (optind < argc)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001811 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001812 "unknown arguments found on commandline");
1813 if (!command)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001814 xtables_error(PARAMETER_PROBLEM, "no command specified");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815 if (invert)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001816 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001817 "nothing appropriate following !");
1818
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001819 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001820 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001821 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001822 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001823 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001824 }
1825
1826 if (shostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001827 xtables_ip6parse_multiple(shostnetworkmask, &saddrs,
1828 &smasks, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001829
1830 if (dhostnetworkmask)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001831 xtables_ip6parse_multiple(dhostnetworkmask, &daddrs,
1832 &dmasks, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833
1834 if ((nsaddrs > 1 || ndaddrs > 1) &&
1835 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001836 xtables_error(PARAMETER_PROBLEM, "! not allowed with multiple"
Rusty Russell5eed48a2000-06-02 20:12:24 +00001837 " source or destination IP addresses");
1838
Rusty Russell5eed48a2000-06-02 20:12:24 +00001839 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001840 xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
Rusty Russell5eed48a2000-06-02 20:12:24 +00001841 "specify a unique address");
1842
1843 generic_opt_check(command, options);
1844
1845 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001846 xtables_error(PARAMETER_PROBLEM,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001847 "chain name `%s' too long (must be under %i chars)",
1848 chain, IP6T_FUNCTION_MAXNAMELEN);
1849
Harald Welte43ac1912002-03-03 09:43:07 +00001850 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001851 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001852 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001853
Rusty Russell8beb0492004-12-22 00:37:10 +00001854 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001855 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001856 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001857
Harald Welte43ac1912002-03-03 09:43:07 +00001858 if (!*handle)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001859 xtables_error(VERSION_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001860 "can't initialize ip6tables table `%s': %s",
1861 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001863 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001864 || command == CMD_DELETE
1865 || command == CMD_INSERT
1866 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001867 if (strcmp(chain, "PREROUTING") == 0
1868 || strcmp(chain, "INPUT") == 0) {
1869 /* -o not valid with incoming packets. */
1870 if (options & OPT_VIANAMEOUT)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001871 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001872 "Can't use -%c with %s\n",
1873 opt2char(OPT_VIANAMEOUT),
1874 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001875 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001876
Harald Welte43ac1912002-03-03 09:43:07 +00001877 if (strcmp(chain, "POSTROUTING") == 0
1878 || strcmp(chain, "OUTPUT") == 0) {
1879 /* -i not valid with outgoing packets */
1880 if (options & OPT_VIANAMEIN)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001881 xtables_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001882 "Can't use -%c with %s\n",
1883 opt2char(OPT_VIANAMEIN),
1884 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001885 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886
1887 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001888 fprintf(stderr,
1889 "Warning: using chain %s, not extension\n",
1890 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001892 if (target->t)
1893 free(target->t);
1894
Rusty Russell5eed48a2000-06-02 20:12:24 +00001895 target = NULL;
1896 }
1897
1898 /* If they didn't specify a target, or it's a chain
1899 name, use standard. */
1900 if (!target
1901 && (strlen(jumpto) == 0
1902 || ip6tc_is_chain(jumpto, *handle))) {
1903 size_t size;
1904
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001905 target = xtables_find_target(IP6T_STANDARD_TARGET,
1906 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001907
1908 size = sizeof(struct ip6t_entry_target)
1909 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001910 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001911 target->t->u.target_size = size;
1912 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001913 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001914 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001915 }
1916
1917 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001918 /* it is no chain, and we can't load a plugin.
1919 * We cannot know if the plugin is corrupt, non
1920 * existant OR if the user just misspelled a
1921 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001922#ifdef IP6T_F_GOTO
1923 if (fw.ipv6.flags & IP6T_F_GOTO)
Jan Engelhardt1829ed42009-02-21 03:29:44 +01001924 xtables_error(PARAMETER_PROBLEM,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001925 "goto '%s' is not a chain\n", jumpto);
1926#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001927 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001929 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001930 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 }
1932 }
1933
1934 switch (command) {
1935 case CMD_APPEND:
1936 ret = append_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001937 nsaddrs, saddrs, smasks,
1938 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001939 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001940 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 case CMD_DELETE:
1943 ret = delete_entry(chain, e,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001944 nsaddrs, saddrs, smasks,
1945 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001946 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001947 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001948 break;
1949 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001950 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 break;
1952 case CMD_REPLACE:
1953 ret = replace_entry(chain, e, rulenum - 1,
1954 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001955 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001956 break;
1957 case CMD_INSERT:
1958 ret = insert_entry(chain, e, rulenum - 1,
Michael Granzow332e4ac2009-04-09 18:24:36 +01001959 nsaddrs, saddrs, smasks,
1960 ndaddrs, daddrs, dmasks,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001961 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001962 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001964 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001965 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001966 break;
1967 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001968 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001969 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001970 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001971 case CMD_LIST|CMD_ZERO:
1972 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001973 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001974 options&OPT_VERBOSE,
1975 options&OPT_NUMERIC,
1976 options&OPT_EXPANDED,
1977 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001978 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001979 if (ret && (command & CMD_ZERO))
1980 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001981 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001982 break;
1983 case CMD_LIST_RULES:
1984 case CMD_LIST_RULES|CMD_ZERO:
1985 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02001986 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001987 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001988 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001989 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001991 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001992 break;
1993 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001994 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001995 break;
1996 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001997 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001998 break;
1999 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002000 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 break;
2002 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002003 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002004 break;
2005 default:
2006 /* We should never reach this... */
2007 exit_tryhelp(2);
2008 }
2009
2010 if (verbose > 1)
2011 dump_entries6(*handle);
2012
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002013 clear_rule_matches(&matches);
2014
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002015 if (e != NULL) {
2016 free(e);
2017 e = NULL;
2018 }
2019
Michael Granzow332e4ac2009-04-09 18:24:36 +01002020 free(saddrs);
2021 free(smasks);
2022 free(daddrs);
2023 free(dmasks);
Jamal Hadi Salim139b3fe2009-02-12 11:43:01 -05002024 xtables_free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002025
Rusty Russell5eed48a2000-06-02 20:12:24 +00002026 return ret;
2027}