blob: c54237780ab5b5de447c8db49b13e0d48924fdc6 [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
Max Kellermann5b76f682008-01-29 13:42:48 +0000340 /* 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
Max Kellermann5b76f682008-01-29 13:42:48 +0000498 * and/or provoke expensive [not working] ldap/nis/...
Harald Weltecbe1ec72006-02-11 09:50:11 +0000499 * 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);
Max Kellermann5b76f682008-01-29 13:42:48 +0000805 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000806 && !(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
Max Kellermann5b76f682008-01-29 13:42:48 +0000953 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000954 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{
Max Kellermann5b76f682008-01-29 13:42:48 +0000994 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000995 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);
Max Kellermann5b76f682008-01-29 13:42:48 +00001003 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004
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);
Max Kellermann5b76f682008-01-29 13:42:48 +00001012 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001013
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;
Max Kellermann5b76f682008-01-29 13:42:48 +00001019 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001020 }
1021
1022 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001023 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001024}
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)
Max Kellermann5b76f682008-01-29 13:42:48 +00001058 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001059 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;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001180 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001181 const char *chain = NULL;
1182 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1183 const char *policy = NULL, *newname = NULL;
1184 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001185 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001186 int ret = 1;
1187 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001188 struct ip6tables_rule_match *matches = NULL;
1189 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001190 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001191 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001192 const char *jumpto = "";
1193 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001194 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001195 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001196
1197 memset(&fw, 0, sizeof(fw));
1198
Harald Welte43ac1912002-03-03 09:43:07 +00001199 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001200 * a second time */
1201 optind = 0;
1202
Harald Welte43ac1912002-03-03 09:43:07 +00001203 /* clear mflags in case do_command gets called a second time
1204 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001205 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001206 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001207
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001208 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001209 t->tflags = 0;
1210 t->used = 0;
1211 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001212
Rusty Russell5eed48a2000-06-02 20:12:24 +00001213 /* Suppress error messages: we may add new options if we
1214 demand-load a protocol. */
1215 opterr = 0;
1216
1217 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001218 "-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 +00001219 opts, NULL)) != -1) {
1220 switch (c) {
1221 /*
1222 * Command selection
1223 */
1224 case 'A':
1225 add_command(&command, CMD_APPEND, CMD_NONE,
1226 invert);
1227 chain = optarg;
1228 break;
1229
1230 case 'D':
1231 add_command(&command, CMD_DELETE, CMD_NONE,
1232 invert);
1233 chain = optarg;
1234 if (optind < argc && argv[optind][0] != '-'
1235 && argv[optind][0] != '!') {
1236 rulenum = parse_rulenumber(argv[optind++]);
1237 command = CMD_DELETE_NUM;
1238 }
1239 break;
1240
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241 case 'R':
1242 add_command(&command, CMD_REPLACE, CMD_NONE,
1243 invert);
1244 chain = optarg;
1245 if (optind < argc && argv[optind][0] != '-'
1246 && argv[optind][0] != '!')
1247 rulenum = parse_rulenumber(argv[optind++]);
1248 else
1249 exit_error(PARAMETER_PROBLEM,
1250 "-%c requires a rule number",
1251 cmd2char(CMD_REPLACE));
1252 break;
1253
1254 case 'I':
1255 add_command(&command, CMD_INSERT, CMD_NONE,
1256 invert);
1257 chain = optarg;
1258 if (optind < argc && argv[optind][0] != '-'
1259 && argv[optind][0] != '!')
1260 rulenum = parse_rulenumber(argv[optind++]);
1261 else rulenum = 1;
1262 break;
1263
1264 case 'L':
1265 add_command(&command, CMD_LIST, CMD_ZERO,
1266 invert);
1267 if (optarg) chain = optarg;
1268 else if (optind < argc && argv[optind][0] != '-'
1269 && argv[optind][0] != '!')
1270 chain = argv[optind++];
1271 break;
1272
1273 case 'F':
1274 add_command(&command, CMD_FLUSH, CMD_NONE,
1275 invert);
1276 if (optarg) chain = optarg;
1277 else if (optind < argc && argv[optind][0] != '-'
1278 && argv[optind][0] != '!')
1279 chain = argv[optind++];
1280 break;
1281
1282 case 'Z':
1283 add_command(&command, CMD_ZERO, CMD_LIST,
1284 invert);
1285 if (optarg) chain = optarg;
1286 else if (optind < argc && argv[optind][0] != '-'
1287 && argv[optind][0] != '!')
1288 chain = argv[optind++];
1289 break;
1290
1291 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001292 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001293 exit_error(PARAMETER_PROBLEM,
1294 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001295 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001296 if (find_target(optarg, TRY_LOAD))
1297 exit_error(PARAMETER_PROBLEM,
1298 "chain name may not clash "
1299 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001300 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1301 invert);
1302 chain = optarg;
1303 break;
1304
1305 case 'X':
1306 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1307 invert);
1308 if (optarg) chain = optarg;
1309 else if (optind < argc && argv[optind][0] != '-'
1310 && argv[optind][0] != '!')
1311 chain = argv[optind++];
1312 break;
1313
1314 case 'E':
1315 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1316 invert);
1317 chain = optarg;
1318 if (optind < argc && argv[optind][0] != '-'
1319 && argv[optind][0] != '!')
1320 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001321 else
1322 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001323 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001324 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001325 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001326 break;
1327
1328 case 'P':
1329 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1330 invert);
1331 chain = optarg;
1332 if (optind < argc && argv[optind][0] != '-'
1333 && argv[optind][0] != '!')
1334 policy = argv[optind++];
1335 else
1336 exit_error(PARAMETER_PROBLEM,
1337 "-%c requires a chain and a policy",
1338 cmd2char(CMD_SET_POLICY));
1339 break;
1340
1341 case 'h':
1342 if (!optarg)
1343 optarg = argv[optind];
1344
Jonas Berlin1b91e592005-04-01 06:58:38 +00001345 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001346 if (!matches && protocol)
1347 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001348
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001349 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001350
1351 /*
1352 * Option selection
1353 */
1354 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001355 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1357 invert);
1358
1359 /* Canonicalize into lower case */
1360 for (protocol = argv[optind-1]; *protocol; protocol++)
1361 *protocol = tolower(*protocol);
1362
1363 protocol = argv[optind-1];
1364 fw.ipv6.proto = parse_protocol(protocol);
1365 fw.ipv6.flags |= IP6T_F_PROTO;
1366
1367 if (fw.ipv6.proto == 0
1368 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1369 exit_error(PARAMETER_PROBLEM,
1370 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001371
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001372 if (is_exthdr(fw.ipv6.proto)
1373 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001374 fprintf(stderr,
1375 "Warning: never matched protocol: %s. "
1376 "use extension match instead.\n",
1377 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378 break;
1379
1380 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001381 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001382 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1383 invert);
1384 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001385 break;
1386
1387 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001388 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001389 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1390 invert);
1391 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392 break;
1393
1394 case 'j':
1395 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1396 invert);
1397 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001398 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001399 target = find_target(jumpto, TRY_LOAD);
1400
1401 if (target) {
1402 size_t size;
1403
Harald Welte43ac1912002-03-03 09:43:07 +00001404 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1405 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001406
1407 target->t = fw_calloc(1, size);
1408 target->t->u.target_size = size;
1409 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001410 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001411 target->init(target->t);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001412 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 }
1414 break;
1415
1416
1417 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001418 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1420 invert);
1421 parse_interface(argv[optind-1],
1422 fw.ipv6.iniface,
1423 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424 break;
1425
1426 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001427 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001428 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1429 invert);
1430 parse_interface(argv[optind-1],
1431 fw.ipv6.outiface,
1432 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433 break;
1434
1435 case 'v':
1436 if (!verbose)
1437 set_option(&options, OPT_VERBOSE,
1438 &fw.ipv6.invflags, invert);
1439 verbose++;
1440 break;
1441
1442 case 'm': {
1443 size_t size;
1444
1445 if (invert)
1446 exit_error(PARAMETER_PROBLEM,
1447 "unexpected ! flag before --match");
1448
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001449 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001450 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1451 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001452 m->m = fw_calloc(1, size);
1453 m->m->u.match_size = size;
1454 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001455 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001456 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001457 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001458 if (m != m->next)
1459 /* Merge options for non-cloned matches */
1460 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001461 }
1462 break;
1463
1464 case 'n':
1465 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1466 invert);
1467 break;
1468
1469 case 't':
1470 if (invert)
1471 exit_error(PARAMETER_PROBLEM,
1472 "unexpected ! flag before --table");
1473 *table = argv[optind-1];
1474 break;
1475
1476 case 'x':
1477 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1478 invert);
1479 break;
1480
1481 case 'V':
1482 if (invert)
1483 printf("Not %s ;-)\n", program_version);
1484 else
1485 printf("%s v%s\n",
1486 program_name, program_version);
1487 exit(0);
1488
1489 case '0':
1490 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1491 invert);
1492 break;
1493
Harald Welte43ac1912002-03-03 09:43:07 +00001494 case 'M':
1495 modprobe = optarg;
1496 break;
1497
1498 case 'c':
1499
1500 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1501 invert);
1502 pcnt = optarg;
1503 if (optind < argc && argv[optind][0] != '-'
1504 && argv[optind][0] != '!')
1505 bcnt = argv[optind++];
1506 else
1507 exit_error(PARAMETER_PROBLEM,
1508 "-%c requires packet and byte counter",
1509 opt2char(OPT_COUNTERS));
1510
Patrick McHardy875441e2007-10-17 08:48:58 +00001511 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001512 exit_error(PARAMETER_PROBLEM,
1513 "-%c packet counter not numeric",
1514 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001515 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001516
Patrick McHardy875441e2007-10-17 08:48:58 +00001517 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001518 exit_error(PARAMETER_PROBLEM,
1519 "-%c byte counter not numeric",
1520 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001521 fw.counters.bcnt = cnt;
1522
Harald Welte43ac1912002-03-03 09:43:07 +00001523 break;
1524
1525
Rusty Russell5eed48a2000-06-02 20:12:24 +00001526 case 1: /* non option */
1527 if (optarg[0] == '!' && optarg[1] == '\0') {
1528 if (invert)
1529 exit_error(PARAMETER_PROBLEM,
1530 "multiple consecutive ! not"
1531 " allowed");
1532 invert = TRUE;
1533 optarg[0] = '\0';
1534 continue;
1535 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001536 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001537 exit_tryhelp(2);
1538
1539 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001540 if (!target
1541 || !(target->parse(c - target->option_offset,
1542 argv, invert,
1543 &target->tflags,
1544 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001545 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001546 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001547 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001548 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001550 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001551 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001552 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001553 break;
1554 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001555 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001556
1557 /* If you listen carefully, you can
1558 actually hear this code suck. */
1559
1560 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001561 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001562 * parameter, that has not been parsed yet,
1563 * it's not an option of an explicitly loaded
1564 * match or a target. However, we support
1565 * implicit loading of the protocol match
1566 * extension. '-p tcp' means 'l4 proto 6' and
1567 * at the same time 'load tcp protocol match on
1568 * demand if we specify --dport'.
1569 *
1570 * To make this work, we need to make sure:
1571 * - the parameter has not been parsed by
1572 * a match (m above)
1573 * - a protocol has been specified
1574 * - the protocol extension has not been
1575 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001576 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001577 * - the protocol extension can be successively
1578 * loaded
1579 */
1580 if (m == NULL
1581 && protocol
1582 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001583 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001584 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001585 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001586 && (proto_used == 0))
1587 )
1588 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001589 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001590 /* Try loading protocol */
1591 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001592
Harald Welte00f5a9c2002-08-26 14:37:35 +00001593 proto_used = 1;
1594
1595 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1596 + m->size;
1597
1598 m->m = fw_calloc(1, size);
1599 m->m->u.match_size = size;
1600 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001601 set_revision(m->m->u.user.name,
1602 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001603 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001604 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001605
1606 opts = merge_options(opts,
1607 m->extra_opts, &m->option_offset);
1608
1609 optind--;
1610 continue;
1611 }
1612
Rusty Russell5eed48a2000-06-02 20:12:24 +00001613 if (!m)
1614 exit_error(PARAMETER_PROBLEM,
1615 "Unknown arg `%s'",
1616 argv[optind-1]);
1617 }
1618 }
1619 invert = FALSE;
1620 }
1621
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001622 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001623 if (matchp->match->final_check != NULL)
1624 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001625
Jan Engelhardt830132a2007-10-04 16:24:50 +00001626 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001627 target->final_check(target->tflags);
1628
1629 /* Fix me: must put inverse options checking here --MN */
1630
1631 if (optind < argc)
1632 exit_error(PARAMETER_PROBLEM,
1633 "unknown arguments found on commandline");
1634 if (!command)
1635 exit_error(PARAMETER_PROBLEM, "no command specified");
1636 if (invert)
1637 exit_error(PARAMETER_PROBLEM,
1638 "nothing appropriate following !");
1639
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001640 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001641 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001642 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001644 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001645 }
1646
1647 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001648 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1649 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650
1651 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001652 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1653 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001654
1655 if ((nsaddrs > 1 || ndaddrs > 1) &&
1656 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1657 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1658 " source or destination IP addresses");
1659
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1661 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1662 "specify a unique address");
1663
1664 generic_opt_check(command, options);
1665
1666 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1667 exit_error(PARAMETER_PROBLEM,
1668 "chain name `%s' too long (must be under %i chars)",
1669 chain, IP6T_FUNCTION_MAXNAMELEN);
1670
Harald Welte43ac1912002-03-03 09:43:07 +00001671 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001672 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001673 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001674
Rusty Russell8beb0492004-12-22 00:37:10 +00001675 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001676 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001677 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001678
Harald Welte43ac1912002-03-03 09:43:07 +00001679 if (!*handle)
1680 exit_error(VERSION_PROBLEM,
1681 "can't initialize ip6tables table `%s': %s",
1682 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001684 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001685 || command == CMD_DELETE
1686 || command == CMD_INSERT
1687 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001688 if (strcmp(chain, "PREROUTING") == 0
1689 || strcmp(chain, "INPUT") == 0) {
1690 /* -o not valid with incoming packets. */
1691 if (options & OPT_VIANAMEOUT)
1692 exit_error(PARAMETER_PROBLEM,
1693 "Can't use -%c with %s\n",
1694 opt2char(OPT_VIANAMEOUT),
1695 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001696 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001697
Harald Welte43ac1912002-03-03 09:43:07 +00001698 if (strcmp(chain, "POSTROUTING") == 0
1699 || strcmp(chain, "OUTPUT") == 0) {
1700 /* -i not valid with outgoing packets */
1701 if (options & OPT_VIANAMEIN)
1702 exit_error(PARAMETER_PROBLEM,
1703 "Can't use -%c with %s\n",
1704 opt2char(OPT_VIANAMEIN),
1705 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001706 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707
1708 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001709 fprintf(stderr,
1710 "Warning: using chain %s, not extension\n",
1711 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001713 if (target->t)
1714 free(target->t);
1715
Rusty Russell5eed48a2000-06-02 20:12:24 +00001716 target = NULL;
1717 }
1718
1719 /* If they didn't specify a target, or it's a chain
1720 name, use standard. */
1721 if (!target
1722 && (strlen(jumpto) == 0
1723 || ip6tc_is_chain(jumpto, *handle))) {
1724 size_t size;
1725
1726 target = find_target(IP6T_STANDARD_TARGET,
1727 LOAD_MUST_SUCCEED);
1728
1729 size = sizeof(struct ip6t_entry_target)
1730 + target->size;
1731 target->t = fw_calloc(1, size);
1732 target->t->u.target_size = size;
1733 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001734 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001735 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001736 }
1737
1738 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001739 /* it is no chain, and we can't load a plugin.
1740 * We cannot know if the plugin is corrupt, non
1741 * existant OR if the user just misspelled a
1742 * chain. */
1743 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001744 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001745 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001746 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001747 }
1748 }
1749
1750 switch (command) {
1751 case CMD_APPEND:
1752 ret = append_entry(chain, e,
1753 nsaddrs, saddrs, ndaddrs, daddrs,
1754 options&OPT_VERBOSE,
1755 handle);
1756 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001757 case CMD_DELETE:
1758 ret = delete_entry(chain, e,
1759 nsaddrs, saddrs, ndaddrs, daddrs,
1760 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001761 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001762 break;
1763 case CMD_DELETE_NUM:
1764 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1765 break;
1766 case CMD_REPLACE:
1767 ret = replace_entry(chain, e, rulenum - 1,
1768 saddrs, daddrs, options&OPT_VERBOSE,
1769 handle);
1770 break;
1771 case CMD_INSERT:
1772 ret = insert_entry(chain, e, rulenum - 1,
1773 nsaddrs, saddrs, ndaddrs, daddrs,
1774 options&OPT_VERBOSE,
1775 handle);
1776 break;
1777 case CMD_LIST:
1778 ret = list_entries(chain,
1779 options&OPT_VERBOSE,
1780 options&OPT_NUMERIC,
1781 options&OPT_EXPANDED,
1782 options&OPT_LINENUMBERS,
1783 handle);
1784 break;
1785 case CMD_FLUSH:
1786 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1787 break;
1788 case CMD_ZERO:
1789 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1790 break;
1791 case CMD_LIST|CMD_ZERO:
1792 ret = list_entries(chain,
1793 options&OPT_VERBOSE,
1794 options&OPT_NUMERIC,
1795 options&OPT_EXPANDED,
1796 options&OPT_LINENUMBERS,
1797 handle);
1798 if (ret)
1799 ret = zero_entries(chain,
1800 options&OPT_VERBOSE, handle);
1801 break;
1802 case CMD_NEW_CHAIN:
1803 ret = ip6tc_create_chain(chain, handle);
1804 break;
1805 case CMD_DELETE_CHAIN:
1806 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1807 break;
1808 case CMD_RENAME_CHAIN:
1809 ret = ip6tc_rename_chain(chain, newname, handle);
1810 break;
1811 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001812 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001813 break;
1814 default:
1815 /* We should never reach this... */
1816 exit_tryhelp(2);
1817 }
1818
1819 if (verbose > 1)
1820 dump_entries6(*handle);
1821
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001822 clear_rule_matches(&matches);
1823
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001824 if (e != NULL) {
1825 free(e);
1826 e = NULL;
1827 }
1828
Max Kellermann9ee386a2008-01-29 13:48:05 +00001829 for (i = 0; i < nsaddrs; i++)
1830 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001831
Max Kellermann9ee386a2008-01-29 13:48:05 +00001832 for (i = 0; i < ndaddrs; i++)
1833 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001834
Pablo Neiradfdcd642005-05-29 19:05:23 +00001835 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001836
Rusty Russell5eed48a2000-06-02 20:12:24 +00001837 return ret;
1838}