blob: 53163b7b954d1dbbf625b3d54f5a62dee86f8398 [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;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200161 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200173/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IP6T_INV_SRCIP,
181/* -d */ IP6T_INV_DSTIP,
182/* -p */ IP6T_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IP6T_INV_VIA_IN,
187/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190};
191
192const char *program_version;
193const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194
Rusty Russell5eed48a2000-06-02 20:12:24 +0000195/* A few hardcoded protocols for 'all' and in case the user has no
196 /etc/protocols */
197struct pprot {
198 char *name;
199 u_int8_t num;
200};
201
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000202struct afinfo afinfo = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100203 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000204 .libprefix = "libip6t_",
205 .ipproto = IPPROTO_IPV6,
206 .kmod = "ip6_tables",
207 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
208 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
209};
210
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100211static const char *
Rusty Russell5eed48a2000-06-02 20:12:24 +0000212proto_to_name(u_int8_t proto, int nolookup)
213{
214 unsigned int i;
215
216 if (proto && !nolookup) {
217 struct protoent *pent = getprotobynumber(proto);
218 if (pent)
219 return pent->p_name;
220 }
221
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100222 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
223 if (xtables_chain_protos[i].num == proto)
224 return xtables_chain_protos[i].name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000225
226 return NULL;
227}
228
Pablo Neiradfdcd642005-05-29 19:05:23 +0000229static void free_opts(int reset_offset)
230{
231 if (opts != original_opts) {
232 free(opts);
233 opts = original_opts;
234 if (reset_offset)
235 global_option_offset = 0;
236 }
237}
238
Patrick McHardy0b639362007-09-08 16:00:01 +0000239static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240exit_tryhelp(int status)
241{
Harald Weltea8658ca2003-03-05 07:46:15 +0000242 if (line != -1)
243 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000244 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
245 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000246 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000247 exit(status);
248}
249
Patrick McHardy0b639362007-09-08 16:00:01 +0000250static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000251exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000252{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000253 struct ip6tables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200254 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000255
256 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000257"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000258" %s -[RI] chain rulenum rule-specification [options]\n"
259" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200260" %s -[LS] [chain [rulenum]] [options]\n"
261" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000262" %s -[NX] chain\n"
263" %s -E old-chain-name new-chain-name\n"
264" %s -P chain target [options]\n"
265" %s -h (print this help information)\n\n",
266 program_name, program_version, program_name, program_name,
267 program_name, program_name, program_name, program_name,
Henrik Nordstrombb340822008-05-13 13:09:23 +0200268 program_name, program_name, program_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000269
270 printf(
271"Commands:\n"
272"Either long or short options are allowed.\n"
273" --append -A chain Append to chain\n"
274" --delete -D chain Delete matching rule from chain\n"
275" --delete -D chain rulenum\n"
276" Delete rule rulenum (1 = first) from chain\n"
277" --insert -I chain [rulenum]\n"
278" Insert in chain as rulenum (default 1=first)\n"
279" --replace -R chain rulenum\n"
280" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200281" --list -L [chain [rulenum]]\n"
282" List the rules in a chain or all chains\n"
283" --list-rules -S [chain [rulenum]]\n"
284" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" --flush -F [chain] Delete all rules in chain or all chains\n"
286" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287" --new -N chain Create a new user-defined chain\n"
288" --delete-chain\n"
289" -X [chain] Delete a user-defined chain\n"
290" --policy -P chain target\n"
291" Change policy on chain to target\n"
292" --rename-chain\n"
293" -E old-chain new-chain\n"
294" Change chain name, (moving any references)\n"
295
296"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200297"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
298"[!] --source -s address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000299" source specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200300"[!] --destination -d address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000301" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200302"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000303" network interface name ([+] for wildcard)\n"
304" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000305" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200306#ifdef IP6T_F_GOTO
307" --goto -g chain\n"
308" jump to chain with no return\n"
309#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000310" --match -m match\n"
311" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000312" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200313"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314" network interface name ([+] for wildcard)\n"
315" --table -t table table to manipulate (default: `filter')\n"
316" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000317" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000318" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000319/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000320" --modprobe=<command> try to insert modules using this command\n"
321" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322"[!] --version -V print package version.\n");
323
Max Kellermann5b76f682008-01-29 13:42:48 +0000324 /* Print out any special helps. A user might like to be able to add a --help
325 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000326 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000327 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000328 if (t->used) {
329 printf("\n");
330 t->help();
331 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000333 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000335 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000336 }
337 exit(0);
338}
339
Patrick McHardy0b639362007-09-08 16:00:01 +0000340void
Jan Engelhardta41545c2009-01-27 21:27:19 +0100341exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000342{
343 va_list args;
344
345 va_start(args, msg);
346 fprintf(stderr, "%s v%s: ", program_name, program_version);
347 vfprintf(stderr, msg, args);
348 va_end(args);
349 fprintf(stderr, "\n");
350 if (status == PARAMETER_PROBLEM)
351 exit_tryhelp(status);
352 if (status == VERSION_PROBLEM)
353 fprintf(stderr,
354 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
355 /* On error paths, make sure that we don't leak memory */
356 free_opts(1);
357 exit(status);
358}
359
Rusty Russell5eed48a2000-06-02 20:12:24 +0000360static void
361generic_opt_check(int command, int options)
362{
363 int i, j, legal = 0;
364
365 /* Check that commands are valid with options. Complicated by the
366 * fact that if an option is legal with *any* command given, it is
367 * legal overall (ie. -z and -l).
368 */
369 for (i = 0; i < NUMBER_OF_OPT; i++) {
370 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
371
372 for (j = 0; j < NUMBER_OF_CMD; j++) {
373 if (!(command & (1<<j)))
374 continue;
375
376 if (!(options & (1<<i))) {
377 if (commands_v_options[j][i] == '+')
378 exit_error(PARAMETER_PROBLEM,
379 "You need to supply the `-%c' "
380 "option for this command\n",
381 optflags[i]);
382 } else {
383 if (commands_v_options[j][i] != 'x')
384 legal = 1;
385 else if (legal == 0)
386 legal = -1;
387 }
388 }
389 if (legal == -1)
390 exit_error(PARAMETER_PROBLEM,
391 "Illegal option `-%c' with this command\n",
392 optflags[i]);
393 }
394}
395
396static char
397opt2char(int option)
398{
399 const char *ptr;
400 for (ptr = optflags; option > 1; option >>= 1, ptr++);
401
402 return *ptr;
403}
404
405static char
406cmd2char(int option)
407{
408 const char *ptr;
409 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
410
411 return *ptr;
412}
413
414static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000415add_command(unsigned int *cmd, const int newcmd, const int othercmds,
416 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000417{
418 if (invert)
419 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
420 if (*cmd & (~othercmds))
421 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
422 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
423 *cmd |= newcmd;
424}
425
Rusty Russell5eed48a2000-06-02 20:12:24 +0000426/*
427 * All functions starting with "parse" should succeed, otherwise
428 * the program fails.
429 * Most routines return pointers to static data that may change
430 * between calls to the same or other routines with a few exceptions:
431 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
432 * return global static data.
433*/
434
Rusty Russell5eed48a2000-06-02 20:12:24 +0000435/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200436static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100437find_proto(const char *pname, enum xtables_tryload tryload,
438 int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000439{
Harald Welteed498492001-07-23 01:24:22 +0000440 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100442 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100443 const char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000444
Harald Welte43ac1912002-03-03 09:43:07 +0000445 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100446 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000447 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100448 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000449
450 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000451}
452
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000453/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000454static int is_exthdr(u_int16_t proto)
455{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000456 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000457 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000458 proto == IPPROTO_AH ||
459 proto == IPPROTO_DSTOPTS);
460}
461
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462/* Can't be zero. */
463static int
464parse_rulenumber(const char *rule)
465{
Harald Welte43ac1912002-03-03 09:43:07 +0000466 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100468 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469 exit_error(PARAMETER_PROBLEM,
470 "Invalid rule number `%s'", rule);
471
472 return rulenum;
473}
474
475static const char *
476parse_target(const char *targetname)
477{
478 const char *ptr;
479
480 if (strlen(targetname) < 1)
481 exit_error(PARAMETER_PROBLEM,
482 "Invalid target name (too short)");
483
484 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
485 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000486 "Invalid target name `%s' (%u chars max)",
487 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488
489 for (ptr = targetname; *ptr; ptr++)
490 if (isspace(*ptr))
491 exit_error(PARAMETER_PROBLEM,
492 "Invalid target name `%s'", targetname);
493 return targetname;
494}
495
Rusty Russell5eed48a2000-06-02 20:12:24 +0000496static void
497set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
498 int invert)
499{
500 if (*options & option)
501 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
502 opt2char(option));
503 *options |= option;
504
505 if (invert) {
506 unsigned int i;
507 for (i = 0; 1 << i != option; i++);
508
509 if (!inverse_for_options[i])
510 exit_error(PARAMETER_PROBLEM,
511 "cannot have ! before -%c",
512 opt2char(option));
513 *invflg |= inverse_for_options[i];
514 }
515}
516
Rusty Russell5eed48a2000-06-02 20:12:24 +0000517static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000518merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519 unsigned int *option_offset)
520{
521 unsigned int num_old, num_new, i;
522 struct option *merge;
523
Jan Engelhardtd0145402007-07-30 14:32:26 +0000524 if (newopts == NULL)
525 return oldopts;
526
Rusty Russell5eed48a2000-06-02 20:12:24 +0000527 for (num_old = 0; oldopts[num_old].name; num_old++);
528 for (num_new = 0; newopts[num_new].name; num_new++);
529
530 global_option_offset += OPTION_OFFSET;
531 *option_offset = global_option_offset;
532
533 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
534 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000535 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000536 for (i = 0; i < num_new; i++) {
537 merge[num_old + i] = newopts[i];
538 merge[num_old + i].val += *option_offset;
539 }
540 memset(merge + num_old + num_new, 0, sizeof(struct option));
541
542 return merge;
543}
544
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000545static void
546print_num(u_int64_t number, unsigned int format)
547{
Harald Welte43ac1912002-03-03 09:43:07 +0000548 if (format & FMT_KILOMEGAGIGA) {
549 if (number > 99999) {
550 number = (number + 500) / 1000;
551 if (number > 9999) {
552 number = (number + 500) / 1000;
553 if (number > 9999) {
554 number = (number + 500) / 1000;
555 if (number > 9999) {
556 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000557 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000558 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000559 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000560 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000561 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000562 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000563 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000564 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000565 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000566 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000567 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568}
569
Harald Welte43ac1912002-03-03 09:43:07 +0000570
Rusty Russell5eed48a2000-06-02 20:12:24 +0000571static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100572print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000573{
574 struct ip6t_counters counters;
575 const char *pol = ip6tc_get_policy(chain, &counters, handle);
576 printf("Chain %s", chain);
577 if (pol) {
578 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000579 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000580 fputc(' ', stdout);
581 print_num(counters.pcnt, (format|FMT_NOTABLE));
582 fputs("packets, ", stdout);
583 print_num(counters.bcnt, (format|FMT_NOTABLE));
584 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000585 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000586 printf(")\n");
587 } else {
588 unsigned int refs;
589 if (!ip6tc_get_references(&refs, chain, handle))
590 printf(" (ERROR obtaining refs)\n");
591 else
592 printf(" (%u references)\n", refs);
593 }
594
595 if (format & FMT_LINENUMBERS)
596 printf(FMT("%-4s ", "%s "), "num");
597 if (!(format & FMT_NOCOUNTS)) {
598 if (format & FMT_KILOMEGAGIGA) {
599 printf(FMT("%5s ","%s "), "pkts");
600 printf(FMT("%5s ","%s "), "bytes");
601 } else {
602 printf(FMT("%8s ","%s "), "pkts");
603 printf(FMT("%10s ","%s "), "bytes");
604 }
605 }
606 if (!(format & FMT_NOTARGET))
607 printf(FMT("%-9s ","%s "), "target");
608 fputs(" prot ", stdout);
609 if (format & FMT_OPTIONS)
610 fputs("opt", stdout);
611 if (format & FMT_VIA) {
612 printf(FMT(" %-6s ","%s "), "in");
613 printf(FMT("%-6s ","%s "), "out");
614 }
615 printf(FMT(" %-19s ","%s "), "source");
616 printf(FMT(" %-19s "," %s "), "destination");
617 printf("\n");
618}
619
Harald Welte43ac1912002-03-03 09:43:07 +0000620
Rusty Russell5eed48a2000-06-02 20:12:24 +0000621static int
622print_match(const struct ip6t_entry_match *m,
623 const struct ip6t_ip6 *ip,
624 int numeric)
625{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100626 struct xtables_match *match =
627 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000628
629 if (match) {
630 if (match->print)
631 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000632 else
633 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000634 } else {
635 if (m->u.user.name[0])
636 printf("UNKNOWN match `%s' ", m->u.user.name);
637 }
638 /* Don't stop iterating. */
639 return 0;
640}
641
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100642/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000643static void
644print_firewall(const struct ip6t_entry *fw,
645 const char *targname,
646 unsigned int num,
647 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100648 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000649{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200650 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000651 const struct ip6t_entry_target *t;
652 u_int8_t flags;
653 char buf[BUFSIZ];
654
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100656 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000657 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100658 target = xtables_find_target(IP6T_STANDARD_TARGET,
659 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660
661 t = ip6t_get_target((struct ip6t_entry *)fw);
662 flags = fw->ipv6.flags;
663
664 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200665 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000666
667 if (!(format & FMT_NOCOUNTS)) {
668 print_num(fw->counters.pcnt, format);
669 print_num(fw->counters.bcnt, format);
670 }
671
672 if (!(format & FMT_NOTARGET))
673 printf(FMT("%-9s ", "%s "), targname);
674
675 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
676 {
Jan Engelhardt1de7edf2009-01-30 05:38:11 +0100677 const char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000678 if (pname)
679 printf(FMT("%-5s", "%s "), pname);
680 else
681 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
682 }
683
684 if (format & FMT_OPTIONS) {
685 if (format & FMT_NOTABLE)
686 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000687 fputc(' ', stdout); /* Invert flag of FRAG */
688 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 fputc(' ', stdout);
690 }
691
692 if (format & FMT_VIA) {
693 char iface[IFNAMSIZ+2];
694
695 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
696 iface[0] = '!';
697 iface[1] = '\0';
698 }
699 else iface[0] = '\0';
700
701 if (fw->ipv6.iniface[0] != '\0') {
702 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 }
704 else if (format & FMT_NUMERIC) strcat(iface, "*");
705 else strcat(iface, "any");
706 printf(FMT(" %-6s ","in %s "), iface);
707
708 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
709 iface[0] = '!';
710 iface[1] = '\0';
711 }
712 else iface[0] = '\0';
713
714 if (fw->ipv6.outiface[0] != '\0') {
715 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716 }
717 else if (format & FMT_NUMERIC) strcat(iface, "*");
718 else strcat(iface, "any");
719 printf(FMT("%-6s ","out %s "), iface);
720 }
721
722 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000723 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724 && !(format & FMT_NUMERIC))
725 printf(FMT("%-19s ","%s "), "anywhere");
726 else {
727 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100728 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000729 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100730 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
731 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000732 printf(FMT("%-19s ","%s "), buf);
733 }
734
735 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
736 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
737 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200738 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739 else {
740 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100741 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000742 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100743 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
744 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200745 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746 }
747
748 if (format & FMT_NOTABLE)
749 fputs(" ", stdout);
750
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200751#ifdef IP6T_F_GOTO
752 if(fw->ipv6.flags & IP6T_F_GOTO)
753 printf("[goto] ");
754#endif
755
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
757
758 if (target) {
759 if (target->print)
760 /* Print the target information. */
761 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
762 } else if (t->u.target_size != sizeof(*t))
763 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000764 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765
766 if (!(format & FMT_NONEWLINE))
767 fputc('\n', stdout);
768}
769
770static void
771print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100772 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000773{
774 struct ip6t_entry_target *t;
775
776 t = ip6t_get_target((struct ip6t_entry *)fw);
777 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
778}
779
780static int
781append_entry(const ip6t_chainlabel chain,
782 struct ip6t_entry *fw,
783 unsigned int nsaddrs,
784 const struct in6_addr saddrs[],
785 unsigned int ndaddrs,
786 const struct in6_addr daddrs[],
787 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100788 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789{
790 unsigned int i, j;
791 int ret = 1;
792
793 for (i = 0; i < nsaddrs; i++) {
794 fw->ipv6.src = saddrs[i];
795 for (j = 0; j < ndaddrs; j++) {
796 fw->ipv6.dst = daddrs[j];
797 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100798 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000799 ret &= ip6tc_append_entry(chain, fw, handle);
800 }
801 }
802
803 return ret;
804}
805
806static int
807replace_entry(const ip6t_chainlabel chain,
808 struct ip6t_entry *fw,
809 unsigned int rulenum,
810 const struct in6_addr *saddr,
811 const struct in6_addr *daddr,
812 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100813 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814{
815 fw->ipv6.src = *saddr;
816 fw->ipv6.dst = *daddr;
817
818 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100819 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 return ip6tc_replace_entry(chain, fw, rulenum, handle);
821}
822
823static int
824insert_entry(const ip6t_chainlabel chain,
825 struct ip6t_entry *fw,
826 unsigned int rulenum,
827 unsigned int nsaddrs,
828 const struct in6_addr saddrs[],
829 unsigned int ndaddrs,
830 const struct in6_addr daddrs[],
831 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100832 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000833{
834 unsigned int i, j;
835 int ret = 1;
836
837 for (i = 0; i < nsaddrs; i++) {
838 fw->ipv6.src = saddrs[i];
839 for (j = 0; j < ndaddrs; j++) {
840 fw->ipv6.dst = daddrs[j];
841 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100842 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000843 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
844 }
845 }
846
847 return ret;
848}
849
850static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000851make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000852{
853 /* Establish mask for comparison */
854 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000855 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856 unsigned char *mask, *mptr;
857
858 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000859 for (matchp = matches; matchp; matchp = matchp->next)
860 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000861
Jan Engelhardt630ef482009-01-27 14:58:41 +0100862 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000863 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000864 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000865
866 memset(mask, 0xFF, sizeof(struct ip6t_entry));
867 mptr = mask + sizeof(struct ip6t_entry);
868
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000869 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000870 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000871 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000872 + matchp->match->userspacesize);
873 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000874 }
875
Max Kellermann5b76f682008-01-29 13:42:48 +0000876 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000877 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000878 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000879
880 return mask;
881}
882
883static int
884delete_entry(const ip6t_chainlabel chain,
885 struct ip6t_entry *fw,
886 unsigned int nsaddrs,
887 const struct in6_addr saddrs[],
888 unsigned int ndaddrs,
889 const struct in6_addr daddrs[],
890 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100891 struct ip6tc_handle *handle,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000892 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000893{
894 unsigned int i, j;
895 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896 unsigned char *mask;
897
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000898 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000899 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000900 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000901 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000902 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100904 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000905 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906 }
907 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000908 free(mask);
909
Rusty Russell5eed48a2000-06-02 20:12:24 +0000910 return ret;
911}
912
András Kis-Szabó764316a2001-02-26 17:31:20 +0000913int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100914for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
915 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916{
Max Kellermann5b76f682008-01-29 13:42:48 +0000917 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000918 const char *chain;
919 char *chains;
920 unsigned int i, chaincount = 0;
921
922 chain = ip6tc_first_chain(handle);
923 while (chain) {
924 chaincount++;
925 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000926 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000927
Jan Engelhardt630ef482009-01-27 14:58:41 +0100928 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000929 i = 0;
930 chain = ip6tc_first_chain(handle);
931 while (chain) {
932 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
933 i++;
934 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000935 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936
937 for (i = 0; i < chaincount; i++) {
938 if (!builtinstoo
939 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100940 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +0000942 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000943 }
944
945 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +0000946 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947}
948
András Kis-Szabó764316a2001-02-26 17:31:20 +0000949int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100951 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000952{
953 if (!chain)
954 return for_each_chain(flush_entries, verbose, 1, handle);
955
956 if (verbose)
957 fprintf(stdout, "Flushing chain `%s'\n", chain);
958 return ip6tc_flush_entries(chain, handle);
959}
960
961static int
962zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100963 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964{
965 if (!chain)
966 return for_each_chain(zero_entries, verbose, 1, handle);
967
968 if (verbose)
969 fprintf(stdout, "Zeroing chain `%s'\n", chain);
970 return ip6tc_zero_entries(chain, handle);
971}
972
András Kis-Szabó764316a2001-02-26 17:31:20 +0000973int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000974delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100975 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976{
977 if (!chain)
978 return for_each_chain(delete_chain, verbose, 0, handle);
979
980 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +0000981 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000982 return ip6tc_delete_chain(chain, handle);
983}
984
985static int
Henrik Nordstrombb340822008-05-13 13:09:23 +0200986list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100987 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000988{
989 int found = 0;
990 unsigned int format;
991 const char *this;
992
993 format = FMT_OPTIONS;
994 if (!verbose)
995 format |= FMT_NOCOUNTS;
996 else
997 format |= FMT_VIA;
998
999 if (numeric)
1000 format |= FMT_NUMERIC;
1001
1002 if (!expanded)
1003 format |= FMT_KILOMEGAGIGA;
1004
1005 if (linenumbers)
1006 format |= FMT_LINENUMBERS;
1007
1008 for (this = ip6tc_first_chain(handle);
1009 this;
1010 this = ip6tc_next_chain(handle)) {
1011 const struct ip6t_entry *i;
1012 unsigned int num;
1013
1014 if (chain && strcmp(chain, this) != 0)
1015 continue;
1016
1017 if (found) printf("\n");
1018
Henrik Nordstrombb340822008-05-13 13:09:23 +02001019 if (!rulenum)
1020 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001021 i = ip6tc_first_rule(this, handle);
1022
1023 num = 0;
1024 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001025 num++;
1026 if (!rulenum || num == rulenum)
1027 print_firewall(i,
1028 ip6tc_get_target(i, handle),
1029 num,
1030 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001031 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001032 i = ip6tc_next_rule(i, handle);
1033 }
1034 found = 1;
1035 }
1036
1037 errno = ENOENT;
1038 return found;
1039}
1040
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001041/* This assumes that mask is contiguous, and byte-bounded. */
1042static void
1043print_iface(char letter, const char *iface, const unsigned char *mask,
1044 int invert)
1045{
1046 unsigned int i;
1047
1048 if (mask[0] == 0)
1049 return;
1050
1051 printf("-%c %s", letter, invert ? "! " : "");
1052
1053 for (i = 0; i < IFNAMSIZ; i++) {
1054 if (mask[i] != 0) {
1055 if (iface[i] != '\0')
1056 printf("%c", iface[i]);
1057 } else {
1058 /* we can access iface[i-1] here, because
1059 * a few lines above we make sure that mask[0] != 0 */
1060 if (iface[i-1] != '\0')
1061 printf("+");
1062 break;
1063 }
1064 }
1065
1066 printf(" ");
1067}
1068
1069/* The ip6tables looks up the /etc/protocols. */
1070static void print_proto(u_int16_t proto, int invert)
1071{
1072 if (proto) {
1073 unsigned int i;
1074 const char *invertstr = invert ? "! " : "";
1075
1076 struct protoent *pent = getprotobynumber(proto);
1077 if (pent) {
1078 printf("-p %s%s ",
1079 invertstr, pent->p_name);
1080 return;
1081 }
1082
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001083 for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
1084 if (xtables_chain_protos[i].num == proto) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001085 printf("-p %s%s ",
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001086 invertstr, xtables_chain_protos[i].name);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001087 return;
1088 }
1089
1090 printf("-p %s%u ", invertstr, proto);
1091 }
1092}
1093
1094static int print_match_save(const struct ip6t_entry_match *e,
1095 const struct ip6t_ip6 *ip)
1096{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001097 struct xtables_match *match =
1098 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001099
1100 if (match) {
1101 printf("-m %s ", e->u.user.name);
1102
1103 /* some matches don't provide a save function */
1104 if (match->save)
1105 match->save(ip, e);
1106 } else {
1107 if (e->u.match_size) {
1108 fprintf(stderr,
1109 "Can't find library for match `%s'\n",
1110 e->u.user.name);
1111 exit(1);
1112 }
1113 }
1114 return 0;
1115}
1116
1117/* print a given ip including mask if neccessary */
1118static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1119{
1120 char buf[51];
1121 int l = ipv6_prefix_length(mask);
1122
1123 if (l == 0 && !invert)
1124 return;
1125
1126 printf("%s %s%s",
1127 prefix,
1128 invert ? "! " : "",
1129 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1130
1131 if (l == -1)
1132 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1133 else
1134 printf("/%d ", l);
1135}
1136
1137/* We want this to be readable, so only print out neccessary fields.
1138 * Because that's the kind of world I want to live in. */
1139void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001140 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001141{
1142 struct ip6t_entry_target *t;
1143 const char *target_name;
1144
1145 /* print counters for iptables-save */
1146 if (counters > 0)
1147 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1148
1149 /* print chain name */
1150 printf("-A %s ", chain);
1151
1152 /* Print IP part. */
1153 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1154 e->ipv6.invflags & IP6T_INV_SRCIP);
1155
1156 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1157 e->ipv6.invflags & IP6T_INV_DSTIP);
1158
1159 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1160 e->ipv6.invflags & IP6T_INV_VIA_IN);
1161
1162 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1163 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1164
1165 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1166
1167#if 0
1168 /* not definied in ipv6
1169 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1170 if (e->ipv6.flags & IPT_F_FRAG)
1171 printf("%s-f ",
1172 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1173#endif
1174
1175 if (e->ipv6.flags & IP6T_F_TOS)
1176 printf("%s-? %d ",
1177 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1178 e->ipv6.tos);
1179
1180 /* Print matchinfo part */
1181 if (e->target_offset) {
1182 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1183 }
1184
1185 /* print counters for iptables -R */
1186 if (counters < 0)
1187 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1188
1189 /* Print target name */
1190 target_name = ip6tc_get_target(e, h);
1191 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001192#ifdef IP6T_F_GOTO
1193 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1194#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001195 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001196#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001197
1198 /* Print targinfo part */
1199 t = ip6t_get_target((struct ip6t_entry *)e);
1200 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001201 struct xtables_target *target =
1202 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001203
1204 if (!target) {
1205 fprintf(stderr, "Can't find library for target `%s'\n",
1206 t->u.user.name);
1207 exit(1);
1208 }
1209
1210 if (target->save)
1211 target->save(&e->ipv6, t);
1212 else {
1213 /* If the target size is greater than ip6t_entry_target
1214 * there is something to be saved, we just don't know
1215 * how to print it */
1216 if (t->u.target_size !=
1217 sizeof(struct ip6t_entry_target)) {
1218 fprintf(stderr, "Target `%s' is missing "
1219 "save function\n",
1220 t->u.user.name);
1221 exit(1);
1222 }
1223 }
1224 }
1225 printf("\n");
1226}
1227
1228static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001229list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001230 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001231{
1232 const char *this = NULL;
1233 int found = 0;
1234
1235 if (counters)
1236 counters = -1; /* iptables -c format */
1237
1238 /* Dump out chain names first,
1239 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001240 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001241 this;
1242 this = ip6tc_next_chain(handle)) {
1243 if (chain && strcmp(this, chain) != 0)
1244 continue;
1245
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001246 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001247 struct ip6t_counters count;
1248 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1249 if (counters)
1250 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1251 printf("\n");
1252 } else {
1253 printf("-N %s\n", this);
1254 }
1255 }
1256
1257 for (this = ip6tc_first_chain(handle);
1258 this;
1259 this = ip6tc_next_chain(handle)) {
1260 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001261 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001262
1263 if (chain && strcmp(this, chain) != 0)
1264 continue;
1265
1266 /* Dump out rules */
1267 e = ip6tc_first_rule(this, handle);
1268 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001269 num++;
1270 if (!rulenum || num == rulenum)
1271 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001272 e = ip6tc_next_rule(e, handle);
1273 }
1274 found = 1;
1275 }
1276
1277 errno = ENOENT;
1278 return found;
1279}
1280
Rusty Russell5eed48a2000-06-02 20:12:24 +00001281static struct ip6t_entry *
1282generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001283 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001284 struct ip6t_entry_target *target)
1285{
1286 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001287 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001288 struct ip6t_entry *e;
1289
1290 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001291 for (matchp = matches; matchp; matchp = matchp->next)
1292 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293
Jan Engelhardt630ef482009-01-27 14:58:41 +01001294 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001295 *e = *fw;
1296 e->target_offset = size;
1297 e->next_offset = size + target->u.target_size;
1298
1299 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001300 for (matchp = matches; matchp; matchp = matchp->next) {
1301 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1302 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001303 }
1304 memcpy(e->elems + size, target, target->u.target_size);
1305
1306 return e;
1307}
1308
Jan Engelhardt33690a12008-02-11 00:54:00 +01001309static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001310{
1311 struct ip6tables_rule_match *matchp, *tmp;
1312
1313 for (matchp = *matches; matchp;) {
1314 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001315 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001316 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001317 matchp->match->m = NULL;
1318 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001319 if (matchp->match == matchp->match->next) {
1320 free(matchp->match);
1321 matchp->match = NULL;
1322 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001323 free(matchp);
1324 matchp = tmp;
1325 }
1326
1327 *matches = NULL;
1328}
1329
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001330static void set_revision(char *name, u_int8_t revision)
1331{
1332 /* Old kernel sources don't have ".revision" field,
1333 but we stole a byte from name. */
1334 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1335 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1336}
1337
Jan Engelhardtfd187312008-11-10 16:59:27 +01001338int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001339{
1340 struct ip6t_entry fw, *e = NULL;
1341 int invert = 0;
1342 unsigned int nsaddrs = 0, ndaddrs = 0;
1343 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1344
1345 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001346 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001347 const char *chain = NULL;
1348 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1349 const char *policy = NULL, *newname = NULL;
1350 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001351 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001352 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001353 struct xtables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001354 struct ip6tables_rule_match *matches = NULL;
1355 struct ip6tables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001356 struct xtables_target *target = NULL;
1357 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001358 const char *jumpto = "";
1359 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001360 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001361 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362
1363 memset(&fw, 0, sizeof(fw));
1364
Harald Welte43ac1912002-03-03 09:43:07 +00001365 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001366 * a second time */
1367 optind = 0;
1368
Harald Welte43ac1912002-03-03 09:43:07 +00001369 /* clear mflags in case do_command gets called a second time
1370 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001371 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001372 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001373
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001374 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001375 t->tflags = 0;
1376 t->used = 0;
1377 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001378
Rusty Russell5eed48a2000-06-02 20:12:24 +00001379 /* Suppress error messages: we may add new options if we
1380 demand-load a protocol. */
1381 opterr = 0;
1382
1383 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001384 "-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 +00001385 opts, NULL)) != -1) {
1386 switch (c) {
1387 /*
1388 * Command selection
1389 */
1390 case 'A':
1391 add_command(&command, CMD_APPEND, CMD_NONE,
1392 invert);
1393 chain = optarg;
1394 break;
1395
1396 case 'D':
1397 add_command(&command, CMD_DELETE, CMD_NONE,
1398 invert);
1399 chain = optarg;
1400 if (optind < argc && argv[optind][0] != '-'
1401 && argv[optind][0] != '!') {
1402 rulenum = parse_rulenumber(argv[optind++]);
1403 command = CMD_DELETE_NUM;
1404 }
1405 break;
1406
Rusty Russell5eed48a2000-06-02 20:12:24 +00001407 case 'R':
1408 add_command(&command, CMD_REPLACE, CMD_NONE,
1409 invert);
1410 chain = optarg;
1411 if (optind < argc && argv[optind][0] != '-'
1412 && argv[optind][0] != '!')
1413 rulenum = parse_rulenumber(argv[optind++]);
1414 else
1415 exit_error(PARAMETER_PROBLEM,
1416 "-%c requires a rule number",
1417 cmd2char(CMD_REPLACE));
1418 break;
1419
1420 case 'I':
1421 add_command(&command, CMD_INSERT, CMD_NONE,
1422 invert);
1423 chain = optarg;
1424 if (optind < argc && argv[optind][0] != '-'
1425 && argv[optind][0] != '!')
1426 rulenum = parse_rulenumber(argv[optind++]);
1427 else rulenum = 1;
1428 break;
1429
1430 case 'L':
1431 add_command(&command, CMD_LIST, CMD_ZERO,
1432 invert);
1433 if (optarg) chain = optarg;
1434 else if (optind < argc && argv[optind][0] != '-'
1435 && argv[optind][0] != '!')
1436 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001437 if (optind < argc && argv[optind][0] != '-'
1438 && argv[optind][0] != '!')
1439 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440 break;
1441
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001442 case 'S':
1443 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1444 invert);
1445 if (optarg) chain = optarg;
1446 else if (optind < argc && argv[optind][0] != '-'
1447 && argv[optind][0] != '!')
1448 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001449 if (optind < argc && argv[optind][0] != '-'
1450 && argv[optind][0] != '!')
1451 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001452 break;
1453
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454 case 'F':
1455 add_command(&command, CMD_FLUSH, CMD_NONE,
1456 invert);
1457 if (optarg) chain = optarg;
1458 else if (optind < argc && argv[optind][0] != '-'
1459 && argv[optind][0] != '!')
1460 chain = argv[optind++];
1461 break;
1462
1463 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001464 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001465 invert);
1466 if (optarg) chain = optarg;
1467 else if (optind < argc && argv[optind][0] != '-'
1468 && argv[optind][0] != '!')
1469 chain = argv[optind++];
1470 break;
1471
1472 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001473 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001474 exit_error(PARAMETER_PROBLEM,
1475 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001476 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001477 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001478 exit_error(PARAMETER_PROBLEM,
1479 "chain name may not clash "
1480 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001481 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1482 invert);
1483 chain = optarg;
1484 break;
1485
1486 case 'X':
1487 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1488 invert);
1489 if (optarg) chain = optarg;
1490 else if (optind < argc && argv[optind][0] != '-'
1491 && argv[optind][0] != '!')
1492 chain = argv[optind++];
1493 break;
1494
1495 case 'E':
1496 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1497 invert);
1498 chain = optarg;
1499 if (optind < argc && argv[optind][0] != '-'
1500 && argv[optind][0] != '!')
1501 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001502 else
1503 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001504 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001505 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001506 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001507 break;
1508
1509 case 'P':
1510 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1511 invert);
1512 chain = optarg;
1513 if (optind < argc && argv[optind][0] != '-'
1514 && argv[optind][0] != '!')
1515 policy = argv[optind++];
1516 else
1517 exit_error(PARAMETER_PROBLEM,
1518 "-%c requires a chain and a policy",
1519 cmd2char(CMD_SET_POLICY));
1520 break;
1521
1522 case 'h':
1523 if (!optarg)
1524 optarg = argv[optind];
1525
Jonas Berlin1b91e592005-04-01 06:58:38 +00001526 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001527 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001528 xtables_find_match(protocol, XTF_TRY_LOAD,
1529 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001531 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001532
1533 /*
1534 * Option selection
1535 */
1536 case 'p':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001537 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1539 invert);
1540
1541 /* Canonicalize into lower case */
1542 for (protocol = argv[optind-1]; *protocol; protocol++)
1543 *protocol = tolower(*protocol);
1544
1545 protocol = argv[optind-1];
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001546 fw.ipv6.proto = xtables_parse_protocol(protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 fw.ipv6.flags |= IP6T_F_PROTO;
1548
1549 if (fw.ipv6.proto == 0
1550 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1551 exit_error(PARAMETER_PROBLEM,
1552 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001553
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001554 if (is_exthdr(fw.ipv6.proto)
1555 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001556 fprintf(stderr,
1557 "Warning: never matched protocol: %s. "
1558 "use extension match instead.\n",
1559 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001560 break;
1561
1562 case 's':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001563 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001564 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1565 invert);
1566 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567 break;
1568
1569 case 'd':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001570 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001571 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1572 invert);
1573 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 break;
1575
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001576#ifdef IP6T_F_GOTO
1577 case 'g':
1578 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1579 invert);
1580 fw.ipv6.flags |= IP6T_F_GOTO;
1581 jumpto = parse_target(optarg);
1582 break;
1583#endif
1584
Rusty Russell5eed48a2000-06-02 20:12:24 +00001585 case 'j':
1586 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1587 invert);
1588 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001589 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001590 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001591
1592 if (target) {
1593 size_t size;
1594
Harald Welte43ac1912002-03-03 09:43:07 +00001595 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1596 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001597
Jan Engelhardt630ef482009-01-27 14:58:41 +01001598 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001599 target->t->u.target_size = size;
1600 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001601 set_revision(target->t->u.user.name,
1602 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001603 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001604 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001605 opts = merge_options(opts,
1606 target->extra_opts,
1607 &target->option_offset);
1608 if (opts == NULL)
1609 exit_error(OTHER_PROBLEM,
1610 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 }
1612 break;
1613
1614
1615 case 'i':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001616 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1618 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001619 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001620 fw.ipv6.iniface,
1621 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 break;
1623
1624 case 'o':
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001625 xtables_check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001626 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1627 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001628 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 fw.ipv6.outiface,
1630 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001631 break;
1632
1633 case 'v':
1634 if (!verbose)
1635 set_option(&options, OPT_VERBOSE,
1636 &fw.ipv6.invflags, invert);
1637 verbose++;
1638 break;
1639
1640 case 'm': {
1641 size_t size;
1642
1643 if (invert)
1644 exit_error(PARAMETER_PROBLEM,
1645 "unexpected ! flag before --match");
1646
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001647 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1648 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001649 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1650 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001651 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001652 m->m->u.match_size = size;
1653 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001654 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001655 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001656 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001657 if (m != m->next)
1658 /* Merge options for non-cloned matches */
1659 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 }
1661 break;
1662
1663 case 'n':
1664 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1665 invert);
1666 break;
1667
1668 case 't':
1669 if (invert)
1670 exit_error(PARAMETER_PROBLEM,
1671 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001672 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673 break;
1674
1675 case 'x':
1676 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1677 invert);
1678 break;
1679
1680 case 'V':
1681 if (invert)
1682 printf("Not %s ;-)\n", program_version);
1683 else
1684 printf("%s v%s\n",
1685 program_name, program_version);
1686 exit(0);
1687
1688 case '0':
1689 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1690 invert);
1691 break;
1692
Harald Welte43ac1912002-03-03 09:43:07 +00001693 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001694 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001695 break;
1696
1697 case 'c':
1698
1699 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1700 invert);
1701 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001702 bcnt = strchr(pcnt + 1, ',');
1703 if (bcnt)
1704 bcnt++;
1705 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001706 && argv[optind][0] != '!')
1707 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001708 if (!bcnt)
Harald Welte43ac1912002-03-03 09:43:07 +00001709 exit_error(PARAMETER_PROBLEM,
1710 "-%c requires packet and byte counter",
1711 opt2char(OPT_COUNTERS));
1712
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001713 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001714 exit_error(PARAMETER_PROBLEM,
1715 "-%c packet counter not numeric",
1716 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001717 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001718
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001719 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001720 exit_error(PARAMETER_PROBLEM,
1721 "-%c byte counter not numeric",
1722 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001723 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001724 break;
1725
Rusty Russell5eed48a2000-06-02 20:12:24 +00001726 case 1: /* non option */
1727 if (optarg[0] == '!' && optarg[1] == '\0') {
1728 if (invert)
1729 exit_error(PARAMETER_PROBLEM,
1730 "multiple consecutive ! not"
1731 " allowed");
1732 invert = TRUE;
1733 optarg[0] = '\0';
1734 continue;
1735 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001736 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001737 exit_tryhelp(2);
1738
1739 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001740 if (!target
1741 || !(target->parse(c - target->option_offset,
1742 argv, invert,
1743 &target->tflags,
1744 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001745 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001746 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001747 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001748 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001749 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001750 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001751 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001752 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001753 break;
1754 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001755 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001756
1757 /* If you listen carefully, you can
1758 actually hear this code suck. */
1759
1760 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001761 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001762 * parameter, that has not been parsed yet,
1763 * it's not an option of an explicitly loaded
1764 * match or a target. However, we support
1765 * implicit loading of the protocol match
1766 * extension. '-p tcp' means 'l4 proto 6' and
1767 * at the same time 'load tcp protocol match on
1768 * demand if we specify --dport'.
1769 *
1770 * To make this work, we need to make sure:
1771 * - the parameter has not been parsed by
1772 * a match (m above)
1773 * - a protocol has been specified
1774 * - the protocol extension has not been
1775 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001776 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001777 * - the protocol extension can be successively
1778 * loaded
1779 */
1780 if (m == NULL
1781 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001782 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001783 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001784 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001785 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001786 && (proto_used == 0))
1787 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001788 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001789 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001790 /* Try loading protocol */
1791 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001792
Harald Welte00f5a9c2002-08-26 14:37:35 +00001793 proto_used = 1;
1794
1795 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1796 + m->size;
1797
Jan Engelhardt630ef482009-01-27 14:58:41 +01001798 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001799 m->m->u.match_size = size;
1800 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001801 set_revision(m->m->u.user.name,
1802 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001803 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001804 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001805
1806 opts = merge_options(opts,
1807 m->extra_opts, &m->option_offset);
1808
1809 optind--;
1810 continue;
1811 }
1812
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001813 if (!m) {
1814 if (c == '?') {
1815 if (optopt) {
1816 exit_error(
1817 PARAMETER_PROBLEM,
1818 "option `%s' "
1819 "requires an "
1820 "argument",
1821 argv[optind-1]);
1822 } else {
1823 exit_error(
1824 PARAMETER_PROBLEM,
1825 "unknown option "
1826 "`%s'",
1827 argv[optind-1]);
1828 }
1829 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 exit_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001831 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001832 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833 }
1834 }
1835 invert = FALSE;
1836 }
1837
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001838 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001839 if (matchp->match->final_check != NULL)
1840 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001841
Jan Engelhardt830132a2007-10-04 16:24:50 +00001842 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001843 target->final_check(target->tflags);
1844
1845 /* Fix me: must put inverse options checking here --MN */
1846
1847 if (optind < argc)
1848 exit_error(PARAMETER_PROBLEM,
1849 "unknown arguments found on commandline");
1850 if (!command)
1851 exit_error(PARAMETER_PROBLEM, "no command specified");
1852 if (invert)
1853 exit_error(PARAMETER_PROBLEM,
1854 "nothing appropriate following !");
1855
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001856 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001857 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001858 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001859 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001860 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001861 }
1862
1863 if (shostnetworkmask)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001864 xtables_ip6parse_any(shostnetworkmask, &saddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001865 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866
1867 if (dhostnetworkmask)
Jan Engelhardta0baae82009-01-30 04:32:50 +01001868 xtables_ip6parse_any(dhostnetworkmask, &daddrs,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001869 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001870
1871 if ((nsaddrs > 1 || ndaddrs > 1) &&
1872 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1873 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1874 " source or destination IP addresses");
1875
Rusty Russell5eed48a2000-06-02 20:12:24 +00001876 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1877 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1878 "specify a unique address");
1879
1880 generic_opt_check(command, options);
1881
1882 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1883 exit_error(PARAMETER_PROBLEM,
1884 "chain name `%s' too long (must be under %i chars)",
1885 chain, IP6T_FUNCTION_MAXNAMELEN);
1886
Harald Welte43ac1912002-03-03 09:43:07 +00001887 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001888 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001889 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001890
Rusty Russell8beb0492004-12-22 00:37:10 +00001891 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001892 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001893 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001894
Harald Welte43ac1912002-03-03 09:43:07 +00001895 if (!*handle)
1896 exit_error(VERSION_PROBLEM,
1897 "can't initialize ip6tables table `%s': %s",
1898 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001900 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001901 || command == CMD_DELETE
1902 || command == CMD_INSERT
1903 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001904 if (strcmp(chain, "PREROUTING") == 0
1905 || strcmp(chain, "INPUT") == 0) {
1906 /* -o not valid with incoming packets. */
1907 if (options & OPT_VIANAMEOUT)
1908 exit_error(PARAMETER_PROBLEM,
1909 "Can't use -%c with %s\n",
1910 opt2char(OPT_VIANAMEOUT),
1911 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001912 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001913
Harald Welte43ac1912002-03-03 09:43:07 +00001914 if (strcmp(chain, "POSTROUTING") == 0
1915 || strcmp(chain, "OUTPUT") == 0) {
1916 /* -i not valid with outgoing packets */
1917 if (options & OPT_VIANAMEIN)
1918 exit_error(PARAMETER_PROBLEM,
1919 "Can't use -%c with %s\n",
1920 opt2char(OPT_VIANAMEIN),
1921 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001922 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001923
1924 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001925 fprintf(stderr,
1926 "Warning: using chain %s, not extension\n",
1927 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001929 if (target->t)
1930 free(target->t);
1931
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932 target = NULL;
1933 }
1934
1935 /* If they didn't specify a target, or it's a chain
1936 name, use standard. */
1937 if (!target
1938 && (strlen(jumpto) == 0
1939 || ip6tc_is_chain(jumpto, *handle))) {
1940 size_t size;
1941
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001942 target = xtables_find_target(IP6T_STANDARD_TARGET,
1943 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001944
1945 size = sizeof(struct ip6t_entry_target)
1946 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001947 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001948 target->t->u.target_size = size;
1949 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001950 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001951 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 }
1953
1954 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001955 /* it is no chain, and we can't load a plugin.
1956 * We cannot know if the plugin is corrupt, non
1957 * existant OR if the user just misspelled a
1958 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001959#ifdef IP6T_F_GOTO
1960 if (fw.ipv6.flags & IP6T_F_GOTO)
1961 exit_error(PARAMETER_PROBLEM,
1962 "goto '%s' is not a chain\n", jumpto);
1963#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001964 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001965 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001966 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001967 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 }
1969 }
1970
1971 switch (command) {
1972 case CMD_APPEND:
1973 ret = append_entry(chain, e,
1974 nsaddrs, saddrs, ndaddrs, daddrs,
1975 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001976 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001977 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 case CMD_DELETE:
1979 ret = delete_entry(chain, e,
1980 nsaddrs, saddrs, ndaddrs, daddrs,
1981 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001982 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001983 break;
1984 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001985 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001986 break;
1987 case CMD_REPLACE:
1988 ret = replace_entry(chain, e, rulenum - 1,
1989 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001990 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001991 break;
1992 case CMD_INSERT:
1993 ret = insert_entry(chain, e, rulenum - 1,
1994 nsaddrs, saddrs, ndaddrs, daddrs,
1995 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001996 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001997 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001998 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001999 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002000 break;
2001 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002002 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002003 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002004 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002005 case CMD_LIST|CMD_ZERO:
2006 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002007 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002008 options&OPT_VERBOSE,
2009 options&OPT_NUMERIC,
2010 options&OPT_EXPANDED,
2011 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002012 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002013 if (ret && (command & CMD_ZERO))
2014 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002015 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002016 break;
2017 case CMD_LIST_RULES:
2018 case CMD_LIST_RULES|CMD_ZERO:
2019 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002020 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002021 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002022 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002023 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002024 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002025 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002026 break;
2027 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002028 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002029 break;
2030 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002031 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002032 break;
2033 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002034 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002035 break;
2036 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002037 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002038 break;
2039 default:
2040 /* We should never reach this... */
2041 exit_tryhelp(2);
2042 }
2043
2044 if (verbose > 1)
2045 dump_entries6(*handle);
2046
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002047 clear_rule_matches(&matches);
2048
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002049 if (e != NULL) {
2050 free(e);
2051 e = NULL;
2052 }
2053
Max Kellermann9ee386a2008-01-29 13:48:05 +00002054 for (i = 0; i < nsaddrs; i++)
2055 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002056
Max Kellermann9ee386a2008-01-29 13:48:05 +00002057 for (i = 0; i < ndaddrs; i++)
2058 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002059
Pablo Neiradfdcd642005-05-29 19:05:23 +00002060 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002061
Rusty Russell5eed48a2000-06-02 20:12:24 +00002062 return ret;
2063}