blob: 94dadf38dd1e24f6b36397a27d383c1952365bdf [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 +0000462/*
463 * All functions starting with "parse" should succeed, otherwise
464 * the program fails.
465 * Most routines return pointers to static data that may change
466 * between calls to the same or other routines with a few exceptions:
467 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
468 * return global static data.
469*/
470
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
472static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000473find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474{
Harald Welteed498492001-07-23 01:24:22 +0000475 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000476
Harald Welte43ac1912002-03-03 09:43:07 +0000477 if (string_to_number(pname, 0, 255, &proto) != -1) {
478 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000479
Harald Welte43ac1912002-03-03 09:43:07 +0000480 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000481 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000482 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000483 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000484
485 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486}
487
Harald Welte43ac1912002-03-03 09:43:07 +0000488u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489parse_protocol(const char *s)
490{
Harald Welteed498492001-07-23 01:24:22 +0000491 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000492
Harald Welteed498492001-07-23 01:24:22 +0000493 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000494 struct protoent *pent;
495
Harald Weltecbe1ec72006-02-11 09:50:11 +0000496 /* first deal with the special case of 'all' to prevent
497 * people from being able to redefine 'all' in nsswitch
498 * and/or provoke expensive [not working] ldap/nis/...
499 * lookups */
500 if (!strcmp(s, "all"))
501 return 0;
502
Rusty Russell5eed48a2000-06-02 20:12:24 +0000503 if ((pent = getprotobyname(s)))
504 proto = pent->p_proto;
505 else {
506 unsigned int i;
507 for (i = 0;
508 i < sizeof(chain_protos)/sizeof(struct pprot);
509 i++) {
510 if (strcmp(s, chain_protos[i].name) == 0) {
511 proto = chain_protos[i].num;
512 break;
513 }
514 }
515 if (i == sizeof(chain_protos)/sizeof(struct pprot))
516 exit_error(PARAMETER_PROBLEM,
517 "unknown protocol `%s' specified",
518 s);
519 }
520 }
521
522 return (u_int16_t)proto;
523}
524
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000525/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000526static int is_exthdr(u_int16_t proto)
527{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000528 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000529 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000530 proto == IPPROTO_AH ||
531 proto == IPPROTO_DSTOPTS);
532}
533
Rusty Russell5eed48a2000-06-02 20:12:24 +0000534/* Can't be zero. */
535static int
536parse_rulenumber(const char *rule)
537{
Harald Welte43ac1912002-03-03 09:43:07 +0000538 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000539
Harald Welte43ac1912002-03-03 09:43:07 +0000540 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000541 exit_error(PARAMETER_PROBLEM,
542 "Invalid rule number `%s'", rule);
543
544 return rulenum;
545}
546
547static const char *
548parse_target(const char *targetname)
549{
550 const char *ptr;
551
552 if (strlen(targetname) < 1)
553 exit_error(PARAMETER_PROBLEM,
554 "Invalid target name (too short)");
555
556 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
557 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000558 "Invalid target name `%s' (%u chars max)",
559 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000560
561 for (ptr = targetname; *ptr; ptr++)
562 if (isspace(*ptr))
563 exit_error(PARAMETER_PROBLEM,
564 "Invalid target name `%s'", targetname);
565 return targetname;
566}
567
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568static void
569set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
570 int invert)
571{
572 if (*options & option)
573 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
574 opt2char(option));
575 *options |= option;
576
577 if (invert) {
578 unsigned int i;
579 for (i = 0; 1 << i != option; i++);
580
581 if (!inverse_for_options[i])
582 exit_error(PARAMETER_PROBLEM,
583 "cannot have ! before -%c",
584 opt2char(option));
585 *invflg |= inverse_for_options[i];
586 }
587}
588
Rusty Russell5eed48a2000-06-02 20:12:24 +0000589static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000590merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000591 unsigned int *option_offset)
592{
593 unsigned int num_old, num_new, i;
594 struct option *merge;
595
Jan Engelhardtd0145402007-07-30 14:32:26 +0000596 if (newopts == NULL)
597 return oldopts;
598
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599 for (num_old = 0; oldopts[num_old].name; num_old++);
600 for (num_new = 0; newopts[num_new].name; num_new++);
601
602 global_option_offset += OPTION_OFFSET;
603 *option_offset = global_option_offset;
604
605 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
606 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000607 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000608 for (i = 0; i < num_new; i++) {
609 merge[num_old + i] = newopts[i];
610 merge[num_old + i].val += *option_offset;
611 }
612 memset(merge + num_old + num_new, 0, sizeof(struct option));
613
614 return merge;
615}
616
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000618{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000619 me->family = AF_INET6;
620 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000621}
622
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000624{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000625 me->family = AF_INET6;
626 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000627}
628
629static void
630print_num(u_int64_t number, unsigned int format)
631{
Harald Welte43ac1912002-03-03 09:43:07 +0000632 if (format & FMT_KILOMEGAGIGA) {
633 if (number > 99999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
637 if (number > 9999) {
638 number = (number + 500) / 1000;
639 if (number > 9999) {
640 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000644 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000650 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000651 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000652}
653
Harald Welte43ac1912002-03-03 09:43:07 +0000654
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655static void
656print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
657{
658 struct ip6t_counters counters;
659 const char *pol = ip6tc_get_policy(chain, &counters, handle);
660 printf("Chain %s", chain);
661 if (pol) {
662 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000663 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000664 fputc(' ', stdout);
665 print_num(counters.pcnt, (format|FMT_NOTABLE));
666 fputs("packets, ", stdout);
667 print_num(counters.bcnt, (format|FMT_NOTABLE));
668 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000669 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000670 printf(")\n");
671 } else {
672 unsigned int refs;
673 if (!ip6tc_get_references(&refs, chain, handle))
674 printf(" (ERROR obtaining refs)\n");
675 else
676 printf(" (%u references)\n", refs);
677 }
678
679 if (format & FMT_LINENUMBERS)
680 printf(FMT("%-4s ", "%s "), "num");
681 if (!(format & FMT_NOCOUNTS)) {
682 if (format & FMT_KILOMEGAGIGA) {
683 printf(FMT("%5s ","%s "), "pkts");
684 printf(FMT("%5s ","%s "), "bytes");
685 } else {
686 printf(FMT("%8s ","%s "), "pkts");
687 printf(FMT("%10s ","%s "), "bytes");
688 }
689 }
690 if (!(format & FMT_NOTARGET))
691 printf(FMT("%-9s ","%s "), "target");
692 fputs(" prot ", stdout);
693 if (format & FMT_OPTIONS)
694 fputs("opt", stdout);
695 if (format & FMT_VIA) {
696 printf(FMT(" %-6s ","%s "), "in");
697 printf(FMT("%-6s ","%s "), "out");
698 }
699 printf(FMT(" %-19s ","%s "), "source");
700 printf(FMT(" %-19s "," %s "), "destination");
701 printf("\n");
702}
703
Harald Welte43ac1912002-03-03 09:43:07 +0000704
Rusty Russell5eed48a2000-06-02 20:12:24 +0000705static int
706print_match(const struct ip6t_entry_match *m,
707 const struct ip6t_ip6 *ip,
708 int numeric)
709{
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000710 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000711
712 if (match) {
713 if (match->print)
714 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000715 else
716 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000717 } else {
718 if (m->u.user.name[0])
719 printf("UNKNOWN match `%s' ", m->u.user.name);
720 }
721 /* Don't stop iterating. */
722 return 0;
723}
724
725/* e is called `fw' here for hysterical raisins */
726static void
727print_firewall(const struct ip6t_entry *fw,
728 const char *targname,
729 unsigned int num,
730 unsigned int format,
731 const ip6tc_handle_t handle)
732{
733 struct ip6tables_target *target = NULL;
734 const struct ip6t_entry_target *t;
735 u_int8_t flags;
736 char buf[BUFSIZ];
737
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738 if (!ip6tc_is_chain(targname, handle))
739 target = find_target(targname, TRY_LOAD);
740 else
741 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
742
743 t = ip6t_get_target((struct ip6t_entry *)fw);
744 flags = fw->ipv6.flags;
745
746 if (format & FMT_LINENUMBERS)
747 printf(FMT("%-4u ", "%u "), num+1);
748
749 if (!(format & FMT_NOCOUNTS)) {
750 print_num(fw->counters.pcnt, format);
751 print_num(fw->counters.bcnt, format);
752 }
753
754 if (!(format & FMT_NOTARGET))
755 printf(FMT("%-9s ", "%s "), targname);
756
757 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
758 {
Harald Welte43ac1912002-03-03 09:43:07 +0000759 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000760 if (pname)
761 printf(FMT("%-5s", "%s "), pname);
762 else
763 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
764 }
765
766 if (format & FMT_OPTIONS) {
767 if (format & FMT_NOTABLE)
768 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000769 fputc(' ', stdout); /* Invert flag of FRAG */
770 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771 fputc(' ', stdout);
772 }
773
774 if (format & FMT_VIA) {
775 char iface[IFNAMSIZ+2];
776
777 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
778 iface[0] = '!';
779 iface[1] = '\0';
780 }
781 else iface[0] = '\0';
782
783 if (fw->ipv6.iniface[0] != '\0') {
784 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000785 }
786 else if (format & FMT_NUMERIC) strcat(iface, "*");
787 else strcat(iface, "any");
788 printf(FMT(" %-6s ","in %s "), iface);
789
790 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
791 iface[0] = '!';
792 iface[1] = '\0';
793 }
794 else iface[0] = '\0';
795
796 if (fw->ipv6.outiface[0] != '\0') {
797 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000798 }
799 else if (format & FMT_NUMERIC) strcat(iface, "*");
800 else strcat(iface, "any");
801 printf(FMT("%-6s ","out %s "), iface);
802 }
803
804 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
805 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
806 && !(format & FMT_NUMERIC))
807 printf(FMT("%-19s ","%s "), "anywhere");
808 else {
809 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000810 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000811 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000812 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
813 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814 printf(FMT("%-19s ","%s "), buf);
815 }
816
817 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
818 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
819 && !(format & FMT_NUMERIC))
820 printf(FMT("%-19s","-> %s"), "anywhere");
821 else {
822 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000823 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000825 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
826 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827 printf(FMT("%-19s","-> %s"), buf);
828 }
829
830 if (format & FMT_NOTABLE)
831 fputs(" ", stdout);
832
833 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
834
835 if (target) {
836 if (target->print)
837 /* Print the target information. */
838 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
839 } else if (t->u.target_size != sizeof(*t))
840 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000841 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000842
843 if (!(format & FMT_NONEWLINE))
844 fputc('\n', stdout);
845}
846
847static void
848print_firewall_line(const struct ip6t_entry *fw,
849 const ip6tc_handle_t h)
850{
851 struct ip6t_entry_target *t;
852
853 t = ip6t_get_target((struct ip6t_entry *)fw);
854 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
855}
856
857static int
858append_entry(const ip6t_chainlabel chain,
859 struct ip6t_entry *fw,
860 unsigned int nsaddrs,
861 const struct in6_addr saddrs[],
862 unsigned int ndaddrs,
863 const struct in6_addr daddrs[],
864 int verbose,
865 ip6tc_handle_t *handle)
866{
867 unsigned int i, j;
868 int ret = 1;
869
870 for (i = 0; i < nsaddrs; i++) {
871 fw->ipv6.src = saddrs[i];
872 for (j = 0; j < ndaddrs; j++) {
873 fw->ipv6.dst = daddrs[j];
874 if (verbose)
875 print_firewall_line(fw, *handle);
876 ret &= ip6tc_append_entry(chain, fw, handle);
877 }
878 }
879
880 return ret;
881}
882
883static int
884replace_entry(const ip6t_chainlabel chain,
885 struct ip6t_entry *fw,
886 unsigned int rulenum,
887 const struct in6_addr *saddr,
888 const struct in6_addr *daddr,
889 int verbose,
890 ip6tc_handle_t *handle)
891{
892 fw->ipv6.src = *saddr;
893 fw->ipv6.dst = *daddr;
894
895 if (verbose)
896 print_firewall_line(fw, *handle);
897 return ip6tc_replace_entry(chain, fw, rulenum, handle);
898}
899
900static int
901insert_entry(const ip6t_chainlabel chain,
902 struct ip6t_entry *fw,
903 unsigned int rulenum,
904 unsigned int nsaddrs,
905 const struct in6_addr saddrs[],
906 unsigned int ndaddrs,
907 const struct in6_addr daddrs[],
908 int verbose,
909 ip6tc_handle_t *handle)
910{
911 unsigned int i, j;
912 int ret = 1;
913
914 for (i = 0; i < nsaddrs; i++) {
915 fw->ipv6.src = saddrs[i];
916 for (j = 0; j < ndaddrs; j++) {
917 fw->ipv6.dst = daddrs[j];
918 if (verbose)
919 print_firewall_line(fw, *handle);
920 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
921 }
922 }
923
924 return ret;
925}
926
927static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000928make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000929{
930 /* Establish mask for comparison */
931 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000932 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933 unsigned char *mask, *mptr;
934
935 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000936 for (matchp = matches; matchp; matchp = matchp->next)
937 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938
939 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000940 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000941 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000942
943 memset(mask, 0xFF, sizeof(struct ip6t_entry));
944 mptr = mask + sizeof(struct ip6t_entry);
945
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000946 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000948 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000949 + matchp->match->userspacesize);
950 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000951 }
952
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000953 memset(mptr, 0xFF,
954 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000955 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000956
957 return mask;
958}
959
960static int
961delete_entry(const ip6t_chainlabel chain,
962 struct ip6t_entry *fw,
963 unsigned int nsaddrs,
964 const struct in6_addr saddrs[],
965 unsigned int ndaddrs,
966 const struct in6_addr daddrs[],
967 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000968 ip6tc_handle_t *handle,
969 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000970{
971 unsigned int i, j;
972 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000973 unsigned char *mask;
974
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000975 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000977 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000979 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 if (verbose)
981 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000982 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 }
984 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000985 free(mask);
986
Rusty Russell5eed48a2000-06-02 20:12:24 +0000987 return ret;
988}
989
András Kis-Szabó764316a2001-02-26 17:31:20 +0000990int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000991for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
992 int verbose, int builtinstoo, ip6tc_handle_t *handle)
993{
994 int ret = 1;
995 const char *chain;
996 char *chains;
997 unsigned int i, chaincount = 0;
998
999 chain = ip6tc_first_chain(handle);
1000 while (chain) {
1001 chaincount++;
1002 chain = ip6tc_next_chain(handle);
1003 }
1004
1005 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1006 i = 0;
1007 chain = ip6tc_first_chain(handle);
1008 while (chain) {
1009 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1010 i++;
1011 chain = ip6tc_next_chain(handle);
1012 }
1013
1014 for (i = 0; i < chaincount; i++) {
1015 if (!builtinstoo
1016 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001017 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001018 continue;
1019 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1020 }
1021
1022 free(chains);
1023 return ret;
1024}
1025
András Kis-Szabó764316a2001-02-26 17:31:20 +00001026int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027flush_entries(const ip6t_chainlabel chain, int verbose,
1028 ip6tc_handle_t *handle)
1029{
1030 if (!chain)
1031 return for_each_chain(flush_entries, verbose, 1, handle);
1032
1033 if (verbose)
1034 fprintf(stdout, "Flushing chain `%s'\n", chain);
1035 return ip6tc_flush_entries(chain, handle);
1036}
1037
1038static int
1039zero_entries(const ip6t_chainlabel chain, int verbose,
1040 ip6tc_handle_t *handle)
1041{
1042 if (!chain)
1043 return for_each_chain(zero_entries, verbose, 1, handle);
1044
1045 if (verbose)
1046 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1047 return ip6tc_zero_entries(chain, handle);
1048}
1049
András Kis-Szabó764316a2001-02-26 17:31:20 +00001050int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001051delete_chain(const ip6t_chainlabel chain, int verbose,
1052 ip6tc_handle_t *handle)
1053{
1054 if (!chain)
1055 return for_each_chain(delete_chain, verbose, 0, handle);
1056
1057 if (verbose)
1058 fprintf(stdout, "Deleting chain `%s'\n", chain);
1059 return ip6tc_delete_chain(chain, handle);
1060}
1061
1062static int
1063list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1064 int expanded, int linenumbers, ip6tc_handle_t *handle)
1065{
1066 int found = 0;
1067 unsigned int format;
1068 const char *this;
1069
1070 format = FMT_OPTIONS;
1071 if (!verbose)
1072 format |= FMT_NOCOUNTS;
1073 else
1074 format |= FMT_VIA;
1075
1076 if (numeric)
1077 format |= FMT_NUMERIC;
1078
1079 if (!expanded)
1080 format |= FMT_KILOMEGAGIGA;
1081
1082 if (linenumbers)
1083 format |= FMT_LINENUMBERS;
1084
1085 for (this = ip6tc_first_chain(handle);
1086 this;
1087 this = ip6tc_next_chain(handle)) {
1088 const struct ip6t_entry *i;
1089 unsigned int num;
1090
1091 if (chain && strcmp(chain, this) != 0)
1092 continue;
1093
1094 if (found) printf("\n");
1095
1096 print_header(format, this, handle);
1097 i = ip6tc_first_rule(this, handle);
1098
1099 num = 0;
1100 while (i) {
1101 print_firewall(i,
1102 ip6tc_get_target(i, handle),
1103 num++,
1104 format,
1105 *handle);
1106 i = ip6tc_next_rule(i, handle);
1107 }
1108 found = 1;
1109 }
1110
1111 errno = ENOENT;
1112 return found;
1113}
1114
1115static struct ip6t_entry *
1116generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001117 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001118 struct ip6t_entry_target *target)
1119{
1120 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001121 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001122 struct ip6t_entry *e;
1123
1124 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001125 for (matchp = matches; matchp; matchp = matchp->next)
1126 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001127
1128 e = fw_malloc(size + target->u.target_size);
1129 *e = *fw;
1130 e->target_offset = size;
1131 e->next_offset = size + target->u.target_size;
1132
1133 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001134 for (matchp = matches; matchp; matchp = matchp->next) {
1135 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1136 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001137 }
1138 memcpy(e->elems + size, target, target->u.target_size);
1139
1140 return e;
1141}
1142
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001143void clear_rule_matches(struct ip6tables_rule_match **matches)
1144{
1145 struct ip6tables_rule_match *matchp, *tmp;
1146
1147 for (matchp = *matches; matchp;) {
1148 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001149 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001150 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001151 matchp->match->m = NULL;
1152 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001153 if (matchp->match == matchp->match->next) {
1154 free(matchp->match);
1155 matchp->match = NULL;
1156 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001157 free(matchp);
1158 matchp = tmp;
1159 }
1160
1161 *matches = NULL;
1162}
1163
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001164static void set_revision(char *name, u_int8_t revision)
1165{
1166 /* Old kernel sources don't have ".revision" field,
1167 but we stole a byte from name. */
1168 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1169 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1170}
1171
Rusty Russell5eed48a2000-06-02 20:12:24 +00001172int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1173{
1174 struct ip6t_entry fw, *e = NULL;
1175 int invert = 0;
1176 unsigned int nsaddrs = 0, ndaddrs = 0;
1177 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1178
1179 int c, verbose = 0;
1180 const char *chain = NULL;
1181 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1182 const char *policy = NULL, *newname = NULL;
1183 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001184 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001185 int ret = 1;
1186 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001187 struct ip6tables_rule_match *matches = NULL;
1188 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001190 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001191 const char *jumpto = "";
1192 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001193 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001194 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001195
1196 memset(&fw, 0, sizeof(fw));
1197
Harald Welte43ac1912002-03-03 09:43:07 +00001198 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001199 * a second time */
1200 optind = 0;
1201
Harald Welte43ac1912002-03-03 09:43:07 +00001202 /* clear mflags in case do_command gets called a second time
1203 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001204 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001205 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001206
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001207 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001208 t->tflags = 0;
1209 t->used = 0;
1210 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001211
Rusty Russell5eed48a2000-06-02 20:12:24 +00001212 /* Suppress error messages: we may add new options if we
1213 demand-load a protocol. */
1214 opterr = 0;
1215
1216 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001217 "-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 +00001218 opts, NULL)) != -1) {
1219 switch (c) {
1220 /*
1221 * Command selection
1222 */
1223 case 'A':
1224 add_command(&command, CMD_APPEND, CMD_NONE,
1225 invert);
1226 chain = optarg;
1227 break;
1228
1229 case 'D':
1230 add_command(&command, CMD_DELETE, CMD_NONE,
1231 invert);
1232 chain = optarg;
1233 if (optind < argc && argv[optind][0] != '-'
1234 && argv[optind][0] != '!') {
1235 rulenum = parse_rulenumber(argv[optind++]);
1236 command = CMD_DELETE_NUM;
1237 }
1238 break;
1239
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 case 'R':
1241 add_command(&command, CMD_REPLACE, CMD_NONE,
1242 invert);
1243 chain = optarg;
1244 if (optind < argc && argv[optind][0] != '-'
1245 && argv[optind][0] != '!')
1246 rulenum = parse_rulenumber(argv[optind++]);
1247 else
1248 exit_error(PARAMETER_PROBLEM,
1249 "-%c requires a rule number",
1250 cmd2char(CMD_REPLACE));
1251 break;
1252
1253 case 'I':
1254 add_command(&command, CMD_INSERT, CMD_NONE,
1255 invert);
1256 chain = optarg;
1257 if (optind < argc && argv[optind][0] != '-'
1258 && argv[optind][0] != '!')
1259 rulenum = parse_rulenumber(argv[optind++]);
1260 else rulenum = 1;
1261 break;
1262
1263 case 'L':
1264 add_command(&command, CMD_LIST, CMD_ZERO,
1265 invert);
1266 if (optarg) chain = optarg;
1267 else if (optind < argc && argv[optind][0] != '-'
1268 && argv[optind][0] != '!')
1269 chain = argv[optind++];
1270 break;
1271
1272 case 'F':
1273 add_command(&command, CMD_FLUSH, CMD_NONE,
1274 invert);
1275 if (optarg) chain = optarg;
1276 else if (optind < argc && argv[optind][0] != '-'
1277 && argv[optind][0] != '!')
1278 chain = argv[optind++];
1279 break;
1280
1281 case 'Z':
1282 add_command(&command, CMD_ZERO, CMD_LIST,
1283 invert);
1284 if (optarg) chain = optarg;
1285 else if (optind < argc && argv[optind][0] != '-'
1286 && argv[optind][0] != '!')
1287 chain = argv[optind++];
1288 break;
1289
1290 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001291 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001292 exit_error(PARAMETER_PROBLEM,
1293 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001294 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001295 if (find_target(optarg, TRY_LOAD))
1296 exit_error(PARAMETER_PROBLEM,
1297 "chain name may not clash "
1298 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001299 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1300 invert);
1301 chain = optarg;
1302 break;
1303
1304 case 'X':
1305 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1306 invert);
1307 if (optarg) chain = optarg;
1308 else if (optind < argc && argv[optind][0] != '-'
1309 && argv[optind][0] != '!')
1310 chain = argv[optind++];
1311 break;
1312
1313 case 'E':
1314 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1315 invert);
1316 chain = optarg;
1317 if (optind < argc && argv[optind][0] != '-'
1318 && argv[optind][0] != '!')
1319 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001320 else
1321 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001322 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001323 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001324 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001325 break;
1326
1327 case 'P':
1328 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1329 invert);
1330 chain = optarg;
1331 if (optind < argc && argv[optind][0] != '-'
1332 && argv[optind][0] != '!')
1333 policy = argv[optind++];
1334 else
1335 exit_error(PARAMETER_PROBLEM,
1336 "-%c requires a chain and a policy",
1337 cmd2char(CMD_SET_POLICY));
1338 break;
1339
1340 case 'h':
1341 if (!optarg)
1342 optarg = argv[optind];
1343
Jonas Berlin1b91e592005-04-01 06:58:38 +00001344 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001345 if (!matches && protocol)
1346 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001347
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001348 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349
1350 /*
1351 * Option selection
1352 */
1353 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001354 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001355 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1356 invert);
1357
1358 /* Canonicalize into lower case */
1359 for (protocol = argv[optind-1]; *protocol; protocol++)
1360 *protocol = tolower(*protocol);
1361
1362 protocol = argv[optind-1];
1363 fw.ipv6.proto = parse_protocol(protocol);
1364 fw.ipv6.flags |= IP6T_F_PROTO;
1365
1366 if (fw.ipv6.proto == 0
1367 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1368 exit_error(PARAMETER_PROBLEM,
1369 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001370
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001371 if (is_exthdr(fw.ipv6.proto)
1372 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001373 fprintf(stderr,
1374 "Warning: never matched protocol: %s. "
1375 "use extension match instead.\n",
1376 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001377 break;
1378
1379 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001380 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001381 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1382 invert);
1383 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 break;
1385
1386 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001387 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001388 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1389 invert);
1390 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 break;
1392
1393 case 'j':
1394 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1395 invert);
1396 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001397 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001398 target = find_target(jumpto, TRY_LOAD);
1399
1400 if (target) {
1401 size_t size;
1402
Harald Welte43ac1912002-03-03 09:43:07 +00001403 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1404 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405
1406 target->t = fw_calloc(1, size);
1407 target->t->u.target_size = size;
1408 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001409 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001410 target->init(target->t);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001411 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001412 }
1413 break;
1414
1415
1416 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001417 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1419 invert);
1420 parse_interface(argv[optind-1],
1421 fw.ipv6.iniface,
1422 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423 break;
1424
1425 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001426 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1428 invert);
1429 parse_interface(argv[optind-1],
1430 fw.ipv6.outiface,
1431 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432 break;
1433
1434 case 'v':
1435 if (!verbose)
1436 set_option(&options, OPT_VERBOSE,
1437 &fw.ipv6.invflags, invert);
1438 verbose++;
1439 break;
1440
1441 case 'm': {
1442 size_t size;
1443
1444 if (invert)
1445 exit_error(PARAMETER_PROBLEM,
1446 "unexpected ! flag before --match");
1447
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001448 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001449 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1450 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001451 m->m = fw_calloc(1, size);
1452 m->m->u.match_size = size;
1453 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001454 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001455 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001456 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001457 if (m != m->next)
1458 /* Merge options for non-cloned matches */
1459 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001460 }
1461 break;
1462
1463 case 'n':
1464 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1465 invert);
1466 break;
1467
1468 case 't':
1469 if (invert)
1470 exit_error(PARAMETER_PROBLEM,
1471 "unexpected ! flag before --table");
1472 *table = argv[optind-1];
1473 break;
1474
1475 case 'x':
1476 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1477 invert);
1478 break;
1479
1480 case 'V':
1481 if (invert)
1482 printf("Not %s ;-)\n", program_version);
1483 else
1484 printf("%s v%s\n",
1485 program_name, program_version);
1486 exit(0);
1487
1488 case '0':
1489 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1490 invert);
1491 break;
1492
Harald Welte43ac1912002-03-03 09:43:07 +00001493 case 'M':
1494 modprobe = optarg;
1495 break;
1496
1497 case 'c':
1498
1499 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1500 invert);
1501 pcnt = optarg;
1502 if (optind < argc && argv[optind][0] != '-'
1503 && argv[optind][0] != '!')
1504 bcnt = argv[optind++];
1505 else
1506 exit_error(PARAMETER_PROBLEM,
1507 "-%c requires packet and byte counter",
1508 opt2char(OPT_COUNTERS));
1509
Patrick McHardy875441e2007-10-17 08:48:58 +00001510 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001511 exit_error(PARAMETER_PROBLEM,
1512 "-%c packet counter not numeric",
1513 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001514 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001515
Patrick McHardy875441e2007-10-17 08:48:58 +00001516 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001517 exit_error(PARAMETER_PROBLEM,
1518 "-%c byte counter not numeric",
1519 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001520 fw.counters.bcnt = cnt;
1521
Harald Welte43ac1912002-03-03 09:43:07 +00001522 break;
1523
1524
Rusty Russell5eed48a2000-06-02 20:12:24 +00001525 case 1: /* non option */
1526 if (optarg[0] == '!' && optarg[1] == '\0') {
1527 if (invert)
1528 exit_error(PARAMETER_PROBLEM,
1529 "multiple consecutive ! not"
1530 " allowed");
1531 invert = TRUE;
1532 optarg[0] = '\0';
1533 continue;
1534 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001535 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001536 exit_tryhelp(2);
1537
1538 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001539 if (!target
1540 || !(target->parse(c - target->option_offset,
1541 argv, invert,
1542 &target->tflags,
1543 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001544 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001545 if (matchp->completed)
1546 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001547 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001548 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001549 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001550 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001551 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001552 break;
1553 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001554 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001555
1556 /* If you listen carefully, you can
1557 actually hear this code suck. */
1558
1559 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001560 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001561 * parameter, that has not been parsed yet,
1562 * it's not an option of an explicitly loaded
1563 * match or a target. However, we support
1564 * implicit loading of the protocol match
1565 * extension. '-p tcp' means 'l4 proto 6' and
1566 * at the same time 'load tcp protocol match on
1567 * demand if we specify --dport'.
1568 *
1569 * To make this work, we need to make sure:
1570 * - the parameter has not been parsed by
1571 * a match (m above)
1572 * - a protocol has been specified
1573 * - the protocol extension has not been
1574 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001575 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001576 * - the protocol extension can be successively
1577 * loaded
1578 */
1579 if (m == NULL
1580 && protocol
1581 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001582 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001583 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001584 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001585 && (proto_used == 0))
1586 )
1587 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001588 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001589 /* Try loading protocol */
1590 size_t size;
1591
1592 proto_used = 1;
1593
1594 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1595 + m->size;
1596
1597 m->m = fw_calloc(1, size);
1598 m->m->u.match_size = size;
1599 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001600 set_revision(m->m->u.user.name,
1601 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001602 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001603 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001604
1605 opts = merge_options(opts,
1606 m->extra_opts, &m->option_offset);
1607
1608 optind--;
1609 continue;
1610 }
1611
Rusty Russell5eed48a2000-06-02 20:12:24 +00001612 if (!m)
1613 exit_error(PARAMETER_PROBLEM,
1614 "Unknown arg `%s'",
1615 argv[optind-1]);
1616 }
1617 }
1618 invert = FALSE;
1619 }
1620
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001621 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001622 if (matchp->match->final_check != NULL)
1623 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001624
Jan Engelhardt830132a2007-10-04 16:24:50 +00001625 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001626 target->final_check(target->tflags);
1627
1628 /* Fix me: must put inverse options checking here --MN */
1629
1630 if (optind < argc)
1631 exit_error(PARAMETER_PROBLEM,
1632 "unknown arguments found on commandline");
1633 if (!command)
1634 exit_error(PARAMETER_PROBLEM, "no command specified");
1635 if (invert)
1636 exit_error(PARAMETER_PROBLEM,
1637 "nothing appropriate following !");
1638
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001639 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001640 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001641 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001642 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001643 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001644 }
1645
1646 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001647 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1648 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649
1650 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001651 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1652 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653
1654 if ((nsaddrs > 1 || ndaddrs > 1) &&
1655 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1656 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1657 " source or destination IP addresses");
1658
Rusty Russell5eed48a2000-06-02 20:12:24 +00001659 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1660 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1661 "specify a unique address");
1662
1663 generic_opt_check(command, options);
1664
1665 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1666 exit_error(PARAMETER_PROBLEM,
1667 "chain name `%s' too long (must be under %i chars)",
1668 chain, IP6T_FUNCTION_MAXNAMELEN);
1669
Harald Welte43ac1912002-03-03 09:43:07 +00001670 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001671 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001672 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001673
Rusty Russell8beb0492004-12-22 00:37:10 +00001674 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001675 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001676 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001677
Harald Welte43ac1912002-03-03 09:43:07 +00001678 if (!*handle)
1679 exit_error(VERSION_PROBLEM,
1680 "can't initialize ip6tables table `%s': %s",
1681 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001682
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001683 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001684 || command == CMD_DELETE
1685 || command == CMD_INSERT
1686 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001687 if (strcmp(chain, "PREROUTING") == 0
1688 || strcmp(chain, "INPUT") == 0) {
1689 /* -o not valid with incoming packets. */
1690 if (options & OPT_VIANAMEOUT)
1691 exit_error(PARAMETER_PROBLEM,
1692 "Can't use -%c with %s\n",
1693 opt2char(OPT_VIANAMEOUT),
1694 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001695 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001696
Harald Welte43ac1912002-03-03 09:43:07 +00001697 if (strcmp(chain, "POSTROUTING") == 0
1698 || strcmp(chain, "OUTPUT") == 0) {
1699 /* -i not valid with outgoing packets */
1700 if (options & OPT_VIANAMEIN)
1701 exit_error(PARAMETER_PROBLEM,
1702 "Can't use -%c with %s\n",
1703 opt2char(OPT_VIANAMEIN),
1704 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001705 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706
1707 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001708 fprintf(stderr,
1709 "Warning: using chain %s, not extension\n",
1710 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001711
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001712 if (target->t)
1713 free(target->t);
1714
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 target = NULL;
1716 }
1717
1718 /* If they didn't specify a target, or it's a chain
1719 name, use standard. */
1720 if (!target
1721 && (strlen(jumpto) == 0
1722 || ip6tc_is_chain(jumpto, *handle))) {
1723 size_t size;
1724
1725 target = find_target(IP6T_STANDARD_TARGET,
1726 LOAD_MUST_SUCCEED);
1727
1728 size = sizeof(struct ip6t_entry_target)
1729 + target->size;
1730 target->t = fw_calloc(1, size);
1731 target->t->u.target_size = size;
1732 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001733 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001734 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001735 }
1736
1737 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001738 /* it is no chain, and we can't load a plugin.
1739 * We cannot know if the plugin is corrupt, non
1740 * existant OR if the user just misspelled a
1741 * chain. */
1742 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001743 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001744 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001745 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001746 }
1747 }
1748
1749 switch (command) {
1750 case CMD_APPEND:
1751 ret = append_entry(chain, e,
1752 nsaddrs, saddrs, ndaddrs, daddrs,
1753 options&OPT_VERBOSE,
1754 handle);
1755 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001756 case CMD_DELETE:
1757 ret = delete_entry(chain, e,
1758 nsaddrs, saddrs, ndaddrs, daddrs,
1759 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001760 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001761 break;
1762 case CMD_DELETE_NUM:
1763 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1764 break;
1765 case CMD_REPLACE:
1766 ret = replace_entry(chain, e, rulenum - 1,
1767 saddrs, daddrs, options&OPT_VERBOSE,
1768 handle);
1769 break;
1770 case CMD_INSERT:
1771 ret = insert_entry(chain, e, rulenum - 1,
1772 nsaddrs, saddrs, ndaddrs, daddrs,
1773 options&OPT_VERBOSE,
1774 handle);
1775 break;
1776 case CMD_LIST:
1777 ret = list_entries(chain,
1778 options&OPT_VERBOSE,
1779 options&OPT_NUMERIC,
1780 options&OPT_EXPANDED,
1781 options&OPT_LINENUMBERS,
1782 handle);
1783 break;
1784 case CMD_FLUSH:
1785 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1786 break;
1787 case CMD_ZERO:
1788 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1789 break;
1790 case CMD_LIST|CMD_ZERO:
1791 ret = list_entries(chain,
1792 options&OPT_VERBOSE,
1793 options&OPT_NUMERIC,
1794 options&OPT_EXPANDED,
1795 options&OPT_LINENUMBERS,
1796 handle);
1797 if (ret)
1798 ret = zero_entries(chain,
1799 options&OPT_VERBOSE, handle);
1800 break;
1801 case CMD_NEW_CHAIN:
1802 ret = ip6tc_create_chain(chain, handle);
1803 break;
1804 case CMD_DELETE_CHAIN:
1805 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1806 break;
1807 case CMD_RENAME_CHAIN:
1808 ret = ip6tc_rename_chain(chain, newname, handle);
1809 break;
1810 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001811 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001812 break;
1813 default:
1814 /* We should never reach this... */
1815 exit_tryhelp(2);
1816 }
1817
1818 if (verbose > 1)
1819 dump_entries6(*handle);
1820
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001821 clear_rule_matches(&matches);
1822
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001823 if (e != NULL) {
1824 free(e);
1825 e = NULL;
1826 }
1827
1828 for (c = 0; c < nsaddrs; c++)
1829 free(&saddrs[c]);
1830
1831 for (c = 0; c < ndaddrs; c++)
1832 free(&daddrs[c]);
1833
Pablo Neiradfdcd642005-05-29 19:05:23 +00001834 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001835
Rusty Russell5eed48a2000-06-02 20:12:24 +00001836 return ret;
1837}