blob: 2b0539247e7c160eb7e5779616e77e5fe6e2b446 [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>
36#include <limits.h>
37#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000039#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000042#include <sys/types.h>
43#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010044#include "ip6tables-multi.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
Rusty Russell5eed48a2000-06-02 20:12:24 +000053#define FMT_NUMERIC 0x0001
54#define FMT_NOCOUNTS 0x0002
55#define FMT_KILOMEGAGIGA 0x0004
56#define FMT_OPTIONS 0x0008
57#define FMT_NOTABLE 0x0010
58#define FMT_NOTARGET 0x0020
59#define FMT_VIA 0x0040
60#define FMT_NONEWLINE 0x0080
61#define FMT_LINENUMBERS 0x0100
62
63#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
64 | FMT_NUMERIC | FMT_NOTABLE)
65#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
66
67
68#define CMD_NONE 0x0000U
69#define CMD_INSERT 0x0001U
70#define CMD_DELETE 0x0002U
71#define CMD_DELETE_NUM 0x0004U
72#define CMD_REPLACE 0x0008U
73#define CMD_APPEND 0x0010U
74#define CMD_LIST 0x0020U
75#define CMD_FLUSH 0x0040U
76#define CMD_ZERO 0x0080U
77#define CMD_NEW_CHAIN 0x0100U
78#define CMD_DELETE_CHAIN 0x0200U
79#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000080#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020081#define CMD_LIST_RULES 0x1000U
82#define NUMBER_OF_CMD 14
Rusty Russell5eed48a2000-06-02 20:12:24 +000083static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020084 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000085
86#define OPTION_OFFSET 256
87
88#define OPT_NONE 0x00000U
89#define OPT_NUMERIC 0x00001U
90#define OPT_SOURCE 0x00002U
91#define OPT_DESTINATION 0x00004U
92#define OPT_PROTOCOL 0x00008U
93#define OPT_JUMP 0x00010U
94#define OPT_VERBOSE 0x00020U
95#define OPT_EXPANDED 0x00040U
96#define OPT_VIANAMEIN 0x00080U
97#define OPT_VIANAMEOUT 0x00100U
98#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000099#define OPT_COUNTERS 0x00400U
100#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000101static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000102= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000103
104static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100105 {.name = "append", .has_arg = 1, .val = 'A'},
106 {.name = "delete", .has_arg = 1, .val = 'D'},
107 {.name = "insert", .has_arg = 1, .val = 'I'},
108 {.name = "replace", .has_arg = 1, .val = 'R'},
109 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200110 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100111 {.name = "flush", .has_arg = 2, .val = 'F'},
112 {.name = "zero", .has_arg = 2, .val = 'Z'},
113 {.name = "new-chain", .has_arg = 1, .val = 'N'},
114 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
115 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
116 {.name = "policy", .has_arg = 1, .val = 'P'},
117 {.name = "source", .has_arg = 1, .val = 's'},
118 {.name = "destination", .has_arg = 1, .val = 'd'},
119 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
120 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
121 {.name = "protocol", .has_arg = 1, .val = 'p'},
122 {.name = "in-interface", .has_arg = 1, .val = 'i'},
123 {.name = "jump", .has_arg = 1, .val = 'j'},
124 {.name = "table", .has_arg = 1, .val = 't'},
125 {.name = "match", .has_arg = 1, .val = 'm'},
126 {.name = "numeric", .has_arg = 0, .val = 'n'},
127 {.name = "out-interface", .has_arg = 1, .val = 'o'},
128 {.name = "verbose", .has_arg = 0, .val = 'v'},
129 {.name = "exact", .has_arg = 0, .val = 'x'},
130 {.name = "version", .has_arg = 0, .val = 'V'},
131 {.name = "help", .has_arg = 2, .val = 'h'},
132 {.name = "line-numbers", .has_arg = 0, .val = '0'},
133 {.name = "modprobe", .has_arg = 1, .val = 'M'},
134 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200135 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100136 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000137};
138
Harald Weltea8658ca2003-03-05 07:46:15 +0000139/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
140 * current line of the input file, in order to give a more precise error
141 * message. ip6tables itself doesn't need this, so it is initialized to the
142 * magic number of -1 */
143int line = -1;
144
Rusty Russell5eed48a2000-06-02 20:12:24 +0000145static struct option *opts = original_opts;
146static unsigned int global_option_offset = 0;
147
148/* Table of legal combinations of commands and options. If any of the
149 * given commands make an option legal, that option is legal (applies to
150 * CMD_LIST and CMD_ZERO only).
151 * Key:
152 * + compulsory
153 * x illegal
154 * optional
155 */
156
157static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
158/* Well, it's better than "Re: Linux vs FreeBSD" */
159{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200160 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
163/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
164/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
165/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
167/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200171/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200172/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000174};
175
176static int inverse_for_options[NUMBER_OF_OPT] =
177{
178/* -n */ 0,
179/* -s */ IP6T_INV_SRCIP,
180/* -d */ IP6T_INV_DSTIP,
181/* -p */ IP6T_INV_PROTO,
182/* -j */ 0,
183/* -v */ 0,
184/* -x */ 0,
185/* -i */ IP6T_INV_VIA_IN,
186/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000187/*--line*/ 0,
188/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189};
190
191const char *program_version;
192const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000193
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194/* A few hardcoded protocols for 'all' and in case the user has no
195 /etc/protocols */
196struct pprot {
197 char *name;
198 u_int8_t num;
199};
200
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000201struct afinfo afinfo = {
202 .family = AF_INET6,
203 .libprefix = "libip6t_",
204 .ipproto = IPPROTO_IPV6,
205 .kmod = "ip6_tables",
206 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
207 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
208};
209
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000210/* Primitive headers... */
211/* defined in netinet/in.h */
212#if 0
213#ifndef IPPROTO_ESP
214#define IPPROTO_ESP 50
215#endif
216#ifndef IPPROTO_AH
217#define IPPROTO_AH 51
218#endif
219#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000220#ifndef IPPROTO_MH
221#define IPPROTO_MH 135
222#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000223
Rusty Russell5eed48a2000-06-02 20:12:24 +0000224static const struct pprot chain_protos[] = {
225 { "tcp", IPPROTO_TCP },
226 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000227 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000228 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000229 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000230 { "esp", IPPROTO_ESP },
231 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000232 { "ipv6-mh", IPPROTO_MH },
233 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000234 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000235};
236
237static char *
238proto_to_name(u_int8_t proto, int nolookup)
239{
240 unsigned int i;
241
242 if (proto && !nolookup) {
243 struct protoent *pent = getprotobynumber(proto);
244 if (pent)
245 return pent->p_name;
246 }
247
248 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
249 if (chain_protos[i].num == proto)
250 return chain_protos[i].name;
251
252 return NULL;
253}
254
Pablo Neiradfdcd642005-05-29 19:05:23 +0000255static void free_opts(int reset_offset)
256{
257 if (opts != original_opts) {
258 free(opts);
259 opts = original_opts;
260 if (reset_offset)
261 global_option_offset = 0;
262 }
263}
264
Patrick McHardy0b639362007-09-08 16:00:01 +0000265static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000266exit_tryhelp(int status)
267{
Harald Weltea8658ca2003-03-05 07:46:15 +0000268 if (line != -1)
269 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
271 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000272 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000273 exit(status);
274}
275
Patrick McHardy0b639362007-09-08 16:00:01 +0000276static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000277exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000279 struct ip6tables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200280 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000281
282 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000283"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000284" %s -[RI] chain rulenum rule-specification [options]\n"
285" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200286" %s -[LS] [chain [rulenum]] [options]\n"
287" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000288" %s -[NX] chain\n"
289" %s -E old-chain-name new-chain-name\n"
290" %s -P chain target [options]\n"
291" %s -h (print this help information)\n\n",
292 program_name, program_version, program_name, program_name,
293 program_name, program_name, program_name, program_name,
Henrik Nordstrombb340822008-05-13 13:09:23 +0200294 program_name, program_name, program_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000295
296 printf(
297"Commands:\n"
298"Either long or short options are allowed.\n"
299" --append -A chain Append to chain\n"
300" --delete -D chain Delete matching rule from chain\n"
301" --delete -D chain rulenum\n"
302" Delete rule rulenum (1 = first) from chain\n"
303" --insert -I chain [rulenum]\n"
304" Insert in chain as rulenum (default 1=first)\n"
305" --replace -R chain rulenum\n"
306" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200307" --list -L [chain [rulenum]]\n"
308" List the rules in a chain or all chains\n"
309" --list-rules -S [chain [rulenum]]\n"
310" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000311" --flush -F [chain] Delete all rules in chain or all chains\n"
312" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000313" --new -N chain Create a new user-defined chain\n"
314" --delete-chain\n"
315" -X [chain] Delete a user-defined chain\n"
316" --policy -P chain target\n"
317" Change policy on chain to target\n"
318" --rename-chain\n"
319" -E old-chain new-chain\n"
320" Change chain name, (moving any references)\n"
321
322"Options:\n"
323" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
324" --source -s [!] address[/mask]\n"
325" source specification\n"
326" --destination -d [!] address[/mask]\n"
327" destination specification\n"
328" --in-interface -i [!] input name[+]\n"
329" network interface name ([+] for wildcard)\n"
330" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000331" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200332#ifdef IP6T_F_GOTO
333" --goto -g chain\n"
334" jump to chain with no return\n"
335#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000336" --match -m match\n"
337" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000338" --numeric -n numeric output of addresses and ports\n"
339" --out-interface -o [!] output name[+]\n"
340" network interface name ([+] for wildcard)\n"
341" --table -t table table to manipulate (default: `filter')\n"
342" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000343" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000344" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000345/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000346" --modprobe=<command> try to insert modules using this command\n"
347" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348"[!] --version -V print package version.\n");
349
Max Kellermann5b76f682008-01-29 13:42:48 +0000350 /* Print out any special helps. A user might like to be able to add a --help
351 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000352 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000353 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354 if (t->used) {
355 printf("\n");
356 t->help();
357 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000358 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000359 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000360 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000361 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000362 }
363 exit(0);
364}
365
Patrick McHardy0b639362007-09-08 16:00:01 +0000366void
367exit_error(enum exittype status, const char *msg, ...)
368{
369 va_list args;
370
371 va_start(args, msg);
372 fprintf(stderr, "%s v%s: ", program_name, program_version);
373 vfprintf(stderr, msg, args);
374 va_end(args);
375 fprintf(stderr, "\n");
376 if (status == PARAMETER_PROBLEM)
377 exit_tryhelp(status);
378 if (status == VERSION_PROBLEM)
379 fprintf(stderr,
380 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
381 /* On error paths, make sure that we don't leak memory */
382 free_opts(1);
383 exit(status);
384}
385
Rusty Russell5eed48a2000-06-02 20:12:24 +0000386static void
387generic_opt_check(int command, int options)
388{
389 int i, j, legal = 0;
390
391 /* Check that commands are valid with options. Complicated by the
392 * fact that if an option is legal with *any* command given, it is
393 * legal overall (ie. -z and -l).
394 */
395 for (i = 0; i < NUMBER_OF_OPT; i++) {
396 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
397
398 for (j = 0; j < NUMBER_OF_CMD; j++) {
399 if (!(command & (1<<j)))
400 continue;
401
402 if (!(options & (1<<i))) {
403 if (commands_v_options[j][i] == '+')
404 exit_error(PARAMETER_PROBLEM,
405 "You need to supply the `-%c' "
406 "option for this command\n",
407 optflags[i]);
408 } else {
409 if (commands_v_options[j][i] != 'x')
410 legal = 1;
411 else if (legal == 0)
412 legal = -1;
413 }
414 }
415 if (legal == -1)
416 exit_error(PARAMETER_PROBLEM,
417 "Illegal option `-%c' with this command\n",
418 optflags[i]);
419 }
420}
421
422static char
423opt2char(int option)
424{
425 const char *ptr;
426 for (ptr = optflags; option > 1; option >>= 1, ptr++);
427
428 return *ptr;
429}
430
431static char
432cmd2char(int option)
433{
434 const char *ptr;
435 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
436
437 return *ptr;
438}
439
440static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000441add_command(unsigned int *cmd, const int newcmd, const int othercmds,
442 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000443{
444 if (invert)
445 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
446 if (*cmd & (~othercmds))
447 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
448 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
449 *cmd |= newcmd;
450}
451
452int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100453check_inverse(const char option[], int *invert, int *my_optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000454{
455 if (option && strcmp(option, "!") == 0) {
456 if (*invert)
457 exit_error(PARAMETER_PROBLEM,
458 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000459 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100460 if (my_optind != NULL) {
461 ++*my_optind;
462 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000463 exit_error(PARAMETER_PROBLEM,
464 "no argument following `!'");
465 }
466
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467 return TRUE;
468 }
469 return FALSE;
470}
471
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472/*
473 * All functions starting with "parse" should succeed, otherwise
474 * the program fails.
475 * Most routines return pointers to static data that may change
476 * between calls to the same or other routines with a few exceptions:
477 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
478 * return global static data.
479*/
480
Rusty Russell5eed48a2000-06-02 20:12:24 +0000481/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200482static struct xtables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000483find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484{
Harald Welteed498492001-07-23 01:24:22 +0000485 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486
Harald Welte43ac1912002-03-03 09:43:07 +0000487 if (string_to_number(pname, 0, 255, &proto) != -1) {
488 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489
Harald Welte43ac1912002-03-03 09:43:07 +0000490 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000491 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000492 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000493 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000494
495 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000496}
497
Harald Welte43ac1912002-03-03 09:43:07 +0000498u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000499parse_protocol(const char *s)
500{
Harald Welteed498492001-07-23 01:24:22 +0000501 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000502
Harald Welteed498492001-07-23 01:24:22 +0000503 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000504 struct protoent *pent;
505
Harald Weltecbe1ec72006-02-11 09:50:11 +0000506 /* first deal with the special case of 'all' to prevent
507 * people from being able to redefine 'all' in nsswitch
Max Kellermann5b76f682008-01-29 13:42:48 +0000508 * and/or provoke expensive [not working] ldap/nis/...
Harald Weltecbe1ec72006-02-11 09:50:11 +0000509 * lookups */
510 if (!strcmp(s, "all"))
511 return 0;
512
Rusty Russell5eed48a2000-06-02 20:12:24 +0000513 if ((pent = getprotobyname(s)))
514 proto = pent->p_proto;
515 else {
516 unsigned int i;
517 for (i = 0;
518 i < sizeof(chain_protos)/sizeof(struct pprot);
519 i++) {
520 if (strcmp(s, chain_protos[i].name) == 0) {
521 proto = chain_protos[i].num;
522 break;
523 }
524 }
525 if (i == sizeof(chain_protos)/sizeof(struct pprot))
526 exit_error(PARAMETER_PROBLEM,
527 "unknown protocol `%s' specified",
528 s);
529 }
530 }
531
532 return (u_int16_t)proto;
533}
534
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000535/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000536static int is_exthdr(u_int16_t proto)
537{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000538 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000539 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000540 proto == IPPROTO_AH ||
541 proto == IPPROTO_DSTOPTS);
542}
543
Rusty Russell5eed48a2000-06-02 20:12:24 +0000544/* Can't be zero. */
545static int
546parse_rulenumber(const char *rule)
547{
Harald Welte43ac1912002-03-03 09:43:07 +0000548 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000549
Harald Welte43ac1912002-03-03 09:43:07 +0000550 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551 exit_error(PARAMETER_PROBLEM,
552 "Invalid rule number `%s'", rule);
553
554 return rulenum;
555}
556
557static const char *
558parse_target(const char *targetname)
559{
560 const char *ptr;
561
562 if (strlen(targetname) < 1)
563 exit_error(PARAMETER_PROBLEM,
564 "Invalid target name (too short)");
565
566 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
567 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000568 "Invalid target name `%s' (%u chars max)",
569 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000570
571 for (ptr = targetname; *ptr; ptr++)
572 if (isspace(*ptr))
573 exit_error(PARAMETER_PROBLEM,
574 "Invalid target name `%s'", targetname);
575 return targetname;
576}
577
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578static void
579set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
580 int invert)
581{
582 if (*options & option)
583 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
584 opt2char(option));
585 *options |= option;
586
587 if (invert) {
588 unsigned int i;
589 for (i = 0; 1 << i != option; i++);
590
591 if (!inverse_for_options[i])
592 exit_error(PARAMETER_PROBLEM,
593 "cannot have ! before -%c",
594 opt2char(option));
595 *invflg |= inverse_for_options[i];
596 }
597}
598
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000600merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601 unsigned int *option_offset)
602{
603 unsigned int num_old, num_new, i;
604 struct option *merge;
605
Jan Engelhardtd0145402007-07-30 14:32:26 +0000606 if (newopts == NULL)
607 return oldopts;
608
Rusty Russell5eed48a2000-06-02 20:12:24 +0000609 for (num_old = 0; oldopts[num_old].name; num_old++);
610 for (num_new = 0; newopts[num_new].name; num_new++);
611
612 global_option_offset += OPTION_OFFSET;
613 *option_offset = global_option_offset;
614
615 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
616 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000617 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000618 for (i = 0; i < num_new; i++) {
619 merge[num_old + i] = newopts[i];
620 merge[num_old + i].val += *option_offset;
621 }
622 memset(merge + num_old + num_new, 0, sizeof(struct option));
623
624 return merge;
625}
626
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000627static void
628print_num(u_int64_t number, unsigned int format)
629{
Harald Welte43ac1912002-03-03 09:43:07 +0000630 if (format & FMT_KILOMEGAGIGA) {
631 if (number > 99999) {
632 number = (number + 500) / 1000;
633 if (number > 9999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
637 if (number > 9999) {
638 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000639 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000640 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000644 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000650}
651
Harald Welte43ac1912002-03-03 09:43:07 +0000652
Rusty Russell5eed48a2000-06-02 20:12:24 +0000653static void
654print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
655{
656 struct ip6t_counters counters;
657 const char *pol = ip6tc_get_policy(chain, &counters, handle);
658 printf("Chain %s", chain);
659 if (pol) {
660 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000661 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000662 fputc(' ', stdout);
663 print_num(counters.pcnt, (format|FMT_NOTABLE));
664 fputs("packets, ", stdout);
665 print_num(counters.bcnt, (format|FMT_NOTABLE));
666 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000667 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000668 printf(")\n");
669 } else {
670 unsigned int refs;
671 if (!ip6tc_get_references(&refs, chain, handle))
672 printf(" (ERROR obtaining refs)\n");
673 else
674 printf(" (%u references)\n", refs);
675 }
676
677 if (format & FMT_LINENUMBERS)
678 printf(FMT("%-4s ", "%s "), "num");
679 if (!(format & FMT_NOCOUNTS)) {
680 if (format & FMT_KILOMEGAGIGA) {
681 printf(FMT("%5s ","%s "), "pkts");
682 printf(FMT("%5s ","%s "), "bytes");
683 } else {
684 printf(FMT("%8s ","%s "), "pkts");
685 printf(FMT("%10s ","%s "), "bytes");
686 }
687 }
688 if (!(format & FMT_NOTARGET))
689 printf(FMT("%-9s ","%s "), "target");
690 fputs(" prot ", stdout);
691 if (format & FMT_OPTIONS)
692 fputs("opt", stdout);
693 if (format & FMT_VIA) {
694 printf(FMT(" %-6s ","%s "), "in");
695 printf(FMT("%-6s ","%s "), "out");
696 }
697 printf(FMT(" %-19s ","%s "), "source");
698 printf(FMT(" %-19s "," %s "), "destination");
699 printf("\n");
700}
701
Harald Welte43ac1912002-03-03 09:43:07 +0000702
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703static int
704print_match(const struct ip6t_entry_match *m,
705 const struct ip6t_ip6 *ip,
706 int numeric)
707{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200708 struct xtables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709
710 if (match) {
711 if (match->print)
712 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000713 else
714 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000715 } else {
716 if (m->u.user.name[0])
717 printf("UNKNOWN match `%s' ", m->u.user.name);
718 }
719 /* Don't stop iterating. */
720 return 0;
721}
722
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100723/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724static void
725print_firewall(const struct ip6t_entry *fw,
726 const char *targname,
727 unsigned int num,
728 unsigned int format,
729 const ip6tc_handle_t handle)
730{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200731 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000732 const struct ip6t_entry_target *t;
733 u_int8_t flags;
734 char buf[BUFSIZ];
735
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 if (!ip6tc_is_chain(targname, handle))
737 target = find_target(targname, TRY_LOAD);
738 else
739 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
740
741 t = ip6t_get_target((struct ip6t_entry *)fw);
742 flags = fw->ipv6.flags;
743
744 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200745 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746
747 if (!(format & FMT_NOCOUNTS)) {
748 print_num(fw->counters.pcnt, format);
749 print_num(fw->counters.bcnt, format);
750 }
751
752 if (!(format & FMT_NOTARGET))
753 printf(FMT("%-9s ", "%s "), targname);
754
755 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
756 {
Harald Welte43ac1912002-03-03 09:43:07 +0000757 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000758 if (pname)
759 printf(FMT("%-5s", "%s "), pname);
760 else
761 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
762 }
763
764 if (format & FMT_OPTIONS) {
765 if (format & FMT_NOTABLE)
766 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000767 fputc(' ', stdout); /* Invert flag of FRAG */
768 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769 fputc(' ', stdout);
770 }
771
772 if (format & FMT_VIA) {
773 char iface[IFNAMSIZ+2];
774
775 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
776 iface[0] = '!';
777 iface[1] = '\0';
778 }
779 else iface[0] = '\0';
780
781 if (fw->ipv6.iniface[0] != '\0') {
782 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 }
784 else if (format & FMT_NUMERIC) strcat(iface, "*");
785 else strcat(iface, "any");
786 printf(FMT(" %-6s ","in %s "), iface);
787
788 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
789 iface[0] = '!';
790 iface[1] = '\0';
791 }
792 else iface[0] = '\0';
793
794 if (fw->ipv6.outiface[0] != '\0') {
795 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000796 }
797 else if (format & FMT_NUMERIC) strcat(iface, "*");
798 else strcat(iface, "any");
799 printf(FMT("%-6s ","out %s "), iface);
800 }
801
802 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000803 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000804 && !(format & FMT_NUMERIC))
805 printf(FMT("%-19s ","%s "), "anywhere");
806 else {
807 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000808 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000810 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
811 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000812 printf(FMT("%-19s ","%s "), buf);
813 }
814
815 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
816 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
817 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200818 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000819 else {
820 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000821 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000823 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
824 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200825 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826 }
827
828 if (format & FMT_NOTABLE)
829 fputs(" ", stdout);
830
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200831#ifdef IP6T_F_GOTO
832 if(fw->ipv6.flags & IP6T_F_GOTO)
833 printf("[goto] ");
834#endif
835
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
837
838 if (target) {
839 if (target->print)
840 /* Print the target information. */
841 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
842 } else if (t->u.target_size != sizeof(*t))
843 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000844 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000845
846 if (!(format & FMT_NONEWLINE))
847 fputc('\n', stdout);
848}
849
850static void
851print_firewall_line(const struct ip6t_entry *fw,
852 const ip6tc_handle_t h)
853{
854 struct ip6t_entry_target *t;
855
856 t = ip6t_get_target((struct ip6t_entry *)fw);
857 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
858}
859
860static int
861append_entry(const ip6t_chainlabel chain,
862 struct ip6t_entry *fw,
863 unsigned int nsaddrs,
864 const struct in6_addr saddrs[],
865 unsigned int ndaddrs,
866 const struct in6_addr daddrs[],
867 int verbose,
868 ip6tc_handle_t *handle)
869{
870 unsigned int i, j;
871 int ret = 1;
872
873 for (i = 0; i < nsaddrs; i++) {
874 fw->ipv6.src = saddrs[i];
875 for (j = 0; j < ndaddrs; j++) {
876 fw->ipv6.dst = daddrs[j];
877 if (verbose)
878 print_firewall_line(fw, *handle);
879 ret &= ip6tc_append_entry(chain, fw, handle);
880 }
881 }
882
883 return ret;
884}
885
886static int
887replace_entry(const ip6t_chainlabel chain,
888 struct ip6t_entry *fw,
889 unsigned int rulenum,
890 const struct in6_addr *saddr,
891 const struct in6_addr *daddr,
892 int verbose,
893 ip6tc_handle_t *handle)
894{
895 fw->ipv6.src = *saddr;
896 fw->ipv6.dst = *daddr;
897
898 if (verbose)
899 print_firewall_line(fw, *handle);
900 return ip6tc_replace_entry(chain, fw, rulenum, handle);
901}
902
903static int
904insert_entry(const ip6t_chainlabel chain,
905 struct ip6t_entry *fw,
906 unsigned int rulenum,
907 unsigned int nsaddrs,
908 const struct in6_addr saddrs[],
909 unsigned int ndaddrs,
910 const struct in6_addr daddrs[],
911 int verbose,
912 ip6tc_handle_t *handle)
913{
914 unsigned int i, j;
915 int ret = 1;
916
917 for (i = 0; i < nsaddrs; i++) {
918 fw->ipv6.src = saddrs[i];
919 for (j = 0; j < ndaddrs; j++) {
920 fw->ipv6.dst = daddrs[j];
921 if (verbose)
922 print_firewall_line(fw, *handle);
923 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
924 }
925 }
926
927 return ret;
928}
929
930static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000931make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000932{
933 /* Establish mask for comparison */
934 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000935 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936 unsigned char *mask, *mptr;
937
938 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000939 for (matchp = matches; matchp; matchp = matchp->next)
940 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941
942 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000943 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000944 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000945
946 memset(mask, 0xFF, sizeof(struct ip6t_entry));
947 mptr = mask + sizeof(struct ip6t_entry);
948
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000949 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000951 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000952 + matchp->match->userspacesize);
953 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000954 }
955
Max Kellermann5b76f682008-01-29 13:42:48 +0000956 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000957 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000958 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000959
960 return mask;
961}
962
963static int
964delete_entry(const ip6t_chainlabel chain,
965 struct ip6t_entry *fw,
966 unsigned int nsaddrs,
967 const struct in6_addr saddrs[],
968 unsigned int ndaddrs,
969 const struct in6_addr daddrs[],
970 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000971 ip6tc_handle_t *handle,
972 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000973{
974 unsigned int i, j;
975 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976 unsigned char *mask;
977
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000978 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000979 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000980 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000981 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000982 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 if (verbose)
984 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000985 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000986 }
987 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000988 free(mask);
989
Rusty Russell5eed48a2000-06-02 20:12:24 +0000990 return ret;
991}
992
András Kis-Szabó764316a2001-02-26 17:31:20 +0000993int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000994for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
995 int verbose, int builtinstoo, ip6tc_handle_t *handle)
996{
Max Kellermann5b76f682008-01-29 13:42:48 +0000997 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000998 const char *chain;
999 char *chains;
1000 unsigned int i, chaincount = 0;
1001
1002 chain = ip6tc_first_chain(handle);
1003 while (chain) {
1004 chaincount++;
1005 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001006 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001007
1008 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1009 i = 0;
1010 chain = ip6tc_first_chain(handle);
1011 while (chain) {
1012 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1013 i++;
1014 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001015 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001016
1017 for (i = 0; i < chaincount; i++) {
1018 if (!builtinstoo
1019 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001020 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001021 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +00001022 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001023 }
1024
1025 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001026 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027}
1028
András Kis-Szabó764316a2001-02-26 17:31:20 +00001029int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030flush_entries(const ip6t_chainlabel chain, int verbose,
1031 ip6tc_handle_t *handle)
1032{
1033 if (!chain)
1034 return for_each_chain(flush_entries, verbose, 1, handle);
1035
1036 if (verbose)
1037 fprintf(stdout, "Flushing chain `%s'\n", chain);
1038 return ip6tc_flush_entries(chain, handle);
1039}
1040
1041static int
1042zero_entries(const ip6t_chainlabel chain, int verbose,
1043 ip6tc_handle_t *handle)
1044{
1045 if (!chain)
1046 return for_each_chain(zero_entries, verbose, 1, handle);
1047
1048 if (verbose)
1049 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1050 return ip6tc_zero_entries(chain, handle);
1051}
1052
András Kis-Szabó764316a2001-02-26 17:31:20 +00001053int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001054delete_chain(const ip6t_chainlabel chain, int verbose,
1055 ip6tc_handle_t *handle)
1056{
1057 if (!chain)
1058 return for_each_chain(delete_chain, verbose, 0, handle);
1059
1060 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001061 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001062 return ip6tc_delete_chain(chain, handle);
1063}
1064
1065static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001066list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001067 int expanded, int linenumbers, ip6tc_handle_t *handle)
1068{
1069 int found = 0;
1070 unsigned int format;
1071 const char *this;
1072
1073 format = FMT_OPTIONS;
1074 if (!verbose)
1075 format |= FMT_NOCOUNTS;
1076 else
1077 format |= FMT_VIA;
1078
1079 if (numeric)
1080 format |= FMT_NUMERIC;
1081
1082 if (!expanded)
1083 format |= FMT_KILOMEGAGIGA;
1084
1085 if (linenumbers)
1086 format |= FMT_LINENUMBERS;
1087
1088 for (this = ip6tc_first_chain(handle);
1089 this;
1090 this = ip6tc_next_chain(handle)) {
1091 const struct ip6t_entry *i;
1092 unsigned int num;
1093
1094 if (chain && strcmp(chain, this) != 0)
1095 continue;
1096
1097 if (found) printf("\n");
1098
Henrik Nordstrombb340822008-05-13 13:09:23 +02001099 if (!rulenum)
1100 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001101 i = ip6tc_first_rule(this, handle);
1102
1103 num = 0;
1104 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001105 num++;
1106 if (!rulenum || num == rulenum)
1107 print_firewall(i,
1108 ip6tc_get_target(i, handle),
1109 num,
1110 format,
1111 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001112 i = ip6tc_next_rule(i, handle);
1113 }
1114 found = 1;
1115 }
1116
1117 errno = ENOENT;
1118 return found;
1119}
1120
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001121/* This assumes that mask is contiguous, and byte-bounded. */
1122static void
1123print_iface(char letter, const char *iface, const unsigned char *mask,
1124 int invert)
1125{
1126 unsigned int i;
1127
1128 if (mask[0] == 0)
1129 return;
1130
1131 printf("-%c %s", letter, invert ? "! " : "");
1132
1133 for (i = 0; i < IFNAMSIZ; i++) {
1134 if (mask[i] != 0) {
1135 if (iface[i] != '\0')
1136 printf("%c", iface[i]);
1137 } else {
1138 /* we can access iface[i-1] here, because
1139 * a few lines above we make sure that mask[0] != 0 */
1140 if (iface[i-1] != '\0')
1141 printf("+");
1142 break;
1143 }
1144 }
1145
1146 printf(" ");
1147}
1148
1149/* The ip6tables looks up the /etc/protocols. */
1150static void print_proto(u_int16_t proto, int invert)
1151{
1152 if (proto) {
1153 unsigned int i;
1154 const char *invertstr = invert ? "! " : "";
1155
1156 struct protoent *pent = getprotobynumber(proto);
1157 if (pent) {
1158 printf("-p %s%s ",
1159 invertstr, pent->p_name);
1160 return;
1161 }
1162
1163 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
1164 if (chain_protos[i].num == proto) {
1165 printf("-p %s%s ",
1166 invertstr, chain_protos[i].name);
1167 return;
1168 }
1169
1170 printf("-p %s%u ", invertstr, proto);
1171 }
1172}
1173
1174static int print_match_save(const struct ip6t_entry_match *e,
1175 const struct ip6t_ip6 *ip)
1176{
1177 struct xtables_match *match
1178 = find_match(e->u.user.name, TRY_LOAD, NULL);
1179
1180 if (match) {
1181 printf("-m %s ", e->u.user.name);
1182
1183 /* some matches don't provide a save function */
1184 if (match->save)
1185 match->save(ip, e);
1186 } else {
1187 if (e->u.match_size) {
1188 fprintf(stderr,
1189 "Can't find library for match `%s'\n",
1190 e->u.user.name);
1191 exit(1);
1192 }
1193 }
1194 return 0;
1195}
1196
1197/* print a given ip including mask if neccessary */
1198static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1199{
1200 char buf[51];
1201 int l = ipv6_prefix_length(mask);
1202
1203 if (l == 0 && !invert)
1204 return;
1205
1206 printf("%s %s%s",
1207 prefix,
1208 invert ? "! " : "",
1209 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1210
1211 if (l == -1)
1212 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1213 else
1214 printf("/%d ", l);
1215}
1216
1217/* We want this to be readable, so only print out neccessary fields.
1218 * Because that's the kind of world I want to live in. */
1219void print_rule(const struct ip6t_entry *e,
1220 ip6tc_handle_t *h, const char *chain, int counters)
1221{
1222 struct ip6t_entry_target *t;
1223 const char *target_name;
1224
1225 /* print counters for iptables-save */
1226 if (counters > 0)
1227 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1228
1229 /* print chain name */
1230 printf("-A %s ", chain);
1231
1232 /* Print IP part. */
1233 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1234 e->ipv6.invflags & IP6T_INV_SRCIP);
1235
1236 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1237 e->ipv6.invflags & IP6T_INV_DSTIP);
1238
1239 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1240 e->ipv6.invflags & IP6T_INV_VIA_IN);
1241
1242 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1243 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1244
1245 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1246
1247#if 0
1248 /* not definied in ipv6
1249 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1250 if (e->ipv6.flags & IPT_F_FRAG)
1251 printf("%s-f ",
1252 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1253#endif
1254
1255 if (e->ipv6.flags & IP6T_F_TOS)
1256 printf("%s-? %d ",
1257 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1258 e->ipv6.tos);
1259
1260 /* Print matchinfo part */
1261 if (e->target_offset) {
1262 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1263 }
1264
1265 /* print counters for iptables -R */
1266 if (counters < 0)
1267 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1268
1269 /* Print target name */
1270 target_name = ip6tc_get_target(e, h);
1271 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001272#ifdef IP6T_F_GOTO
1273 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1274#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001275 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001276#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001277
1278 /* Print targinfo part */
1279 t = ip6t_get_target((struct ip6t_entry *)e);
1280 if (t->u.user.name[0]) {
1281 struct xtables_target *target
1282 = find_target(t->u.user.name, TRY_LOAD);
1283
1284 if (!target) {
1285 fprintf(stderr, "Can't find library for target `%s'\n",
1286 t->u.user.name);
1287 exit(1);
1288 }
1289
1290 if (target->save)
1291 target->save(&e->ipv6, t);
1292 else {
1293 /* If the target size is greater than ip6t_entry_target
1294 * there is something to be saved, we just don't know
1295 * how to print it */
1296 if (t->u.target_size !=
1297 sizeof(struct ip6t_entry_target)) {
1298 fprintf(stderr, "Target `%s' is missing "
1299 "save function\n",
1300 t->u.user.name);
1301 exit(1);
1302 }
1303 }
1304 }
1305 printf("\n");
1306}
1307
1308static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001309list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001310 ip6tc_handle_t *handle)
1311{
1312 const char *this = NULL;
1313 int found = 0;
1314
1315 if (counters)
1316 counters = -1; /* iptables -c format */
1317
1318 /* Dump out chain names first,
1319 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001320 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001321 this;
1322 this = ip6tc_next_chain(handle)) {
1323 if (chain && strcmp(this, chain) != 0)
1324 continue;
1325
1326 if (ip6tc_builtin(this, *handle)) {
1327 struct ip6t_counters count;
1328 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1329 if (counters)
1330 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1331 printf("\n");
1332 } else {
1333 printf("-N %s\n", this);
1334 }
1335 }
1336
1337 for (this = ip6tc_first_chain(handle);
1338 this;
1339 this = ip6tc_next_chain(handle)) {
1340 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001341 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001342
1343 if (chain && strcmp(this, chain) != 0)
1344 continue;
1345
1346 /* Dump out rules */
1347 e = ip6tc_first_rule(this, handle);
1348 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001349 num++;
1350 if (!rulenum || num == rulenum)
1351 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001352 e = ip6tc_next_rule(e, handle);
1353 }
1354 found = 1;
1355 }
1356
1357 errno = ENOENT;
1358 return found;
1359}
1360
Rusty Russell5eed48a2000-06-02 20:12:24 +00001361static struct ip6t_entry *
1362generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001363 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 struct ip6t_entry_target *target)
1365{
1366 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001367 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001368 struct ip6t_entry *e;
1369
1370 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001371 for (matchp = matches; matchp; matchp = matchp->next)
1372 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001373
1374 e = fw_malloc(size + target->u.target_size);
1375 *e = *fw;
1376 e->target_offset = size;
1377 e->next_offset = size + target->u.target_size;
1378
1379 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001380 for (matchp = matches; matchp; matchp = matchp->next) {
1381 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1382 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001383 }
1384 memcpy(e->elems + size, target, target->u.target_size);
1385
1386 return e;
1387}
1388
Jan Engelhardt33690a12008-02-11 00:54:00 +01001389static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001390{
1391 struct ip6tables_rule_match *matchp, *tmp;
1392
1393 for (matchp = *matches; matchp;) {
1394 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001395 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001396 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001397 matchp->match->m = NULL;
1398 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001399 if (matchp->match == matchp->match->next) {
1400 free(matchp->match);
1401 matchp->match = NULL;
1402 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001403 free(matchp);
1404 matchp = tmp;
1405 }
1406
1407 *matches = NULL;
1408}
1409
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001410static void set_revision(char *name, u_int8_t revision)
1411{
1412 /* Old kernel sources don't have ".revision" field,
1413 but we stole a byte from name. */
1414 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1415 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1416}
1417
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1419{
1420 struct ip6t_entry fw, *e = NULL;
1421 int invert = 0;
1422 unsigned int nsaddrs = 0, ndaddrs = 0;
1423 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1424
1425 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001426 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 const char *chain = NULL;
1428 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1429 const char *policy = NULL, *newname = NULL;
1430 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001431 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001433 struct xtables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001434 struct ip6tables_rule_match *matches = NULL;
1435 struct ip6tables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001436 struct xtables_target *target = NULL;
1437 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001438 const char *jumpto = "";
1439 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001440 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001441 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001442
1443 memset(&fw, 0, sizeof(fw));
1444
Harald Welte43ac1912002-03-03 09:43:07 +00001445 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001446 * a second time */
1447 optind = 0;
1448
Harald Welte43ac1912002-03-03 09:43:07 +00001449 /* clear mflags in case do_command gets called a second time
1450 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001451 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001452 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001453
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001454 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001455 t->tflags = 0;
1456 t->used = 0;
1457 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001458
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459 /* Suppress error messages: we may add new options if we
1460 demand-load a protocol. */
1461 opterr = 0;
1462
1463 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001464 "-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 +00001465 opts, NULL)) != -1) {
1466 switch (c) {
1467 /*
1468 * Command selection
1469 */
1470 case 'A':
1471 add_command(&command, CMD_APPEND, CMD_NONE,
1472 invert);
1473 chain = optarg;
1474 break;
1475
1476 case 'D':
1477 add_command(&command, CMD_DELETE, CMD_NONE,
1478 invert);
1479 chain = optarg;
1480 if (optind < argc && argv[optind][0] != '-'
1481 && argv[optind][0] != '!') {
1482 rulenum = parse_rulenumber(argv[optind++]);
1483 command = CMD_DELETE_NUM;
1484 }
1485 break;
1486
Rusty Russell5eed48a2000-06-02 20:12:24 +00001487 case 'R':
1488 add_command(&command, CMD_REPLACE, CMD_NONE,
1489 invert);
1490 chain = optarg;
1491 if (optind < argc && argv[optind][0] != '-'
1492 && argv[optind][0] != '!')
1493 rulenum = parse_rulenumber(argv[optind++]);
1494 else
1495 exit_error(PARAMETER_PROBLEM,
1496 "-%c requires a rule number",
1497 cmd2char(CMD_REPLACE));
1498 break;
1499
1500 case 'I':
1501 add_command(&command, CMD_INSERT, CMD_NONE,
1502 invert);
1503 chain = optarg;
1504 if (optind < argc && argv[optind][0] != '-'
1505 && argv[optind][0] != '!')
1506 rulenum = parse_rulenumber(argv[optind++]);
1507 else rulenum = 1;
1508 break;
1509
1510 case 'L':
1511 add_command(&command, CMD_LIST, CMD_ZERO,
1512 invert);
1513 if (optarg) chain = optarg;
1514 else if (optind < argc && argv[optind][0] != '-'
1515 && argv[optind][0] != '!')
1516 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001517 if (optind < argc && argv[optind][0] != '-'
1518 && argv[optind][0] != '!')
1519 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001520 break;
1521
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001522 case 'S':
1523 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1524 invert);
1525 if (optarg) chain = optarg;
1526 else if (optind < argc && argv[optind][0] != '-'
1527 && argv[optind][0] != '!')
1528 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001529 if (optind < argc && argv[optind][0] != '-'
1530 && argv[optind][0] != '!')
1531 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001532 break;
1533
Rusty Russell5eed48a2000-06-02 20:12:24 +00001534 case 'F':
1535 add_command(&command, CMD_FLUSH, CMD_NONE,
1536 invert);
1537 if (optarg) chain = optarg;
1538 else if (optind < argc && argv[optind][0] != '-'
1539 && argv[optind][0] != '!')
1540 chain = argv[optind++];
1541 break;
1542
1543 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001544 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001545 invert);
1546 if (optarg) chain = optarg;
1547 else if (optind < argc && argv[optind][0] != '-'
1548 && argv[optind][0] != '!')
1549 chain = argv[optind++];
1550 break;
1551
1552 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001553 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001554 exit_error(PARAMETER_PROBLEM,
1555 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001556 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001557 if (find_target(optarg, TRY_LOAD))
1558 exit_error(PARAMETER_PROBLEM,
1559 "chain name may not clash "
1560 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1562 invert);
1563 chain = optarg;
1564 break;
1565
1566 case 'X':
1567 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1568 invert);
1569 if (optarg) chain = optarg;
1570 else if (optind < argc && argv[optind][0] != '-'
1571 && argv[optind][0] != '!')
1572 chain = argv[optind++];
1573 break;
1574
1575 case 'E':
1576 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1577 invert);
1578 chain = optarg;
1579 if (optind < argc && argv[optind][0] != '-'
1580 && argv[optind][0] != '!')
1581 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001582 else
1583 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001584 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001585 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001586 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587 break;
1588
1589 case 'P':
1590 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1591 invert);
1592 chain = optarg;
1593 if (optind < argc && argv[optind][0] != '-'
1594 && argv[optind][0] != '!')
1595 policy = argv[optind++];
1596 else
1597 exit_error(PARAMETER_PROBLEM,
1598 "-%c requires a chain and a policy",
1599 cmd2char(CMD_SET_POLICY));
1600 break;
1601
1602 case 'h':
1603 if (!optarg)
1604 optarg = argv[optind];
1605
Jonas Berlin1b91e592005-04-01 06:58:38 +00001606 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001607 if (!matches && protocol)
1608 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001609
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001610 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611
1612 /*
1613 * Option selection
1614 */
1615 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001616 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1618 invert);
1619
1620 /* Canonicalize into lower case */
1621 for (protocol = argv[optind-1]; *protocol; protocol++)
1622 *protocol = tolower(*protocol);
1623
1624 protocol = argv[optind-1];
1625 fw.ipv6.proto = parse_protocol(protocol);
1626 fw.ipv6.flags |= IP6T_F_PROTO;
1627
1628 if (fw.ipv6.proto == 0
1629 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1630 exit_error(PARAMETER_PROBLEM,
1631 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001632
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001633 if (is_exthdr(fw.ipv6.proto)
1634 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001635 fprintf(stderr,
1636 "Warning: never matched protocol: %s. "
1637 "use extension match instead.\n",
1638 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001639 break;
1640
1641 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001642 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1644 invert);
1645 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001646 break;
1647
1648 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001649 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1651 invert);
1652 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653 break;
1654
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001655#ifdef IP6T_F_GOTO
1656 case 'g':
1657 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1658 invert);
1659 fw.ipv6.flags |= IP6T_F_GOTO;
1660 jumpto = parse_target(optarg);
1661 break;
1662#endif
1663
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 case 'j':
1665 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1666 invert);
1667 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001668 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001669 target = find_target(jumpto, TRY_LOAD);
1670
1671 if (target) {
1672 size_t size;
1673
Harald Welte43ac1912002-03-03 09:43:07 +00001674 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1675 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001676
1677 target->t = fw_calloc(1, size);
1678 target->t->u.target_size = size;
1679 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001680 set_revision(target->t->u.user.name,
1681 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001682 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001683 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001684 opts = merge_options(opts,
1685 target->extra_opts,
1686 &target->option_offset);
1687 if (opts == NULL)
1688 exit_error(OTHER_PROBLEM,
1689 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001690 }
1691 break;
1692
1693
1694 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001695 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001696 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1697 invert);
1698 parse_interface(argv[optind-1],
1699 fw.ipv6.iniface,
1700 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001701 break;
1702
1703 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001704 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001705 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1706 invert);
1707 parse_interface(argv[optind-1],
1708 fw.ipv6.outiface,
1709 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 break;
1711
1712 case 'v':
1713 if (!verbose)
1714 set_option(&options, OPT_VERBOSE,
1715 &fw.ipv6.invflags, invert);
1716 verbose++;
1717 break;
1718
1719 case 'm': {
1720 size_t size;
1721
1722 if (invert)
1723 exit_error(PARAMETER_PROBLEM,
1724 "unexpected ! flag before --match");
1725
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001726 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001727 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1728 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001729 m->m = fw_calloc(1, size);
1730 m->m->u.match_size = size;
1731 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001732 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001733 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001734 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001735 if (m != m->next)
1736 /* Merge options for non-cloned matches */
1737 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001738 }
1739 break;
1740
1741 case 'n':
1742 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1743 invert);
1744 break;
1745
1746 case 't':
1747 if (invert)
1748 exit_error(PARAMETER_PROBLEM,
1749 "unexpected ! flag before --table");
1750 *table = argv[optind-1];
1751 break;
1752
1753 case 'x':
1754 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1755 invert);
1756 break;
1757
1758 case 'V':
1759 if (invert)
1760 printf("Not %s ;-)\n", program_version);
1761 else
1762 printf("%s v%s\n",
1763 program_name, program_version);
1764 exit(0);
1765
1766 case '0':
1767 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1768 invert);
1769 break;
1770
Harald Welte43ac1912002-03-03 09:43:07 +00001771 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001772 modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001773 break;
1774
1775 case 'c':
1776
1777 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1778 invert);
1779 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001780 bcnt = strchr(pcnt + 1, ',');
1781 if (bcnt)
1782 bcnt++;
1783 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001784 && argv[optind][0] != '!')
1785 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001786 if (!bcnt)
Harald Welte43ac1912002-03-03 09:43:07 +00001787 exit_error(PARAMETER_PROBLEM,
1788 "-%c requires packet and byte counter",
1789 opt2char(OPT_COUNTERS));
1790
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001791 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001792 exit_error(PARAMETER_PROBLEM,
1793 "-%c packet counter not numeric",
1794 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001795 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001796
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001797 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001798 exit_error(PARAMETER_PROBLEM,
1799 "-%c byte counter not numeric",
1800 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001801 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001802 break;
1803
Rusty Russell5eed48a2000-06-02 20:12:24 +00001804 case 1: /* non option */
1805 if (optarg[0] == '!' && optarg[1] == '\0') {
1806 if (invert)
1807 exit_error(PARAMETER_PROBLEM,
1808 "multiple consecutive ! not"
1809 " allowed");
1810 invert = TRUE;
1811 optarg[0] = '\0';
1812 continue;
1813 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001814 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001815 exit_tryhelp(2);
1816
1817 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001818 if (!target
1819 || !(target->parse(c - target->option_offset,
1820 argv, invert,
1821 &target->tflags,
1822 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001823 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001824 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001825 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001826 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001827 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001828 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001829 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001830 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001831 break;
1832 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001833 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001834
1835 /* If you listen carefully, you can
1836 actually hear this code suck. */
1837
1838 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001839 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001840 * parameter, that has not been parsed yet,
1841 * it's not an option of an explicitly loaded
1842 * match or a target. However, we support
1843 * implicit loading of the protocol match
1844 * extension. '-p tcp' means 'l4 proto 6' and
1845 * at the same time 'load tcp protocol match on
1846 * demand if we specify --dport'.
1847 *
1848 * To make this work, we need to make sure:
1849 * - the parameter has not been parsed by
1850 * a match (m above)
1851 * - a protocol has been specified
1852 * - the protocol extension has not been
1853 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001854 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001855 * - the protocol extension can be successively
1856 * loaded
1857 */
1858 if (m == NULL
1859 && protocol
1860 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001861 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001862 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001863 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001864 && (proto_used == 0))
1865 )
1866 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001867 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001868 /* Try loading protocol */
1869 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001870
Harald Welte00f5a9c2002-08-26 14:37:35 +00001871 proto_used = 1;
1872
1873 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1874 + m->size;
1875
1876 m->m = fw_calloc(1, size);
1877 m->m->u.match_size = size;
1878 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001879 set_revision(m->m->u.user.name,
1880 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001881 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001882 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001883
1884 opts = merge_options(opts,
1885 m->extra_opts, &m->option_offset);
1886
1887 optind--;
1888 continue;
1889 }
1890
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891 if (!m)
1892 exit_error(PARAMETER_PROBLEM,
1893 "Unknown arg `%s'",
1894 argv[optind-1]);
1895 }
1896 }
1897 invert = FALSE;
1898 }
1899
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001900 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001901 if (matchp->match->final_check != NULL)
1902 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001903
Jan Engelhardt830132a2007-10-04 16:24:50 +00001904 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905 target->final_check(target->tflags);
1906
1907 /* Fix me: must put inverse options checking here --MN */
1908
1909 if (optind < argc)
1910 exit_error(PARAMETER_PROBLEM,
1911 "unknown arguments found on commandline");
1912 if (!command)
1913 exit_error(PARAMETER_PROBLEM, "no command specified");
1914 if (invert)
1915 exit_error(PARAMETER_PROBLEM,
1916 "nothing appropriate following !");
1917
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001918 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001919 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001920 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001922 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001923 }
1924
1925 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001926 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1927 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928
1929 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001930 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1931 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932
1933 if ((nsaddrs > 1 || ndaddrs > 1) &&
1934 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1935 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1936 " source or destination IP addresses");
1937
Rusty Russell5eed48a2000-06-02 20:12:24 +00001938 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1939 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1940 "specify a unique address");
1941
1942 generic_opt_check(command, options);
1943
1944 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1945 exit_error(PARAMETER_PROBLEM,
1946 "chain name `%s' too long (must be under %i chars)",
1947 chain, IP6T_FUNCTION_MAXNAMELEN);
1948
Harald Welte43ac1912002-03-03 09:43:07 +00001949 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001950 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001951 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001952
Rusty Russell8beb0492004-12-22 00:37:10 +00001953 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001954 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001955 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001956
Harald Welte43ac1912002-03-03 09:43:07 +00001957 if (!*handle)
1958 exit_error(VERSION_PROBLEM,
1959 "can't initialize ip6tables table `%s': %s",
1960 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001961
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001962 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 || command == CMD_DELETE
1964 || command == CMD_INSERT
1965 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001966 if (strcmp(chain, "PREROUTING") == 0
1967 || strcmp(chain, "INPUT") == 0) {
1968 /* -o not valid with incoming packets. */
1969 if (options & OPT_VIANAMEOUT)
1970 exit_error(PARAMETER_PROBLEM,
1971 "Can't use -%c with %s\n",
1972 opt2char(OPT_VIANAMEOUT),
1973 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001974 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975
Harald Welte43ac1912002-03-03 09:43:07 +00001976 if (strcmp(chain, "POSTROUTING") == 0
1977 || strcmp(chain, "OUTPUT") == 0) {
1978 /* -i not valid with outgoing packets */
1979 if (options & OPT_VIANAMEIN)
1980 exit_error(PARAMETER_PROBLEM,
1981 "Can't use -%c with %s\n",
1982 opt2char(OPT_VIANAMEIN),
1983 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001984 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985
1986 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001987 fprintf(stderr,
1988 "Warning: using chain %s, not extension\n",
1989 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001991 if (target->t)
1992 free(target->t);
1993
Rusty Russell5eed48a2000-06-02 20:12:24 +00001994 target = NULL;
1995 }
1996
1997 /* If they didn't specify a target, or it's a chain
1998 name, use standard. */
1999 if (!target
2000 && (strlen(jumpto) == 0
2001 || ip6tc_is_chain(jumpto, *handle))) {
2002 size_t size;
2003
2004 target = find_target(IP6T_STANDARD_TARGET,
2005 LOAD_MUST_SUCCEED);
2006
2007 size = sizeof(struct ip6t_entry_target)
2008 + target->size;
2009 target->t = fw_calloc(1, size);
2010 target->t->u.target_size = size;
2011 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002012 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00002013 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002014 }
2015
2016 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002017 /* it is no chain, and we can't load a plugin.
2018 * We cannot know if the plugin is corrupt, non
2019 * existant OR if the user just misspelled a
2020 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02002021#ifdef IP6T_F_GOTO
2022 if (fw.ipv6.flags & IP6T_F_GOTO)
2023 exit_error(PARAMETER_PROBLEM,
2024 "goto '%s' is not a chain\n", jumpto);
2025#endif
Harald Welte43ac1912002-03-03 09:43:07 +00002026 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002027 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002028 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002029 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002030 }
2031 }
2032
2033 switch (command) {
2034 case CMD_APPEND:
2035 ret = append_entry(chain, e,
2036 nsaddrs, saddrs, ndaddrs, daddrs,
2037 options&OPT_VERBOSE,
2038 handle);
2039 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002040 case CMD_DELETE:
2041 ret = delete_entry(chain, e,
2042 nsaddrs, saddrs, ndaddrs, daddrs,
2043 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002044 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002045 break;
2046 case CMD_DELETE_NUM:
2047 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2048 break;
2049 case CMD_REPLACE:
2050 ret = replace_entry(chain, e, rulenum - 1,
2051 saddrs, daddrs, options&OPT_VERBOSE,
2052 handle);
2053 break;
2054 case CMD_INSERT:
2055 ret = insert_entry(chain, e, rulenum - 1,
2056 nsaddrs, saddrs, ndaddrs, daddrs,
2057 options&OPT_VERBOSE,
2058 handle);
2059 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002060 case CMD_FLUSH:
2061 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2062 break;
2063 case CMD_ZERO:
2064 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2065 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002066 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002067 case CMD_LIST|CMD_ZERO:
2068 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002069 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002070 options&OPT_VERBOSE,
2071 options&OPT_NUMERIC,
2072 options&OPT_EXPANDED,
2073 options&OPT_LINENUMBERS,
2074 handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002075 if (ret && (command & CMD_ZERO))
2076 ret = zero_entries(chain,
2077 options&OPT_VERBOSE, handle);
2078 break;
2079 case CMD_LIST_RULES:
2080 case CMD_LIST_RULES|CMD_ZERO:
2081 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002082 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002083 options&OPT_VERBOSE,
2084 handle);
2085 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002086 ret = zero_entries(chain,
2087 options&OPT_VERBOSE, handle);
2088 break;
2089 case CMD_NEW_CHAIN:
2090 ret = ip6tc_create_chain(chain, handle);
2091 break;
2092 case CMD_DELETE_CHAIN:
2093 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2094 break;
2095 case CMD_RENAME_CHAIN:
2096 ret = ip6tc_rename_chain(chain, newname, handle);
2097 break;
2098 case CMD_SET_POLICY:
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +02002099 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002100 break;
2101 default:
2102 /* We should never reach this... */
2103 exit_tryhelp(2);
2104 }
2105
2106 if (verbose > 1)
2107 dump_entries6(*handle);
2108
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002109 clear_rule_matches(&matches);
2110
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002111 if (e != NULL) {
2112 free(e);
2113 e = NULL;
2114 }
2115
Max Kellermann9ee386a2008-01-29 13:48:05 +00002116 for (i = 0; i < nsaddrs; i++)
2117 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002118
Max Kellermann9ee386a2008-01-29 13:48:05 +00002119 for (i = 0; i < ndaddrs; i++)
2120 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002121
Pablo Neiradfdcd642005-05-29 19:05:23 +00002122 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002123
Rusty Russell5eed48a2000-06-02 20:12:24 +00002124 return ret;
2125}