blob: 01487f706f2172c96eb2bd3296e412fb85d9a197 [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
Pablo Neiradfdcd642005-05-29 19:05:23 +0000253static void free_opts(int reset_offset)
254{
255 if (opts != original_opts) {
256 free(opts);
257 opts = original_opts;
258 if (reset_offset)
259 global_option_offset = 0;
260 }
261}
262
Patrick McHardy0b639362007-09-08 16:00:01 +0000263static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000264exit_tryhelp(int status)
265{
Harald Weltea8658ca2003-03-05 07:46:15 +0000266 if (line != -1)
267 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000268 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
269 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000270 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271 exit(status);
272}
273
Patrick McHardy0b639362007-09-08 16:00:01 +0000274static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000275exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000276{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000277 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000278 struct ip6tables_target *t = NULL;
279
280 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000281"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282" %s -[RI] chain rulenum rule-specification [options]\n"
283" %s -D chain rulenum [options]\n"
284" %s -[LFZ] [chain] [options]\n"
285" %s -[NX] chain\n"
286" %s -E old-chain-name new-chain-name\n"
287" %s -P chain target [options]\n"
288" %s -h (print this help information)\n\n",
289 program_name, program_version, program_name, program_name,
290 program_name, program_name, program_name, program_name,
291 program_name, program_name);
292
293 printf(
294"Commands:\n"
295"Either long or short options are allowed.\n"
296" --append -A chain Append to chain\n"
297" --delete -D chain Delete matching rule from chain\n"
298" --delete -D chain rulenum\n"
299" Delete rule rulenum (1 = first) from chain\n"
300" --insert -I chain [rulenum]\n"
301" Insert in chain as rulenum (default 1=first)\n"
302" --replace -R chain rulenum\n"
303" Replace rule rulenum (1 = first) in chain\n"
304" --list -L [chain] List the rules in a chain or all chains\n"
305" --flush -F [chain] Delete all rules in chain or all chains\n"
306" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000307" --new -N chain Create a new user-defined chain\n"
308" --delete-chain\n"
309" -X [chain] Delete a user-defined chain\n"
310" --policy -P chain target\n"
311" Change policy on chain to target\n"
312" --rename-chain\n"
313" -E old-chain new-chain\n"
314" Change chain name, (moving any references)\n"
315
316"Options:\n"
317" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
318" --source -s [!] address[/mask]\n"
319" source specification\n"
320" --destination -d [!] address[/mask]\n"
321" destination specification\n"
322" --in-interface -i [!] input name[+]\n"
323" network interface name ([+] for wildcard)\n"
324" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000325" target for rule (may load target extension)\n"
326" --match -m match\n"
327" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000328" --numeric -n numeric output of addresses and ports\n"
329" --out-interface -o [!] output name[+]\n"
330" network interface name ([+] for wildcard)\n"
331" --table -t table table to manipulate (default: `filter')\n"
332" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000333" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000334" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000335/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000336" --modprobe=<command> try to insert modules using this command\n"
337" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000338"[!] --version -V print package version.\n");
339
340 /* Print out any special helps. A user might like to be able to add a --help
341 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000342 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000343 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000344 if (t->used) {
345 printf("\n");
346 t->help();
347 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000349 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000350 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000351 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000352 }
353 exit(0);
354}
355
Patrick McHardy0b639362007-09-08 16:00:01 +0000356void
357exit_error(enum exittype status, const char *msg, ...)
358{
359 va_list args;
360
361 va_start(args, msg);
362 fprintf(stderr, "%s v%s: ", program_name, program_version);
363 vfprintf(stderr, msg, args);
364 va_end(args);
365 fprintf(stderr, "\n");
366 if (status == PARAMETER_PROBLEM)
367 exit_tryhelp(status);
368 if (status == VERSION_PROBLEM)
369 fprintf(stderr,
370 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
371 /* On error paths, make sure that we don't leak memory */
372 free_opts(1);
373 exit(status);
374}
375
Rusty Russell5eed48a2000-06-02 20:12:24 +0000376static void
377generic_opt_check(int command, int options)
378{
379 int i, j, legal = 0;
380
381 /* Check that commands are valid with options. Complicated by the
382 * fact that if an option is legal with *any* command given, it is
383 * legal overall (ie. -z and -l).
384 */
385 for (i = 0; i < NUMBER_OF_OPT; i++) {
386 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
387
388 for (j = 0; j < NUMBER_OF_CMD; j++) {
389 if (!(command & (1<<j)))
390 continue;
391
392 if (!(options & (1<<i))) {
393 if (commands_v_options[j][i] == '+')
394 exit_error(PARAMETER_PROBLEM,
395 "You need to supply the `-%c' "
396 "option for this command\n",
397 optflags[i]);
398 } else {
399 if (commands_v_options[j][i] != 'x')
400 legal = 1;
401 else if (legal == 0)
402 legal = -1;
403 }
404 }
405 if (legal == -1)
406 exit_error(PARAMETER_PROBLEM,
407 "Illegal option `-%c' with this command\n",
408 optflags[i]);
409 }
410}
411
412static char
413opt2char(int option)
414{
415 const char *ptr;
416 for (ptr = optflags; option > 1; option >>= 1, ptr++);
417
418 return *ptr;
419}
420
421static char
422cmd2char(int option)
423{
424 const char *ptr;
425 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
426
427 return *ptr;
428}
429
430static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000431add_command(unsigned int *cmd, const int newcmd, const int othercmds,
432 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000433{
434 if (invert)
435 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
436 if (*cmd & (~othercmds))
437 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
438 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
439 *cmd |= newcmd;
440}
441
442int
Harald Welteb77f1da2002-03-14 11:35:58 +0000443check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000444{
445 if (option && strcmp(option, "!") == 0) {
446 if (*invert)
447 exit_error(PARAMETER_PROBLEM,
448 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000449 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000450 if (optind) {
451 *optind = *optind+1;
452 if (argc && *optind > argc)
453 exit_error(PARAMETER_PROBLEM,
454 "no argument following `!'");
455 }
456
Rusty Russell5eed48a2000-06-02 20:12:24 +0000457 return TRUE;
458 }
459 return FALSE;
460}
461
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462static struct in6_addr *
463numeric_to_addr(const char *num)
464{
465 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000466 int err;
467 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000468 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000469#ifdef DEBUG
470 fprintf(stderr, "\nnumeric2addr: %d\n", err);
471#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472 return (struct in6_addr *)NULL;
473}
474
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000475
476static struct in6_addr *
477host_to_addr(const char *name, unsigned int *naddr)
478{
479 struct addrinfo hints;
480 struct addrinfo *res;
481 static struct in6_addr *addr;
482 int err;
483
484 memset(&hints, 0, sizeof(hints));
485 hints.ai_flags=AI_CANONNAME;
486 hints.ai_family=AF_INET6;
487 hints.ai_socktype=SOCK_RAW;
488 hints.ai_protocol=41;
489 hints.ai_next=NULL;
490
491 *naddr = 0;
492 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
493#ifdef DEBUG
494 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
495#endif
496 return (struct in6_addr *) NULL;
497 } else {
498 if (res->ai_family != AF_INET6 ||
499 res->ai_addrlen != sizeof(struct sockaddr_in6))
500 return (struct in6_addr *) NULL;
501
502#ifdef DEBUG
503 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
504 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
505#endif
506 /* Get the first element of the address-chain */
507 addr = fw_calloc(1, sizeof(struct in6_addr));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000508 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
509 sizeof(struct in6_addr));
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000510 freeaddrinfo(res);
511 *naddr = 1;
512 return addr;
513 }
514
515 return (struct in6_addr *) NULL;
516}
517
Rusty Russell5eed48a2000-06-02 20:12:24 +0000518static struct in6_addr *
519network_to_addr(const char *name)
520{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000521 /* abort();*/
522 /* TODO: not implemented yet, but the exception breaks the
523 * name resolvation */
524 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000525}
526
Rusty Russell5eed48a2000-06-02 20:12:24 +0000527/*
528 * All functions starting with "parse" should succeed, otherwise
529 * the program fails.
530 * Most routines return pointers to static data that may change
531 * between calls to the same or other routines with a few exceptions:
532 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
533 * return global static data.
534*/
535
536static struct in6_addr *
537parse_hostnetwork(const char *name, unsigned int *naddrs)
538{
539 struct in6_addr *addrp, *addrptmp;
540
541 if ((addrptmp = numeric_to_addr(name)) != NULL ||
542 (addrptmp = network_to_addr(name)) != NULL) {
543 addrp = fw_malloc(sizeof(struct in6_addr));
Jan Engelhardt08b16162008-01-20 13:36:08 +0000544 memcpy(addrp, addrptmp, sizeof(*addrp));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000545 *naddrs = 1;
546 return addrp;
547 }
548 if ((addrp = host_to_addr(name, naddrs)) != NULL)
549 return addrp;
550
551 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
552}
553
554static struct in6_addr *
555parse_mask(char *mask)
556{
557 static struct in6_addr maskaddr;
558 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000559 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000560
561 if (mask == NULL) {
562 /* no mask at all defaults to 128 bits */
563 memset(&maskaddr, 0xff, sizeof maskaddr);
564 return &maskaddr;
565 }
566 if ((addrp = numeric_to_addr(mask)) != NULL)
567 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000568 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000569 exit_error(PARAMETER_PROBLEM,
570 "invalid mask `%s' specified", mask);
571 if (bits != 0) {
572 char *p = (char *)&maskaddr;
573 memset(p, 0xff, bits / 8);
574 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
575 p[bits / 8] = 0xff << (8 - (bits & 7));
576 return &maskaddr;
577 }
578
579 memset(&maskaddr, 0, sizeof maskaddr);
580 return &maskaddr;
581}
582
Harald Welte43ac1912002-03-03 09:43:07 +0000583void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000584parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
585 struct in6_addr *maskp, unsigned int *naddrs)
586{
587 struct in6_addr *addrp;
588 char buf[256];
589 char *p;
590 int i, j, n;
591
592 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000593 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000594 if ((p = strrchr(buf, '/')) != NULL) {
595 *p = '\0';
596 addrp = parse_mask(p + 1);
597 } else
598 addrp = parse_mask(NULL);
Jan Engelhardt08b16162008-01-20 13:36:08 +0000599 memcpy(maskp, addrp, sizeof(*maskp));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000600
601 /* if a null mask is given, the name is ignored, like in "any/0" */
602 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
603 strcpy(buf, "::");
604
605 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
606 n = *naddrs;
607 for (i = 0, j = 0; i < n; i++) {
608 int k;
609 for (k = 0; k < 4; k++)
610 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
611 j++;
612 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000613 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614 (*naddrs)--;
615 j--;
616 break;
617 }
618 }
619 }
620}
621
Rusty Russell5eed48a2000-06-02 20:12:24 +0000622/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
623static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000624find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000625{
Harald Welteed498492001-07-23 01:24:22 +0000626 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000627
Harald Welte43ac1912002-03-03 09:43:07 +0000628 if (string_to_number(pname, 0, 255, &proto) != -1) {
629 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000630
Harald Welte43ac1912002-03-03 09:43:07 +0000631 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000632 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000633 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000634 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000635
636 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000637}
638
Harald Welte43ac1912002-03-03 09:43:07 +0000639u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000640parse_protocol(const char *s)
641{
Harald Welteed498492001-07-23 01:24:22 +0000642 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000643
Harald Welteed498492001-07-23 01:24:22 +0000644 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000645 struct protoent *pent;
646
Harald Weltecbe1ec72006-02-11 09:50:11 +0000647 /* first deal with the special case of 'all' to prevent
648 * people from being able to redefine 'all' in nsswitch
649 * and/or provoke expensive [not working] ldap/nis/...
650 * lookups */
651 if (!strcmp(s, "all"))
652 return 0;
653
Rusty Russell5eed48a2000-06-02 20:12:24 +0000654 if ((pent = getprotobyname(s)))
655 proto = pent->p_proto;
656 else {
657 unsigned int i;
658 for (i = 0;
659 i < sizeof(chain_protos)/sizeof(struct pprot);
660 i++) {
661 if (strcmp(s, chain_protos[i].name) == 0) {
662 proto = chain_protos[i].num;
663 break;
664 }
665 }
666 if (i == sizeof(chain_protos)/sizeof(struct pprot))
667 exit_error(PARAMETER_PROBLEM,
668 "unknown protocol `%s' specified",
669 s);
670 }
671 }
672
673 return (u_int16_t)proto;
674}
675
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000676/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000677static int is_exthdr(u_int16_t proto)
678{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000679 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000680 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000681 proto == IPPROTO_AH ||
682 proto == IPPROTO_DSTOPTS);
683}
684
Rusty Russell5eed48a2000-06-02 20:12:24 +0000685/* Can't be zero. */
686static int
687parse_rulenumber(const char *rule)
688{
Harald Welte43ac1912002-03-03 09:43:07 +0000689 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000690
Harald Welte43ac1912002-03-03 09:43:07 +0000691 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000692 exit_error(PARAMETER_PROBLEM,
693 "Invalid rule number `%s'", rule);
694
695 return rulenum;
696}
697
698static const char *
699parse_target(const char *targetname)
700{
701 const char *ptr;
702
703 if (strlen(targetname) < 1)
704 exit_error(PARAMETER_PROBLEM,
705 "Invalid target name (too short)");
706
707 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
708 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000709 "Invalid target name `%s' (%u chars max)",
710 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711
712 for (ptr = targetname; *ptr; ptr++)
713 if (isspace(*ptr))
714 exit_error(PARAMETER_PROBLEM,
715 "Invalid target name `%s'", targetname);
716 return targetname;
717}
718
Rusty Russell5eed48a2000-06-02 20:12:24 +0000719static void
720set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
721 int invert)
722{
723 if (*options & option)
724 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
725 opt2char(option));
726 *options |= option;
727
728 if (invert) {
729 unsigned int i;
730 for (i = 0; 1 << i != option; i++);
731
732 if (!inverse_for_options[i])
733 exit_error(PARAMETER_PROBLEM,
734 "cannot have ! before -%c",
735 opt2char(option));
736 *invflg |= inverse_for_options[i];
737 }
738}
739
Rusty Russell5eed48a2000-06-02 20:12:24 +0000740static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000741merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000742 unsigned int *option_offset)
743{
744 unsigned int num_old, num_new, i;
745 struct option *merge;
746
Jan Engelhardtd0145402007-07-30 14:32:26 +0000747 if (newopts == NULL)
748 return oldopts;
749
Rusty Russell5eed48a2000-06-02 20:12:24 +0000750 for (num_old = 0; oldopts[num_old].name; num_old++);
751 for (num_new = 0; newopts[num_new].name; num_new++);
752
753 global_option_offset += OPTION_OFFSET;
754 *option_offset = global_option_offset;
755
756 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
757 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000758 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000759 for (i = 0; i < num_new; i++) {
760 merge[num_old + i] = newopts[i];
761 merge[num_old + i].val += *option_offset;
762 }
763 memset(merge + num_old + num_new, 0, sizeof(struct option));
764
765 return merge;
766}
767
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000768void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000769{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000770 me->family = AF_INET6;
771 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000772}
773
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000774void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000775{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000776 me->family = AF_INET6;
777 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000778}
779
780static void
781print_num(u_int64_t number, unsigned int format)
782{
Harald Welte43ac1912002-03-03 09:43:07 +0000783 if (format & FMT_KILOMEGAGIGA) {
784 if (number > 99999) {
785 number = (number + 500) / 1000;
786 if (number > 9999) {
787 number = (number + 500) / 1000;
788 if (number > 9999) {
789 number = (number + 500) / 1000;
790 if (number > 9999) {
791 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000792 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000793 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000794 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000795 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000796 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000797 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000798 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000799 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000800 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000801 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000802 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803}
804
Harald Welte43ac1912002-03-03 09:43:07 +0000805
Rusty Russell5eed48a2000-06-02 20:12:24 +0000806static void
807print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
808{
809 struct ip6t_counters counters;
810 const char *pol = ip6tc_get_policy(chain, &counters, handle);
811 printf("Chain %s", chain);
812 if (pol) {
813 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000814 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000815 fputc(' ', stdout);
816 print_num(counters.pcnt, (format|FMT_NOTABLE));
817 fputs("packets, ", stdout);
818 print_num(counters.bcnt, (format|FMT_NOTABLE));
819 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000820 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000821 printf(")\n");
822 } else {
823 unsigned int refs;
824 if (!ip6tc_get_references(&refs, chain, handle))
825 printf(" (ERROR obtaining refs)\n");
826 else
827 printf(" (%u references)\n", refs);
828 }
829
830 if (format & FMT_LINENUMBERS)
831 printf(FMT("%-4s ", "%s "), "num");
832 if (!(format & FMT_NOCOUNTS)) {
833 if (format & FMT_KILOMEGAGIGA) {
834 printf(FMT("%5s ","%s "), "pkts");
835 printf(FMT("%5s ","%s "), "bytes");
836 } else {
837 printf(FMT("%8s ","%s "), "pkts");
838 printf(FMT("%10s ","%s "), "bytes");
839 }
840 }
841 if (!(format & FMT_NOTARGET))
842 printf(FMT("%-9s ","%s "), "target");
843 fputs(" prot ", stdout);
844 if (format & FMT_OPTIONS)
845 fputs("opt", stdout);
846 if (format & FMT_VIA) {
847 printf(FMT(" %-6s ","%s "), "in");
848 printf(FMT("%-6s ","%s "), "out");
849 }
850 printf(FMT(" %-19s ","%s "), "source");
851 printf(FMT(" %-19s "," %s "), "destination");
852 printf("\n");
853}
854
Harald Welte43ac1912002-03-03 09:43:07 +0000855
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856static int
857print_match(const struct ip6t_entry_match *m,
858 const struct ip6t_ip6 *ip,
859 int numeric)
860{
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000861 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862
863 if (match) {
864 if (match->print)
865 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000866 else
867 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000868 } else {
869 if (m->u.user.name[0])
870 printf("UNKNOWN match `%s' ", m->u.user.name);
871 }
872 /* Don't stop iterating. */
873 return 0;
874}
875
876/* e is called `fw' here for hysterical raisins */
877static void
878print_firewall(const struct ip6t_entry *fw,
879 const char *targname,
880 unsigned int num,
881 unsigned int format,
882 const ip6tc_handle_t handle)
883{
884 struct ip6tables_target *target = NULL;
885 const struct ip6t_entry_target *t;
886 u_int8_t flags;
887 char buf[BUFSIZ];
888
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889 if (!ip6tc_is_chain(targname, handle))
890 target = find_target(targname, TRY_LOAD);
891 else
892 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
893
894 t = ip6t_get_target((struct ip6t_entry *)fw);
895 flags = fw->ipv6.flags;
896
897 if (format & FMT_LINENUMBERS)
898 printf(FMT("%-4u ", "%u "), num+1);
899
900 if (!(format & FMT_NOCOUNTS)) {
901 print_num(fw->counters.pcnt, format);
902 print_num(fw->counters.bcnt, format);
903 }
904
905 if (!(format & FMT_NOTARGET))
906 printf(FMT("%-9s ", "%s "), targname);
907
908 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
909 {
Harald Welte43ac1912002-03-03 09:43:07 +0000910 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000911 if (pname)
912 printf(FMT("%-5s", "%s "), pname);
913 else
914 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
915 }
916
917 if (format & FMT_OPTIONS) {
918 if (format & FMT_NOTABLE)
919 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000920 fputc(' ', stdout); /* Invert flag of FRAG */
921 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000922 fputc(' ', stdout);
923 }
924
925 if (format & FMT_VIA) {
926 char iface[IFNAMSIZ+2];
927
928 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
929 iface[0] = '!';
930 iface[1] = '\0';
931 }
932 else iface[0] = '\0';
933
934 if (fw->ipv6.iniface[0] != '\0') {
935 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936 }
937 else if (format & FMT_NUMERIC) strcat(iface, "*");
938 else strcat(iface, "any");
939 printf(FMT(" %-6s ","in %s "), iface);
940
941 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
942 iface[0] = '!';
943 iface[1] = '\0';
944 }
945 else iface[0] = '\0';
946
947 if (fw->ipv6.outiface[0] != '\0') {
948 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000949 }
950 else if (format & FMT_NUMERIC) strcat(iface, "*");
951 else strcat(iface, "any");
952 printf(FMT("%-6s ","out %s "), iface);
953 }
954
955 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
956 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
957 && !(format & FMT_NUMERIC))
958 printf(FMT("%-19s ","%s "), "anywhere");
959 else {
960 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000961 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000962 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000963 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
964 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000965 printf(FMT("%-19s ","%s "), buf);
966 }
967
968 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
969 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
970 && !(format & FMT_NUMERIC))
971 printf(FMT("%-19s","-> %s"), "anywhere");
972 else {
973 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000974 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000975 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000976 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
977 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 printf(FMT("%-19s","-> %s"), buf);
979 }
980
981 if (format & FMT_NOTABLE)
982 fputs(" ", stdout);
983
984 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
985
986 if (target) {
987 if (target->print)
988 /* Print the target information. */
989 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
990 } else if (t->u.target_size != sizeof(*t))
991 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000992 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993
994 if (!(format & FMT_NONEWLINE))
995 fputc('\n', stdout);
996}
997
998static void
999print_firewall_line(const struct ip6t_entry *fw,
1000 const ip6tc_handle_t h)
1001{
1002 struct ip6t_entry_target *t;
1003
1004 t = ip6t_get_target((struct ip6t_entry *)fw);
1005 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1006}
1007
1008static int
1009append_entry(const ip6t_chainlabel chain,
1010 struct ip6t_entry *fw,
1011 unsigned int nsaddrs,
1012 const struct in6_addr saddrs[],
1013 unsigned int ndaddrs,
1014 const struct in6_addr daddrs[],
1015 int verbose,
1016 ip6tc_handle_t *handle)
1017{
1018 unsigned int i, j;
1019 int ret = 1;
1020
1021 for (i = 0; i < nsaddrs; i++) {
1022 fw->ipv6.src = saddrs[i];
1023 for (j = 0; j < ndaddrs; j++) {
1024 fw->ipv6.dst = daddrs[j];
1025 if (verbose)
1026 print_firewall_line(fw, *handle);
1027 ret &= ip6tc_append_entry(chain, fw, handle);
1028 }
1029 }
1030
1031 return ret;
1032}
1033
1034static int
1035replace_entry(const ip6t_chainlabel chain,
1036 struct ip6t_entry *fw,
1037 unsigned int rulenum,
1038 const struct in6_addr *saddr,
1039 const struct in6_addr *daddr,
1040 int verbose,
1041 ip6tc_handle_t *handle)
1042{
1043 fw->ipv6.src = *saddr;
1044 fw->ipv6.dst = *daddr;
1045
1046 if (verbose)
1047 print_firewall_line(fw, *handle);
1048 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1049}
1050
1051static int
1052insert_entry(const ip6t_chainlabel chain,
1053 struct ip6t_entry *fw,
1054 unsigned int rulenum,
1055 unsigned int nsaddrs,
1056 const struct in6_addr saddrs[],
1057 unsigned int ndaddrs,
1058 const struct in6_addr daddrs[],
1059 int verbose,
1060 ip6tc_handle_t *handle)
1061{
1062 unsigned int i, j;
1063 int ret = 1;
1064
1065 for (i = 0; i < nsaddrs; i++) {
1066 fw->ipv6.src = saddrs[i];
1067 for (j = 0; j < ndaddrs; j++) {
1068 fw->ipv6.dst = daddrs[j];
1069 if (verbose)
1070 print_firewall_line(fw, *handle);
1071 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1072 }
1073 }
1074
1075 return ret;
1076}
1077
1078static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001079make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001080{
1081 /* Establish mask for comparison */
1082 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001083 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001084 unsigned char *mask, *mptr;
1085
1086 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001087 for (matchp = matches; matchp; matchp = matchp->next)
1088 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001089
1090 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001091 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001092 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001093
1094 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1095 mptr = mask + sizeof(struct ip6t_entry);
1096
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001097 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001098 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001099 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001100 + matchp->match->userspacesize);
1101 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001102 }
1103
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001104 memset(mptr, 0xFF,
1105 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001106 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001107
1108 return mask;
1109}
1110
1111static int
1112delete_entry(const ip6t_chainlabel chain,
1113 struct ip6t_entry *fw,
1114 unsigned int nsaddrs,
1115 const struct in6_addr saddrs[],
1116 unsigned int ndaddrs,
1117 const struct in6_addr daddrs[],
1118 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001119 ip6tc_handle_t *handle,
1120 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001121{
1122 unsigned int i, j;
1123 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001124 unsigned char *mask;
1125
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001126 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001127 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001128 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001129 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001130 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001131 if (verbose)
1132 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001133 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001134 }
1135 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001136 free(mask);
1137
Rusty Russell5eed48a2000-06-02 20:12:24 +00001138 return ret;
1139}
1140
András Kis-Szabó764316a2001-02-26 17:31:20 +00001141int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001142for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1143 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1144{
1145 int ret = 1;
1146 const char *chain;
1147 char *chains;
1148 unsigned int i, chaincount = 0;
1149
1150 chain = ip6tc_first_chain(handle);
1151 while (chain) {
1152 chaincount++;
1153 chain = ip6tc_next_chain(handle);
1154 }
1155
1156 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1157 i = 0;
1158 chain = ip6tc_first_chain(handle);
1159 while (chain) {
1160 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1161 i++;
1162 chain = ip6tc_next_chain(handle);
1163 }
1164
1165 for (i = 0; i < chaincount; i++) {
1166 if (!builtinstoo
1167 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001168 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001169 continue;
1170 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1171 }
1172
1173 free(chains);
1174 return ret;
1175}
1176
András Kis-Szabó764316a2001-02-26 17:31:20 +00001177int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001178flush_entries(const ip6t_chainlabel chain, int verbose,
1179 ip6tc_handle_t *handle)
1180{
1181 if (!chain)
1182 return for_each_chain(flush_entries, verbose, 1, handle);
1183
1184 if (verbose)
1185 fprintf(stdout, "Flushing chain `%s'\n", chain);
1186 return ip6tc_flush_entries(chain, handle);
1187}
1188
1189static int
1190zero_entries(const ip6t_chainlabel chain, int verbose,
1191 ip6tc_handle_t *handle)
1192{
1193 if (!chain)
1194 return for_each_chain(zero_entries, verbose, 1, handle);
1195
1196 if (verbose)
1197 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1198 return ip6tc_zero_entries(chain, handle);
1199}
1200
András Kis-Szabó764316a2001-02-26 17:31:20 +00001201int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001202delete_chain(const ip6t_chainlabel chain, int verbose,
1203 ip6tc_handle_t *handle)
1204{
1205 if (!chain)
1206 return for_each_chain(delete_chain, verbose, 0, handle);
1207
1208 if (verbose)
1209 fprintf(stdout, "Deleting chain `%s'\n", chain);
1210 return ip6tc_delete_chain(chain, handle);
1211}
1212
1213static int
1214list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1215 int expanded, int linenumbers, ip6tc_handle_t *handle)
1216{
1217 int found = 0;
1218 unsigned int format;
1219 const char *this;
1220
1221 format = FMT_OPTIONS;
1222 if (!verbose)
1223 format |= FMT_NOCOUNTS;
1224 else
1225 format |= FMT_VIA;
1226
1227 if (numeric)
1228 format |= FMT_NUMERIC;
1229
1230 if (!expanded)
1231 format |= FMT_KILOMEGAGIGA;
1232
1233 if (linenumbers)
1234 format |= FMT_LINENUMBERS;
1235
1236 for (this = ip6tc_first_chain(handle);
1237 this;
1238 this = ip6tc_next_chain(handle)) {
1239 const struct ip6t_entry *i;
1240 unsigned int num;
1241
1242 if (chain && strcmp(chain, this) != 0)
1243 continue;
1244
1245 if (found) printf("\n");
1246
1247 print_header(format, this, handle);
1248 i = ip6tc_first_rule(this, handle);
1249
1250 num = 0;
1251 while (i) {
1252 print_firewall(i,
1253 ip6tc_get_target(i, handle),
1254 num++,
1255 format,
1256 *handle);
1257 i = ip6tc_next_rule(i, handle);
1258 }
1259 found = 1;
1260 }
1261
1262 errno = ENOENT;
1263 return found;
1264}
1265
1266static struct ip6t_entry *
1267generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001268 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001269 struct ip6t_entry_target *target)
1270{
1271 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001272 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001273 struct ip6t_entry *e;
1274
1275 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001276 for (matchp = matches; matchp; matchp = matchp->next)
1277 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001278
1279 e = fw_malloc(size + target->u.target_size);
1280 *e = *fw;
1281 e->target_offset = size;
1282 e->next_offset = size + target->u.target_size;
1283
1284 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001285 for (matchp = matches; matchp; matchp = matchp->next) {
1286 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1287 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001288 }
1289 memcpy(e->elems + size, target, target->u.target_size);
1290
1291 return e;
1292}
1293
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001294void clear_rule_matches(struct ip6tables_rule_match **matches)
1295{
1296 struct ip6tables_rule_match *matchp, *tmp;
1297
1298 for (matchp = *matches; matchp;) {
1299 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001300 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001301 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001302 matchp->match->m = NULL;
1303 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001304 if (matchp->match == matchp->match->next) {
1305 free(matchp->match);
1306 matchp->match = NULL;
1307 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001308 free(matchp);
1309 matchp = tmp;
1310 }
1311
1312 *matches = NULL;
1313}
1314
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001315static void set_revision(char *name, u_int8_t revision)
1316{
1317 /* Old kernel sources don't have ".revision" field,
1318 but we stole a byte from name. */
1319 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1320 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1321}
1322
Rusty Russell5eed48a2000-06-02 20:12:24 +00001323int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1324{
1325 struct ip6t_entry fw, *e = NULL;
1326 int invert = 0;
1327 unsigned int nsaddrs = 0, ndaddrs = 0;
1328 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1329
1330 int c, verbose = 0;
1331 const char *chain = NULL;
1332 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1333 const char *policy = NULL, *newname = NULL;
1334 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001335 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001336 int ret = 1;
1337 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001338 struct ip6tables_rule_match *matches = NULL;
1339 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001340 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001341 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001342 const char *jumpto = "";
1343 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001344 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001345 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001346
1347 memset(&fw, 0, sizeof(fw));
1348
Harald Welte43ac1912002-03-03 09:43:07 +00001349 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001350 * a second time */
1351 optind = 0;
1352
Harald Welte43ac1912002-03-03 09:43:07 +00001353 /* clear mflags in case do_command gets called a second time
1354 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001355 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001356 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001357
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001358 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001359 t->tflags = 0;
1360 t->used = 0;
1361 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001362
Rusty Russell5eed48a2000-06-02 20:12:24 +00001363 /* Suppress error messages: we may add new options if we
1364 demand-load a protocol. */
1365 opterr = 0;
1366
1367 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001368 "-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 +00001369 opts, NULL)) != -1) {
1370 switch (c) {
1371 /*
1372 * Command selection
1373 */
1374 case 'A':
1375 add_command(&command, CMD_APPEND, CMD_NONE,
1376 invert);
1377 chain = optarg;
1378 break;
1379
1380 case 'D':
1381 add_command(&command, CMD_DELETE, CMD_NONE,
1382 invert);
1383 chain = optarg;
1384 if (optind < argc && argv[optind][0] != '-'
1385 && argv[optind][0] != '!') {
1386 rulenum = parse_rulenumber(argv[optind++]);
1387 command = CMD_DELETE_NUM;
1388 }
1389 break;
1390
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 case 'R':
1392 add_command(&command, CMD_REPLACE, CMD_NONE,
1393 invert);
1394 chain = optarg;
1395 if (optind < argc && argv[optind][0] != '-'
1396 && argv[optind][0] != '!')
1397 rulenum = parse_rulenumber(argv[optind++]);
1398 else
1399 exit_error(PARAMETER_PROBLEM,
1400 "-%c requires a rule number",
1401 cmd2char(CMD_REPLACE));
1402 break;
1403
1404 case 'I':
1405 add_command(&command, CMD_INSERT, CMD_NONE,
1406 invert);
1407 chain = optarg;
1408 if (optind < argc && argv[optind][0] != '-'
1409 && argv[optind][0] != '!')
1410 rulenum = parse_rulenumber(argv[optind++]);
1411 else rulenum = 1;
1412 break;
1413
1414 case 'L':
1415 add_command(&command, CMD_LIST, CMD_ZERO,
1416 invert);
1417 if (optarg) chain = optarg;
1418 else if (optind < argc && argv[optind][0] != '-'
1419 && argv[optind][0] != '!')
1420 chain = argv[optind++];
1421 break;
1422
1423 case 'F':
1424 add_command(&command, CMD_FLUSH, CMD_NONE,
1425 invert);
1426 if (optarg) chain = optarg;
1427 else if (optind < argc && argv[optind][0] != '-'
1428 && argv[optind][0] != '!')
1429 chain = argv[optind++];
1430 break;
1431
1432 case 'Z':
1433 add_command(&command, CMD_ZERO, CMD_LIST,
1434 invert);
1435 if (optarg) chain = optarg;
1436 else if (optind < argc && argv[optind][0] != '-'
1437 && argv[optind][0] != '!')
1438 chain = argv[optind++];
1439 break;
1440
1441 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001442 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001443 exit_error(PARAMETER_PROBLEM,
1444 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001445 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001446 if (find_target(optarg, TRY_LOAD))
1447 exit_error(PARAMETER_PROBLEM,
1448 "chain name may not clash "
1449 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001450 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1451 invert);
1452 chain = optarg;
1453 break;
1454
1455 case 'X':
1456 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1457 invert);
1458 if (optarg) chain = optarg;
1459 else if (optind < argc && argv[optind][0] != '-'
1460 && argv[optind][0] != '!')
1461 chain = argv[optind++];
1462 break;
1463
1464 case 'E':
1465 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1466 invert);
1467 chain = optarg;
1468 if (optind < argc && argv[optind][0] != '-'
1469 && argv[optind][0] != '!')
1470 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001471 else
1472 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001473 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001474 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001475 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001476 break;
1477
1478 case 'P':
1479 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1480 invert);
1481 chain = optarg;
1482 if (optind < argc && argv[optind][0] != '-'
1483 && argv[optind][0] != '!')
1484 policy = argv[optind++];
1485 else
1486 exit_error(PARAMETER_PROBLEM,
1487 "-%c requires a chain and a policy",
1488 cmd2char(CMD_SET_POLICY));
1489 break;
1490
1491 case 'h':
1492 if (!optarg)
1493 optarg = argv[optind];
1494
Jonas Berlin1b91e592005-04-01 06:58:38 +00001495 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001496 if (!matches && protocol)
1497 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001498
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001499 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500
1501 /*
1502 * Option selection
1503 */
1504 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001505 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001506 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1507 invert);
1508
1509 /* Canonicalize into lower case */
1510 for (protocol = argv[optind-1]; *protocol; protocol++)
1511 *protocol = tolower(*protocol);
1512
1513 protocol = argv[optind-1];
1514 fw.ipv6.proto = parse_protocol(protocol);
1515 fw.ipv6.flags |= IP6T_F_PROTO;
1516
1517 if (fw.ipv6.proto == 0
1518 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1519 exit_error(PARAMETER_PROBLEM,
1520 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001521
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001522 if (is_exthdr(fw.ipv6.proto)
1523 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001524 fprintf(stderr,
1525 "Warning: never matched protocol: %s. "
1526 "use extension match instead.\n",
1527 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001528 break;
1529
1530 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001531 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001532 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1533 invert);
1534 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001535 break;
1536
1537 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001538 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001539 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1540 invert);
1541 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001542 break;
1543
1544 case 'j':
1545 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1546 invert);
1547 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001548 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549 target = find_target(jumpto, TRY_LOAD);
1550
1551 if (target) {
1552 size_t size;
1553
Harald Welte43ac1912002-03-03 09:43:07 +00001554 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1555 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001556
1557 target->t = fw_calloc(1, size);
1558 target->t->u.target_size = size;
1559 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001560 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001561 target->init(target->t);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001562 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 }
1564 break;
1565
1566
1567 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001568 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001569 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1570 invert);
1571 parse_interface(argv[optind-1],
1572 fw.ipv6.iniface,
1573 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 break;
1575
1576 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001577 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001578 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1579 invert);
1580 parse_interface(argv[optind-1],
1581 fw.ipv6.outiface,
1582 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001583 break;
1584
1585 case 'v':
1586 if (!verbose)
1587 set_option(&options, OPT_VERBOSE,
1588 &fw.ipv6.invflags, invert);
1589 verbose++;
1590 break;
1591
1592 case 'm': {
1593 size_t size;
1594
1595 if (invert)
1596 exit_error(PARAMETER_PROBLEM,
1597 "unexpected ! flag before --match");
1598
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001599 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001600 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1601 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001602 m->m = fw_calloc(1, size);
1603 m->m->u.match_size = size;
1604 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001605 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001606 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001607 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001608 if (m != m->next)
1609 /* Merge options for non-cloned matches */
1610 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 }
1612 break;
1613
1614 case 'n':
1615 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1616 invert);
1617 break;
1618
1619 case 't':
1620 if (invert)
1621 exit_error(PARAMETER_PROBLEM,
1622 "unexpected ! flag before --table");
1623 *table = argv[optind-1];
1624 break;
1625
1626 case 'x':
1627 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1628 invert);
1629 break;
1630
1631 case 'V':
1632 if (invert)
1633 printf("Not %s ;-)\n", program_version);
1634 else
1635 printf("%s v%s\n",
1636 program_name, program_version);
1637 exit(0);
1638
1639 case '0':
1640 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1641 invert);
1642 break;
1643
Harald Welte43ac1912002-03-03 09:43:07 +00001644 case 'M':
1645 modprobe = optarg;
1646 break;
1647
1648 case 'c':
1649
1650 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1651 invert);
1652 pcnt = optarg;
1653 if (optind < argc && argv[optind][0] != '-'
1654 && argv[optind][0] != '!')
1655 bcnt = argv[optind++];
1656 else
1657 exit_error(PARAMETER_PROBLEM,
1658 "-%c requires packet and byte counter",
1659 opt2char(OPT_COUNTERS));
1660
Patrick McHardy875441e2007-10-17 08:48:58 +00001661 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001662 exit_error(PARAMETER_PROBLEM,
1663 "-%c packet counter not numeric",
1664 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001665 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001666
Patrick McHardy875441e2007-10-17 08:48:58 +00001667 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001668 exit_error(PARAMETER_PROBLEM,
1669 "-%c byte counter not numeric",
1670 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001671 fw.counters.bcnt = cnt;
1672
Harald Welte43ac1912002-03-03 09:43:07 +00001673 break;
1674
1675
Rusty Russell5eed48a2000-06-02 20:12:24 +00001676 case 1: /* non option */
1677 if (optarg[0] == '!' && optarg[1] == '\0') {
1678 if (invert)
1679 exit_error(PARAMETER_PROBLEM,
1680 "multiple consecutive ! not"
1681 " allowed");
1682 invert = TRUE;
1683 optarg[0] = '\0';
1684 continue;
1685 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001686 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001687 exit_tryhelp(2);
1688
1689 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001690 if (!target
1691 || !(target->parse(c - target->option_offset,
1692 argv, invert,
1693 &target->tflags,
1694 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001695 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001696 if (matchp->completed)
1697 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001698 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001700 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001701 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001702 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001703 break;
1704 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001705 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001706
1707 /* If you listen carefully, you can
1708 actually hear this code suck. */
1709
1710 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001711 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001712 * parameter, that has not been parsed yet,
1713 * it's not an option of an explicitly loaded
1714 * match or a target. However, we support
1715 * implicit loading of the protocol match
1716 * extension. '-p tcp' means 'l4 proto 6' and
1717 * at the same time 'load tcp protocol match on
1718 * demand if we specify --dport'.
1719 *
1720 * To make this work, we need to make sure:
1721 * - the parameter has not been parsed by
1722 * a match (m above)
1723 * - a protocol has been specified
1724 * - the protocol extension has not been
1725 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001726 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001727 * - the protocol extension can be successively
1728 * loaded
1729 */
1730 if (m == NULL
1731 && protocol
1732 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001733 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001734 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001735 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001736 && (proto_used == 0))
1737 )
1738 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001739 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001740 /* Try loading protocol */
1741 size_t size;
1742
1743 proto_used = 1;
1744
1745 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1746 + m->size;
1747
1748 m->m = fw_calloc(1, size);
1749 m->m->u.match_size = size;
1750 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001751 set_revision(m->m->u.user.name,
1752 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001753 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001754 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001755
1756 opts = merge_options(opts,
1757 m->extra_opts, &m->option_offset);
1758
1759 optind--;
1760 continue;
1761 }
1762
Rusty Russell5eed48a2000-06-02 20:12:24 +00001763 if (!m)
1764 exit_error(PARAMETER_PROBLEM,
1765 "Unknown arg `%s'",
1766 argv[optind-1]);
1767 }
1768 }
1769 invert = FALSE;
1770 }
1771
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001772 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001773 if (matchp->match->final_check != NULL)
1774 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001775
Jan Engelhardt830132a2007-10-04 16:24:50 +00001776 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001777 target->final_check(target->tflags);
1778
1779 /* Fix me: must put inverse options checking here --MN */
1780
1781 if (optind < argc)
1782 exit_error(PARAMETER_PROBLEM,
1783 "unknown arguments found on commandline");
1784 if (!command)
1785 exit_error(PARAMETER_PROBLEM, "no command specified");
1786 if (invert)
1787 exit_error(PARAMETER_PROBLEM,
1788 "nothing appropriate following !");
1789
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001790 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001791 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001792 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001793 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001794 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001795 }
1796
1797 if (shostnetworkmask)
1798 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1799 &(fw.ipv6.smsk), &nsaddrs);
1800
1801 if (dhostnetworkmask)
1802 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1803 &(fw.ipv6.dmsk), &ndaddrs);
1804
1805 if ((nsaddrs > 1 || ndaddrs > 1) &&
1806 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1807 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1808 " source or destination IP addresses");
1809
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1811 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1812 "specify a unique address");
1813
1814 generic_opt_check(command, options);
1815
1816 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1817 exit_error(PARAMETER_PROBLEM,
1818 "chain name `%s' too long (must be under %i chars)",
1819 chain, IP6T_FUNCTION_MAXNAMELEN);
1820
Harald Welte43ac1912002-03-03 09:43:07 +00001821 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001822 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001823 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001824
Rusty Russell8beb0492004-12-22 00:37:10 +00001825 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001826 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001827 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001828
Harald Welte43ac1912002-03-03 09:43:07 +00001829 if (!*handle)
1830 exit_error(VERSION_PROBLEM,
1831 "can't initialize ip6tables table `%s': %s",
1832 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001834 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835 || command == CMD_DELETE
1836 || command == CMD_INSERT
1837 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001838 if (strcmp(chain, "PREROUTING") == 0
1839 || strcmp(chain, "INPUT") == 0) {
1840 /* -o not valid with incoming packets. */
1841 if (options & OPT_VIANAMEOUT)
1842 exit_error(PARAMETER_PROBLEM,
1843 "Can't use -%c with %s\n",
1844 opt2char(OPT_VIANAMEOUT),
1845 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001846 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001847
Harald Welte43ac1912002-03-03 09:43:07 +00001848 if (strcmp(chain, "POSTROUTING") == 0
1849 || strcmp(chain, "OUTPUT") == 0) {
1850 /* -i not valid with outgoing packets */
1851 if (options & OPT_VIANAMEIN)
1852 exit_error(PARAMETER_PROBLEM,
1853 "Can't use -%c with %s\n",
1854 opt2char(OPT_VIANAMEIN),
1855 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001856 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001857
1858 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001859 fprintf(stderr,
1860 "Warning: using chain %s, not extension\n",
1861 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001863 if (target->t)
1864 free(target->t);
1865
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866 target = NULL;
1867 }
1868
1869 /* If they didn't specify a target, or it's a chain
1870 name, use standard. */
1871 if (!target
1872 && (strlen(jumpto) == 0
1873 || ip6tc_is_chain(jumpto, *handle))) {
1874 size_t size;
1875
1876 target = find_target(IP6T_STANDARD_TARGET,
1877 LOAD_MUST_SUCCEED);
1878
1879 size = sizeof(struct ip6t_entry_target)
1880 + target->size;
1881 target->t = fw_calloc(1, size);
1882 target->t->u.target_size = size;
1883 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001884 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001885 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886 }
1887
1888 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001889 /* it is no chain, and we can't load a plugin.
1890 * We cannot know if the plugin is corrupt, non
1891 * existant OR if the user just misspelled a
1892 * chain. */
1893 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001894 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001895 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001896 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897 }
1898 }
1899
1900 switch (command) {
1901 case CMD_APPEND:
1902 ret = append_entry(chain, e,
1903 nsaddrs, saddrs, ndaddrs, daddrs,
1904 options&OPT_VERBOSE,
1905 handle);
1906 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001907 case CMD_DELETE:
1908 ret = delete_entry(chain, e,
1909 nsaddrs, saddrs, ndaddrs, daddrs,
1910 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001911 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912 break;
1913 case CMD_DELETE_NUM:
1914 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1915 break;
1916 case CMD_REPLACE:
1917 ret = replace_entry(chain, e, rulenum - 1,
1918 saddrs, daddrs, options&OPT_VERBOSE,
1919 handle);
1920 break;
1921 case CMD_INSERT:
1922 ret = insert_entry(chain, e, rulenum - 1,
1923 nsaddrs, saddrs, ndaddrs, daddrs,
1924 options&OPT_VERBOSE,
1925 handle);
1926 break;
1927 case CMD_LIST:
1928 ret = list_entries(chain,
1929 options&OPT_VERBOSE,
1930 options&OPT_NUMERIC,
1931 options&OPT_EXPANDED,
1932 options&OPT_LINENUMBERS,
1933 handle);
1934 break;
1935 case CMD_FLUSH:
1936 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1937 break;
1938 case CMD_ZERO:
1939 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1940 break;
1941 case CMD_LIST|CMD_ZERO:
1942 ret = list_entries(chain,
1943 options&OPT_VERBOSE,
1944 options&OPT_NUMERIC,
1945 options&OPT_EXPANDED,
1946 options&OPT_LINENUMBERS,
1947 handle);
1948 if (ret)
1949 ret = zero_entries(chain,
1950 options&OPT_VERBOSE, handle);
1951 break;
1952 case CMD_NEW_CHAIN:
1953 ret = ip6tc_create_chain(chain, handle);
1954 break;
1955 case CMD_DELETE_CHAIN:
1956 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1957 break;
1958 case CMD_RENAME_CHAIN:
1959 ret = ip6tc_rename_chain(chain, newname, handle);
1960 break;
1961 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001962 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 break;
1964 default:
1965 /* We should never reach this... */
1966 exit_tryhelp(2);
1967 }
1968
1969 if (verbose > 1)
1970 dump_entries6(*handle);
1971
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001972 clear_rule_matches(&matches);
1973
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001974 if (e != NULL) {
1975 free(e);
1976 e = NULL;
1977 }
1978
1979 for (c = 0; c < nsaddrs; c++)
1980 free(&saddrs[c]);
1981
1982 for (c = 0; c < ndaddrs; c++)
1983 free(&daddrs[c]);
1984
Pablo Neiradfdcd642005-05-29 19:05:23 +00001985 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001986
Rusty Russell5eed48a2000-06-02 20:12:24 +00001987 return ret;
1988}