blob: 8f8c2c2127bcf488f37de5a2f118c0aea6713d81 [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
271exit_error(enum exittype status, char *msg, ...)
272{
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
818 for (num_old = 0; oldopts[num_old].name; num_old++);
819 for (num_new = 0; newopts[num_new].name; num_new++);
820
821 global_option_offset += OPTION_OFFSET;
822 *option_offset = global_option_offset;
823
824 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
825 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000826 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827 for (i = 0; i < num_new; i++) {
828 merge[num_old + i] = newopts[i];
829 merge[num_old + i].val += *option_offset;
830 }
831 memset(merge + num_old + num_new, 0, sizeof(struct option));
832
833 return merge;
834}
835
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000836void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000837{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000838 me->family = AF_INET6;
839 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000840}
841
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000842void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000843{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000844 me->family = AF_INET6;
845 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000846}
847
848static void
849print_num(u_int64_t number, unsigned int format)
850{
Harald Welte43ac1912002-03-03 09:43:07 +0000851 if (format & FMT_KILOMEGAGIGA) {
852 if (number > 99999) {
853 number = (number + 500) / 1000;
854 if (number > 9999) {
855 number = (number + 500) / 1000;
856 if (number > 9999) {
857 number = (number + 500) / 1000;
858 if (number > 9999) {
859 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000860 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000861 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000862 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000863 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000864 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000865 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000866 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000867 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000868 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000869 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000870 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000871}
872
Harald Welte43ac1912002-03-03 09:43:07 +0000873
Rusty Russell5eed48a2000-06-02 20:12:24 +0000874static void
875print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
876{
877 struct ip6t_counters counters;
878 const char *pol = ip6tc_get_policy(chain, &counters, handle);
879 printf("Chain %s", chain);
880 if (pol) {
881 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000882 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000883 fputc(' ', stdout);
884 print_num(counters.pcnt, (format|FMT_NOTABLE));
885 fputs("packets, ", stdout);
886 print_num(counters.bcnt, (format|FMT_NOTABLE));
887 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000888 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889 printf(")\n");
890 } else {
891 unsigned int refs;
892 if (!ip6tc_get_references(&refs, chain, handle))
893 printf(" (ERROR obtaining refs)\n");
894 else
895 printf(" (%u references)\n", refs);
896 }
897
898 if (format & FMT_LINENUMBERS)
899 printf(FMT("%-4s ", "%s "), "num");
900 if (!(format & FMT_NOCOUNTS)) {
901 if (format & FMT_KILOMEGAGIGA) {
902 printf(FMT("%5s ","%s "), "pkts");
903 printf(FMT("%5s ","%s "), "bytes");
904 } else {
905 printf(FMT("%8s ","%s "), "pkts");
906 printf(FMT("%10s ","%s "), "bytes");
907 }
908 }
909 if (!(format & FMT_NOTARGET))
910 printf(FMT("%-9s ","%s "), "target");
911 fputs(" prot ", stdout);
912 if (format & FMT_OPTIONS)
913 fputs("opt", stdout);
914 if (format & FMT_VIA) {
915 printf(FMT(" %-6s ","%s "), "in");
916 printf(FMT("%-6s ","%s "), "out");
917 }
918 printf(FMT(" %-19s ","%s "), "source");
919 printf(FMT(" %-19s "," %s "), "destination");
920 printf("\n");
921}
922
Harald Welte43ac1912002-03-03 09:43:07 +0000923
Rusty Russell5eed48a2000-06-02 20:12:24 +0000924static int
925print_match(const struct ip6t_entry_match *m,
926 const struct ip6t_ip6 *ip,
927 int numeric)
928{
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000929 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000930
931 if (match) {
932 if (match->print)
933 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000934 else
935 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936 } else {
937 if (m->u.user.name[0])
938 printf("UNKNOWN match `%s' ", m->u.user.name);
939 }
940 /* Don't stop iterating. */
941 return 0;
942}
943
944/* e is called `fw' here for hysterical raisins */
945static void
946print_firewall(const struct ip6t_entry *fw,
947 const char *targname,
948 unsigned int num,
949 unsigned int format,
950 const ip6tc_handle_t handle)
951{
952 struct ip6tables_target *target = NULL;
953 const struct ip6t_entry_target *t;
954 u_int8_t flags;
955 char buf[BUFSIZ];
956
Rusty Russell5eed48a2000-06-02 20:12:24 +0000957 if (!ip6tc_is_chain(targname, handle))
958 target = find_target(targname, TRY_LOAD);
959 else
960 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
961
962 t = ip6t_get_target((struct ip6t_entry *)fw);
963 flags = fw->ipv6.flags;
964
965 if (format & FMT_LINENUMBERS)
966 printf(FMT("%-4u ", "%u "), num+1);
967
968 if (!(format & FMT_NOCOUNTS)) {
969 print_num(fw->counters.pcnt, format);
970 print_num(fw->counters.bcnt, format);
971 }
972
973 if (!(format & FMT_NOTARGET))
974 printf(FMT("%-9s ", "%s "), targname);
975
976 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
977 {
Harald Welte43ac1912002-03-03 09:43:07 +0000978 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000979 if (pname)
980 printf(FMT("%-5s", "%s "), pname);
981 else
982 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
983 }
984
985 if (format & FMT_OPTIONS) {
986 if (format & FMT_NOTABLE)
987 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000988 fputc(' ', stdout); /* Invert flag of FRAG */
989 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000990 fputc(' ', stdout);
991 }
992
993 if (format & FMT_VIA) {
994 char iface[IFNAMSIZ+2];
995
996 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
997 iface[0] = '!';
998 iface[1] = '\0';
999 }
1000 else iface[0] = '\0';
1001
1002 if (fw->ipv6.iniface[0] != '\0') {
1003 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004 }
1005 else if (format & FMT_NUMERIC) strcat(iface, "*");
1006 else strcat(iface, "any");
1007 printf(FMT(" %-6s ","in %s "), iface);
1008
1009 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1010 iface[0] = '!';
1011 iface[1] = '\0';
1012 }
1013 else iface[0] = '\0';
1014
1015 if (fw->ipv6.outiface[0] != '\0') {
1016 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001017 }
1018 else if (format & FMT_NUMERIC) strcat(iface, "*");
1019 else strcat(iface, "any");
1020 printf(FMT("%-6s ","out %s "), iface);
1021 }
1022
1023 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1024 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1025 && !(format & FMT_NUMERIC))
1026 printf(FMT("%-19s ","%s "), "anywhere");
1027 else {
1028 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001029 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030 else
Harald Welte43ac1912002-03-03 09:43:07 +00001031 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001032 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1033 printf(FMT("%-19s ","%s "), buf);
1034 }
1035
1036 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1037 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1038 && !(format & FMT_NUMERIC))
1039 printf(FMT("%-19s","-> %s"), "anywhere");
1040 else {
1041 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001042 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001043 else
Harald Welte43ac1912002-03-03 09:43:07 +00001044 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001045 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1046 printf(FMT("%-19s","-> %s"), buf);
1047 }
1048
1049 if (format & FMT_NOTABLE)
1050 fputs(" ", stdout);
1051
1052 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1053
1054 if (target) {
1055 if (target->print)
1056 /* Print the target information. */
1057 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1058 } else if (t->u.target_size != sizeof(*t))
1059 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001060 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001061
1062 if (!(format & FMT_NONEWLINE))
1063 fputc('\n', stdout);
1064}
1065
1066static void
1067print_firewall_line(const struct ip6t_entry *fw,
1068 const ip6tc_handle_t h)
1069{
1070 struct ip6t_entry_target *t;
1071
1072 t = ip6t_get_target((struct ip6t_entry *)fw);
1073 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1074}
1075
1076static int
1077append_entry(const ip6t_chainlabel chain,
1078 struct ip6t_entry *fw,
1079 unsigned int nsaddrs,
1080 const struct in6_addr saddrs[],
1081 unsigned int ndaddrs,
1082 const struct in6_addr daddrs[],
1083 int verbose,
1084 ip6tc_handle_t *handle)
1085{
1086 unsigned int i, j;
1087 int ret = 1;
1088
1089 for (i = 0; i < nsaddrs; i++) {
1090 fw->ipv6.src = saddrs[i];
1091 for (j = 0; j < ndaddrs; j++) {
1092 fw->ipv6.dst = daddrs[j];
1093 if (verbose)
1094 print_firewall_line(fw, *handle);
1095 ret &= ip6tc_append_entry(chain, fw, handle);
1096 }
1097 }
1098
1099 return ret;
1100}
1101
1102static int
1103replace_entry(const ip6t_chainlabel chain,
1104 struct ip6t_entry *fw,
1105 unsigned int rulenum,
1106 const struct in6_addr *saddr,
1107 const struct in6_addr *daddr,
1108 int verbose,
1109 ip6tc_handle_t *handle)
1110{
1111 fw->ipv6.src = *saddr;
1112 fw->ipv6.dst = *daddr;
1113
1114 if (verbose)
1115 print_firewall_line(fw, *handle);
1116 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1117}
1118
1119static int
1120insert_entry(const ip6t_chainlabel chain,
1121 struct ip6t_entry *fw,
1122 unsigned int rulenum,
1123 unsigned int nsaddrs,
1124 const struct in6_addr saddrs[],
1125 unsigned int ndaddrs,
1126 const struct in6_addr daddrs[],
1127 int verbose,
1128 ip6tc_handle_t *handle)
1129{
1130 unsigned int i, j;
1131 int ret = 1;
1132
1133 for (i = 0; i < nsaddrs; i++) {
1134 fw->ipv6.src = saddrs[i];
1135 for (j = 0; j < ndaddrs; j++) {
1136 fw->ipv6.dst = daddrs[j];
1137 if (verbose)
1138 print_firewall_line(fw, *handle);
1139 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1140 }
1141 }
1142
1143 return ret;
1144}
1145
1146static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001147make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001148{
1149 /* Establish mask for comparison */
1150 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001151 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001152 unsigned char *mask, *mptr;
1153
1154 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001155 for (matchp = matches; matchp; matchp = matchp->next)
1156 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001157
1158 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001159 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001160 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001161
1162 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1163 mptr = mask + sizeof(struct ip6t_entry);
1164
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001165 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001166 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001167 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001168 + matchp->match->userspacesize);
1169 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001170 }
1171
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001172 memset(mptr, 0xFF,
1173 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001174 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001175
1176 return mask;
1177}
1178
1179static int
1180delete_entry(const ip6t_chainlabel chain,
1181 struct ip6t_entry *fw,
1182 unsigned int nsaddrs,
1183 const struct in6_addr saddrs[],
1184 unsigned int ndaddrs,
1185 const struct in6_addr daddrs[],
1186 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001187 ip6tc_handle_t *handle,
1188 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189{
1190 unsigned int i, j;
1191 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001192 unsigned char *mask;
1193
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001194 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001195 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001196 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001197 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001198 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001199 if (verbose)
1200 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001201 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001202 }
1203 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001204 free(mask);
1205
Rusty Russell5eed48a2000-06-02 20:12:24 +00001206 return ret;
1207}
1208
András Kis-Szabó764316a2001-02-26 17:31:20 +00001209int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001210for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1211 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1212{
1213 int ret = 1;
1214 const char *chain;
1215 char *chains;
1216 unsigned int i, chaincount = 0;
1217
1218 chain = ip6tc_first_chain(handle);
1219 while (chain) {
1220 chaincount++;
1221 chain = ip6tc_next_chain(handle);
1222 }
1223
1224 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1225 i = 0;
1226 chain = ip6tc_first_chain(handle);
1227 while (chain) {
1228 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1229 i++;
1230 chain = ip6tc_next_chain(handle);
1231 }
1232
1233 for (i = 0; i < chaincount; i++) {
1234 if (!builtinstoo
1235 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001236 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001237 continue;
1238 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1239 }
1240
1241 free(chains);
1242 return ret;
1243}
1244
András Kis-Szabó764316a2001-02-26 17:31:20 +00001245int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001246flush_entries(const ip6t_chainlabel chain, int verbose,
1247 ip6tc_handle_t *handle)
1248{
1249 if (!chain)
1250 return for_each_chain(flush_entries, verbose, 1, handle);
1251
1252 if (verbose)
1253 fprintf(stdout, "Flushing chain `%s'\n", chain);
1254 return ip6tc_flush_entries(chain, handle);
1255}
1256
1257static int
1258zero_entries(const ip6t_chainlabel chain, int verbose,
1259 ip6tc_handle_t *handle)
1260{
1261 if (!chain)
1262 return for_each_chain(zero_entries, verbose, 1, handle);
1263
1264 if (verbose)
1265 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1266 return ip6tc_zero_entries(chain, handle);
1267}
1268
András Kis-Szabó764316a2001-02-26 17:31:20 +00001269int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001270delete_chain(const ip6t_chainlabel chain, int verbose,
1271 ip6tc_handle_t *handle)
1272{
1273 if (!chain)
1274 return for_each_chain(delete_chain, verbose, 0, handle);
1275
1276 if (verbose)
1277 fprintf(stdout, "Deleting chain `%s'\n", chain);
1278 return ip6tc_delete_chain(chain, handle);
1279}
1280
1281static int
1282list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1283 int expanded, int linenumbers, ip6tc_handle_t *handle)
1284{
1285 int found = 0;
1286 unsigned int format;
1287 const char *this;
1288
1289 format = FMT_OPTIONS;
1290 if (!verbose)
1291 format |= FMT_NOCOUNTS;
1292 else
1293 format |= FMT_VIA;
1294
1295 if (numeric)
1296 format |= FMT_NUMERIC;
1297
1298 if (!expanded)
1299 format |= FMT_KILOMEGAGIGA;
1300
1301 if (linenumbers)
1302 format |= FMT_LINENUMBERS;
1303
1304 for (this = ip6tc_first_chain(handle);
1305 this;
1306 this = ip6tc_next_chain(handle)) {
1307 const struct ip6t_entry *i;
1308 unsigned int num;
1309
1310 if (chain && strcmp(chain, this) != 0)
1311 continue;
1312
1313 if (found) printf("\n");
1314
1315 print_header(format, this, handle);
1316 i = ip6tc_first_rule(this, handle);
1317
1318 num = 0;
1319 while (i) {
1320 print_firewall(i,
1321 ip6tc_get_target(i, handle),
1322 num++,
1323 format,
1324 *handle);
1325 i = ip6tc_next_rule(i, handle);
1326 }
1327 found = 1;
1328 }
1329
1330 errno = ENOENT;
1331 return found;
1332}
1333
1334static struct ip6t_entry *
1335generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001336 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001337 struct ip6t_entry_target *target)
1338{
1339 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001340 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001341 struct ip6t_entry *e;
1342
1343 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001344 for (matchp = matches; matchp; matchp = matchp->next)
1345 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001346
1347 e = fw_malloc(size + target->u.target_size);
1348 *e = *fw;
1349 e->target_offset = size;
1350 e->next_offset = size + target->u.target_size;
1351
1352 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001353 for (matchp = matches; matchp; matchp = matchp->next) {
1354 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1355 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356 }
1357 memcpy(e->elems + size, target, target->u.target_size);
1358
1359 return e;
1360}
1361
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001362void clear_rule_matches(struct ip6tables_rule_match **matches)
1363{
1364 struct ip6tables_rule_match *matchp, *tmp;
1365
1366 for (matchp = *matches; matchp;) {
1367 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001368 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001369 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001370 matchp->match->m = NULL;
1371 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001372 if (matchp->match == matchp->match->next) {
1373 free(matchp->match);
1374 matchp->match = NULL;
1375 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001376 free(matchp);
1377 matchp = tmp;
1378 }
1379
1380 *matches = NULL;
1381}
1382
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001383static void set_revision(char *name, u_int8_t revision)
1384{
1385 /* Old kernel sources don't have ".revision" field,
1386 but we stole a byte from name. */
1387 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1388 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1389}
1390
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1392{
1393 struct ip6t_entry fw, *e = NULL;
1394 int invert = 0;
1395 unsigned int nsaddrs = 0, ndaddrs = 0;
1396 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1397
1398 int c, verbose = 0;
1399 const char *chain = NULL;
1400 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1401 const char *policy = NULL, *newname = NULL;
1402 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001403 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001404 int ret = 1;
1405 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001406 struct ip6tables_rule_match *matches = NULL;
1407 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001408 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001409 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410 const char *jumpto = "";
1411 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001412 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413
1414 memset(&fw, 0, sizeof(fw));
1415
Harald Welte43ac1912002-03-03 09:43:07 +00001416 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001417 * a second time */
1418 optind = 0;
1419
Harald Welte43ac1912002-03-03 09:43:07 +00001420 /* clear mflags in case do_command gets called a second time
1421 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001422 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001423 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001424
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001425 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001426 t->tflags = 0;
1427 t->used = 0;
1428 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001429
Rusty Russell5eed48a2000-06-02 20:12:24 +00001430 /* Suppress error messages: we may add new options if we
1431 demand-load a protocol. */
1432 opterr = 0;
1433
1434 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001435 "-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 +00001436 opts, NULL)) != -1) {
1437 switch (c) {
1438 /*
1439 * Command selection
1440 */
1441 case 'A':
1442 add_command(&command, CMD_APPEND, CMD_NONE,
1443 invert);
1444 chain = optarg;
1445 break;
1446
1447 case 'D':
1448 add_command(&command, CMD_DELETE, CMD_NONE,
1449 invert);
1450 chain = optarg;
1451 if (optind < argc && argv[optind][0] != '-'
1452 && argv[optind][0] != '!') {
1453 rulenum = parse_rulenumber(argv[optind++]);
1454 command = CMD_DELETE_NUM;
1455 }
1456 break;
1457
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458 case 'R':
1459 add_command(&command, CMD_REPLACE, CMD_NONE,
1460 invert);
1461 chain = optarg;
1462 if (optind < argc && argv[optind][0] != '-'
1463 && argv[optind][0] != '!')
1464 rulenum = parse_rulenumber(argv[optind++]);
1465 else
1466 exit_error(PARAMETER_PROBLEM,
1467 "-%c requires a rule number",
1468 cmd2char(CMD_REPLACE));
1469 break;
1470
1471 case 'I':
1472 add_command(&command, CMD_INSERT, CMD_NONE,
1473 invert);
1474 chain = optarg;
1475 if (optind < argc && argv[optind][0] != '-'
1476 && argv[optind][0] != '!')
1477 rulenum = parse_rulenumber(argv[optind++]);
1478 else rulenum = 1;
1479 break;
1480
1481 case 'L':
1482 add_command(&command, CMD_LIST, CMD_ZERO,
1483 invert);
1484 if (optarg) chain = optarg;
1485 else if (optind < argc && argv[optind][0] != '-'
1486 && argv[optind][0] != '!')
1487 chain = argv[optind++];
1488 break;
1489
1490 case 'F':
1491 add_command(&command, CMD_FLUSH, CMD_NONE,
1492 invert);
1493 if (optarg) chain = optarg;
1494 else if (optind < argc && argv[optind][0] != '-'
1495 && argv[optind][0] != '!')
1496 chain = argv[optind++];
1497 break;
1498
1499 case 'Z':
1500 add_command(&command, CMD_ZERO, CMD_LIST,
1501 invert);
1502 if (optarg) chain = optarg;
1503 else if (optind < argc && argv[optind][0] != '-'
1504 && argv[optind][0] != '!')
1505 chain = argv[optind++];
1506 break;
1507
1508 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001509 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001510 exit_error(PARAMETER_PROBLEM,
1511 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001512 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001513 if (find_target(optarg, TRY_LOAD))
1514 exit_error(PARAMETER_PROBLEM,
1515 "chain name may not clash "
1516 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001517 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1518 invert);
1519 chain = optarg;
1520 break;
1521
1522 case 'X':
1523 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1524 invert);
1525 if (optarg) chain = optarg;
1526 else if (optind < argc && argv[optind][0] != '-'
1527 && argv[optind][0] != '!')
1528 chain = argv[optind++];
1529 break;
1530
1531 case 'E':
1532 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1533 invert);
1534 chain = optarg;
1535 if (optind < argc && argv[optind][0] != '-'
1536 && argv[optind][0] != '!')
1537 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001538 else
1539 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001540 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001541 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001542 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001543 break;
1544
1545 case 'P':
1546 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1547 invert);
1548 chain = optarg;
1549 if (optind < argc && argv[optind][0] != '-'
1550 && argv[optind][0] != '!')
1551 policy = argv[optind++];
1552 else
1553 exit_error(PARAMETER_PROBLEM,
1554 "-%c requires a chain and a policy",
1555 cmd2char(CMD_SET_POLICY));
1556 break;
1557
1558 case 'h':
1559 if (!optarg)
1560 optarg = argv[optind];
1561
Jonas Berlin1b91e592005-04-01 06:58:38 +00001562 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001563 if (!matches && protocol)
1564 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001565
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001566 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001567
1568 /*
1569 * Option selection
1570 */
1571 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001572 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001573 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1574 invert);
1575
1576 /* Canonicalize into lower case */
1577 for (protocol = argv[optind-1]; *protocol; protocol++)
1578 *protocol = tolower(*protocol);
1579
1580 protocol = argv[optind-1];
1581 fw.ipv6.proto = parse_protocol(protocol);
1582 fw.ipv6.flags |= IP6T_F_PROTO;
1583
1584 if (fw.ipv6.proto == 0
1585 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1586 exit_error(PARAMETER_PROBLEM,
1587 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001588
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001589 if (is_exthdr(fw.ipv6.proto)
1590 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001591 printf("Warning: never matched protocol: %s. "
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001592 "use extension match instead.\n",
1593 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001594 break;
1595
1596 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001597 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001598 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1599 invert);
1600 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601 break;
1602
1603 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001604 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001605 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1606 invert);
1607 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 break;
1609
1610 case 'j':
1611 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1612 invert);
1613 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001614 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001615 target = find_target(jumpto, TRY_LOAD);
1616
1617 if (target) {
1618 size_t size;
1619
Harald Welte43ac1912002-03-03 09:43:07 +00001620 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1621 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622
1623 target->t = fw_calloc(1, size);
1624 target->t->u.target_size = size;
1625 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001626 if (target->init != NULL)
1627 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001628 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 }
1630 break;
1631
1632
1633 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001634 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1636 invert);
1637 parse_interface(argv[optind-1],
1638 fw.ipv6.iniface,
1639 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001640 break;
1641
1642 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001643 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001644 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1645 invert);
1646 parse_interface(argv[optind-1],
1647 fw.ipv6.outiface,
1648 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649 break;
1650
1651 case 'v':
1652 if (!verbose)
1653 set_option(&options, OPT_VERBOSE,
1654 &fw.ipv6.invflags, invert);
1655 verbose++;
1656 break;
1657
1658 case 'm': {
1659 size_t size;
1660
1661 if (invert)
1662 exit_error(PARAMETER_PROBLEM,
1663 "unexpected ! flag before --match");
1664
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001665 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001666 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1667 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 m->m = fw_calloc(1, size);
1669 m->m->u.match_size = size;
1670 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001671 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001672 if (m->init != NULL)
1673 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001674 if (m != m->next)
1675 /* Merge options for non-cloned matches */
1676 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001677 }
1678 break;
1679
1680 case 'n':
1681 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1682 invert);
1683 break;
1684
1685 case 't':
1686 if (invert)
1687 exit_error(PARAMETER_PROBLEM,
1688 "unexpected ! flag before --table");
1689 *table = argv[optind-1];
1690 break;
1691
1692 case 'x':
1693 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1694 invert);
1695 break;
1696
1697 case 'V':
1698 if (invert)
1699 printf("Not %s ;-)\n", program_version);
1700 else
1701 printf("%s v%s\n",
1702 program_name, program_version);
1703 exit(0);
1704
1705 case '0':
1706 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1707 invert);
1708 break;
1709
Harald Welte43ac1912002-03-03 09:43:07 +00001710 case 'M':
1711 modprobe = optarg;
1712 break;
1713
1714 case 'c':
1715
1716 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1717 invert);
1718 pcnt = optarg;
1719 if (optind < argc && argv[optind][0] != '-'
1720 && argv[optind][0] != '!')
1721 bcnt = argv[optind++];
1722 else
1723 exit_error(PARAMETER_PROBLEM,
1724 "-%c requires packet and byte counter",
1725 opt2char(OPT_COUNTERS));
1726
Martin Josefssona28d4952004-05-26 16:04:48 +00001727 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001728 exit_error(PARAMETER_PROBLEM,
1729 "-%c packet counter not numeric",
1730 opt2char(OPT_COUNTERS));
1731
Martin Josefssona28d4952004-05-26 16:04:48 +00001732 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001733 exit_error(PARAMETER_PROBLEM,
1734 "-%c byte counter not numeric",
1735 opt2char(OPT_COUNTERS));
1736
1737 break;
1738
1739
Rusty Russell5eed48a2000-06-02 20:12:24 +00001740 case 1: /* non option */
1741 if (optarg[0] == '!' && optarg[1] == '\0') {
1742 if (invert)
1743 exit_error(PARAMETER_PROBLEM,
1744 "multiple consecutive ! not"
1745 " allowed");
1746 invert = TRUE;
1747 optarg[0] = '\0';
1748 continue;
1749 }
1750 printf("Bad argument `%s'\n", optarg);
1751 exit_tryhelp(2);
1752
1753 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001754 if (!target
1755 || !(target->parse(c - target->option_offset,
1756 argv, invert,
1757 &target->tflags,
1758 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001759 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001760 if (matchp->completed)
1761 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001762 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001764 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001765 &fw,
1766 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001767 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001768 break;
1769 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001770 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001771
1772 /* If you listen carefully, you can
1773 actually hear this code suck. */
1774
1775 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001776 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001777 * parameter, that has not been parsed yet,
1778 * it's not an option of an explicitly loaded
1779 * match or a target. However, we support
1780 * implicit loading of the protocol match
1781 * extension. '-p tcp' means 'l4 proto 6' and
1782 * at the same time 'load tcp protocol match on
1783 * demand if we specify --dport'.
1784 *
1785 * To make this work, we need to make sure:
1786 * - the parameter has not been parsed by
1787 * a match (m above)
1788 * - a protocol has been specified
1789 * - the protocol extension has not been
1790 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001791 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001792 * - the protocol extension can be successively
1793 * loaded
1794 */
1795 if (m == NULL
1796 && protocol
1797 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001798 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001799 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001800 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001801 && (proto_used == 0))
1802 )
1803 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001804 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001805 /* Try loading protocol */
1806 size_t size;
1807
1808 proto_used = 1;
1809
1810 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1811 + m->size;
1812
1813 m->m = fw_calloc(1, size);
1814 m->m->u.match_size = size;
1815 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001816 set_revision(m->m->u.user.name,
1817 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001818 if (m->init != NULL)
1819 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001820
1821 opts = merge_options(opts,
1822 m->extra_opts, &m->option_offset);
1823
1824 optind--;
1825 continue;
1826 }
1827
Rusty Russell5eed48a2000-06-02 20:12:24 +00001828 if (!m)
1829 exit_error(PARAMETER_PROBLEM,
1830 "Unknown arg `%s'",
1831 argv[optind-1]);
1832 }
1833 }
1834 invert = FALSE;
1835 }
1836
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001837 for (matchp = matches; matchp; matchp = matchp->next)
1838 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001839
Harald Welte43ac1912002-03-03 09:43:07 +00001840 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001841 target->final_check(target->tflags);
1842
1843 /* Fix me: must put inverse options checking here --MN */
1844
1845 if (optind < argc)
1846 exit_error(PARAMETER_PROBLEM,
1847 "unknown arguments found on commandline");
1848 if (!command)
1849 exit_error(PARAMETER_PROBLEM, "no command specified");
1850 if (invert)
1851 exit_error(PARAMETER_PROBLEM,
1852 "nothing appropriate following !");
1853
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001854 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001855 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001856 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001857 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001858 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001859 }
1860
1861 if (shostnetworkmask)
1862 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1863 &(fw.ipv6.smsk), &nsaddrs);
1864
1865 if (dhostnetworkmask)
1866 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1867 &(fw.ipv6.dmsk), &ndaddrs);
1868
1869 if ((nsaddrs > 1 || ndaddrs > 1) &&
1870 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1871 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1872 " source or destination IP addresses");
1873
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1875 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1876 "specify a unique address");
1877
1878 generic_opt_check(command, options);
1879
1880 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1881 exit_error(PARAMETER_PROBLEM,
1882 "chain name `%s' too long (must be under %i chars)",
1883 chain, IP6T_FUNCTION_MAXNAMELEN);
1884
Harald Welte43ac1912002-03-03 09:43:07 +00001885 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001886 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001887 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001888
Rusty Russell8beb0492004-12-22 00:37:10 +00001889 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001890 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001891 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001892
Harald Welte43ac1912002-03-03 09:43:07 +00001893 if (!*handle)
1894 exit_error(VERSION_PROBLEM,
1895 "can't initialize ip6tables table `%s': %s",
1896 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001898 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 || command == CMD_DELETE
1900 || command == CMD_INSERT
1901 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001902 if (strcmp(chain, "PREROUTING") == 0
1903 || strcmp(chain, "INPUT") == 0) {
1904 /* -o not valid with incoming packets. */
1905 if (options & OPT_VIANAMEOUT)
1906 exit_error(PARAMETER_PROBLEM,
1907 "Can't use -%c with %s\n",
1908 opt2char(OPT_VIANAMEOUT),
1909 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001910 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001911
Harald Welte43ac1912002-03-03 09:43:07 +00001912 if (strcmp(chain, "POSTROUTING") == 0
1913 || strcmp(chain, "OUTPUT") == 0) {
1914 /* -i not valid with outgoing packets */
1915 if (options & OPT_VIANAMEIN)
1916 exit_error(PARAMETER_PROBLEM,
1917 "Can't use -%c with %s\n",
1918 opt2char(OPT_VIANAMEIN),
1919 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001920 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921
1922 if (target && ip6tc_is_chain(jumpto, *handle)) {
1923 printf("Warning: using chain %s, not extension\n",
1924 jumpto);
1925
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001926 if (target->t)
1927 free(target->t);
1928
Rusty Russell5eed48a2000-06-02 20:12:24 +00001929 target = NULL;
1930 }
1931
1932 /* If they didn't specify a target, or it's a chain
1933 name, use standard. */
1934 if (!target
1935 && (strlen(jumpto) == 0
1936 || ip6tc_is_chain(jumpto, *handle))) {
1937 size_t size;
1938
1939 target = find_target(IP6T_STANDARD_TARGET,
1940 LOAD_MUST_SUCCEED);
1941
1942 size = sizeof(struct ip6t_entry_target)
1943 + target->size;
1944 target->t = fw_calloc(1, size);
1945 target->t->u.target_size = size;
1946 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001947 if (target->init != NULL)
1948 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001949 }
1950
1951 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001952 /* it is no chain, and we can't load a plugin.
1953 * We cannot know if the plugin is corrupt, non
1954 * existant OR if the user just misspelled a
1955 * chain. */
1956 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001958 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001959 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 }
1961 }
1962
1963 switch (command) {
1964 case CMD_APPEND:
1965 ret = append_entry(chain, e,
1966 nsaddrs, saddrs, ndaddrs, daddrs,
1967 options&OPT_VERBOSE,
1968 handle);
1969 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001970 case CMD_DELETE:
1971 ret = delete_entry(chain, e,
1972 nsaddrs, saddrs, ndaddrs, daddrs,
1973 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001974 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975 break;
1976 case CMD_DELETE_NUM:
1977 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1978 break;
1979 case CMD_REPLACE:
1980 ret = replace_entry(chain, e, rulenum - 1,
1981 saddrs, daddrs, options&OPT_VERBOSE,
1982 handle);
1983 break;
1984 case CMD_INSERT:
1985 ret = insert_entry(chain, e, rulenum - 1,
1986 nsaddrs, saddrs, ndaddrs, daddrs,
1987 options&OPT_VERBOSE,
1988 handle);
1989 break;
1990 case CMD_LIST:
1991 ret = list_entries(chain,
1992 options&OPT_VERBOSE,
1993 options&OPT_NUMERIC,
1994 options&OPT_EXPANDED,
1995 options&OPT_LINENUMBERS,
1996 handle);
1997 break;
1998 case CMD_FLUSH:
1999 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2000 break;
2001 case CMD_ZERO:
2002 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2003 break;
2004 case CMD_LIST|CMD_ZERO:
2005 ret = list_entries(chain,
2006 options&OPT_VERBOSE,
2007 options&OPT_NUMERIC,
2008 options&OPT_EXPANDED,
2009 options&OPT_LINENUMBERS,
2010 handle);
2011 if (ret)
2012 ret = zero_entries(chain,
2013 options&OPT_VERBOSE, handle);
2014 break;
2015 case CMD_NEW_CHAIN:
2016 ret = ip6tc_create_chain(chain, handle);
2017 break;
2018 case CMD_DELETE_CHAIN:
2019 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2020 break;
2021 case CMD_RENAME_CHAIN:
2022 ret = ip6tc_rename_chain(chain, newname, handle);
2023 break;
2024 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002025 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002026 break;
2027 default:
2028 /* We should never reach this... */
2029 exit_tryhelp(2);
2030 }
2031
2032 if (verbose > 1)
2033 dump_entries6(*handle);
2034
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002035 clear_rule_matches(&matches);
2036
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002037 if (e != NULL) {
2038 free(e);
2039 e = NULL;
2040 }
2041
2042 for (c = 0; c < nsaddrs; c++)
2043 free(&saddrs[c]);
2044
2045 for (c = 0; c < ndaddrs; c++)
2046 free(&daddrs[c]);
2047
Pablo Neiradfdcd642005-05-29 19:05:23 +00002048 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002049
Rusty Russell5eed48a2000-06-02 20:12:24 +00002050 return ret;
2051}