blob: aeebed4cf4284dcea60d3cc5615542263ed94ef1 [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
Rusty Russell5eed48a2000-06-02 20:12:24 +0000270void
Jan Engelhardtd8840512007-08-01 15:19:15 +0000271exit_error(enum exittype status, const char *msg, ...)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272{
273 va_list args;
274
275 va_start(args, msg);
276 fprintf(stderr, "%s v%s: ", program_name, program_version);
277 vfprintf(stderr, msg, args);
278 va_end(args);
279 fprintf(stderr, "\n");
280 if (status == PARAMETER_PROBLEM)
281 exit_tryhelp(status);
282 if (status == VERSION_PROBLEM)
283 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000284 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000285 /* On error paths, make sure that we don't leak memory */
286 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000287 exit(status);
288}
289
290void
291exit_tryhelp(int status)
292{
Harald Weltea8658ca2003-03-05 07:46:15 +0000293 if (line != -1)
294 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000295 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
296 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000297 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000298 exit(status);
299}
300
301void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000302exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000303{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000304 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000305 struct ip6tables_target *t = NULL;
306
307 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000308"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000309" %s -[RI] chain rulenum rule-specification [options]\n"
310" %s -D chain rulenum [options]\n"
311" %s -[LFZ] [chain] [options]\n"
312" %s -[NX] chain\n"
313" %s -E old-chain-name new-chain-name\n"
314" %s -P chain target [options]\n"
315" %s -h (print this help information)\n\n",
316 program_name, program_version, program_name, program_name,
317 program_name, program_name, program_name, program_name,
318 program_name, program_name);
319
320 printf(
321"Commands:\n"
322"Either long or short options are allowed.\n"
323" --append -A chain Append to chain\n"
324" --delete -D chain Delete matching rule from chain\n"
325" --delete -D chain rulenum\n"
326" Delete rule rulenum (1 = first) from chain\n"
327" --insert -I chain [rulenum]\n"
328" Insert in chain as rulenum (default 1=first)\n"
329" --replace -R chain rulenum\n"
330" Replace rule rulenum (1 = first) in chain\n"
331" --list -L [chain] List the rules in a chain or all chains\n"
332" --flush -F [chain] Delete all rules in chain or all chains\n"
333" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334" --new -N chain Create a new user-defined chain\n"
335" --delete-chain\n"
336" -X [chain] Delete a user-defined chain\n"
337" --policy -P chain target\n"
338" Change policy on chain to target\n"
339" --rename-chain\n"
340" -E old-chain new-chain\n"
341" Change chain name, (moving any references)\n"
342
343"Options:\n"
344" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
345" --source -s [!] address[/mask]\n"
346" source specification\n"
347" --destination -d [!] address[/mask]\n"
348" destination specification\n"
349" --in-interface -i [!] input name[+]\n"
350" network interface name ([+] for wildcard)\n"
351" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000352" target for rule (may load target extension)\n"
353" --match -m match\n"
354" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000355" --numeric -n numeric output of addresses and ports\n"
356" --out-interface -o [!] output name[+]\n"
357" network interface name ([+] for wildcard)\n"
358" --table -t table table to manipulate (default: `filter')\n"
359" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000360" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000362/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000363" --modprobe=<command> try to insert modules using this command\n"
364" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000365"[!] --version -V print package version.\n");
366
367 /* Print out any special helps. A user might like to be able to add a --help
368 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000369 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000370 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000371 if (t->used) {
372 printf("\n");
373 t->help();
374 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000375 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000376 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000377 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000378 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000379 }
380 exit(0);
381}
382
383static 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;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416
1417 memset(&fw, 0, sizeof(fw));
1418
Harald Welte43ac1912002-03-03 09:43:07 +00001419 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001420 * a second time */
1421 optind = 0;
1422
Harald Welte43ac1912002-03-03 09:43:07 +00001423 /* clear mflags in case do_command gets called a second time
1424 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001425 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001426 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001427
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001428 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001429 t->tflags = 0;
1430 t->used = 0;
1431 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001432
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433 /* Suppress error messages: we may add new options if we
1434 demand-load a protocol. */
1435 opterr = 0;
1436
1437 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001438 "-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 +00001439 opts, NULL)) != -1) {
1440 switch (c) {
1441 /*
1442 * Command selection
1443 */
1444 case 'A':
1445 add_command(&command, CMD_APPEND, CMD_NONE,
1446 invert);
1447 chain = optarg;
1448 break;
1449
1450 case 'D':
1451 add_command(&command, CMD_DELETE, CMD_NONE,
1452 invert);
1453 chain = optarg;
1454 if (optind < argc && argv[optind][0] != '-'
1455 && argv[optind][0] != '!') {
1456 rulenum = parse_rulenumber(argv[optind++]);
1457 command = CMD_DELETE_NUM;
1458 }
1459 break;
1460
Rusty Russell5eed48a2000-06-02 20:12:24 +00001461 case 'R':
1462 add_command(&command, CMD_REPLACE, CMD_NONE,
1463 invert);
1464 chain = optarg;
1465 if (optind < argc && argv[optind][0] != '-'
1466 && argv[optind][0] != '!')
1467 rulenum = parse_rulenumber(argv[optind++]);
1468 else
1469 exit_error(PARAMETER_PROBLEM,
1470 "-%c requires a rule number",
1471 cmd2char(CMD_REPLACE));
1472 break;
1473
1474 case 'I':
1475 add_command(&command, CMD_INSERT, CMD_NONE,
1476 invert);
1477 chain = optarg;
1478 if (optind < argc && argv[optind][0] != '-'
1479 && argv[optind][0] != '!')
1480 rulenum = parse_rulenumber(argv[optind++]);
1481 else rulenum = 1;
1482 break;
1483
1484 case 'L':
1485 add_command(&command, CMD_LIST, CMD_ZERO,
1486 invert);
1487 if (optarg) chain = optarg;
1488 else if (optind < argc && argv[optind][0] != '-'
1489 && argv[optind][0] != '!')
1490 chain = argv[optind++];
1491 break;
1492
1493 case 'F':
1494 add_command(&command, CMD_FLUSH, CMD_NONE,
1495 invert);
1496 if (optarg) chain = optarg;
1497 else if (optind < argc && argv[optind][0] != '-'
1498 && argv[optind][0] != '!')
1499 chain = argv[optind++];
1500 break;
1501
1502 case 'Z':
1503 add_command(&command, CMD_ZERO, CMD_LIST,
1504 invert);
1505 if (optarg) chain = optarg;
1506 else if (optind < argc && argv[optind][0] != '-'
1507 && argv[optind][0] != '!')
1508 chain = argv[optind++];
1509 break;
1510
1511 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001512 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001513 exit_error(PARAMETER_PROBLEM,
1514 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001515 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001516 if (find_target(optarg, TRY_LOAD))
1517 exit_error(PARAMETER_PROBLEM,
1518 "chain name may not clash "
1519 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001520 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1521 invert);
1522 chain = optarg;
1523 break;
1524
1525 case 'X':
1526 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1527 invert);
1528 if (optarg) chain = optarg;
1529 else if (optind < argc && argv[optind][0] != '-'
1530 && argv[optind][0] != '!')
1531 chain = argv[optind++];
1532 break;
1533
1534 case 'E':
1535 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1536 invert);
1537 chain = optarg;
1538 if (optind < argc && argv[optind][0] != '-'
1539 && argv[optind][0] != '!')
1540 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001541 else
1542 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001543 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001544 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001545 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546 break;
1547
1548 case 'P':
1549 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1550 invert);
1551 chain = optarg;
1552 if (optind < argc && argv[optind][0] != '-'
1553 && argv[optind][0] != '!')
1554 policy = argv[optind++];
1555 else
1556 exit_error(PARAMETER_PROBLEM,
1557 "-%c requires a chain and a policy",
1558 cmd2char(CMD_SET_POLICY));
1559 break;
1560
1561 case 'h':
1562 if (!optarg)
1563 optarg = argv[optind];
1564
Jonas Berlin1b91e592005-04-01 06:58:38 +00001565 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001566 if (!matches && protocol)
1567 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001568
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001569 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001570
1571 /*
1572 * Option selection
1573 */
1574 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001575 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001576 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1577 invert);
1578
1579 /* Canonicalize into lower case */
1580 for (protocol = argv[optind-1]; *protocol; protocol++)
1581 *protocol = tolower(*protocol);
1582
1583 protocol = argv[optind-1];
1584 fw.ipv6.proto = parse_protocol(protocol);
1585 fw.ipv6.flags |= IP6T_F_PROTO;
1586
1587 if (fw.ipv6.proto == 0
1588 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1589 exit_error(PARAMETER_PROBLEM,
1590 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001591
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001592 if (is_exthdr(fw.ipv6.proto)
1593 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001594 printf("Warning: never matched protocol: %s. "
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001595 "use extension match instead.\n",
1596 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001597 break;
1598
1599 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001600 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1602 invert);
1603 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001604 break;
1605
1606 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001607 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1609 invert);
1610 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 break;
1612
1613 case 'j':
1614 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1615 invert);
1616 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001617 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001618 target = find_target(jumpto, TRY_LOAD);
1619
1620 if (target) {
1621 size_t size;
1622
Harald Welte43ac1912002-03-03 09:43:07 +00001623 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1624 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625
1626 target->t = fw_calloc(1, size);
1627 target->t->u.target_size = size;
1628 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001629 if (target->init != NULL)
1630 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001631 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632 }
1633 break;
1634
1635
1636 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001637 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001638 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1639 invert);
1640 parse_interface(argv[optind-1],
1641 fw.ipv6.iniface,
1642 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 break;
1644
1645 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001646 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001647 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1648 invert);
1649 parse_interface(argv[optind-1],
1650 fw.ipv6.outiface,
1651 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001652 break;
1653
1654 case 'v':
1655 if (!verbose)
1656 set_option(&options, OPT_VERBOSE,
1657 &fw.ipv6.invflags, invert);
1658 verbose++;
1659 break;
1660
1661 case 'm': {
1662 size_t size;
1663
1664 if (invert)
1665 exit_error(PARAMETER_PROBLEM,
1666 "unexpected ! flag before --match");
1667
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001668 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001669 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1670 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001671 m->m = fw_calloc(1, size);
1672 m->m->u.match_size = size;
1673 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001674 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001675 if (m->init != NULL)
1676 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001677 if (m != m->next)
1678 /* Merge options for non-cloned matches */
1679 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001680 }
1681 break;
1682
1683 case 'n':
1684 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1685 invert);
1686 break;
1687
1688 case 't':
1689 if (invert)
1690 exit_error(PARAMETER_PROBLEM,
1691 "unexpected ! flag before --table");
1692 *table = argv[optind-1];
1693 break;
1694
1695 case 'x':
1696 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1697 invert);
1698 break;
1699
1700 case 'V':
1701 if (invert)
1702 printf("Not %s ;-)\n", program_version);
1703 else
1704 printf("%s v%s\n",
1705 program_name, program_version);
1706 exit(0);
1707
1708 case '0':
1709 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1710 invert);
1711 break;
1712
Harald Welte43ac1912002-03-03 09:43:07 +00001713 case 'M':
1714 modprobe = optarg;
1715 break;
1716
1717 case 'c':
1718
1719 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1720 invert);
1721 pcnt = optarg;
1722 if (optind < argc && argv[optind][0] != '-'
1723 && argv[optind][0] != '!')
1724 bcnt = argv[optind++];
1725 else
1726 exit_error(PARAMETER_PROBLEM,
1727 "-%c requires packet and byte counter",
1728 opt2char(OPT_COUNTERS));
1729
Martin Josefssona28d4952004-05-26 16:04:48 +00001730 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001731 exit_error(PARAMETER_PROBLEM,
1732 "-%c packet counter not numeric",
1733 opt2char(OPT_COUNTERS));
1734
Martin Josefssona28d4952004-05-26 16:04:48 +00001735 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001736 exit_error(PARAMETER_PROBLEM,
1737 "-%c byte counter not numeric",
1738 opt2char(OPT_COUNTERS));
1739
1740 break;
1741
1742
Rusty Russell5eed48a2000-06-02 20:12:24 +00001743 case 1: /* non option */
1744 if (optarg[0] == '!' && optarg[1] == '\0') {
1745 if (invert)
1746 exit_error(PARAMETER_PROBLEM,
1747 "multiple consecutive ! not"
1748 " allowed");
1749 invert = TRUE;
1750 optarg[0] = '\0';
1751 continue;
1752 }
1753 printf("Bad argument `%s'\n", optarg);
1754 exit_tryhelp(2);
1755
1756 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001757 if (!target
1758 || !(target->parse(c - target->option_offset,
1759 argv, invert,
1760 &target->tflags,
1761 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001762 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001763 if (matchp->completed)
1764 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001765 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001766 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001767 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001768 &fw,
1769 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001770 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001771 break;
1772 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001773 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001774
1775 /* If you listen carefully, you can
1776 actually hear this code suck. */
1777
1778 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001779 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001780 * parameter, that has not been parsed yet,
1781 * it's not an option of an explicitly loaded
1782 * match or a target. However, we support
1783 * implicit loading of the protocol match
1784 * extension. '-p tcp' means 'l4 proto 6' and
1785 * at the same time 'load tcp protocol match on
1786 * demand if we specify --dport'.
1787 *
1788 * To make this work, we need to make sure:
1789 * - the parameter has not been parsed by
1790 * a match (m above)
1791 * - a protocol has been specified
1792 * - the protocol extension has not been
1793 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001794 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001795 * - the protocol extension can be successively
1796 * loaded
1797 */
1798 if (m == NULL
1799 && protocol
1800 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001801 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001802 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001803 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001804 && (proto_used == 0))
1805 )
1806 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001807 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001808 /* Try loading protocol */
1809 size_t size;
1810
1811 proto_used = 1;
1812
1813 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1814 + m->size;
1815
1816 m->m = fw_calloc(1, size);
1817 m->m->u.match_size = size;
1818 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001819 set_revision(m->m->u.user.name,
1820 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001821 if (m->init != NULL)
1822 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001823
1824 opts = merge_options(opts,
1825 m->extra_opts, &m->option_offset);
1826
1827 optind--;
1828 continue;
1829 }
1830
Rusty Russell5eed48a2000-06-02 20:12:24 +00001831 if (!m)
1832 exit_error(PARAMETER_PROBLEM,
1833 "Unknown arg `%s'",
1834 argv[optind-1]);
1835 }
1836 }
1837 invert = FALSE;
1838 }
1839
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001840 for (matchp = matches; matchp; matchp = matchp->next)
1841 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001842
Harald Welte43ac1912002-03-03 09:43:07 +00001843 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001844 target->final_check(target->tflags);
1845
1846 /* Fix me: must put inverse options checking here --MN */
1847
1848 if (optind < argc)
1849 exit_error(PARAMETER_PROBLEM,
1850 "unknown arguments found on commandline");
1851 if (!command)
1852 exit_error(PARAMETER_PROBLEM, "no command specified");
1853 if (invert)
1854 exit_error(PARAMETER_PROBLEM,
1855 "nothing appropriate following !");
1856
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001857 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001859 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001861 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862 }
1863
1864 if (shostnetworkmask)
1865 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1866 &(fw.ipv6.smsk), &nsaddrs);
1867
1868 if (dhostnetworkmask)
1869 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1870 &(fw.ipv6.dmsk), &ndaddrs);
1871
1872 if ((nsaddrs > 1 || ndaddrs > 1) &&
1873 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1874 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1875 " source or destination IP addresses");
1876
Rusty Russell5eed48a2000-06-02 20:12:24 +00001877 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1878 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1879 "specify a unique address");
1880
1881 generic_opt_check(command, options);
1882
1883 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1884 exit_error(PARAMETER_PROBLEM,
1885 "chain name `%s' too long (must be under %i chars)",
1886 chain, IP6T_FUNCTION_MAXNAMELEN);
1887
Harald Welte43ac1912002-03-03 09:43:07 +00001888 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001889 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001890 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001891
Rusty Russell8beb0492004-12-22 00:37:10 +00001892 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001893 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001894 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001895
Harald Welte43ac1912002-03-03 09:43:07 +00001896 if (!*handle)
1897 exit_error(VERSION_PROBLEM,
1898 "can't initialize ip6tables table `%s': %s",
1899 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001901 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001902 || command == CMD_DELETE
1903 || command == CMD_INSERT
1904 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001905 if (strcmp(chain, "PREROUTING") == 0
1906 || strcmp(chain, "INPUT") == 0) {
1907 /* -o not valid with incoming packets. */
1908 if (options & OPT_VIANAMEOUT)
1909 exit_error(PARAMETER_PROBLEM,
1910 "Can't use -%c with %s\n",
1911 opt2char(OPT_VIANAMEOUT),
1912 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001913 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001914
Harald Welte43ac1912002-03-03 09:43:07 +00001915 if (strcmp(chain, "POSTROUTING") == 0
1916 || strcmp(chain, "OUTPUT") == 0) {
1917 /* -i not valid with outgoing packets */
1918 if (options & OPT_VIANAMEIN)
1919 exit_error(PARAMETER_PROBLEM,
1920 "Can't use -%c with %s\n",
1921 opt2char(OPT_VIANAMEIN),
1922 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001923 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001924
1925 if (target && ip6tc_is_chain(jumpto, *handle)) {
1926 printf("Warning: using chain %s, not extension\n",
1927 jumpto);
1928
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001929 if (target->t)
1930 free(target->t);
1931
Rusty Russell5eed48a2000-06-02 20:12:24 +00001932 target = NULL;
1933 }
1934
1935 /* If they didn't specify a target, or it's a chain
1936 name, use standard. */
1937 if (!target
1938 && (strlen(jumpto) == 0
1939 || ip6tc_is_chain(jumpto, *handle))) {
1940 size_t size;
1941
1942 target = find_target(IP6T_STANDARD_TARGET,
1943 LOAD_MUST_SUCCEED);
1944
1945 size = sizeof(struct ip6t_entry_target)
1946 + target->size;
1947 target->t = fw_calloc(1, size);
1948 target->t->u.target_size = size;
1949 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001950 if (target->init != NULL)
1951 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 }
1953
1954 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001955 /* it is no chain, and we can't load a plugin.
1956 * We cannot know if the plugin is corrupt, non
1957 * existant OR if the user just misspelled a
1958 * chain. */
1959 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001961 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001962 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 }
1964 }
1965
1966 switch (command) {
1967 case CMD_APPEND:
1968 ret = append_entry(chain, e,
1969 nsaddrs, saddrs, ndaddrs, daddrs,
1970 options&OPT_VERBOSE,
1971 handle);
1972 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001973 case CMD_DELETE:
1974 ret = delete_entry(chain, e,
1975 nsaddrs, saddrs, ndaddrs, daddrs,
1976 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001977 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 break;
1979 case CMD_DELETE_NUM:
1980 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1981 break;
1982 case CMD_REPLACE:
1983 ret = replace_entry(chain, e, rulenum - 1,
1984 saddrs, daddrs, options&OPT_VERBOSE,
1985 handle);
1986 break;
1987 case CMD_INSERT:
1988 ret = insert_entry(chain, e, rulenum - 1,
1989 nsaddrs, saddrs, ndaddrs, daddrs,
1990 options&OPT_VERBOSE,
1991 handle);
1992 break;
1993 case CMD_LIST:
1994 ret = list_entries(chain,
1995 options&OPT_VERBOSE,
1996 options&OPT_NUMERIC,
1997 options&OPT_EXPANDED,
1998 options&OPT_LINENUMBERS,
1999 handle);
2000 break;
2001 case CMD_FLUSH:
2002 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2003 break;
2004 case CMD_ZERO:
2005 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2006 break;
2007 case CMD_LIST|CMD_ZERO:
2008 ret = list_entries(chain,
2009 options&OPT_VERBOSE,
2010 options&OPT_NUMERIC,
2011 options&OPT_EXPANDED,
2012 options&OPT_LINENUMBERS,
2013 handle);
2014 if (ret)
2015 ret = zero_entries(chain,
2016 options&OPT_VERBOSE, handle);
2017 break;
2018 case CMD_NEW_CHAIN:
2019 ret = ip6tc_create_chain(chain, handle);
2020 break;
2021 case CMD_DELETE_CHAIN:
2022 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2023 break;
2024 case CMD_RENAME_CHAIN:
2025 ret = ip6tc_rename_chain(chain, newname, handle);
2026 break;
2027 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002028 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002029 break;
2030 default:
2031 /* We should never reach this... */
2032 exit_tryhelp(2);
2033 }
2034
2035 if (verbose > 1)
2036 dump_entries6(*handle);
2037
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002038 clear_rule_matches(&matches);
2039
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002040 if (e != NULL) {
2041 free(e);
2042 e = NULL;
2043 }
2044
2045 for (c = 0; c < nsaddrs; c++)
2046 free(&saddrs[c]);
2047
2048 for (c = 0; c < ndaddrs; c++)
2049 free(&daddrs[c]);
2050
Pablo Neiradfdcd642005-05-29 19:05:23 +00002051 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002052
Rusty Russell5eed48a2000-06-02 20:12:24 +00002053 return ret;
2054}