blob: 606de3c990ff299e3a1e0d9a322412c082aabb43 [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'},
135 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000136};
137
Harald Weltea8658ca2003-03-05 07:46:15 +0000138/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
139 * current line of the input file, in order to give a more precise error
140 * message. ip6tables itself doesn't need this, so it is initialized to the
141 * magic number of -1 */
142int line = -1;
143
Rusty Russell5eed48a2000-06-02 20:12:24 +0000144static struct option *opts = original_opts;
145static unsigned int global_option_offset = 0;
146
147/* Table of legal combinations of commands and options. If any of the
148 * given commands make an option legal, that option is legal (applies to
149 * CMD_LIST and CMD_ZERO only).
150 * Key:
151 * + compulsory
152 * x illegal
153 * optional
154 */
155
156static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
157/* Well, it's better than "Re: Linux vs FreeBSD" */
158{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200159 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000160/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
161/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
162/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
163/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
164/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
165/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
166/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200170/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200171/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000173};
174
175static int inverse_for_options[NUMBER_OF_OPT] =
176{
177/* -n */ 0,
178/* -s */ IP6T_INV_SRCIP,
179/* -d */ IP6T_INV_DSTIP,
180/* -p */ IP6T_INV_PROTO,
181/* -j */ 0,
182/* -v */ 0,
183/* -x */ 0,
184/* -i */ IP6T_INV_VIA_IN,
185/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000186/*--line*/ 0,
187/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000188};
189
190const char *program_version;
191const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000192
Rusty Russell5eed48a2000-06-02 20:12:24 +0000193/* A few hardcoded protocols for 'all' and in case the user has no
194 /etc/protocols */
195struct pprot {
196 char *name;
197 u_int8_t num;
198};
199
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000200struct afinfo afinfo = {
201 .family = AF_INET6,
202 .libprefix = "libip6t_",
203 .ipproto = IPPROTO_IPV6,
204 .kmod = "ip6_tables",
205 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
206 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
207};
208
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000209/* Primitive headers... */
210/* defined in netinet/in.h */
211#if 0
212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
218#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000219#ifndef IPPROTO_MH
220#define IPPROTO_MH 135
221#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000222
Rusty Russell5eed48a2000-06-02 20:12:24 +0000223static const struct pprot chain_protos[] = {
224 { "tcp", IPPROTO_TCP },
225 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000226 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000227 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000228 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000231 { "ipv6-mh", IPPROTO_MH },
232 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000233 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000234};
235
236static char *
237proto_to_name(u_int8_t proto, int nolookup)
238{
239 unsigned int i;
240
241 if (proto && !nolookup) {
242 struct protoent *pent = getprotobynumber(proto);
243 if (pent)
244 return pent->p_name;
245 }
246
247 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
248 if (chain_protos[i].num == proto)
249 return chain_protos[i].name;
250
251 return NULL;
252}
253
Pablo Neiradfdcd642005-05-29 19:05:23 +0000254static void free_opts(int reset_offset)
255{
256 if (opts != original_opts) {
257 free(opts);
258 opts = original_opts;
259 if (reset_offset)
260 global_option_offset = 0;
261 }
262}
263
Patrick McHardy0b639362007-09-08 16:00:01 +0000264static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000265exit_tryhelp(int status)
266{
Harald Weltea8658ca2003-03-05 07:46:15 +0000267 if (line != -1)
268 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000269 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
270 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000271 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272 exit(status);
273}
274
Patrick McHardy0b639362007-09-08 16:00:01 +0000275static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000276exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000277{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000278 struct ip6tables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200279 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280
281 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000282"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283" %s -[RI] chain rulenum rule-specification [options]\n"
284" %s -D chain rulenum [options]\n"
285" %s -[LFZ] [chain] [options]\n"
286" %s -[NX] chain\n"
287" %s -E old-chain-name new-chain-name\n"
288" %s -P chain target [options]\n"
289" %s -h (print this help information)\n\n",
290 program_name, program_version, program_name, program_name,
291 program_name, program_name, program_name, program_name,
292 program_name, program_name);
293
294 printf(
295"Commands:\n"
296"Either long or short options are allowed.\n"
297" --append -A chain Append to chain\n"
298" --delete -D chain Delete matching rule from chain\n"
299" --delete -D chain rulenum\n"
300" Delete rule rulenum (1 = first) from chain\n"
301" --insert -I chain [rulenum]\n"
302" Insert in chain as rulenum (default 1=first)\n"
303" --replace -R chain rulenum\n"
304" Replace rule rulenum (1 = first) in chain\n"
305" --list -L [chain] List the rules in a chain or all chains\n"
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200306" --list-rules\n"
307" -S [chain] Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000308" --flush -F [chain] Delete all rules in chain or all chains\n"
309" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000310" --new -N chain Create a new user-defined chain\n"
311" --delete-chain\n"
312" -X [chain] Delete a user-defined chain\n"
313" --policy -P chain target\n"
314" Change policy on chain to target\n"
315" --rename-chain\n"
316" -E old-chain new-chain\n"
317" Change chain name, (moving any references)\n"
318
319"Options:\n"
320" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
321" --source -s [!] address[/mask]\n"
322" source specification\n"
323" --destination -d [!] address[/mask]\n"
324" destination specification\n"
325" --in-interface -i [!] input name[+]\n"
326" network interface name ([+] for wildcard)\n"
327" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000328" target for rule (may load target extension)\n"
329" --match -m match\n"
330" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000331" --numeric -n numeric output of addresses and ports\n"
332" --out-interface -o [!] output name[+]\n"
333" network interface name ([+] for wildcard)\n"
334" --table -t table table to manipulate (default: `filter')\n"
335" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000336" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000337" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000338/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000339" --modprobe=<command> try to insert modules using this command\n"
340" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000341"[!] --version -V print package version.\n");
342
Max Kellermann5b76f682008-01-29 13:42:48 +0000343 /* Print out any special helps. A user might like to be able to add a --help
344 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000345 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000346 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000347 if (t->used) {
348 printf("\n");
349 t->help();
350 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000351 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000352 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000353 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000354 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000355 }
356 exit(0);
357}
358
Patrick McHardy0b639362007-09-08 16:00:01 +0000359void
360exit_error(enum exittype status, const char *msg, ...)
361{
362 va_list args;
363
364 va_start(args, msg);
365 fprintf(stderr, "%s v%s: ", program_name, program_version);
366 vfprintf(stderr, msg, args);
367 va_end(args);
368 fprintf(stderr, "\n");
369 if (status == PARAMETER_PROBLEM)
370 exit_tryhelp(status);
371 if (status == VERSION_PROBLEM)
372 fprintf(stderr,
373 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
374 /* On error paths, make sure that we don't leak memory */
375 free_opts(1);
376 exit(status);
377}
378
Rusty Russell5eed48a2000-06-02 20:12:24 +0000379static void
380generic_opt_check(int command, int options)
381{
382 int i, j, legal = 0;
383
384 /* Check that commands are valid with options. Complicated by the
385 * fact that if an option is legal with *any* command given, it is
386 * legal overall (ie. -z and -l).
387 */
388 for (i = 0; i < NUMBER_OF_OPT; i++) {
389 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
390
391 for (j = 0; j < NUMBER_OF_CMD; j++) {
392 if (!(command & (1<<j)))
393 continue;
394
395 if (!(options & (1<<i))) {
396 if (commands_v_options[j][i] == '+')
397 exit_error(PARAMETER_PROBLEM,
398 "You need to supply the `-%c' "
399 "option for this command\n",
400 optflags[i]);
401 } else {
402 if (commands_v_options[j][i] != 'x')
403 legal = 1;
404 else if (legal == 0)
405 legal = -1;
406 }
407 }
408 if (legal == -1)
409 exit_error(PARAMETER_PROBLEM,
410 "Illegal option `-%c' with this command\n",
411 optflags[i]);
412 }
413}
414
415static char
416opt2char(int option)
417{
418 const char *ptr;
419 for (ptr = optflags; option > 1; option >>= 1, ptr++);
420
421 return *ptr;
422}
423
424static char
425cmd2char(int option)
426{
427 const char *ptr;
428 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
429
430 return *ptr;
431}
432
433static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000434add_command(unsigned int *cmd, const int newcmd, const int othercmds,
435 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000436{
437 if (invert)
438 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
439 if (*cmd & (~othercmds))
440 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
441 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
442 *cmd |= newcmd;
443}
444
445int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100446check_inverse(const char option[], int *invert, int *my_optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000447{
448 if (option && strcmp(option, "!") == 0) {
449 if (*invert)
450 exit_error(PARAMETER_PROBLEM,
451 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000452 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100453 if (my_optind != NULL) {
454 ++*my_optind;
455 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000456 exit_error(PARAMETER_PROBLEM,
457 "no argument following `!'");
458 }
459
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460 return TRUE;
461 }
462 return FALSE;
463}
464
Rusty Russell5eed48a2000-06-02 20:12:24 +0000465/*
466 * All functions starting with "parse" should succeed, otherwise
467 * the program fails.
468 * Most routines return pointers to static data that may change
469 * between calls to the same or other routines with a few exceptions:
470 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
471 * return global static data.
472*/
473
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200475static struct xtables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000476find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477{
Harald Welteed498492001-07-23 01:24:22 +0000478 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000479
Harald Welte43ac1912002-03-03 09:43:07 +0000480 if (string_to_number(pname, 0, 255, &proto) != -1) {
481 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000482
Harald Welte43ac1912002-03-03 09:43:07 +0000483 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000484 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000485 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000486 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000487
488 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489}
490
Harald Welte43ac1912002-03-03 09:43:07 +0000491u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000492parse_protocol(const char *s)
493{
Harald Welteed498492001-07-23 01:24:22 +0000494 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000495
Harald Welteed498492001-07-23 01:24:22 +0000496 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000497 struct protoent *pent;
498
Harald Weltecbe1ec72006-02-11 09:50:11 +0000499 /* first deal with the special case of 'all' to prevent
500 * people from being able to redefine 'all' in nsswitch
Max Kellermann5b76f682008-01-29 13:42:48 +0000501 * and/or provoke expensive [not working] ldap/nis/...
Harald Weltecbe1ec72006-02-11 09:50:11 +0000502 * lookups */
503 if (!strcmp(s, "all"))
504 return 0;
505
Rusty Russell5eed48a2000-06-02 20:12:24 +0000506 if ((pent = getprotobyname(s)))
507 proto = pent->p_proto;
508 else {
509 unsigned int i;
510 for (i = 0;
511 i < sizeof(chain_protos)/sizeof(struct pprot);
512 i++) {
513 if (strcmp(s, chain_protos[i].name) == 0) {
514 proto = chain_protos[i].num;
515 break;
516 }
517 }
518 if (i == sizeof(chain_protos)/sizeof(struct pprot))
519 exit_error(PARAMETER_PROBLEM,
520 "unknown protocol `%s' specified",
521 s);
522 }
523 }
524
525 return (u_int16_t)proto;
526}
527
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000528/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000529static int is_exthdr(u_int16_t proto)
530{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000531 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000532 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000533 proto == IPPROTO_AH ||
534 proto == IPPROTO_DSTOPTS);
535}
536
Rusty Russell5eed48a2000-06-02 20:12:24 +0000537/* Can't be zero. */
538static int
539parse_rulenumber(const char *rule)
540{
Harald Welte43ac1912002-03-03 09:43:07 +0000541 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000542
Harald Welte43ac1912002-03-03 09:43:07 +0000543 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000544 exit_error(PARAMETER_PROBLEM,
545 "Invalid rule number `%s'", rule);
546
547 return rulenum;
548}
549
550static const char *
551parse_target(const char *targetname)
552{
553 const char *ptr;
554
555 if (strlen(targetname) < 1)
556 exit_error(PARAMETER_PROBLEM,
557 "Invalid target name (too short)");
558
559 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
560 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000561 "Invalid target name `%s' (%u chars max)",
562 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000563
564 for (ptr = targetname; *ptr; ptr++)
565 if (isspace(*ptr))
566 exit_error(PARAMETER_PROBLEM,
567 "Invalid target name `%s'", targetname);
568 return targetname;
569}
570
Rusty Russell5eed48a2000-06-02 20:12:24 +0000571static void
572set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
573 int invert)
574{
575 if (*options & option)
576 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
577 opt2char(option));
578 *options |= option;
579
580 if (invert) {
581 unsigned int i;
582 for (i = 0; 1 << i != option; i++);
583
584 if (!inverse_for_options[i])
585 exit_error(PARAMETER_PROBLEM,
586 "cannot have ! before -%c",
587 opt2char(option));
588 *invflg |= inverse_for_options[i];
589 }
590}
591
Rusty Russell5eed48a2000-06-02 20:12:24 +0000592static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000593merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000594 unsigned int *option_offset)
595{
596 unsigned int num_old, num_new, i;
597 struct option *merge;
598
Jan Engelhardtd0145402007-07-30 14:32:26 +0000599 if (newopts == NULL)
600 return oldopts;
601
Rusty Russell5eed48a2000-06-02 20:12:24 +0000602 for (num_old = 0; oldopts[num_old].name; num_old++);
603 for (num_new = 0; newopts[num_new].name; num_new++);
604
605 global_option_offset += OPTION_OFFSET;
606 *option_offset = global_option_offset;
607
608 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
609 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000610 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000611 for (i = 0; i < num_new; i++) {
612 merge[num_old + i] = newopts[i];
613 merge[num_old + i].val += *option_offset;
614 }
615 memset(merge + num_old + num_new, 0, sizeof(struct option));
616
617 return merge;
618}
619
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000620static void
621print_num(u_int64_t number, unsigned int format)
622{
Harald Welte43ac1912002-03-03 09:43:07 +0000623 if (format & FMT_KILOMEGAGIGA) {
624 if (number > 99999) {
625 number = (number + 500) / 1000;
626 if (number > 9999) {
627 number = (number + 500) / 1000;
628 if (number > 9999) {
629 number = (number + 500) / 1000;
630 if (number > 9999) {
631 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000632 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000633 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000634 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000635 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000636 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000637 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000638 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000639 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000640 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000641 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000642 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000643}
644
Harald Welte43ac1912002-03-03 09:43:07 +0000645
Rusty Russell5eed48a2000-06-02 20:12:24 +0000646static void
647print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
648{
649 struct ip6t_counters counters;
650 const char *pol = ip6tc_get_policy(chain, &counters, handle);
651 printf("Chain %s", chain);
652 if (pol) {
653 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000654 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000655 fputc(' ', stdout);
656 print_num(counters.pcnt, (format|FMT_NOTABLE));
657 fputs("packets, ", stdout);
658 print_num(counters.bcnt, (format|FMT_NOTABLE));
659 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000660 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000661 printf(")\n");
662 } else {
663 unsigned int refs;
664 if (!ip6tc_get_references(&refs, chain, handle))
665 printf(" (ERROR obtaining refs)\n");
666 else
667 printf(" (%u references)\n", refs);
668 }
669
670 if (format & FMT_LINENUMBERS)
671 printf(FMT("%-4s ", "%s "), "num");
672 if (!(format & FMT_NOCOUNTS)) {
673 if (format & FMT_KILOMEGAGIGA) {
674 printf(FMT("%5s ","%s "), "pkts");
675 printf(FMT("%5s ","%s "), "bytes");
676 } else {
677 printf(FMT("%8s ","%s "), "pkts");
678 printf(FMT("%10s ","%s "), "bytes");
679 }
680 }
681 if (!(format & FMT_NOTARGET))
682 printf(FMT("%-9s ","%s "), "target");
683 fputs(" prot ", stdout);
684 if (format & FMT_OPTIONS)
685 fputs("opt", stdout);
686 if (format & FMT_VIA) {
687 printf(FMT(" %-6s ","%s "), "in");
688 printf(FMT("%-6s ","%s "), "out");
689 }
690 printf(FMT(" %-19s ","%s "), "source");
691 printf(FMT(" %-19s "," %s "), "destination");
692 printf("\n");
693}
694
Harald Welte43ac1912002-03-03 09:43:07 +0000695
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696static int
697print_match(const struct ip6t_entry_match *m,
698 const struct ip6t_ip6 *ip,
699 int numeric)
700{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200701 struct xtables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000702
703 if (match) {
704 if (match->print)
705 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000706 else
707 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708 } else {
709 if (m->u.user.name[0])
710 printf("UNKNOWN match `%s' ", m->u.user.name);
711 }
712 /* Don't stop iterating. */
713 return 0;
714}
715
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100716/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000717static void
718print_firewall(const struct ip6t_entry *fw,
719 const char *targname,
720 unsigned int num,
721 unsigned int format,
722 const ip6tc_handle_t handle)
723{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200724 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000725 const struct ip6t_entry_target *t;
726 u_int8_t flags;
727 char buf[BUFSIZ];
728
Rusty Russell5eed48a2000-06-02 20:12:24 +0000729 if (!ip6tc_is_chain(targname, handle))
730 target = find_target(targname, TRY_LOAD);
731 else
732 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
733
734 t = ip6t_get_target((struct ip6t_entry *)fw);
735 flags = fw->ipv6.flags;
736
737 if (format & FMT_LINENUMBERS)
738 printf(FMT("%-4u ", "%u "), num+1);
739
740 if (!(format & FMT_NOCOUNTS)) {
741 print_num(fw->counters.pcnt, format);
742 print_num(fw->counters.bcnt, format);
743 }
744
745 if (!(format & FMT_NOTARGET))
746 printf(FMT("%-9s ", "%s "), targname);
747
748 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
749 {
Harald Welte43ac1912002-03-03 09:43:07 +0000750 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751 if (pname)
752 printf(FMT("%-5s", "%s "), pname);
753 else
754 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
755 }
756
757 if (format & FMT_OPTIONS) {
758 if (format & FMT_NOTABLE)
759 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000760 fputc(' ', stdout); /* Invert flag of FRAG */
761 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000762 fputc(' ', stdout);
763 }
764
765 if (format & FMT_VIA) {
766 char iface[IFNAMSIZ+2];
767
768 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
769 iface[0] = '!';
770 iface[1] = '\0';
771 }
772 else iface[0] = '\0';
773
774 if (fw->ipv6.iniface[0] != '\0') {
775 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000776 }
777 else if (format & FMT_NUMERIC) strcat(iface, "*");
778 else strcat(iface, "any");
779 printf(FMT(" %-6s ","in %s "), iface);
780
781 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
782 iface[0] = '!';
783 iface[1] = '\0';
784 }
785 else iface[0] = '\0';
786
787 if (fw->ipv6.outiface[0] != '\0') {
788 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789 }
790 else if (format & FMT_NUMERIC) strcat(iface, "*");
791 else strcat(iface, "any");
792 printf(FMT("%-6s ","out %s "), iface);
793 }
794
795 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000796 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797 && !(format & FMT_NUMERIC))
798 printf(FMT("%-19s ","%s "), "anywhere");
799 else {
800 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000801 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000802 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000803 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
804 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805 printf(FMT("%-19s ","%s "), buf);
806 }
807
808 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
809 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
810 && !(format & FMT_NUMERIC))
811 printf(FMT("%-19s","-> %s"), "anywhere");
812 else {
813 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000814 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000815 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000816 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
817 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000818 printf(FMT("%-19s","-> %s"), buf);
819 }
820
821 if (format & FMT_NOTABLE)
822 fputs(" ", stdout);
823
824 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
825
826 if (target) {
827 if (target->print)
828 /* Print the target information. */
829 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
830 } else if (t->u.target_size != sizeof(*t))
831 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000832 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000833
834 if (!(format & FMT_NONEWLINE))
835 fputc('\n', stdout);
836}
837
838static void
839print_firewall_line(const struct ip6t_entry *fw,
840 const ip6tc_handle_t h)
841{
842 struct ip6t_entry_target *t;
843
844 t = ip6t_get_target((struct ip6t_entry *)fw);
845 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
846}
847
848static int
849append_entry(const ip6t_chainlabel chain,
850 struct ip6t_entry *fw,
851 unsigned int nsaddrs,
852 const struct in6_addr saddrs[],
853 unsigned int ndaddrs,
854 const struct in6_addr daddrs[],
855 int verbose,
856 ip6tc_handle_t *handle)
857{
858 unsigned int i, j;
859 int ret = 1;
860
861 for (i = 0; i < nsaddrs; i++) {
862 fw->ipv6.src = saddrs[i];
863 for (j = 0; j < ndaddrs; j++) {
864 fw->ipv6.dst = daddrs[j];
865 if (verbose)
866 print_firewall_line(fw, *handle);
867 ret &= ip6tc_append_entry(chain, fw, handle);
868 }
869 }
870
871 return ret;
872}
873
874static int
875replace_entry(const ip6t_chainlabel chain,
876 struct ip6t_entry *fw,
877 unsigned int rulenum,
878 const struct in6_addr *saddr,
879 const struct in6_addr *daddr,
880 int verbose,
881 ip6tc_handle_t *handle)
882{
883 fw->ipv6.src = *saddr;
884 fw->ipv6.dst = *daddr;
885
886 if (verbose)
887 print_firewall_line(fw, *handle);
888 return ip6tc_replace_entry(chain, fw, rulenum, handle);
889}
890
891static int
892insert_entry(const ip6t_chainlabel chain,
893 struct ip6t_entry *fw,
894 unsigned int rulenum,
895 unsigned int nsaddrs,
896 const struct in6_addr saddrs[],
897 unsigned int ndaddrs,
898 const struct in6_addr daddrs[],
899 int verbose,
900 ip6tc_handle_t *handle)
901{
902 unsigned int i, j;
903 int ret = 1;
904
905 for (i = 0; i < nsaddrs; i++) {
906 fw->ipv6.src = saddrs[i];
907 for (j = 0; j < ndaddrs; j++) {
908 fw->ipv6.dst = daddrs[j];
909 if (verbose)
910 print_firewall_line(fw, *handle);
911 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
912 }
913 }
914
915 return ret;
916}
917
918static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000919make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000920{
921 /* Establish mask for comparison */
922 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000923 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000924 unsigned char *mask, *mptr;
925
926 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000927 for (matchp = matches; matchp; matchp = matchp->next)
928 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000929
930 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000931 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000932 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933
934 memset(mask, 0xFF, sizeof(struct ip6t_entry));
935 mptr = mask + sizeof(struct ip6t_entry);
936
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000937 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000939 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000940 + matchp->match->userspacesize);
941 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000942 }
943
Max Kellermann5b76f682008-01-29 13:42:48 +0000944 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000945 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000946 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947
948 return mask;
949}
950
951static int
952delete_entry(const ip6t_chainlabel chain,
953 struct ip6t_entry *fw,
954 unsigned int nsaddrs,
955 const struct in6_addr saddrs[],
956 unsigned int ndaddrs,
957 const struct in6_addr daddrs[],
958 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000959 ip6tc_handle_t *handle,
960 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000961{
962 unsigned int i, j;
963 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964 unsigned char *mask;
965
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000966 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000967 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000968 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000969 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000970 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000971 if (verbose)
972 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000973 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000974 }
975 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000976 free(mask);
977
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 return ret;
979}
980
András Kis-Szabó764316a2001-02-26 17:31:20 +0000981int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000982for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
983 int verbose, int builtinstoo, ip6tc_handle_t *handle)
984{
Max Kellermann5b76f682008-01-29 13:42:48 +0000985 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000986 const char *chain;
987 char *chains;
988 unsigned int i, chaincount = 0;
989
990 chain = ip6tc_first_chain(handle);
991 while (chain) {
992 chaincount++;
993 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000994 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000995
996 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
997 i = 0;
998 chain = ip6tc_first_chain(handle);
999 while (chain) {
1000 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1001 i++;
1002 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001003 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004
1005 for (i = 0; i < chaincount; i++) {
1006 if (!builtinstoo
1007 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001008 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001009 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +00001010 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001011 }
1012
1013 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001014 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001015}
1016
András Kis-Szabó764316a2001-02-26 17:31:20 +00001017int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001018flush_entries(const ip6t_chainlabel chain, int verbose,
1019 ip6tc_handle_t *handle)
1020{
1021 if (!chain)
1022 return for_each_chain(flush_entries, verbose, 1, handle);
1023
1024 if (verbose)
1025 fprintf(stdout, "Flushing chain `%s'\n", chain);
1026 return ip6tc_flush_entries(chain, handle);
1027}
1028
1029static int
1030zero_entries(const ip6t_chainlabel chain, int verbose,
1031 ip6tc_handle_t *handle)
1032{
1033 if (!chain)
1034 return for_each_chain(zero_entries, verbose, 1, handle);
1035
1036 if (verbose)
1037 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1038 return ip6tc_zero_entries(chain, handle);
1039}
1040
András Kis-Szabó764316a2001-02-26 17:31:20 +00001041int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001042delete_chain(const ip6t_chainlabel chain, int verbose,
1043 ip6tc_handle_t *handle)
1044{
1045 if (!chain)
1046 return for_each_chain(delete_chain, verbose, 0, handle);
1047
1048 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001049 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001050 return ip6tc_delete_chain(chain, handle);
1051}
1052
1053static int
1054list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1055 int expanded, int linenumbers, ip6tc_handle_t *handle)
1056{
1057 int found = 0;
1058 unsigned int format;
1059 const char *this;
1060
1061 format = FMT_OPTIONS;
1062 if (!verbose)
1063 format |= FMT_NOCOUNTS;
1064 else
1065 format |= FMT_VIA;
1066
1067 if (numeric)
1068 format |= FMT_NUMERIC;
1069
1070 if (!expanded)
1071 format |= FMT_KILOMEGAGIGA;
1072
1073 if (linenumbers)
1074 format |= FMT_LINENUMBERS;
1075
1076 for (this = ip6tc_first_chain(handle);
1077 this;
1078 this = ip6tc_next_chain(handle)) {
1079 const struct ip6t_entry *i;
1080 unsigned int num;
1081
1082 if (chain && strcmp(chain, this) != 0)
1083 continue;
1084
1085 if (found) printf("\n");
1086
1087 print_header(format, this, handle);
1088 i = ip6tc_first_rule(this, handle);
1089
1090 num = 0;
1091 while (i) {
1092 print_firewall(i,
1093 ip6tc_get_target(i, handle),
1094 num++,
1095 format,
1096 *handle);
1097 i = ip6tc_next_rule(i, handle);
1098 }
1099 found = 1;
1100 }
1101
1102 errno = ENOENT;
1103 return found;
1104}
1105
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001106/* This assumes that mask is contiguous, and byte-bounded. */
1107static void
1108print_iface(char letter, const char *iface, const unsigned char *mask,
1109 int invert)
1110{
1111 unsigned int i;
1112
1113 if (mask[0] == 0)
1114 return;
1115
1116 printf("-%c %s", letter, invert ? "! " : "");
1117
1118 for (i = 0; i < IFNAMSIZ; i++) {
1119 if (mask[i] != 0) {
1120 if (iface[i] != '\0')
1121 printf("%c", iface[i]);
1122 } else {
1123 /* we can access iface[i-1] here, because
1124 * a few lines above we make sure that mask[0] != 0 */
1125 if (iface[i-1] != '\0')
1126 printf("+");
1127 break;
1128 }
1129 }
1130
1131 printf(" ");
1132}
1133
1134/* The ip6tables looks up the /etc/protocols. */
1135static void print_proto(u_int16_t proto, int invert)
1136{
1137 if (proto) {
1138 unsigned int i;
1139 const char *invertstr = invert ? "! " : "";
1140
1141 struct protoent *pent = getprotobynumber(proto);
1142 if (pent) {
1143 printf("-p %s%s ",
1144 invertstr, pent->p_name);
1145 return;
1146 }
1147
1148 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
1149 if (chain_protos[i].num == proto) {
1150 printf("-p %s%s ",
1151 invertstr, chain_protos[i].name);
1152 return;
1153 }
1154
1155 printf("-p %s%u ", invertstr, proto);
1156 }
1157}
1158
1159static int print_match_save(const struct ip6t_entry_match *e,
1160 const struct ip6t_ip6 *ip)
1161{
1162 struct xtables_match *match
1163 = find_match(e->u.user.name, TRY_LOAD, NULL);
1164
1165 if (match) {
1166 printf("-m %s ", e->u.user.name);
1167
1168 /* some matches don't provide a save function */
1169 if (match->save)
1170 match->save(ip, e);
1171 } else {
1172 if (e->u.match_size) {
1173 fprintf(stderr,
1174 "Can't find library for match `%s'\n",
1175 e->u.user.name);
1176 exit(1);
1177 }
1178 }
1179 return 0;
1180}
1181
1182/* print a given ip including mask if neccessary */
1183static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1184{
1185 char buf[51];
1186 int l = ipv6_prefix_length(mask);
1187
1188 if (l == 0 && !invert)
1189 return;
1190
1191 printf("%s %s%s",
1192 prefix,
1193 invert ? "! " : "",
1194 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1195
1196 if (l == -1)
1197 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1198 else
1199 printf("/%d ", l);
1200}
1201
1202/* We want this to be readable, so only print out neccessary fields.
1203 * Because that's the kind of world I want to live in. */
1204void print_rule(const struct ip6t_entry *e,
1205 ip6tc_handle_t *h, const char *chain, int counters)
1206{
1207 struct ip6t_entry_target *t;
1208 const char *target_name;
1209
1210 /* print counters for iptables-save */
1211 if (counters > 0)
1212 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1213
1214 /* print chain name */
1215 printf("-A %s ", chain);
1216
1217 /* Print IP part. */
1218 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1219 e->ipv6.invflags & IP6T_INV_SRCIP);
1220
1221 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1222 e->ipv6.invflags & IP6T_INV_DSTIP);
1223
1224 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1225 e->ipv6.invflags & IP6T_INV_VIA_IN);
1226
1227 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1228 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1229
1230 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1231
1232#if 0
1233 /* not definied in ipv6
1234 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1235 if (e->ipv6.flags & IPT_F_FRAG)
1236 printf("%s-f ",
1237 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1238#endif
1239
1240 if (e->ipv6.flags & IP6T_F_TOS)
1241 printf("%s-? %d ",
1242 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1243 e->ipv6.tos);
1244
1245 /* Print matchinfo part */
1246 if (e->target_offset) {
1247 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1248 }
1249
1250 /* print counters for iptables -R */
1251 if (counters < 0)
1252 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1253
1254 /* Print target name */
1255 target_name = ip6tc_get_target(e, h);
1256 if (target_name && (*target_name != '\0'))
1257 printf("-j %s ", target_name);
1258
1259 /* Print targinfo part */
1260 t = ip6t_get_target((struct ip6t_entry *)e);
1261 if (t->u.user.name[0]) {
1262 struct xtables_target *target
1263 = find_target(t->u.user.name, TRY_LOAD);
1264
1265 if (!target) {
1266 fprintf(stderr, "Can't find library for target `%s'\n",
1267 t->u.user.name);
1268 exit(1);
1269 }
1270
1271 if (target->save)
1272 target->save(&e->ipv6, t);
1273 else {
1274 /* If the target size is greater than ip6t_entry_target
1275 * there is something to be saved, we just don't know
1276 * how to print it */
1277 if (t->u.target_size !=
1278 sizeof(struct ip6t_entry_target)) {
1279 fprintf(stderr, "Target `%s' is missing "
1280 "save function\n",
1281 t->u.user.name);
1282 exit(1);
1283 }
1284 }
1285 }
1286 printf("\n");
1287}
1288
1289static int
1290list_rules(const ip6t_chainlabel chain, int counters,
1291 ip6tc_handle_t *handle)
1292{
1293 const char *this = NULL;
1294 int found = 0;
1295
1296 if (counters)
1297 counters = -1; /* iptables -c format */
1298
1299 /* Dump out chain names first,
1300 * thereby preventing dependency conflicts */
1301 for (this = ip6tc_first_chain(handle);
1302 this;
1303 this = ip6tc_next_chain(handle)) {
1304 if (chain && strcmp(this, chain) != 0)
1305 continue;
1306
1307 if (ip6tc_builtin(this, *handle)) {
1308 struct ip6t_counters count;
1309 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1310 if (counters)
1311 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1312 printf("\n");
1313 } else {
1314 printf("-N %s\n", this);
1315 }
1316 }
1317
1318 for (this = ip6tc_first_chain(handle);
1319 this;
1320 this = ip6tc_next_chain(handle)) {
1321 const struct ip6t_entry *e;
1322
1323 if (chain && strcmp(this, chain) != 0)
1324 continue;
1325
1326 /* Dump out rules */
1327 e = ip6tc_first_rule(this, handle);
1328 while(e) {
1329 print_rule(e, handle, this, counters);
1330 e = ip6tc_next_rule(e, handle);
1331 }
1332 found = 1;
1333 }
1334
1335 errno = ENOENT;
1336 return found;
1337}
1338
Rusty Russell5eed48a2000-06-02 20:12:24 +00001339static struct ip6t_entry *
1340generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001341 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001342 struct ip6t_entry_target *target)
1343{
1344 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001345 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001346 struct ip6t_entry *e;
1347
1348 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001349 for (matchp = matches; matchp; matchp = matchp->next)
1350 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001351
1352 e = fw_malloc(size + target->u.target_size);
1353 *e = *fw;
1354 e->target_offset = size;
1355 e->next_offset = size + target->u.target_size;
1356
1357 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001358 for (matchp = matches; matchp; matchp = matchp->next) {
1359 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1360 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001361 }
1362 memcpy(e->elems + size, target, target->u.target_size);
1363
1364 return e;
1365}
1366
Jan Engelhardt33690a12008-02-11 00:54:00 +01001367static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001368{
1369 struct ip6tables_rule_match *matchp, *tmp;
1370
1371 for (matchp = *matches; matchp;) {
1372 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001373 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001374 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001375 matchp->match->m = NULL;
1376 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001377 if (matchp->match == matchp->match->next) {
1378 free(matchp->match);
1379 matchp->match = NULL;
1380 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001381 free(matchp);
1382 matchp = tmp;
1383 }
1384
1385 *matches = NULL;
1386}
1387
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001388static void set_revision(char *name, u_int8_t revision)
1389{
1390 /* Old kernel sources don't have ".revision" field,
1391 but we stole a byte from name. */
1392 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1393 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1394}
1395
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1397{
1398 struct ip6t_entry fw, *e = NULL;
1399 int invert = 0;
1400 unsigned int nsaddrs = 0, ndaddrs = 0;
1401 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1402
1403 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001404 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405 const char *chain = NULL;
1406 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1407 const char *policy = NULL, *newname = NULL;
1408 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001409 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001411 struct xtables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001412 struct ip6tables_rule_match *matches = NULL;
1413 struct ip6tables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001414 struct xtables_target *target = NULL;
1415 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 const char *jumpto = "";
1417 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001418 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001419 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001420
1421 memset(&fw, 0, sizeof(fw));
1422
Harald Welte43ac1912002-03-03 09:43:07 +00001423 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001424 * a second time */
1425 optind = 0;
1426
Harald Welte43ac1912002-03-03 09:43:07 +00001427 /* clear mflags in case do_command gets called a second time
1428 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001429 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001430 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001431
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001432 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001433 t->tflags = 0;
1434 t->used = 0;
1435 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001436
Rusty Russell5eed48a2000-06-02 20:12:24 +00001437 /* Suppress error messages: we may add new options if we
1438 demand-load a protocol. */
1439 opterr = 0;
1440
1441 while ((c = getopt_long(argc, argv,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001442 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 opts, NULL)) != -1) {
1444 switch (c) {
1445 /*
1446 * Command selection
1447 */
1448 case 'A':
1449 add_command(&command, CMD_APPEND, CMD_NONE,
1450 invert);
1451 chain = optarg;
1452 break;
1453
1454 case 'D':
1455 add_command(&command, CMD_DELETE, CMD_NONE,
1456 invert);
1457 chain = optarg;
1458 if (optind < argc && argv[optind][0] != '-'
1459 && argv[optind][0] != '!') {
1460 rulenum = parse_rulenumber(argv[optind++]);
1461 command = CMD_DELETE_NUM;
1462 }
1463 break;
1464
Rusty Russell5eed48a2000-06-02 20:12:24 +00001465 case 'R':
1466 add_command(&command, CMD_REPLACE, CMD_NONE,
1467 invert);
1468 chain = optarg;
1469 if (optind < argc && argv[optind][0] != '-'
1470 && argv[optind][0] != '!')
1471 rulenum = parse_rulenumber(argv[optind++]);
1472 else
1473 exit_error(PARAMETER_PROBLEM,
1474 "-%c requires a rule number",
1475 cmd2char(CMD_REPLACE));
1476 break;
1477
1478 case 'I':
1479 add_command(&command, CMD_INSERT, CMD_NONE,
1480 invert);
1481 chain = optarg;
1482 if (optind < argc && argv[optind][0] != '-'
1483 && argv[optind][0] != '!')
1484 rulenum = parse_rulenumber(argv[optind++]);
1485 else rulenum = 1;
1486 break;
1487
1488 case 'L':
1489 add_command(&command, CMD_LIST, CMD_ZERO,
1490 invert);
1491 if (optarg) chain = optarg;
1492 else if (optind < argc && argv[optind][0] != '-'
1493 && argv[optind][0] != '!')
1494 chain = argv[optind++];
1495 break;
1496
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001497 case 'S':
1498 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1499 invert);
1500 if (optarg) chain = optarg;
1501 else if (optind < argc && argv[optind][0] != '-'
1502 && argv[optind][0] != '!')
1503 chain = argv[optind++];
1504 break;
1505
Rusty Russell5eed48a2000-06-02 20:12:24 +00001506 case 'F':
1507 add_command(&command, CMD_FLUSH, CMD_NONE,
1508 invert);
1509 if (optarg) chain = optarg;
1510 else if (optind < argc && argv[optind][0] != '-'
1511 && argv[optind][0] != '!')
1512 chain = argv[optind++];
1513 break;
1514
1515 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001516 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001517 invert);
1518 if (optarg) chain = optarg;
1519 else if (optind < argc && argv[optind][0] != '-'
1520 && argv[optind][0] != '!')
1521 chain = argv[optind++];
1522 break;
1523
1524 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001525 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001526 exit_error(PARAMETER_PROBLEM,
1527 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001528 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001529 if (find_target(optarg, TRY_LOAD))
1530 exit_error(PARAMETER_PROBLEM,
1531 "chain name may not clash "
1532 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1534 invert);
1535 chain = optarg;
1536 break;
1537
1538 case 'X':
1539 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1540 invert);
1541 if (optarg) chain = optarg;
1542 else if (optind < argc && argv[optind][0] != '-'
1543 && argv[optind][0] != '!')
1544 chain = argv[optind++];
1545 break;
1546
1547 case 'E':
1548 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1549 invert);
1550 chain = optarg;
1551 if (optind < argc && argv[optind][0] != '-'
1552 && argv[optind][0] != '!')
1553 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001554 else
1555 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001556 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001557 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001558 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001559 break;
1560
1561 case 'P':
1562 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1563 invert);
1564 chain = optarg;
1565 if (optind < argc && argv[optind][0] != '-'
1566 && argv[optind][0] != '!')
1567 policy = argv[optind++];
1568 else
1569 exit_error(PARAMETER_PROBLEM,
1570 "-%c requires a chain and a policy",
1571 cmd2char(CMD_SET_POLICY));
1572 break;
1573
1574 case 'h':
1575 if (!optarg)
1576 optarg = argv[optind];
1577
Jonas Berlin1b91e592005-04-01 06:58:38 +00001578 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001579 if (!matches && protocol)
1580 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001582 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001583
1584 /*
1585 * Option selection
1586 */
1587 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001588 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001589 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1590 invert);
1591
1592 /* Canonicalize into lower case */
1593 for (protocol = argv[optind-1]; *protocol; protocol++)
1594 *protocol = tolower(*protocol);
1595
1596 protocol = argv[optind-1];
1597 fw.ipv6.proto = parse_protocol(protocol);
1598 fw.ipv6.flags |= IP6T_F_PROTO;
1599
1600 if (fw.ipv6.proto == 0
1601 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1602 exit_error(PARAMETER_PROBLEM,
1603 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001604
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001605 if (is_exthdr(fw.ipv6.proto)
1606 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001607 fprintf(stderr,
1608 "Warning: never matched protocol: %s. "
1609 "use extension match instead.\n",
1610 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 break;
1612
1613 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001614 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001615 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1616 invert);
1617 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001618 break;
1619
1620 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001621 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1623 invert);
1624 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625 break;
1626
1627 case 'j':
1628 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1629 invert);
1630 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001631 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632 target = find_target(jumpto, TRY_LOAD);
1633
1634 if (target) {
1635 size_t size;
1636
Harald Welte43ac1912002-03-03 09:43:07 +00001637 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1638 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001639
1640 target->t = fw_calloc(1, size);
1641 target->t->u.target_size = size;
1642 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001643 set_revision(target->t->u.user.name,
1644 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001645 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001646 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001647 opts = merge_options(opts,
1648 target->extra_opts,
1649 &target->option_offset);
1650 if (opts == NULL)
1651 exit_error(OTHER_PROBLEM,
1652 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653 }
1654 break;
1655
1656
1657 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001658 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001659 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1660 invert);
1661 parse_interface(argv[optind-1],
1662 fw.ipv6.iniface,
1663 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 break;
1665
1666 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001667 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1669 invert);
1670 parse_interface(argv[optind-1],
1671 fw.ipv6.outiface,
1672 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673 break;
1674
1675 case 'v':
1676 if (!verbose)
1677 set_option(&options, OPT_VERBOSE,
1678 &fw.ipv6.invflags, invert);
1679 verbose++;
1680 break;
1681
1682 case 'm': {
1683 size_t size;
1684
1685 if (invert)
1686 exit_error(PARAMETER_PROBLEM,
1687 "unexpected ! flag before --match");
1688
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001689 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001690 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1691 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001692 m->m = fw_calloc(1, size);
1693 m->m->u.match_size = size;
1694 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001695 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001696 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001697 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001698 if (m != m->next)
1699 /* Merge options for non-cloned matches */
1700 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001701 }
1702 break;
1703
1704 case 'n':
1705 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1706 invert);
1707 break;
1708
1709 case 't':
1710 if (invert)
1711 exit_error(PARAMETER_PROBLEM,
1712 "unexpected ! flag before --table");
1713 *table = argv[optind-1];
1714 break;
1715
1716 case 'x':
1717 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1718 invert);
1719 break;
1720
1721 case 'V':
1722 if (invert)
1723 printf("Not %s ;-)\n", program_version);
1724 else
1725 printf("%s v%s\n",
1726 program_name, program_version);
1727 exit(0);
1728
1729 case '0':
1730 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1731 invert);
1732 break;
1733
Harald Welte43ac1912002-03-03 09:43:07 +00001734 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001735 modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001736 break;
1737
1738 case 'c':
1739
1740 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1741 invert);
1742 pcnt = optarg;
1743 if (optind < argc && argv[optind][0] != '-'
1744 && argv[optind][0] != '!')
1745 bcnt = argv[optind++];
1746 else
1747 exit_error(PARAMETER_PROBLEM,
1748 "-%c requires packet and byte counter",
1749 opt2char(OPT_COUNTERS));
1750
Patrick McHardy875441e2007-10-17 08:48:58 +00001751 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001752 exit_error(PARAMETER_PROBLEM,
1753 "-%c packet counter not numeric",
1754 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001755 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001756
Patrick McHardy875441e2007-10-17 08:48:58 +00001757 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001758 exit_error(PARAMETER_PROBLEM,
1759 "-%c byte counter not numeric",
1760 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001761 fw.counters.bcnt = cnt;
1762
Harald Welte43ac1912002-03-03 09:43:07 +00001763 break;
1764
Rusty Russell5eed48a2000-06-02 20:12:24 +00001765 case 1: /* non option */
1766 if (optarg[0] == '!' && optarg[1] == '\0') {
1767 if (invert)
1768 exit_error(PARAMETER_PROBLEM,
1769 "multiple consecutive ! not"
1770 " allowed");
1771 invert = TRUE;
1772 optarg[0] = '\0';
1773 continue;
1774 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001775 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001776 exit_tryhelp(2);
1777
1778 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001779 if (!target
1780 || !(target->parse(c - target->option_offset,
1781 argv, invert,
1782 &target->tflags,
1783 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001784 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001785 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001786 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001787 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001788 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001789 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001790 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001791 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001792 break;
1793 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001794 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001795
1796 /* If you listen carefully, you can
1797 actually hear this code suck. */
1798
1799 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001800 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001801 * parameter, that has not been parsed yet,
1802 * it's not an option of an explicitly loaded
1803 * match or a target. However, we support
1804 * implicit loading of the protocol match
1805 * extension. '-p tcp' means 'l4 proto 6' and
1806 * at the same time 'load tcp protocol match on
1807 * demand if we specify --dport'.
1808 *
1809 * To make this work, we need to make sure:
1810 * - the parameter has not been parsed by
1811 * a match (m above)
1812 * - a protocol has been specified
1813 * - the protocol extension has not been
1814 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001815 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001816 * - the protocol extension can be successively
1817 * loaded
1818 */
1819 if (m == NULL
1820 && protocol
1821 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001822 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001823 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001824 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001825 && (proto_used == 0))
1826 )
1827 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001828 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001829 /* Try loading protocol */
1830 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001831
Harald Welte00f5a9c2002-08-26 14:37:35 +00001832 proto_used = 1;
1833
1834 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1835 + m->size;
1836
1837 m->m = fw_calloc(1, size);
1838 m->m->u.match_size = size;
1839 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001840 set_revision(m->m->u.user.name,
1841 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001842 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001843 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001844
1845 opts = merge_options(opts,
1846 m->extra_opts, &m->option_offset);
1847
1848 optind--;
1849 continue;
1850 }
1851
Rusty Russell5eed48a2000-06-02 20:12:24 +00001852 if (!m)
1853 exit_error(PARAMETER_PROBLEM,
1854 "Unknown arg `%s'",
1855 argv[optind-1]);
1856 }
1857 }
1858 invert = FALSE;
1859 }
1860
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001861 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001862 if (matchp->match->final_check != NULL)
1863 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001864
Jan Engelhardt830132a2007-10-04 16:24:50 +00001865 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866 target->final_check(target->tflags);
1867
1868 /* Fix me: must put inverse options checking here --MN */
1869
1870 if (optind < argc)
1871 exit_error(PARAMETER_PROBLEM,
1872 "unknown arguments found on commandline");
1873 if (!command)
1874 exit_error(PARAMETER_PROBLEM, "no command specified");
1875 if (invert)
1876 exit_error(PARAMETER_PROBLEM,
1877 "nothing appropriate following !");
1878
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001879 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001880 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001881 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001883 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001884 }
1885
1886 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001887 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1888 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001889
1890 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001891 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1892 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893
1894 if ((nsaddrs > 1 || ndaddrs > 1) &&
1895 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1896 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1897 " source or destination IP addresses");
1898
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1900 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1901 "specify a unique address");
1902
1903 generic_opt_check(command, options);
1904
1905 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1906 exit_error(PARAMETER_PROBLEM,
1907 "chain name `%s' too long (must be under %i chars)",
1908 chain, IP6T_FUNCTION_MAXNAMELEN);
1909
Harald Welte43ac1912002-03-03 09:43:07 +00001910 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001911 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001912 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001913
Rusty Russell8beb0492004-12-22 00:37:10 +00001914 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001915 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001916 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001917
Harald Welte43ac1912002-03-03 09:43:07 +00001918 if (!*handle)
1919 exit_error(VERSION_PROBLEM,
1920 "can't initialize ip6tables table `%s': %s",
1921 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001923 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001924 || command == CMD_DELETE
1925 || command == CMD_INSERT
1926 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001927 if (strcmp(chain, "PREROUTING") == 0
1928 || strcmp(chain, "INPUT") == 0) {
1929 /* -o not valid with incoming packets. */
1930 if (options & OPT_VIANAMEOUT)
1931 exit_error(PARAMETER_PROBLEM,
1932 "Can't use -%c with %s\n",
1933 opt2char(OPT_VIANAMEOUT),
1934 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001935 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001936
Harald Welte43ac1912002-03-03 09:43:07 +00001937 if (strcmp(chain, "POSTROUTING") == 0
1938 || strcmp(chain, "OUTPUT") == 0) {
1939 /* -i not valid with outgoing packets */
1940 if (options & OPT_VIANAMEIN)
1941 exit_error(PARAMETER_PROBLEM,
1942 "Can't use -%c with %s\n",
1943 opt2char(OPT_VIANAMEIN),
1944 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001945 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001946
1947 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001948 fprintf(stderr,
1949 "Warning: using chain %s, not extension\n",
1950 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001952 if (target->t)
1953 free(target->t);
1954
Rusty Russell5eed48a2000-06-02 20:12:24 +00001955 target = NULL;
1956 }
1957
1958 /* If they didn't specify a target, or it's a chain
1959 name, use standard. */
1960 if (!target
1961 && (strlen(jumpto) == 0
1962 || ip6tc_is_chain(jumpto, *handle))) {
1963 size_t size;
1964
1965 target = find_target(IP6T_STANDARD_TARGET,
1966 LOAD_MUST_SUCCEED);
1967
1968 size = sizeof(struct ip6t_entry_target)
1969 + target->size;
1970 target->t = fw_calloc(1, size);
1971 target->t->u.target_size = size;
1972 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001973 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001974 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975 }
1976
1977 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001978 /* it is no chain, and we can't load a plugin.
1979 * We cannot know if the plugin is corrupt, non
1980 * existant OR if the user just misspelled a
1981 * chain. */
1982 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001983 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001984 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001985 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001986 }
1987 }
1988
1989 switch (command) {
1990 case CMD_APPEND:
1991 ret = append_entry(chain, e,
1992 nsaddrs, saddrs, ndaddrs, daddrs,
1993 options&OPT_VERBOSE,
1994 handle);
1995 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001996 case CMD_DELETE:
1997 ret = delete_entry(chain, e,
1998 nsaddrs, saddrs, ndaddrs, daddrs,
1999 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002000 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 break;
2002 case CMD_DELETE_NUM:
2003 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2004 break;
2005 case CMD_REPLACE:
2006 ret = replace_entry(chain, e, rulenum - 1,
2007 saddrs, daddrs, options&OPT_VERBOSE,
2008 handle);
2009 break;
2010 case CMD_INSERT:
2011 ret = insert_entry(chain, e, rulenum - 1,
2012 nsaddrs, saddrs, ndaddrs, daddrs,
2013 options&OPT_VERBOSE,
2014 handle);
2015 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002016 case CMD_FLUSH:
2017 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2018 break;
2019 case CMD_ZERO:
2020 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2021 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002022 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002023 case CMD_LIST|CMD_ZERO:
2024 ret = list_entries(chain,
2025 options&OPT_VERBOSE,
2026 options&OPT_NUMERIC,
2027 options&OPT_EXPANDED,
2028 options&OPT_LINENUMBERS,
2029 handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002030 if (ret && (command & CMD_ZERO))
2031 ret = zero_entries(chain,
2032 options&OPT_VERBOSE, handle);
2033 break;
2034 case CMD_LIST_RULES:
2035 case CMD_LIST_RULES|CMD_ZERO:
2036 ret = list_rules(chain,
2037 options&OPT_VERBOSE,
2038 handle);
2039 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002040 ret = zero_entries(chain,
2041 options&OPT_VERBOSE, handle);
2042 break;
2043 case CMD_NEW_CHAIN:
2044 ret = ip6tc_create_chain(chain, handle);
2045 break;
2046 case CMD_DELETE_CHAIN:
2047 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2048 break;
2049 case CMD_RENAME_CHAIN:
2050 ret = ip6tc_rename_chain(chain, newname, handle);
2051 break;
2052 case CMD_SET_POLICY:
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +02002053 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002054 break;
2055 default:
2056 /* We should never reach this... */
2057 exit_tryhelp(2);
2058 }
2059
2060 if (verbose > 1)
2061 dump_entries6(*handle);
2062
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002063 clear_rule_matches(&matches);
2064
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002065 if (e != NULL) {
2066 free(e);
2067 e = NULL;
2068 }
2069
Max Kellermann9ee386a2008-01-29 13:48:05 +00002070 for (i = 0; i < nsaddrs; i++)
2071 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002072
Max Kellermann9ee386a2008-01-29 13:48:05 +00002073 for (i = 0; i < ndaddrs; i++)
2074 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002075
Pablo Neiradfdcd642005-05-29 19:05:23 +00002076 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002077
Rusty Russell5eed48a2000-06-02 20:12:24 +00002078 return ret;
2079}