blob: 938c91eeca67d019321c26cc43cfda4be17cfa53 [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>
Rusty Russell5eed48a2000-06-02 20:12:24 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Rusty Russell5eed48a2000-06-02 20:12:24 +000052#define FMT_NUMERIC 0x0001
53#define FMT_NOCOUNTS 0x0002
54#define FMT_KILOMEGAGIGA 0x0004
55#define FMT_OPTIONS 0x0008
56#define FMT_NOTABLE 0x0010
57#define FMT_NOTARGET 0x0020
58#define FMT_VIA 0x0040
59#define FMT_NONEWLINE 0x0080
60#define FMT_LINENUMBERS 0x0100
61
62#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
63 | FMT_NUMERIC | FMT_NOTABLE)
64#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
65
66
67#define CMD_NONE 0x0000U
68#define CMD_INSERT 0x0001U
69#define CMD_DELETE 0x0002U
70#define CMD_DELETE_NUM 0x0004U
71#define CMD_REPLACE 0x0008U
72#define CMD_APPEND 0x0010U
73#define CMD_LIST 0x0020U
74#define CMD_FLUSH 0x0040U
75#define CMD_ZERO 0x0080U
76#define CMD_NEW_CHAIN 0x0100U
77#define CMD_DELETE_CHAIN 0x0200U
78#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000079#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000080#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000082 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000083
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000097#define OPT_COUNTERS 0x00400U
98#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +000099static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000100= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000101
102static struct option original_opts[] = {
103 { "append", 1, 0, 'A' },
104 { "delete", 1, 0, 'D' },
105 { "insert", 1, 0, 'I' },
106 { "replace", 1, 0, 'R' },
107 { "list", 2, 0, 'L' },
108 { "flush", 2, 0, 'F' },
109 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000110 { "new-chain", 1, 0, 'N' },
111 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000112 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000113 { "policy", 1, 0, 'P' },
114 { "source", 1, 0, 's' },
115 { "destination", 1, 0, 'd' },
116 { "src", 1, 0, 's' }, /* synonym */
117 { "dst", 1, 0, 'd' }, /* synonym */
118 { "protocol", 1, 0, 'p' },
119 { "in-interface", 1, 0, 'i' },
120 { "jump", 1, 0, 'j' },
121 { "table", 1, 0, 't' },
122 { "match", 1, 0, 'm' },
123 { "numeric", 0, 0, 'n' },
124 { "out-interface", 1, 0, 'o' },
125 { "verbose", 0, 0, 'v' },
126 { "exact", 0, 0, 'x' },
127 { "version", 0, 0, 'V' },
128 { "help", 2, 0, 'h' },
129 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000130 { "modprobe", 1, 0, 'M' },
131 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000132 { 0 }
133};
134
Harald Weltea8658ca2003-03-05 07:46:15 +0000135/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
136 * current line of the input file, in order to give a more precise error
137 * message. ip6tables itself doesn't need this, so it is initialized to the
138 * magic number of -1 */
139int line = -1;
140
Rusty Russell5eed48a2000-06-02 20:12:24 +0000141static struct option *opts = original_opts;
142static unsigned int global_option_offset = 0;
143
144/* Table of legal combinations of commands and options. If any of the
145 * given commands make an option legal, that option is legal (applies to
146 * CMD_LIST and CMD_ZERO only).
147 * Key:
148 * + compulsory
149 * x illegal
150 * optional
151 */
152
153static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
154/* Well, it's better than "Re: Linux vs FreeBSD" */
155{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000156 /* -n -s -d -p -j -v -x -i -o --line -c */
157/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
158/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
159/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
160/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
161/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
163/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
164/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
166/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000168/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000169};
170
171static int inverse_for_options[NUMBER_OF_OPT] =
172{
173/* -n */ 0,
174/* -s */ IP6T_INV_SRCIP,
175/* -d */ IP6T_INV_DSTIP,
176/* -p */ IP6T_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IP6T_INV_VIA_IN,
181/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000182/*--line*/ 0,
183/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184};
185
186const char *program_version;
187const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000188
189/* Extra debugging from libiptc */
190extern void dump_entries6(const ip6tc_handle_t handle);
191
192/* A few hardcoded protocols for 'all' and in case the user has no
193 /etc/protocols */
194struct pprot {
195 char *name;
196 u_int8_t num;
197};
198
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000199struct afinfo afinfo = {
200 .family = AF_INET6,
201 .libprefix = "libip6t_",
202 .ipproto = IPPROTO_IPV6,
203 .kmod = "ip6_tables",
204 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
205 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
206};
207
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000208/* Primitive headers... */
209/* defined in netinet/in.h */
210#if 0
211#ifndef IPPROTO_ESP
212#define IPPROTO_ESP 50
213#endif
214#ifndef IPPROTO_AH
215#define IPPROTO_AH 51
216#endif
217#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000218#ifndef IPPROTO_MH
219#define IPPROTO_MH 135
220#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000221
Rusty Russell5eed48a2000-06-02 20:12:24 +0000222static const struct pprot chain_protos[] = {
223 { "tcp", IPPROTO_TCP },
224 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000225 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000226 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000227 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000228 { "esp", IPPROTO_ESP },
229 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000230 { "ipv6-mh", IPPROTO_MH },
231 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000232 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000233};
234
235static char *
236proto_to_name(u_int8_t proto, int nolookup)
237{
238 unsigned int i;
239
240 if (proto && !nolookup) {
241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
253static void
254in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
255{
256 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000257 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000258}
259
Pablo Neiradfdcd642005-05-29 19:05:23 +0000260static void free_opts(int reset_offset)
261{
262 if (opts != original_opts) {
263 free(opts);
264 opts = original_opts;
265 if (reset_offset)
266 global_option_offset = 0;
267 }
268}
269
Patrick McHardy0b639362007-09-08 16:00:01 +0000270static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271exit_tryhelp(int status)
272{
Harald Weltea8658ca2003-03-05 07:46:15 +0000273 if (line != -1)
274 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000275 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
276 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000277 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278 exit(status);
279}
280
Patrick McHardy0b639362007-09-08 16:00:01 +0000281static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000282exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000283{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000284 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285 struct ip6tables_target *t = NULL;
286
287 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000288"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289" %s -[RI] chain rulenum rule-specification [options]\n"
290" %s -D chain rulenum [options]\n"
291" %s -[LFZ] [chain] [options]\n"
292" %s -[NX] chain\n"
293" %s -E old-chain-name new-chain-name\n"
294" %s -P chain target [options]\n"
295" %s -h (print this help information)\n\n",
296 program_name, program_version, program_name, program_name,
297 program_name, program_name, program_name, program_name,
298 program_name, program_name);
299
300 printf(
301"Commands:\n"
302"Either long or short options are allowed.\n"
303" --append -A chain Append to chain\n"
304" --delete -D chain Delete matching rule from chain\n"
305" --delete -D chain rulenum\n"
306" Delete rule rulenum (1 = first) from chain\n"
307" --insert -I chain [rulenum]\n"
308" Insert in chain as rulenum (default 1=first)\n"
309" --replace -R chain rulenum\n"
310" Replace rule rulenum (1 = first) in chain\n"
311" --list -L [chain] List the rules in a chain or all chains\n"
312" --flush -F [chain] Delete all rules in chain or all chains\n"
313" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314" --new -N chain Create a new user-defined chain\n"
315" --delete-chain\n"
316" -X [chain] Delete a user-defined chain\n"
317" --policy -P chain target\n"
318" Change policy on chain to target\n"
319" --rename-chain\n"
320" -E old-chain new-chain\n"
321" Change chain name, (moving any references)\n"
322
323"Options:\n"
324" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
325" --source -s [!] address[/mask]\n"
326" source specification\n"
327" --destination -d [!] address[/mask]\n"
328" destination specification\n"
329" --in-interface -i [!] input name[+]\n"
330" network interface name ([+] for wildcard)\n"
331" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000332" target for rule (may load target extension)\n"
333" --match -m match\n"
334" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335" --numeric -n numeric output of addresses and ports\n"
336" --out-interface -o [!] output name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --table -t table table to manipulate (default: `filter')\n"
339" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000340" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000341" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000342/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000343" --modprobe=<command> try to insert modules using this command\n"
344" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000345"[!] --version -V print package version.\n");
346
347 /* Print out any special helps. A user might like to be able to add a --help
348 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000349 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000350 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351 if (t->used) {
352 printf("\n");
353 t->help();
354 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000355 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000356 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000357 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000358 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000359 }
360 exit(0);
361}
362
Patrick McHardy0b639362007-09-08 16:00:01 +0000363void
364exit_error(enum exittype status, const char *msg, ...)
365{
366 va_list args;
367
368 va_start(args, msg);
369 fprintf(stderr, "%s v%s: ", program_name, program_version);
370 vfprintf(stderr, msg, args);
371 va_end(args);
372 fprintf(stderr, "\n");
373 if (status == PARAMETER_PROBLEM)
374 exit_tryhelp(status);
375 if (status == VERSION_PROBLEM)
376 fprintf(stderr,
377 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
378 /* On error paths, make sure that we don't leak memory */
379 free_opts(1);
380 exit(status);
381}
382
Rusty Russell5eed48a2000-06-02 20:12:24 +0000383static void
384generic_opt_check(int command, int options)
385{
386 int i, j, legal = 0;
387
388 /* Check that commands are valid with options. Complicated by the
389 * fact that if an option is legal with *any* command given, it is
390 * legal overall (ie. -z and -l).
391 */
392 for (i = 0; i < NUMBER_OF_OPT; i++) {
393 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
394
395 for (j = 0; j < NUMBER_OF_CMD; j++) {
396 if (!(command & (1<<j)))
397 continue;
398
399 if (!(options & (1<<i))) {
400 if (commands_v_options[j][i] == '+')
401 exit_error(PARAMETER_PROBLEM,
402 "You need to supply the `-%c' "
403 "option for this command\n",
404 optflags[i]);
405 } else {
406 if (commands_v_options[j][i] != 'x')
407 legal = 1;
408 else if (legal == 0)
409 legal = -1;
410 }
411 }
412 if (legal == -1)
413 exit_error(PARAMETER_PROBLEM,
414 "Illegal option `-%c' with this command\n",
415 optflags[i]);
416 }
417}
418
419static char
420opt2char(int option)
421{
422 const char *ptr;
423 for (ptr = optflags; option > 1; option >>= 1, ptr++);
424
425 return *ptr;
426}
427
428static char
429cmd2char(int option)
430{
431 const char *ptr;
432 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
433
434 return *ptr;
435}
436
437static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000438add_command(unsigned int *cmd, const int newcmd, const int othercmds,
439 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000440{
441 if (invert)
442 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
443 if (*cmd & (~othercmds))
444 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
445 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
446 *cmd |= newcmd;
447}
448
449int
Harald Welteb77f1da2002-03-14 11:35:58 +0000450check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000451{
452 if (option && strcmp(option, "!") == 0) {
453 if (*invert)
454 exit_error(PARAMETER_PROBLEM,
455 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000456 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000457 if (optind) {
458 *optind = *optind+1;
459 if (argc && *optind > argc)
460 exit_error(PARAMETER_PROBLEM,
461 "no argument following `!'");
462 }
463
Rusty Russell5eed48a2000-06-02 20:12:24 +0000464 return TRUE;
465 }
466 return FALSE;
467}
468
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469static char *
470addr_to_numeric(const struct in6_addr *addrp)
471{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000472 /* 0000:0000:0000:0000:0000:000.000.000.000
473 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000474 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000475 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000476}
477
478static struct in6_addr *
479numeric_to_addr(const char *num)
480{
481 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000482 int err;
483 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000485#ifdef DEBUG
486 fprintf(stderr, "\nnumeric2addr: %d\n", err);
487#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488 return (struct in6_addr *)NULL;
489}
490
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000491
492static struct in6_addr *
493host_to_addr(const char *name, unsigned int *naddr)
494{
495 struct addrinfo hints;
496 struct addrinfo *res;
497 static struct in6_addr *addr;
498 int err;
499
500 memset(&hints, 0, sizeof(hints));
501 hints.ai_flags=AI_CANONNAME;
502 hints.ai_family=AF_INET6;
503 hints.ai_socktype=SOCK_RAW;
504 hints.ai_protocol=41;
505 hints.ai_next=NULL;
506
507 *naddr = 0;
508 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
509#ifdef DEBUG
510 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
511#endif
512 return (struct in6_addr *) NULL;
513 } else {
514 if (res->ai_family != AF_INET6 ||
515 res->ai_addrlen != sizeof(struct sockaddr_in6))
516 return (struct in6_addr *) NULL;
517
518#ifdef DEBUG
519 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
520 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
521#endif
522 /* Get the first element of the address-chain */
523 addr = fw_calloc(1, sizeof(struct in6_addr));
524 in6addrcpy(addr, (struct in6_addr *)
525 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
526 freeaddrinfo(res);
527 *naddr = 1;
528 return addr;
529 }
530
531 return (struct in6_addr *) NULL;
532}
533
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000534static char *
535addr_to_host(const struct in6_addr *addr)
536{
537 struct sockaddr_in6 saddr;
538 int err;
539 static char hostname[NI_MAXHOST];
540
541 memset(&saddr, 0, sizeof(struct sockaddr_in6));
542 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000543 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000544
545 if ( (err=getnameinfo((struct sockaddr *)&saddr,
546 sizeof(struct sockaddr_in6),
547 hostname, sizeof(hostname)-1,
548 NULL, 0, 0)) != 0 ){
549#ifdef DEBUG
550 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
551#endif
552 return (char *) NULL;
553 } else {
554#ifdef DEBUG
555 fprintf (stderr, "\naddr2host: %s\n", hostname);
556#endif
557
558 return hostname;
559 }
560
561 return (char *) NULL;
562}
563
Rusty Russell5eed48a2000-06-02 20:12:24 +0000564static char *
565mask_to_numeric(const struct in6_addr *addrp)
566{
Harald Welte8a50ab82003-06-24 18:15:59 +0000567 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000569 if (l == -1) {
570 strcpy(buf, "/");
571 strcat(buf, addr_to_numeric(addrp));
572 return buf;
573 }
Harald Welte43ac1912002-03-03 09:43:07 +0000574 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000575 return buf;
576}
577
578static struct in6_addr *
579network_to_addr(const char *name)
580{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000581 /* abort();*/
582 /* TODO: not implemented yet, but the exception breaks the
583 * name resolvation */
584 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000585}
586
587static char *
588addr_to_anyname(const struct in6_addr *addr)
589{
590 char *name;
591
592 if ((name = addr_to_host(addr)) != NULL)
593 return name;
594
595 return addr_to_numeric(addr);
596}
597
598/*
599 * All functions starting with "parse" should succeed, otherwise
600 * the program fails.
601 * Most routines return pointers to static data that may change
602 * between calls to the same or other routines with a few exceptions:
603 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
604 * return global static data.
605*/
606
607static struct in6_addr *
608parse_hostnetwork(const char *name, unsigned int *naddrs)
609{
610 struct in6_addr *addrp, *addrptmp;
611
612 if ((addrptmp = numeric_to_addr(name)) != NULL ||
613 (addrptmp = network_to_addr(name)) != NULL) {
614 addrp = fw_malloc(sizeof(struct in6_addr));
615 in6addrcpy(addrp, addrptmp);
616 *naddrs = 1;
617 return addrp;
618 }
619 if ((addrp = host_to_addr(name, naddrs)) != NULL)
620 return addrp;
621
622 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
623}
624
625static struct in6_addr *
626parse_mask(char *mask)
627{
628 static struct in6_addr maskaddr;
629 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000630 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000631
632 if (mask == NULL) {
633 /* no mask at all defaults to 128 bits */
634 memset(&maskaddr, 0xff, sizeof maskaddr);
635 return &maskaddr;
636 }
637 if ((addrp = numeric_to_addr(mask)) != NULL)
638 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000639 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000640 exit_error(PARAMETER_PROBLEM,
641 "invalid mask `%s' specified", mask);
642 if (bits != 0) {
643 char *p = (char *)&maskaddr;
644 memset(p, 0xff, bits / 8);
645 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
646 p[bits / 8] = 0xff << (8 - (bits & 7));
647 return &maskaddr;
648 }
649
650 memset(&maskaddr, 0, sizeof maskaddr);
651 return &maskaddr;
652}
653
Harald Welte43ac1912002-03-03 09:43:07 +0000654void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
656 struct in6_addr *maskp, unsigned int *naddrs)
657{
658 struct in6_addr *addrp;
659 char buf[256];
660 char *p;
661 int i, j, n;
662
663 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000664 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000665 if ((p = strrchr(buf, '/')) != NULL) {
666 *p = '\0';
667 addrp = parse_mask(p + 1);
668 } else
669 addrp = parse_mask(NULL);
670 in6addrcpy(maskp, addrp);
671
672 /* if a null mask is given, the name is ignored, like in "any/0" */
673 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
674 strcpy(buf, "::");
675
676 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
677 n = *naddrs;
678 for (i = 0, j = 0; i < n; i++) {
679 int k;
680 for (k = 0; k < 4; k++)
681 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
682 j++;
683 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000684 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000685 (*naddrs)--;
686 j--;
687 break;
688 }
689 }
690 }
691}
692
Rusty Russell5eed48a2000-06-02 20:12:24 +0000693/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
694static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000695find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000696{
Harald Welteed498492001-07-23 01:24:22 +0000697 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000698
Harald Welte43ac1912002-03-03 09:43:07 +0000699 if (string_to_number(pname, 0, 255, &proto) != -1) {
700 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000701
Harald Welte43ac1912002-03-03 09:43:07 +0000702 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000703 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000704 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000705 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000706
707 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708}
709
Harald Welte43ac1912002-03-03 09:43:07 +0000710u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711parse_protocol(const char *s)
712{
Harald Welteed498492001-07-23 01:24:22 +0000713 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000714
Harald Welteed498492001-07-23 01:24:22 +0000715 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716 struct protoent *pent;
717
Harald Weltecbe1ec72006-02-11 09:50:11 +0000718 /* first deal with the special case of 'all' to prevent
719 * people from being able to redefine 'all' in nsswitch
720 * and/or provoke expensive [not working] ldap/nis/...
721 * lookups */
722 if (!strcmp(s, "all"))
723 return 0;
724
Rusty Russell5eed48a2000-06-02 20:12:24 +0000725 if ((pent = getprotobyname(s)))
726 proto = pent->p_proto;
727 else {
728 unsigned int i;
729 for (i = 0;
730 i < sizeof(chain_protos)/sizeof(struct pprot);
731 i++) {
732 if (strcmp(s, chain_protos[i].name) == 0) {
733 proto = chain_protos[i].num;
734 break;
735 }
736 }
737 if (i == sizeof(chain_protos)/sizeof(struct pprot))
738 exit_error(PARAMETER_PROBLEM,
739 "unknown protocol `%s' specified",
740 s);
741 }
742 }
743
744 return (u_int16_t)proto;
745}
746
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000747/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000748static int is_exthdr(u_int16_t proto)
749{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000750 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000751 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000752 proto == IPPROTO_AH ||
753 proto == IPPROTO_DSTOPTS);
754}
755
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756/* Can't be zero. */
757static int
758parse_rulenumber(const char *rule)
759{
Harald Welte43ac1912002-03-03 09:43:07 +0000760 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761
Harald Welte43ac1912002-03-03 09:43:07 +0000762 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000763 exit_error(PARAMETER_PROBLEM,
764 "Invalid rule number `%s'", rule);
765
766 return rulenum;
767}
768
769static const char *
770parse_target(const char *targetname)
771{
772 const char *ptr;
773
774 if (strlen(targetname) < 1)
775 exit_error(PARAMETER_PROBLEM,
776 "Invalid target name (too short)");
777
778 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
779 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000780 "Invalid target name `%s' (%u chars max)",
781 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782
783 for (ptr = targetname; *ptr; ptr++)
784 if (isspace(*ptr))
785 exit_error(PARAMETER_PROBLEM,
786 "Invalid target name `%s'", targetname);
787 return targetname;
788}
789
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790static void
791set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
792 int invert)
793{
794 if (*options & option)
795 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
796 opt2char(option));
797 *options |= option;
798
799 if (invert) {
800 unsigned int i;
801 for (i = 0; 1 << i != option; i++);
802
803 if (!inverse_for_options[i])
804 exit_error(PARAMETER_PROBLEM,
805 "cannot have ! before -%c",
806 opt2char(option));
807 *invflg |= inverse_for_options[i];
808 }
809}
810
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000812merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 unsigned int *option_offset)
814{
815 unsigned int num_old, num_new, i;
816 struct option *merge;
817
Jan Engelhardtd0145402007-07-30 14:32:26 +0000818 if (newopts == NULL)
819 return oldopts;
820
Rusty Russell5eed48a2000-06-02 20:12:24 +0000821 for (num_old = 0; oldopts[num_old].name; num_old++);
822 for (num_new = 0; newopts[num_new].name; num_new++);
823
824 global_option_offset += OPTION_OFFSET;
825 *option_offset = global_option_offset;
826
827 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
828 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000829 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830 for (i = 0; i < num_new; i++) {
831 merge[num_old + i] = newopts[i];
832 merge[num_old + i].val += *option_offset;
833 }
834 memset(merge + num_old + num_new, 0, sizeof(struct option));
835
836 return merge;
837}
838
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000839void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000840{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000841 me->family = AF_INET6;
842 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000843}
844
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000845void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000846{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000847 me->family = AF_INET6;
848 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000849}
850
851static void
852print_num(u_int64_t number, unsigned int format)
853{
Harald Welte43ac1912002-03-03 09:43:07 +0000854 if (format & FMT_KILOMEGAGIGA) {
855 if (number > 99999) {
856 number = (number + 500) / 1000;
857 if (number > 9999) {
858 number = (number + 500) / 1000;
859 if (number > 9999) {
860 number = (number + 500) / 1000;
861 if (number > 9999) {
862 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000863 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000864 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000865 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000866 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000867 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000868 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000869 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000870 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000871 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000872 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000873 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000874}
875
Harald Welte43ac1912002-03-03 09:43:07 +0000876
Rusty Russell5eed48a2000-06-02 20:12:24 +0000877static void
878print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
879{
880 struct ip6t_counters counters;
881 const char *pol = ip6tc_get_policy(chain, &counters, handle);
882 printf("Chain %s", chain);
883 if (pol) {
884 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000885 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000886 fputc(' ', stdout);
887 print_num(counters.pcnt, (format|FMT_NOTABLE));
888 fputs("packets, ", stdout);
889 print_num(counters.bcnt, (format|FMT_NOTABLE));
890 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000891 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892 printf(")\n");
893 } else {
894 unsigned int refs;
895 if (!ip6tc_get_references(&refs, chain, handle))
896 printf(" (ERROR obtaining refs)\n");
897 else
898 printf(" (%u references)\n", refs);
899 }
900
901 if (format & FMT_LINENUMBERS)
902 printf(FMT("%-4s ", "%s "), "num");
903 if (!(format & FMT_NOCOUNTS)) {
904 if (format & FMT_KILOMEGAGIGA) {
905 printf(FMT("%5s ","%s "), "pkts");
906 printf(FMT("%5s ","%s "), "bytes");
907 } else {
908 printf(FMT("%8s ","%s "), "pkts");
909 printf(FMT("%10s ","%s "), "bytes");
910 }
911 }
912 if (!(format & FMT_NOTARGET))
913 printf(FMT("%-9s ","%s "), "target");
914 fputs(" prot ", stdout);
915 if (format & FMT_OPTIONS)
916 fputs("opt", stdout);
917 if (format & FMT_VIA) {
918 printf(FMT(" %-6s ","%s "), "in");
919 printf(FMT("%-6s ","%s "), "out");
920 }
921 printf(FMT(" %-19s ","%s "), "source");
922 printf(FMT(" %-19s "," %s "), "destination");
923 printf("\n");
924}
925
Harald Welte43ac1912002-03-03 09:43:07 +0000926
Rusty Russell5eed48a2000-06-02 20:12:24 +0000927static int
928print_match(const struct ip6t_entry_match *m,
929 const struct ip6t_ip6 *ip,
930 int numeric)
931{
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000932 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933
934 if (match) {
935 if (match->print)
936 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000937 else
938 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000939 } else {
940 if (m->u.user.name[0])
941 printf("UNKNOWN match `%s' ", m->u.user.name);
942 }
943 /* Don't stop iterating. */
944 return 0;
945}
946
947/* e is called `fw' here for hysterical raisins */
948static void
949print_firewall(const struct ip6t_entry *fw,
950 const char *targname,
951 unsigned int num,
952 unsigned int format,
953 const ip6tc_handle_t handle)
954{
955 struct ip6tables_target *target = NULL;
956 const struct ip6t_entry_target *t;
957 u_int8_t flags;
958 char buf[BUFSIZ];
959
Rusty Russell5eed48a2000-06-02 20:12:24 +0000960 if (!ip6tc_is_chain(targname, handle))
961 target = find_target(targname, TRY_LOAD);
962 else
963 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
964
965 t = ip6t_get_target((struct ip6t_entry *)fw);
966 flags = fw->ipv6.flags;
967
968 if (format & FMT_LINENUMBERS)
969 printf(FMT("%-4u ", "%u "), num+1);
970
971 if (!(format & FMT_NOCOUNTS)) {
972 print_num(fw->counters.pcnt, format);
973 print_num(fw->counters.bcnt, format);
974 }
975
976 if (!(format & FMT_NOTARGET))
977 printf(FMT("%-9s ", "%s "), targname);
978
979 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
980 {
Harald Welte43ac1912002-03-03 09:43:07 +0000981 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000982 if (pname)
983 printf(FMT("%-5s", "%s "), pname);
984 else
985 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
986 }
987
988 if (format & FMT_OPTIONS) {
989 if (format & FMT_NOTABLE)
990 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000991 fputc(' ', stdout); /* Invert flag of FRAG */
992 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993 fputc(' ', stdout);
994 }
995
996 if (format & FMT_VIA) {
997 char iface[IFNAMSIZ+2];
998
999 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1000 iface[0] = '!';
1001 iface[1] = '\0';
1002 }
1003 else iface[0] = '\0';
1004
1005 if (fw->ipv6.iniface[0] != '\0') {
1006 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001007 }
1008 else if (format & FMT_NUMERIC) strcat(iface, "*");
1009 else strcat(iface, "any");
1010 printf(FMT(" %-6s ","in %s "), iface);
1011
1012 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1013 iface[0] = '!';
1014 iface[1] = '\0';
1015 }
1016 else iface[0] = '\0';
1017
1018 if (fw->ipv6.outiface[0] != '\0') {
1019 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001020 }
1021 else if (format & FMT_NUMERIC) strcat(iface, "*");
1022 else strcat(iface, "any");
1023 printf(FMT("%-6s ","out %s "), iface);
1024 }
1025
1026 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1027 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1028 && !(format & FMT_NUMERIC))
1029 printf(FMT("%-19s ","%s "), "anywhere");
1030 else {
1031 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001032 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001033 else
Harald Welte43ac1912002-03-03 09:43:07 +00001034 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001035 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1036 printf(FMT("%-19s ","%s "), buf);
1037 }
1038
1039 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1040 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1041 && !(format & FMT_NUMERIC))
1042 printf(FMT("%-19s","-> %s"), "anywhere");
1043 else {
1044 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001045 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001046 else
Harald Welte43ac1912002-03-03 09:43:07 +00001047 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1049 printf(FMT("%-19s","-> %s"), buf);
1050 }
1051
1052 if (format & FMT_NOTABLE)
1053 fputs(" ", stdout);
1054
1055 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1056
1057 if (target) {
1058 if (target->print)
1059 /* Print the target information. */
1060 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1061 } else if (t->u.target_size != sizeof(*t))
1062 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001063 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001064
1065 if (!(format & FMT_NONEWLINE))
1066 fputc('\n', stdout);
1067}
1068
1069static void
1070print_firewall_line(const struct ip6t_entry *fw,
1071 const ip6tc_handle_t h)
1072{
1073 struct ip6t_entry_target *t;
1074
1075 t = ip6t_get_target((struct ip6t_entry *)fw);
1076 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1077}
1078
1079static int
1080append_entry(const ip6t_chainlabel chain,
1081 struct ip6t_entry *fw,
1082 unsigned int nsaddrs,
1083 const struct in6_addr saddrs[],
1084 unsigned int ndaddrs,
1085 const struct in6_addr daddrs[],
1086 int verbose,
1087 ip6tc_handle_t *handle)
1088{
1089 unsigned int i, j;
1090 int ret = 1;
1091
1092 for (i = 0; i < nsaddrs; i++) {
1093 fw->ipv6.src = saddrs[i];
1094 for (j = 0; j < ndaddrs; j++) {
1095 fw->ipv6.dst = daddrs[j];
1096 if (verbose)
1097 print_firewall_line(fw, *handle);
1098 ret &= ip6tc_append_entry(chain, fw, handle);
1099 }
1100 }
1101
1102 return ret;
1103}
1104
1105static int
1106replace_entry(const ip6t_chainlabel chain,
1107 struct ip6t_entry *fw,
1108 unsigned int rulenum,
1109 const struct in6_addr *saddr,
1110 const struct in6_addr *daddr,
1111 int verbose,
1112 ip6tc_handle_t *handle)
1113{
1114 fw->ipv6.src = *saddr;
1115 fw->ipv6.dst = *daddr;
1116
1117 if (verbose)
1118 print_firewall_line(fw, *handle);
1119 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1120}
1121
1122static int
1123insert_entry(const ip6t_chainlabel chain,
1124 struct ip6t_entry *fw,
1125 unsigned int rulenum,
1126 unsigned int nsaddrs,
1127 const struct in6_addr saddrs[],
1128 unsigned int ndaddrs,
1129 const struct in6_addr daddrs[],
1130 int verbose,
1131 ip6tc_handle_t *handle)
1132{
1133 unsigned int i, j;
1134 int ret = 1;
1135
1136 for (i = 0; i < nsaddrs; i++) {
1137 fw->ipv6.src = saddrs[i];
1138 for (j = 0; j < ndaddrs; j++) {
1139 fw->ipv6.dst = daddrs[j];
1140 if (verbose)
1141 print_firewall_line(fw, *handle);
1142 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1143 }
1144 }
1145
1146 return ret;
1147}
1148
1149static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001150make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001151{
1152 /* Establish mask for comparison */
1153 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001154 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001155 unsigned char *mask, *mptr;
1156
1157 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001158 for (matchp = matches; matchp; matchp = matchp->next)
1159 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001160
1161 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001162 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001163 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001164
1165 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1166 mptr = mask + sizeof(struct ip6t_entry);
1167
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001168 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001169 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001170 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001171 + matchp->match->userspacesize);
1172 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001173 }
1174
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001175 memset(mptr, 0xFF,
1176 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001177 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001178
1179 return mask;
1180}
1181
1182static int
1183delete_entry(const ip6t_chainlabel chain,
1184 struct ip6t_entry *fw,
1185 unsigned int nsaddrs,
1186 const struct in6_addr saddrs[],
1187 unsigned int ndaddrs,
1188 const struct in6_addr daddrs[],
1189 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001190 ip6tc_handle_t *handle,
1191 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001192{
1193 unsigned int i, j;
1194 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001195 unsigned char *mask;
1196
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001197 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001198 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001199 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001200 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001201 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001202 if (verbose)
1203 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001204 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001205 }
1206 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001207 free(mask);
1208
Rusty Russell5eed48a2000-06-02 20:12:24 +00001209 return ret;
1210}
1211
András Kis-Szabó764316a2001-02-26 17:31:20 +00001212int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001213for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1214 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1215{
1216 int ret = 1;
1217 const char *chain;
1218 char *chains;
1219 unsigned int i, chaincount = 0;
1220
1221 chain = ip6tc_first_chain(handle);
1222 while (chain) {
1223 chaincount++;
1224 chain = ip6tc_next_chain(handle);
1225 }
1226
1227 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1228 i = 0;
1229 chain = ip6tc_first_chain(handle);
1230 while (chain) {
1231 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1232 i++;
1233 chain = ip6tc_next_chain(handle);
1234 }
1235
1236 for (i = 0; i < chaincount; i++) {
1237 if (!builtinstoo
1238 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001239 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 continue;
1241 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1242 }
1243
1244 free(chains);
1245 return ret;
1246}
1247
András Kis-Szabó764316a2001-02-26 17:31:20 +00001248int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001249flush_entries(const ip6t_chainlabel chain, int verbose,
1250 ip6tc_handle_t *handle)
1251{
1252 if (!chain)
1253 return for_each_chain(flush_entries, verbose, 1, handle);
1254
1255 if (verbose)
1256 fprintf(stdout, "Flushing chain `%s'\n", chain);
1257 return ip6tc_flush_entries(chain, handle);
1258}
1259
1260static int
1261zero_entries(const ip6t_chainlabel chain, int verbose,
1262 ip6tc_handle_t *handle)
1263{
1264 if (!chain)
1265 return for_each_chain(zero_entries, verbose, 1, handle);
1266
1267 if (verbose)
1268 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1269 return ip6tc_zero_entries(chain, handle);
1270}
1271
András Kis-Szabó764316a2001-02-26 17:31:20 +00001272int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001273delete_chain(const ip6t_chainlabel chain, int verbose,
1274 ip6tc_handle_t *handle)
1275{
1276 if (!chain)
1277 return for_each_chain(delete_chain, verbose, 0, handle);
1278
1279 if (verbose)
1280 fprintf(stdout, "Deleting chain `%s'\n", chain);
1281 return ip6tc_delete_chain(chain, handle);
1282}
1283
1284static int
1285list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1286 int expanded, int linenumbers, ip6tc_handle_t *handle)
1287{
1288 int found = 0;
1289 unsigned int format;
1290 const char *this;
1291
1292 format = FMT_OPTIONS;
1293 if (!verbose)
1294 format |= FMT_NOCOUNTS;
1295 else
1296 format |= FMT_VIA;
1297
1298 if (numeric)
1299 format |= FMT_NUMERIC;
1300
1301 if (!expanded)
1302 format |= FMT_KILOMEGAGIGA;
1303
1304 if (linenumbers)
1305 format |= FMT_LINENUMBERS;
1306
1307 for (this = ip6tc_first_chain(handle);
1308 this;
1309 this = ip6tc_next_chain(handle)) {
1310 const struct ip6t_entry *i;
1311 unsigned int num;
1312
1313 if (chain && strcmp(chain, this) != 0)
1314 continue;
1315
1316 if (found) printf("\n");
1317
1318 print_header(format, this, handle);
1319 i = ip6tc_first_rule(this, handle);
1320
1321 num = 0;
1322 while (i) {
1323 print_firewall(i,
1324 ip6tc_get_target(i, handle),
1325 num++,
1326 format,
1327 *handle);
1328 i = ip6tc_next_rule(i, handle);
1329 }
1330 found = 1;
1331 }
1332
1333 errno = ENOENT;
1334 return found;
1335}
1336
1337static struct ip6t_entry *
1338generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001339 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001340 struct ip6t_entry_target *target)
1341{
1342 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001343 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001344 struct ip6t_entry *e;
1345
1346 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001347 for (matchp = matches; matchp; matchp = matchp->next)
1348 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349
1350 e = fw_malloc(size + target->u.target_size);
1351 *e = *fw;
1352 e->target_offset = size;
1353 e->next_offset = size + target->u.target_size;
1354
1355 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001356 for (matchp = matches; matchp; matchp = matchp->next) {
1357 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1358 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001359 }
1360 memcpy(e->elems + size, target, target->u.target_size);
1361
1362 return e;
1363}
1364
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001365void clear_rule_matches(struct ip6tables_rule_match **matches)
1366{
1367 struct ip6tables_rule_match *matchp, *tmp;
1368
1369 for (matchp = *matches; matchp;) {
1370 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001371 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001372 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001373 matchp->match->m = NULL;
1374 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001375 if (matchp->match == matchp->match->next) {
1376 free(matchp->match);
1377 matchp->match = NULL;
1378 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001379 free(matchp);
1380 matchp = tmp;
1381 }
1382
1383 *matches = NULL;
1384}
1385
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001386static void set_revision(char *name, u_int8_t revision)
1387{
1388 /* Old kernel sources don't have ".revision" field,
1389 but we stole a byte from name. */
1390 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1391 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1392}
1393
Rusty Russell5eed48a2000-06-02 20:12:24 +00001394int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1395{
1396 struct ip6t_entry fw, *e = NULL;
1397 int invert = 0;
1398 unsigned int nsaddrs = 0, ndaddrs = 0;
1399 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1400
1401 int c, verbose = 0;
1402 const char *chain = NULL;
1403 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1404 const char *policy = NULL, *newname = NULL;
1405 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001406 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001407 int ret = 1;
1408 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001409 struct ip6tables_rule_match *matches = NULL;
1410 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001411 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001412 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 const char *jumpto = "";
1414 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001415 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001416 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417
1418 memset(&fw, 0, sizeof(fw));
1419
Harald Welte43ac1912002-03-03 09:43:07 +00001420 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001421 * a second time */
1422 optind = 0;
1423
Harald Welte43ac1912002-03-03 09:43:07 +00001424 /* clear mflags in case do_command gets called a second time
1425 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001426 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001427 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001428
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001429 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001430 t->tflags = 0;
1431 t->used = 0;
1432 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001433
Rusty Russell5eed48a2000-06-02 20:12:24 +00001434 /* Suppress error messages: we may add new options if we
1435 demand-load a protocol. */
1436 opterr = 0;
1437
1438 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001439 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440 opts, NULL)) != -1) {
1441 switch (c) {
1442 /*
1443 * Command selection
1444 */
1445 case 'A':
1446 add_command(&command, CMD_APPEND, CMD_NONE,
1447 invert);
1448 chain = optarg;
1449 break;
1450
1451 case 'D':
1452 add_command(&command, CMD_DELETE, CMD_NONE,
1453 invert);
1454 chain = optarg;
1455 if (optind < argc && argv[optind][0] != '-'
1456 && argv[optind][0] != '!') {
1457 rulenum = parse_rulenumber(argv[optind++]);
1458 command = CMD_DELETE_NUM;
1459 }
1460 break;
1461
Rusty Russell5eed48a2000-06-02 20:12:24 +00001462 case 'R':
1463 add_command(&command, CMD_REPLACE, CMD_NONE,
1464 invert);
1465 chain = optarg;
1466 if (optind < argc && argv[optind][0] != '-'
1467 && argv[optind][0] != '!')
1468 rulenum = parse_rulenumber(argv[optind++]);
1469 else
1470 exit_error(PARAMETER_PROBLEM,
1471 "-%c requires a rule number",
1472 cmd2char(CMD_REPLACE));
1473 break;
1474
1475 case 'I':
1476 add_command(&command, CMD_INSERT, CMD_NONE,
1477 invert);
1478 chain = optarg;
1479 if (optind < argc && argv[optind][0] != '-'
1480 && argv[optind][0] != '!')
1481 rulenum = parse_rulenumber(argv[optind++]);
1482 else rulenum = 1;
1483 break;
1484
1485 case 'L':
1486 add_command(&command, CMD_LIST, CMD_ZERO,
1487 invert);
1488 if (optarg) chain = optarg;
1489 else if (optind < argc && argv[optind][0] != '-'
1490 && argv[optind][0] != '!')
1491 chain = argv[optind++];
1492 break;
1493
1494 case 'F':
1495 add_command(&command, CMD_FLUSH, CMD_NONE,
1496 invert);
1497 if (optarg) chain = optarg;
1498 else if (optind < argc && argv[optind][0] != '-'
1499 && argv[optind][0] != '!')
1500 chain = argv[optind++];
1501 break;
1502
1503 case 'Z':
1504 add_command(&command, CMD_ZERO, CMD_LIST,
1505 invert);
1506 if (optarg) chain = optarg;
1507 else if (optind < argc && argv[optind][0] != '-'
1508 && argv[optind][0] != '!')
1509 chain = argv[optind++];
1510 break;
1511
1512 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001513 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001514 exit_error(PARAMETER_PROBLEM,
1515 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001516 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001517 if (find_target(optarg, TRY_LOAD))
1518 exit_error(PARAMETER_PROBLEM,
1519 "chain name may not clash "
1520 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001521 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1522 invert);
1523 chain = optarg;
1524 break;
1525
1526 case 'X':
1527 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1528 invert);
1529 if (optarg) chain = optarg;
1530 else if (optind < argc && argv[optind][0] != '-'
1531 && argv[optind][0] != '!')
1532 chain = argv[optind++];
1533 break;
1534
1535 case 'E':
1536 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1537 invert);
1538 chain = optarg;
1539 if (optind < argc && argv[optind][0] != '-'
1540 && argv[optind][0] != '!')
1541 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001542 else
1543 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001544 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001545 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001546 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 break;
1548
1549 case 'P':
1550 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1551 invert);
1552 chain = optarg;
1553 if (optind < argc && argv[optind][0] != '-'
1554 && argv[optind][0] != '!')
1555 policy = argv[optind++];
1556 else
1557 exit_error(PARAMETER_PROBLEM,
1558 "-%c requires a chain and a policy",
1559 cmd2char(CMD_SET_POLICY));
1560 break;
1561
1562 case 'h':
1563 if (!optarg)
1564 optarg = argv[optind];
1565
Jonas Berlin1b91e592005-04-01 06:58:38 +00001566 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001567 if (!matches && protocol)
1568 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001569
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001570 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001571
1572 /*
1573 * Option selection
1574 */
1575 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001576 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001577 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1578 invert);
1579
1580 /* Canonicalize into lower case */
1581 for (protocol = argv[optind-1]; *protocol; protocol++)
1582 *protocol = tolower(*protocol);
1583
1584 protocol = argv[optind-1];
1585 fw.ipv6.proto = parse_protocol(protocol);
1586 fw.ipv6.flags |= IP6T_F_PROTO;
1587
1588 if (fw.ipv6.proto == 0
1589 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1590 exit_error(PARAMETER_PROBLEM,
1591 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001592
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001593 if (is_exthdr(fw.ipv6.proto)
1594 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001595 fprintf(stderr,
1596 "Warning: never matched protocol: %s. "
1597 "use extension match instead.\n",
1598 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001599 break;
1600
1601 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001602 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001603 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1604 invert);
1605 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001606 break;
1607
1608 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001609 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001610 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1611 invert);
1612 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001613 break;
1614
1615 case 'j':
1616 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1617 invert);
1618 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001619 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001620 target = find_target(jumpto, TRY_LOAD);
1621
1622 if (target) {
1623 size_t size;
1624
Harald Welte43ac1912002-03-03 09:43:07 +00001625 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1626 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001627
1628 target->t = fw_calloc(1, size);
1629 target->t->u.target_size = size;
1630 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001631 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001632 target->init(target->t);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001633 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 }
1635 break;
1636
1637
1638 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001639 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001640 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1641 invert);
1642 parse_interface(argv[optind-1],
1643 fw.ipv6.iniface,
1644 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001645 break;
1646
1647 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001648 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1650 invert);
1651 parse_interface(argv[optind-1],
1652 fw.ipv6.outiface,
1653 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001654 break;
1655
1656 case 'v':
1657 if (!verbose)
1658 set_option(&options, OPT_VERBOSE,
1659 &fw.ipv6.invflags, invert);
1660 verbose++;
1661 break;
1662
1663 case 'm': {
1664 size_t size;
1665
1666 if (invert)
1667 exit_error(PARAMETER_PROBLEM,
1668 "unexpected ! flag before --match");
1669
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001670 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001671 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1672 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673 m->m = fw_calloc(1, size);
1674 m->m->u.match_size = size;
1675 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001676 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001677 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001678 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001679 if (m != m->next)
1680 /* Merge options for non-cloned matches */
1681 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001682 }
1683 break;
1684
1685 case 'n':
1686 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1687 invert);
1688 break;
1689
1690 case 't':
1691 if (invert)
1692 exit_error(PARAMETER_PROBLEM,
1693 "unexpected ! flag before --table");
1694 *table = argv[optind-1];
1695 break;
1696
1697 case 'x':
1698 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1699 invert);
1700 break;
1701
1702 case 'V':
1703 if (invert)
1704 printf("Not %s ;-)\n", program_version);
1705 else
1706 printf("%s v%s\n",
1707 program_name, program_version);
1708 exit(0);
1709
1710 case '0':
1711 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1712 invert);
1713 break;
1714
Harald Welte43ac1912002-03-03 09:43:07 +00001715 case 'M':
1716 modprobe = optarg;
1717 break;
1718
1719 case 'c':
1720
1721 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1722 invert);
1723 pcnt = optarg;
1724 if (optind < argc && argv[optind][0] != '-'
1725 && argv[optind][0] != '!')
1726 bcnt = argv[optind++];
1727 else
1728 exit_error(PARAMETER_PROBLEM,
1729 "-%c requires packet and byte counter",
1730 opt2char(OPT_COUNTERS));
1731
Patrick McHardy875441e2007-10-17 08:48:58 +00001732 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001733 exit_error(PARAMETER_PROBLEM,
1734 "-%c packet counter not numeric",
1735 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001736 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001737
Patrick McHardy875441e2007-10-17 08:48:58 +00001738 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001739 exit_error(PARAMETER_PROBLEM,
1740 "-%c byte counter not numeric",
1741 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001742 fw.counters.bcnt = cnt;
1743
Harald Welte43ac1912002-03-03 09:43:07 +00001744 break;
1745
1746
Rusty Russell5eed48a2000-06-02 20:12:24 +00001747 case 1: /* non option */
1748 if (optarg[0] == '!' && optarg[1] == '\0') {
1749 if (invert)
1750 exit_error(PARAMETER_PROBLEM,
1751 "multiple consecutive ! not"
1752 " allowed");
1753 invert = TRUE;
1754 optarg[0] = '\0';
1755 continue;
1756 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001757 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001758 exit_tryhelp(2);
1759
1760 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001761 if (!target
1762 || !(target->parse(c - target->option_offset,
1763 argv, invert,
1764 &target->tflags,
1765 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001766 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001767 if (matchp->completed)
1768 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001769 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001770 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001771 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001772 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001773 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001774 break;
1775 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001776 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001777
1778 /* If you listen carefully, you can
1779 actually hear this code suck. */
1780
1781 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001782 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001783 * parameter, that has not been parsed yet,
1784 * it's not an option of an explicitly loaded
1785 * match or a target. However, we support
1786 * implicit loading of the protocol match
1787 * extension. '-p tcp' means 'l4 proto 6' and
1788 * at the same time 'load tcp protocol match on
1789 * demand if we specify --dport'.
1790 *
1791 * To make this work, we need to make sure:
1792 * - the parameter has not been parsed by
1793 * a match (m above)
1794 * - a protocol has been specified
1795 * - the protocol extension has not been
1796 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001797 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001798 * - the protocol extension can be successively
1799 * loaded
1800 */
1801 if (m == NULL
1802 && protocol
1803 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001804 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001805 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001806 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001807 && (proto_used == 0))
1808 )
1809 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001810 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001811 /* Try loading protocol */
1812 size_t size;
1813
1814 proto_used = 1;
1815
1816 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1817 + m->size;
1818
1819 m->m = fw_calloc(1, size);
1820 m->m->u.match_size = size;
1821 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001822 set_revision(m->m->u.user.name,
1823 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001824 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001825 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001826
1827 opts = merge_options(opts,
1828 m->extra_opts, &m->option_offset);
1829
1830 optind--;
1831 continue;
1832 }
1833
Rusty Russell5eed48a2000-06-02 20:12:24 +00001834 if (!m)
1835 exit_error(PARAMETER_PROBLEM,
1836 "Unknown arg `%s'",
1837 argv[optind-1]);
1838 }
1839 }
1840 invert = FALSE;
1841 }
1842
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001843 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001844 if (matchp->match->final_check != NULL)
1845 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001846
Jan Engelhardt830132a2007-10-04 16:24:50 +00001847 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001848 target->final_check(target->tflags);
1849
1850 /* Fix me: must put inverse options checking here --MN */
1851
1852 if (optind < argc)
1853 exit_error(PARAMETER_PROBLEM,
1854 "unknown arguments found on commandline");
1855 if (!command)
1856 exit_error(PARAMETER_PROBLEM, "no command specified");
1857 if (invert)
1858 exit_error(PARAMETER_PROBLEM,
1859 "nothing appropriate following !");
1860
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001861 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001863 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001864 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001865 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866 }
1867
1868 if (shostnetworkmask)
1869 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1870 &(fw.ipv6.smsk), &nsaddrs);
1871
1872 if (dhostnetworkmask)
1873 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1874 &(fw.ipv6.dmsk), &ndaddrs);
1875
1876 if ((nsaddrs > 1 || ndaddrs > 1) &&
1877 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1878 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1879 " source or destination IP addresses");
1880
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1882 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1883 "specify a unique address");
1884
1885 generic_opt_check(command, options);
1886
1887 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1888 exit_error(PARAMETER_PROBLEM,
1889 "chain name `%s' too long (must be under %i chars)",
1890 chain, IP6T_FUNCTION_MAXNAMELEN);
1891
Harald Welte43ac1912002-03-03 09:43:07 +00001892 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001893 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001894 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001895
Rusty Russell8beb0492004-12-22 00:37:10 +00001896 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001897 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001898 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001899
Harald Welte43ac1912002-03-03 09:43:07 +00001900 if (!*handle)
1901 exit_error(VERSION_PROBLEM,
1902 "can't initialize ip6tables table `%s': %s",
1903 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001904
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001905 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001906 || command == CMD_DELETE
1907 || command == CMD_INSERT
1908 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001909 if (strcmp(chain, "PREROUTING") == 0
1910 || strcmp(chain, "INPUT") == 0) {
1911 /* -o not valid with incoming packets. */
1912 if (options & OPT_VIANAMEOUT)
1913 exit_error(PARAMETER_PROBLEM,
1914 "Can't use -%c with %s\n",
1915 opt2char(OPT_VIANAMEOUT),
1916 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001917 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001918
Harald Welte43ac1912002-03-03 09:43:07 +00001919 if (strcmp(chain, "POSTROUTING") == 0
1920 || strcmp(chain, "OUTPUT") == 0) {
1921 /* -i not valid with outgoing packets */
1922 if (options & OPT_VIANAMEIN)
1923 exit_error(PARAMETER_PROBLEM,
1924 "Can't use -%c with %s\n",
1925 opt2char(OPT_VIANAMEIN),
1926 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001927 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928
1929 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001930 fprintf(stderr,
1931 "Warning: using chain %s, not extension\n",
1932 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001934 if (target->t)
1935 free(target->t);
1936
Rusty Russell5eed48a2000-06-02 20:12:24 +00001937 target = NULL;
1938 }
1939
1940 /* If they didn't specify a target, or it's a chain
1941 name, use standard. */
1942 if (!target
1943 && (strlen(jumpto) == 0
1944 || ip6tc_is_chain(jumpto, *handle))) {
1945 size_t size;
1946
1947 target = find_target(IP6T_STANDARD_TARGET,
1948 LOAD_MUST_SUCCEED);
1949
1950 size = sizeof(struct ip6t_entry_target)
1951 + target->size;
1952 target->t = fw_calloc(1, size);
1953 target->t->u.target_size = size;
1954 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001955 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001956 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 }
1958
1959 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001960 /* it is no chain, and we can't load a plugin.
1961 * We cannot know if the plugin is corrupt, non
1962 * existant OR if the user just misspelled a
1963 * chain. */
1964 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001965 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001966 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001967 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 }
1969 }
1970
1971 switch (command) {
1972 case CMD_APPEND:
1973 ret = append_entry(chain, e,
1974 nsaddrs, saddrs, ndaddrs, daddrs,
1975 options&OPT_VERBOSE,
1976 handle);
1977 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 case CMD_DELETE:
1979 ret = delete_entry(chain, e,
1980 nsaddrs, saddrs, ndaddrs, daddrs,
1981 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001982 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001983 break;
1984 case CMD_DELETE_NUM:
1985 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1986 break;
1987 case CMD_REPLACE:
1988 ret = replace_entry(chain, e, rulenum - 1,
1989 saddrs, daddrs, options&OPT_VERBOSE,
1990 handle);
1991 break;
1992 case CMD_INSERT:
1993 ret = insert_entry(chain, e, rulenum - 1,
1994 nsaddrs, saddrs, ndaddrs, daddrs,
1995 options&OPT_VERBOSE,
1996 handle);
1997 break;
1998 case CMD_LIST:
1999 ret = list_entries(chain,
2000 options&OPT_VERBOSE,
2001 options&OPT_NUMERIC,
2002 options&OPT_EXPANDED,
2003 options&OPT_LINENUMBERS,
2004 handle);
2005 break;
2006 case CMD_FLUSH:
2007 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2008 break;
2009 case CMD_ZERO:
2010 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2011 break;
2012 case CMD_LIST|CMD_ZERO:
2013 ret = list_entries(chain,
2014 options&OPT_VERBOSE,
2015 options&OPT_NUMERIC,
2016 options&OPT_EXPANDED,
2017 options&OPT_LINENUMBERS,
2018 handle);
2019 if (ret)
2020 ret = zero_entries(chain,
2021 options&OPT_VERBOSE, handle);
2022 break;
2023 case CMD_NEW_CHAIN:
2024 ret = ip6tc_create_chain(chain, handle);
2025 break;
2026 case CMD_DELETE_CHAIN:
2027 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2028 break;
2029 case CMD_RENAME_CHAIN:
2030 ret = ip6tc_rename_chain(chain, newname, handle);
2031 break;
2032 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002033 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002034 break;
2035 default:
2036 /* We should never reach this... */
2037 exit_tryhelp(2);
2038 }
2039
2040 if (verbose > 1)
2041 dump_entries6(*handle);
2042
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002043 clear_rule_matches(&matches);
2044
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002045 if (e != NULL) {
2046 free(e);
2047 e = NULL;
2048 }
2049
2050 for (c = 0; c < nsaddrs; c++)
2051 free(&saddrs[c]);
2052
2053 for (c = 0; c < ndaddrs; c++)
2054 free(&daddrs[c]);
2055
Pablo Neiradfdcd642005-05-29 19:05:23 +00002056 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002057
Rusty Russell5eed48a2000-06-02 20:12:24 +00002058 return ret;
2059}