blob: 908700e25052fa237815027338286042ed4e7249 [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>
Jan Engelhardt33690a12008-02-11 00:54:00 +010044#include "ip6tables-multi.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
Rusty Russell5eed48a2000-06-02 20:12:24 +000053#define FMT_NUMERIC 0x0001
54#define FMT_NOCOUNTS 0x0002
55#define FMT_KILOMEGAGIGA 0x0004
56#define FMT_OPTIONS 0x0008
57#define FMT_NOTABLE 0x0010
58#define FMT_NOTARGET 0x0020
59#define FMT_VIA 0x0040
60#define FMT_NONEWLINE 0x0080
61#define FMT_LINENUMBERS 0x0100
62
63#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
64 | FMT_NUMERIC | FMT_NOTABLE)
65#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
66
67
68#define CMD_NONE 0x0000U
69#define CMD_INSERT 0x0001U
70#define CMD_DELETE 0x0002U
71#define CMD_DELETE_NUM 0x0004U
72#define CMD_REPLACE 0x0008U
73#define CMD_APPEND 0x0010U
74#define CMD_LIST 0x0020U
75#define CMD_FLUSH 0x0040U
76#define CMD_ZERO 0x0080U
77#define CMD_NEW_CHAIN 0x0100U
78#define CMD_DELETE_CHAIN 0x0200U
79#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000080#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000081#define NUMBER_OF_CMD 13
82static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000083 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000084
85#define OPTION_OFFSET 256
86
87#define OPT_NONE 0x00000U
88#define OPT_NUMERIC 0x00001U
89#define OPT_SOURCE 0x00002U
90#define OPT_DESTINATION 0x00004U
91#define OPT_PROTOCOL 0x00008U
92#define OPT_JUMP 0x00010U
93#define OPT_VERBOSE 0x00020U
94#define OPT_EXPANDED 0x00040U
95#define OPT_VIANAMEIN 0x00080U
96#define OPT_VIANAMEOUT 0x00100U
97#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000098#define OPT_COUNTERS 0x00400U
99#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000100static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000101= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102
103static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100104 {.name = "append", .has_arg = 1, .val = 'A'},
105 {.name = "delete", .has_arg = 1, .val = 'D'},
106 {.name = "insert", .has_arg = 1, .val = 'I'},
107 {.name = "replace", .has_arg = 1, .val = 'R'},
108 {.name = "list", .has_arg = 2, .val = 'L'},
109 {.name = "flush", .has_arg = 2, .val = 'F'},
110 {.name = "zero", .has_arg = 2, .val = 'Z'},
111 {.name = "new-chain", .has_arg = 1, .val = 'N'},
112 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
113 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
114 {.name = "policy", .has_arg = 1, .val = 'P'},
115 {.name = "source", .has_arg = 1, .val = 's'},
116 {.name = "destination", .has_arg = 1, .val = 'd'},
117 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
118 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
119 {.name = "protocol", .has_arg = 1, .val = 'p'},
120 {.name = "in-interface", .has_arg = 1, .val = 'i'},
121 {.name = "jump", .has_arg = 1, .val = 'j'},
122 {.name = "table", .has_arg = 1, .val = 't'},
123 {.name = "match", .has_arg = 1, .val = 'm'},
124 {.name = "numeric", .has_arg = 0, .val = 'n'},
125 {.name = "out-interface", .has_arg = 1, .val = 'o'},
126 {.name = "verbose", .has_arg = 0, .val = 'v'},
127 {.name = "exact", .has_arg = 0, .val = 'x'},
128 {.name = "version", .has_arg = 0, .val = 'V'},
129 {.name = "help", .has_arg = 2, .val = 'h'},
130 {.name = "line-numbers", .has_arg = 0, .val = '0'},
131 {.name = "modprobe", .has_arg = 1, .val = 'M'},
132 {.name = "set-counters", .has_arg = 1, .val = 'c'},
133 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000134};
135
Harald Weltea8658ca2003-03-05 07:46:15 +0000136/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
137 * current line of the input file, in order to give a more precise error
138 * message. ip6tables itself doesn't need this, so it is initialized to the
139 * magic number of -1 */
140int line = -1;
141
Rusty Russell5eed48a2000-06-02 20:12:24 +0000142static struct option *opts = original_opts;
143static unsigned int global_option_offset = 0;
144
145/* Table of legal combinations of commands and options. If any of the
146 * given commands make an option legal, that option is legal (applies to
147 * CMD_LIST and CMD_ZERO only).
148 * Key:
149 * + compulsory
150 * x illegal
151 * optional
152 */
153
154static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
155/* Well, it's better than "Re: Linux vs FreeBSD" */
156{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000157 /* -n -s -d -p -j -v -x -i -o --line -c */
158/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
159/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
160/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
161/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
164/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
166/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
168/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000169/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000170};
171
172static int inverse_for_options[NUMBER_OF_OPT] =
173{
174/* -n */ 0,
175/* -s */ IP6T_INV_SRCIP,
176/* -d */ IP6T_INV_DSTIP,
177/* -p */ IP6T_INV_PROTO,
178/* -j */ 0,
179/* -v */ 0,
180/* -x */ 0,
181/* -i */ IP6T_INV_VIA_IN,
182/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000183/*--line*/ 0,
184/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000185};
186
187const char *program_version;
188const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000189
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190/* A few hardcoded protocols for 'all' and in case the user has no
191 /etc/protocols */
192struct pprot {
193 char *name;
194 u_int8_t num;
195};
196
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000197struct afinfo afinfo = {
198 .family = AF_INET6,
199 .libprefix = "libip6t_",
200 .ipproto = IPPROTO_IPV6,
201 .kmod = "ip6_tables",
202 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
203 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
204};
205
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000206/* Primitive headers... */
207/* defined in netinet/in.h */
208#if 0
209#ifndef IPPROTO_ESP
210#define IPPROTO_ESP 50
211#endif
212#ifndef IPPROTO_AH
213#define IPPROTO_AH 51
214#endif
215#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000216#ifndef IPPROTO_MH
217#define IPPROTO_MH 135
218#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000219
Rusty Russell5eed48a2000-06-02 20:12:24 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000223 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000224 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000225 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000226 { "esp", IPPROTO_ESP },
227 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000228 { "ipv6-mh", IPPROTO_MH },
229 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000230 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000231};
232
233static char *
234proto_to_name(u_int8_t proto, int nolookup)
235{
236 unsigned int i;
237
238 if (proto && !nolookup) {
239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249}
250
Pablo Neiradfdcd642005-05-29 19:05:23 +0000251static void free_opts(int reset_offset)
252{
253 if (opts != original_opts) {
254 free(opts);
255 opts = original_opts;
256 if (reset_offset)
257 global_option_offset = 0;
258 }
259}
260
Patrick McHardy0b639362007-09-08 16:00:01 +0000261static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000262exit_tryhelp(int status)
263{
Harald Weltea8658ca2003-03-05 07:46:15 +0000264 if (line != -1)
265 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000266 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
267 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000268 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000269 exit(status);
270}
271
Patrick McHardy0b639362007-09-08 16:00:01 +0000272static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000273exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000275 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000276 struct ip6tables_target *t = NULL;
277
278 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000279"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000280" %s -[RI] chain rulenum rule-specification [options]\n"
281" %s -D chain rulenum [options]\n"
282" %s -[LFZ] [chain] [options]\n"
283" %s -[NX] chain\n"
284" %s -E old-chain-name new-chain-name\n"
285" %s -P chain target [options]\n"
286" %s -h (print this help information)\n\n",
287 program_name, program_version, program_name, program_name,
288 program_name, program_name, program_name, program_name,
289 program_name, program_name);
290
291 printf(
292"Commands:\n"
293"Either long or short options are allowed.\n"
294" --append -A chain Append to chain\n"
295" --delete -D chain Delete matching rule from chain\n"
296" --delete -D chain rulenum\n"
297" Delete rule rulenum (1 = first) from chain\n"
298" --insert -I chain [rulenum]\n"
299" Insert in chain as rulenum (default 1=first)\n"
300" --replace -R chain rulenum\n"
301" Replace rule rulenum (1 = first) in chain\n"
302" --list -L [chain] List the rules in a chain or all chains\n"
303" --flush -F [chain] Delete all rules in chain or all chains\n"
304" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000305" --new -N chain Create a new user-defined chain\n"
306" --delete-chain\n"
307" -X [chain] Delete a user-defined chain\n"
308" --policy -P chain target\n"
309" Change policy on chain to target\n"
310" --rename-chain\n"
311" -E old-chain new-chain\n"
312" Change chain name, (moving any references)\n"
313
314"Options:\n"
315" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
316" --source -s [!] address[/mask]\n"
317" source specification\n"
318" --destination -d [!] address[/mask]\n"
319" destination specification\n"
320" --in-interface -i [!] input name[+]\n"
321" network interface name ([+] for wildcard)\n"
322" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000323" target for rule (may load target extension)\n"
324" --match -m match\n"
325" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000326" --numeric -n numeric output of addresses and ports\n"
327" --out-interface -o [!] output name[+]\n"
328" network interface name ([+] for wildcard)\n"
329" --table -t table table to manipulate (default: `filter')\n"
330" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000331" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000332" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000333/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000334" --modprobe=<command> try to insert modules using this command\n"
335" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000336"[!] --version -V print package version.\n");
337
Max Kellermann5b76f682008-01-29 13:42:48 +0000338 /* Print out any special helps. A user might like to be able to add a --help
339 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000340 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000341 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000342 if (t->used) {
343 printf("\n");
344 t->help();
345 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000346 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000347 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000349 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000350 }
351 exit(0);
352}
353
Patrick McHardy0b639362007-09-08 16:00:01 +0000354void
355exit_error(enum exittype status, const char *msg, ...)
356{
357 va_list args;
358
359 va_start(args, msg);
360 fprintf(stderr, "%s v%s: ", program_name, program_version);
361 vfprintf(stderr, msg, args);
362 va_end(args);
363 fprintf(stderr, "\n");
364 if (status == PARAMETER_PROBLEM)
365 exit_tryhelp(status);
366 if (status == VERSION_PROBLEM)
367 fprintf(stderr,
368 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
369 /* On error paths, make sure that we don't leak memory */
370 free_opts(1);
371 exit(status);
372}
373
Rusty Russell5eed48a2000-06-02 20:12:24 +0000374static void
375generic_opt_check(int command, int options)
376{
377 int i, j, legal = 0;
378
379 /* Check that commands are valid with options. Complicated by the
380 * fact that if an option is legal with *any* command given, it is
381 * legal overall (ie. -z and -l).
382 */
383 for (i = 0; i < NUMBER_OF_OPT; i++) {
384 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
385
386 for (j = 0; j < NUMBER_OF_CMD; j++) {
387 if (!(command & (1<<j)))
388 continue;
389
390 if (!(options & (1<<i))) {
391 if (commands_v_options[j][i] == '+')
392 exit_error(PARAMETER_PROBLEM,
393 "You need to supply the `-%c' "
394 "option for this command\n",
395 optflags[i]);
396 } else {
397 if (commands_v_options[j][i] != 'x')
398 legal = 1;
399 else if (legal == 0)
400 legal = -1;
401 }
402 }
403 if (legal == -1)
404 exit_error(PARAMETER_PROBLEM,
405 "Illegal option `-%c' with this command\n",
406 optflags[i]);
407 }
408}
409
410static char
411opt2char(int option)
412{
413 const char *ptr;
414 for (ptr = optflags; option > 1; option >>= 1, ptr++);
415
416 return *ptr;
417}
418
419static char
420cmd2char(int option)
421{
422 const char *ptr;
423 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
424
425 return *ptr;
426}
427
428static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000429add_command(unsigned int *cmd, const int newcmd, const int othercmds,
430 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000431{
432 if (invert)
433 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
434 if (*cmd & (~othercmds))
435 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
436 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
437 *cmd |= newcmd;
438}
439
440int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100441check_inverse(const char option[], int *invert, int *my_optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000442{
443 if (option && strcmp(option, "!") == 0) {
444 if (*invert)
445 exit_error(PARAMETER_PROBLEM,
446 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000447 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100448 if (my_optind != NULL) {
449 ++*my_optind;
450 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000451 exit_error(PARAMETER_PROBLEM,
452 "no argument following `!'");
453 }
454
Rusty Russell5eed48a2000-06-02 20:12:24 +0000455 return TRUE;
456 }
457 return FALSE;
458}
459
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460/*
461 * All functions starting with "parse" should succeed, otherwise
462 * the program fails.
463 * Most routines return pointers to static data that may change
464 * between calls to the same or other routines with a few exceptions:
465 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
466 * return global static data.
467*/
468
Rusty Russell5eed48a2000-06-02 20:12:24 +0000469/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
470static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000471find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472{
Harald Welteed498492001-07-23 01:24:22 +0000473 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474
Harald Welte43ac1912002-03-03 09:43:07 +0000475 if (string_to_number(pname, 0, 255, &proto) != -1) {
476 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477
Harald Welte43ac1912002-03-03 09:43:07 +0000478 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000479 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000480 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000481 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000482
483 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484}
485
Harald Welte43ac1912002-03-03 09:43:07 +0000486u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000487parse_protocol(const char *s)
488{
Harald Welteed498492001-07-23 01:24:22 +0000489 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000490
Harald Welteed498492001-07-23 01:24:22 +0000491 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000492 struct protoent *pent;
493
Harald Weltecbe1ec72006-02-11 09:50:11 +0000494 /* first deal with the special case of 'all' to prevent
495 * people from being able to redefine 'all' in nsswitch
Max Kellermann5b76f682008-01-29 13:42:48 +0000496 * and/or provoke expensive [not working] ldap/nis/...
Harald Weltecbe1ec72006-02-11 09:50:11 +0000497 * lookups */
498 if (!strcmp(s, "all"))
499 return 0;
500
Rusty Russell5eed48a2000-06-02 20:12:24 +0000501 if ((pent = getprotobyname(s)))
502 proto = pent->p_proto;
503 else {
504 unsigned int i;
505 for (i = 0;
506 i < sizeof(chain_protos)/sizeof(struct pprot);
507 i++) {
508 if (strcmp(s, chain_protos[i].name) == 0) {
509 proto = chain_protos[i].num;
510 break;
511 }
512 }
513 if (i == sizeof(chain_protos)/sizeof(struct pprot))
514 exit_error(PARAMETER_PROBLEM,
515 "unknown protocol `%s' specified",
516 s);
517 }
518 }
519
520 return (u_int16_t)proto;
521}
522
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000523/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000524static int is_exthdr(u_int16_t proto)
525{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000526 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000527 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000528 proto == IPPROTO_AH ||
529 proto == IPPROTO_DSTOPTS);
530}
531
Rusty Russell5eed48a2000-06-02 20:12:24 +0000532/* Can't be zero. */
533static int
534parse_rulenumber(const char *rule)
535{
Harald Welte43ac1912002-03-03 09:43:07 +0000536 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000537
Harald Welte43ac1912002-03-03 09:43:07 +0000538 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000539 exit_error(PARAMETER_PROBLEM,
540 "Invalid rule number `%s'", rule);
541
542 return rulenum;
543}
544
545static const char *
546parse_target(const char *targetname)
547{
548 const char *ptr;
549
550 if (strlen(targetname) < 1)
551 exit_error(PARAMETER_PROBLEM,
552 "Invalid target name (too short)");
553
554 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
555 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000556 "Invalid target name `%s' (%u chars max)",
557 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000558
559 for (ptr = targetname; *ptr; ptr++)
560 if (isspace(*ptr))
561 exit_error(PARAMETER_PROBLEM,
562 "Invalid target name `%s'", targetname);
563 return targetname;
564}
565
Rusty Russell5eed48a2000-06-02 20:12:24 +0000566static void
567set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
568 int invert)
569{
570 if (*options & option)
571 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
572 opt2char(option));
573 *options |= option;
574
575 if (invert) {
576 unsigned int i;
577 for (i = 0; 1 << i != option; i++);
578
579 if (!inverse_for_options[i])
580 exit_error(PARAMETER_PROBLEM,
581 "cannot have ! before -%c",
582 opt2char(option));
583 *invflg |= inverse_for_options[i];
584 }
585}
586
Rusty Russell5eed48a2000-06-02 20:12:24 +0000587static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000588merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000589 unsigned int *option_offset)
590{
591 unsigned int num_old, num_new, i;
592 struct option *merge;
593
Jan Engelhardtd0145402007-07-30 14:32:26 +0000594 if (newopts == NULL)
595 return oldopts;
596
Rusty Russell5eed48a2000-06-02 20:12:24 +0000597 for (num_old = 0; oldopts[num_old].name; num_old++);
598 for (num_new = 0; newopts[num_new].name; num_new++);
599
600 global_option_offset += OPTION_OFFSET;
601 *option_offset = global_option_offset;
602
603 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
604 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000605 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000606 for (i = 0; i < num_new; i++) {
607 merge[num_old + i] = newopts[i];
608 merge[num_old + i].val += *option_offset;
609 }
610 memset(merge + num_old + num_new, 0, sizeof(struct option));
611
612 return merge;
613}
614
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000615void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000616{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000617 me->family = AF_INET6;
618 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000619}
620
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000621void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000622{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000623 me->family = AF_INET6;
624 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000625}
626
627static void
628print_num(u_int64_t number, unsigned int format)
629{
Harald Welte43ac1912002-03-03 09:43:07 +0000630 if (format & FMT_KILOMEGAGIGA) {
631 if (number > 99999) {
632 number = (number + 500) / 1000;
633 if (number > 9999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
637 if (number > 9999) {
638 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000639 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000640 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000644 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000650}
651
Harald Welte43ac1912002-03-03 09:43:07 +0000652
Rusty Russell5eed48a2000-06-02 20:12:24 +0000653static void
654print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
655{
656 struct ip6t_counters counters;
657 const char *pol = ip6tc_get_policy(chain, &counters, handle);
658 printf("Chain %s", chain);
659 if (pol) {
660 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000661 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000662 fputc(' ', stdout);
663 print_num(counters.pcnt, (format|FMT_NOTABLE));
664 fputs("packets, ", stdout);
665 print_num(counters.bcnt, (format|FMT_NOTABLE));
666 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000667 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000668 printf(")\n");
669 } else {
670 unsigned int refs;
671 if (!ip6tc_get_references(&refs, chain, handle))
672 printf(" (ERROR obtaining refs)\n");
673 else
674 printf(" (%u references)\n", refs);
675 }
676
677 if (format & FMT_LINENUMBERS)
678 printf(FMT("%-4s ", "%s "), "num");
679 if (!(format & FMT_NOCOUNTS)) {
680 if (format & FMT_KILOMEGAGIGA) {
681 printf(FMT("%5s ","%s "), "pkts");
682 printf(FMT("%5s ","%s "), "bytes");
683 } else {
684 printf(FMT("%8s ","%s "), "pkts");
685 printf(FMT("%10s ","%s "), "bytes");
686 }
687 }
688 if (!(format & FMT_NOTARGET))
689 printf(FMT("%-9s ","%s "), "target");
690 fputs(" prot ", stdout);
691 if (format & FMT_OPTIONS)
692 fputs("opt", stdout);
693 if (format & FMT_VIA) {
694 printf(FMT(" %-6s ","%s "), "in");
695 printf(FMT("%-6s ","%s "), "out");
696 }
697 printf(FMT(" %-19s ","%s "), "source");
698 printf(FMT(" %-19s "," %s "), "destination");
699 printf("\n");
700}
701
Harald Welte43ac1912002-03-03 09:43:07 +0000702
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703static int
704print_match(const struct ip6t_entry_match *m,
705 const struct ip6t_ip6 *ip,
706 int numeric)
707{
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000708 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709
710 if (match) {
711 if (match->print)
712 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000713 else
714 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000715 } else {
716 if (m->u.user.name[0])
717 printf("UNKNOWN match `%s' ", m->u.user.name);
718 }
719 /* Don't stop iterating. */
720 return 0;
721}
722
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100723/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724static void
725print_firewall(const struct ip6t_entry *fw,
726 const char *targname,
727 unsigned int num,
728 unsigned int format,
729 const ip6tc_handle_t handle)
730{
731 struct ip6tables_target *target = NULL;
732 const struct ip6t_entry_target *t;
733 u_int8_t flags;
734 char buf[BUFSIZ];
735
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 if (!ip6tc_is_chain(targname, handle))
737 target = find_target(targname, TRY_LOAD);
738 else
739 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
740
741 t = ip6t_get_target((struct ip6t_entry *)fw);
742 flags = fw->ipv6.flags;
743
744 if (format & FMT_LINENUMBERS)
745 printf(FMT("%-4u ", "%u "), num+1);
746
747 if (!(format & FMT_NOCOUNTS)) {
748 print_num(fw->counters.pcnt, format);
749 print_num(fw->counters.bcnt, format);
750 }
751
752 if (!(format & FMT_NOTARGET))
753 printf(FMT("%-9s ", "%s "), targname);
754
755 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
756 {
Harald Welte43ac1912002-03-03 09:43:07 +0000757 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000758 if (pname)
759 printf(FMT("%-5s", "%s "), pname);
760 else
761 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
762 }
763
764 if (format & FMT_OPTIONS) {
765 if (format & FMT_NOTABLE)
766 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000767 fputc(' ', stdout); /* Invert flag of FRAG */
768 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769 fputc(' ', stdout);
770 }
771
772 if (format & FMT_VIA) {
773 char iface[IFNAMSIZ+2];
774
775 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
776 iface[0] = '!';
777 iface[1] = '\0';
778 }
779 else iface[0] = '\0';
780
781 if (fw->ipv6.iniface[0] != '\0') {
782 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 }
784 else if (format & FMT_NUMERIC) strcat(iface, "*");
785 else strcat(iface, "any");
786 printf(FMT(" %-6s ","in %s "), iface);
787
788 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
789 iface[0] = '!';
790 iface[1] = '\0';
791 }
792 else iface[0] = '\0';
793
794 if (fw->ipv6.outiface[0] != '\0') {
795 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000796 }
797 else if (format & FMT_NUMERIC) strcat(iface, "*");
798 else strcat(iface, "any");
799 printf(FMT("%-6s ","out %s "), iface);
800 }
801
802 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000803 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000804 && !(format & FMT_NUMERIC))
805 printf(FMT("%-19s ","%s "), "anywhere");
806 else {
807 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000808 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000809 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000810 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
811 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000812 printf(FMT("%-19s ","%s "), buf);
813 }
814
815 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
816 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
817 && !(format & FMT_NUMERIC))
818 printf(FMT("%-19s","-> %s"), "anywhere");
819 else {
820 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000821 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000823 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
824 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000825 printf(FMT("%-19s","-> %s"), buf);
826 }
827
828 if (format & FMT_NOTABLE)
829 fputs(" ", stdout);
830
831 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
832
833 if (target) {
834 if (target->print)
835 /* Print the target information. */
836 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
837 } else if (t->u.target_size != sizeof(*t))
838 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000839 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840
841 if (!(format & FMT_NONEWLINE))
842 fputc('\n', stdout);
843}
844
845static void
846print_firewall_line(const struct ip6t_entry *fw,
847 const ip6tc_handle_t h)
848{
849 struct ip6t_entry_target *t;
850
851 t = ip6t_get_target((struct ip6t_entry *)fw);
852 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
853}
854
855static int
856append_entry(const ip6t_chainlabel chain,
857 struct ip6t_entry *fw,
858 unsigned int nsaddrs,
859 const struct in6_addr saddrs[],
860 unsigned int ndaddrs,
861 const struct in6_addr daddrs[],
862 int verbose,
863 ip6tc_handle_t *handle)
864{
865 unsigned int i, j;
866 int ret = 1;
867
868 for (i = 0; i < nsaddrs; i++) {
869 fw->ipv6.src = saddrs[i];
870 for (j = 0; j < ndaddrs; j++) {
871 fw->ipv6.dst = daddrs[j];
872 if (verbose)
873 print_firewall_line(fw, *handle);
874 ret &= ip6tc_append_entry(chain, fw, handle);
875 }
876 }
877
878 return ret;
879}
880
881static int
882replace_entry(const ip6t_chainlabel chain,
883 struct ip6t_entry *fw,
884 unsigned int rulenum,
885 const struct in6_addr *saddr,
886 const struct in6_addr *daddr,
887 int verbose,
888 ip6tc_handle_t *handle)
889{
890 fw->ipv6.src = *saddr;
891 fw->ipv6.dst = *daddr;
892
893 if (verbose)
894 print_firewall_line(fw, *handle);
895 return ip6tc_replace_entry(chain, fw, rulenum, handle);
896}
897
898static int
899insert_entry(const ip6t_chainlabel chain,
900 struct ip6t_entry *fw,
901 unsigned int rulenum,
902 unsigned int nsaddrs,
903 const struct in6_addr saddrs[],
904 unsigned int ndaddrs,
905 const struct in6_addr daddrs[],
906 int verbose,
907 ip6tc_handle_t *handle)
908{
909 unsigned int i, j;
910 int ret = 1;
911
912 for (i = 0; i < nsaddrs; i++) {
913 fw->ipv6.src = saddrs[i];
914 for (j = 0; j < ndaddrs; j++) {
915 fw->ipv6.dst = daddrs[j];
916 if (verbose)
917 print_firewall_line(fw, *handle);
918 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
919 }
920 }
921
922 return ret;
923}
924
925static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000926make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000927{
928 /* Establish mask for comparison */
929 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000930 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000931 unsigned char *mask, *mptr;
932
933 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000934 for (matchp = matches; matchp; matchp = matchp->next)
935 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936
937 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000938 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000939 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000940
941 memset(mask, 0xFF, sizeof(struct ip6t_entry));
942 mptr = mask + sizeof(struct ip6t_entry);
943
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000944 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000945 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000946 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000947 + matchp->match->userspacesize);
948 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000949 }
950
Max Kellermann5b76f682008-01-29 13:42:48 +0000951 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000952 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000953 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000954
955 return mask;
956}
957
958static int
959delete_entry(const ip6t_chainlabel chain,
960 struct ip6t_entry *fw,
961 unsigned int nsaddrs,
962 const struct in6_addr saddrs[],
963 unsigned int ndaddrs,
964 const struct in6_addr daddrs[],
965 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000966 ip6tc_handle_t *handle,
967 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000968{
969 unsigned int i, j;
970 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000971 unsigned char *mask;
972
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000973 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000974 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000975 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000977 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978 if (verbose)
979 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000980 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000981 }
982 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000983 free(mask);
984
Rusty Russell5eed48a2000-06-02 20:12:24 +0000985 return ret;
986}
987
András Kis-Szabó764316a2001-02-26 17:31:20 +0000988int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000989for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
990 int verbose, int builtinstoo, ip6tc_handle_t *handle)
991{
Max Kellermann5b76f682008-01-29 13:42:48 +0000992 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993 const char *chain;
994 char *chains;
995 unsigned int i, chaincount = 0;
996
997 chain = ip6tc_first_chain(handle);
998 while (chain) {
999 chaincount++;
1000 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001001 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001002
1003 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1004 i = 0;
1005 chain = ip6tc_first_chain(handle);
1006 while (chain) {
1007 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1008 i++;
1009 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001010 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001011
1012 for (i = 0; i < chaincount; i++) {
1013 if (!builtinstoo
1014 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001015 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001016 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +00001017 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001018 }
1019
1020 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001021 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001022}
1023
András Kis-Szabó764316a2001-02-26 17:31:20 +00001024int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001025flush_entries(const ip6t_chainlabel chain, int verbose,
1026 ip6tc_handle_t *handle)
1027{
1028 if (!chain)
1029 return for_each_chain(flush_entries, verbose, 1, handle);
1030
1031 if (verbose)
1032 fprintf(stdout, "Flushing chain `%s'\n", chain);
1033 return ip6tc_flush_entries(chain, handle);
1034}
1035
1036static int
1037zero_entries(const ip6t_chainlabel chain, int verbose,
1038 ip6tc_handle_t *handle)
1039{
1040 if (!chain)
1041 return for_each_chain(zero_entries, verbose, 1, handle);
1042
1043 if (verbose)
1044 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1045 return ip6tc_zero_entries(chain, handle);
1046}
1047
András Kis-Szabó764316a2001-02-26 17:31:20 +00001048int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001049delete_chain(const ip6t_chainlabel chain, int verbose,
1050 ip6tc_handle_t *handle)
1051{
1052 if (!chain)
1053 return for_each_chain(delete_chain, verbose, 0, handle);
1054
1055 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001056 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001057 return ip6tc_delete_chain(chain, handle);
1058}
1059
1060static int
1061list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1062 int expanded, int linenumbers, ip6tc_handle_t *handle)
1063{
1064 int found = 0;
1065 unsigned int format;
1066 const char *this;
1067
1068 format = FMT_OPTIONS;
1069 if (!verbose)
1070 format |= FMT_NOCOUNTS;
1071 else
1072 format |= FMT_VIA;
1073
1074 if (numeric)
1075 format |= FMT_NUMERIC;
1076
1077 if (!expanded)
1078 format |= FMT_KILOMEGAGIGA;
1079
1080 if (linenumbers)
1081 format |= FMT_LINENUMBERS;
1082
1083 for (this = ip6tc_first_chain(handle);
1084 this;
1085 this = ip6tc_next_chain(handle)) {
1086 const struct ip6t_entry *i;
1087 unsigned int num;
1088
1089 if (chain && strcmp(chain, this) != 0)
1090 continue;
1091
1092 if (found) printf("\n");
1093
1094 print_header(format, this, handle);
1095 i = ip6tc_first_rule(this, handle);
1096
1097 num = 0;
1098 while (i) {
1099 print_firewall(i,
1100 ip6tc_get_target(i, handle),
1101 num++,
1102 format,
1103 *handle);
1104 i = ip6tc_next_rule(i, handle);
1105 }
1106 found = 1;
1107 }
1108
1109 errno = ENOENT;
1110 return found;
1111}
1112
1113static struct ip6t_entry *
1114generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001115 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001116 struct ip6t_entry_target *target)
1117{
1118 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001119 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001120 struct ip6t_entry *e;
1121
1122 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001123 for (matchp = matches; matchp; matchp = matchp->next)
1124 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001125
1126 e = fw_malloc(size + target->u.target_size);
1127 *e = *fw;
1128 e->target_offset = size;
1129 e->next_offset = size + target->u.target_size;
1130
1131 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001132 for (matchp = matches; matchp; matchp = matchp->next) {
1133 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1134 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001135 }
1136 memcpy(e->elems + size, target, target->u.target_size);
1137
1138 return e;
1139}
1140
Jan Engelhardt33690a12008-02-11 00:54:00 +01001141static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001142{
1143 struct ip6tables_rule_match *matchp, *tmp;
1144
1145 for (matchp = *matches; matchp;) {
1146 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001147 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001148 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001149 matchp->match->m = NULL;
1150 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001151 if (matchp->match == matchp->match->next) {
1152 free(matchp->match);
1153 matchp->match = NULL;
1154 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001155 free(matchp);
1156 matchp = tmp;
1157 }
1158
1159 *matches = NULL;
1160}
1161
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001162static void set_revision(char *name, u_int8_t revision)
1163{
1164 /* Old kernel sources don't have ".revision" field,
1165 but we stole a byte from name. */
1166 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1167 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1168}
1169
Rusty Russell5eed48a2000-06-02 20:12:24 +00001170int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1171{
1172 struct ip6t_entry fw, *e = NULL;
1173 int invert = 0;
1174 unsigned int nsaddrs = 0, ndaddrs = 0;
1175 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1176
1177 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001178 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001179 const char *chain = NULL;
1180 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1181 const char *policy = NULL, *newname = NULL;
1182 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001183 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001184 int ret = 1;
1185 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001186 struct ip6tables_rule_match *matches = NULL;
1187 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001188 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001189 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001190 const char *jumpto = "";
1191 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001192 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001193 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001194
1195 memset(&fw, 0, sizeof(fw));
1196
Harald Welte43ac1912002-03-03 09:43:07 +00001197 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001198 * a second time */
1199 optind = 0;
1200
Harald Welte43ac1912002-03-03 09:43:07 +00001201 /* clear mflags in case do_command gets called a second time
1202 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001203 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001204 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001205
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001206 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001207 t->tflags = 0;
1208 t->used = 0;
1209 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001210
Rusty Russell5eed48a2000-06-02 20:12:24 +00001211 /* Suppress error messages: we may add new options if we
1212 demand-load a protocol. */
1213 opterr = 0;
1214
1215 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001216 "-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 +00001217 opts, NULL)) != -1) {
1218 switch (c) {
1219 /*
1220 * Command selection
1221 */
1222 case 'A':
1223 add_command(&command, CMD_APPEND, CMD_NONE,
1224 invert);
1225 chain = optarg;
1226 break;
1227
1228 case 'D':
1229 add_command(&command, CMD_DELETE, CMD_NONE,
1230 invert);
1231 chain = optarg;
1232 if (optind < argc && argv[optind][0] != '-'
1233 && argv[optind][0] != '!') {
1234 rulenum = parse_rulenumber(argv[optind++]);
1235 command = CMD_DELETE_NUM;
1236 }
1237 break;
1238
Rusty Russell5eed48a2000-06-02 20:12:24 +00001239 case 'R':
1240 add_command(&command, CMD_REPLACE, CMD_NONE,
1241 invert);
1242 chain = optarg;
1243 if (optind < argc && argv[optind][0] != '-'
1244 && argv[optind][0] != '!')
1245 rulenum = parse_rulenumber(argv[optind++]);
1246 else
1247 exit_error(PARAMETER_PROBLEM,
1248 "-%c requires a rule number",
1249 cmd2char(CMD_REPLACE));
1250 break;
1251
1252 case 'I':
1253 add_command(&command, CMD_INSERT, CMD_NONE,
1254 invert);
1255 chain = optarg;
1256 if (optind < argc && argv[optind][0] != '-'
1257 && argv[optind][0] != '!')
1258 rulenum = parse_rulenumber(argv[optind++]);
1259 else rulenum = 1;
1260 break;
1261
1262 case 'L':
1263 add_command(&command, CMD_LIST, CMD_ZERO,
1264 invert);
1265 if (optarg) chain = optarg;
1266 else if (optind < argc && argv[optind][0] != '-'
1267 && argv[optind][0] != '!')
1268 chain = argv[optind++];
1269 break;
1270
1271 case 'F':
1272 add_command(&command, CMD_FLUSH, CMD_NONE,
1273 invert);
1274 if (optarg) chain = optarg;
1275 else if (optind < argc && argv[optind][0] != '-'
1276 && argv[optind][0] != '!')
1277 chain = argv[optind++];
1278 break;
1279
1280 case 'Z':
1281 add_command(&command, CMD_ZERO, CMD_LIST,
1282 invert);
1283 if (optarg) chain = optarg;
1284 else if (optind < argc && argv[optind][0] != '-'
1285 && argv[optind][0] != '!')
1286 chain = argv[optind++];
1287 break;
1288
1289 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001290 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001291 exit_error(PARAMETER_PROBLEM,
1292 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001293 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001294 if (find_target(optarg, TRY_LOAD))
1295 exit_error(PARAMETER_PROBLEM,
1296 "chain name may not clash "
1297 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001298 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1299 invert);
1300 chain = optarg;
1301 break;
1302
1303 case 'X':
1304 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1305 invert);
1306 if (optarg) chain = optarg;
1307 else if (optind < argc && argv[optind][0] != '-'
1308 && argv[optind][0] != '!')
1309 chain = argv[optind++];
1310 break;
1311
1312 case 'E':
1313 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1314 invert);
1315 chain = optarg;
1316 if (optind < argc && argv[optind][0] != '-'
1317 && argv[optind][0] != '!')
1318 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001319 else
1320 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001321 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001322 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001323 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001324 break;
1325
1326 case 'P':
1327 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1328 invert);
1329 chain = optarg;
1330 if (optind < argc && argv[optind][0] != '-'
1331 && argv[optind][0] != '!')
1332 policy = argv[optind++];
1333 else
1334 exit_error(PARAMETER_PROBLEM,
1335 "-%c requires a chain and a policy",
1336 cmd2char(CMD_SET_POLICY));
1337 break;
1338
1339 case 'h':
1340 if (!optarg)
1341 optarg = argv[optind];
1342
Jonas Berlin1b91e592005-04-01 06:58:38 +00001343 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001344 if (!matches && protocol)
1345 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001346
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001347 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001348
1349 /*
1350 * Option selection
1351 */
1352 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001353 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001354 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1355 invert);
1356
1357 /* Canonicalize into lower case */
1358 for (protocol = argv[optind-1]; *protocol; protocol++)
1359 *protocol = tolower(*protocol);
1360
1361 protocol = argv[optind-1];
1362 fw.ipv6.proto = parse_protocol(protocol);
1363 fw.ipv6.flags |= IP6T_F_PROTO;
1364
1365 if (fw.ipv6.proto == 0
1366 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1367 exit_error(PARAMETER_PROBLEM,
1368 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001369
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001370 if (is_exthdr(fw.ipv6.proto)
1371 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001372 fprintf(stderr,
1373 "Warning: never matched protocol: %s. "
1374 "use extension match instead.\n",
1375 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001376 break;
1377
1378 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001379 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001380 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1381 invert);
1382 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001383 break;
1384
1385 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001386 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1388 invert);
1389 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001390 break;
1391
1392 case 'j':
1393 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1394 invert);
1395 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001396 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001397 target = find_target(jumpto, TRY_LOAD);
1398
1399 if (target) {
1400 size_t size;
1401
Harald Welte43ac1912002-03-03 09:43:07 +00001402 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1403 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001404
1405 target->t = fw_calloc(1, size);
1406 target->t->u.target_size = size;
1407 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001408 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001409 target->init(target->t);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001410 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001411 }
1412 break;
1413
1414
1415 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001416 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1418 invert);
1419 parse_interface(argv[optind-1],
1420 fw.ipv6.iniface,
1421 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 break;
1423
1424 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001425 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001426 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1427 invert);
1428 parse_interface(argv[optind-1],
1429 fw.ipv6.outiface,
1430 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 break;
1432
1433 case 'v':
1434 if (!verbose)
1435 set_option(&options, OPT_VERBOSE,
1436 &fw.ipv6.invflags, invert);
1437 verbose++;
1438 break;
1439
1440 case 'm': {
1441 size_t size;
1442
1443 if (invert)
1444 exit_error(PARAMETER_PROBLEM,
1445 "unexpected ! flag before --match");
1446
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001447 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001448 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1449 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001450 m->m = fw_calloc(1, size);
1451 m->m->u.match_size = size;
1452 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001453 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001454 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001455 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001456 if (m != m->next)
1457 /* Merge options for non-cloned matches */
1458 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459 }
1460 break;
1461
1462 case 'n':
1463 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1464 invert);
1465 break;
1466
1467 case 't':
1468 if (invert)
1469 exit_error(PARAMETER_PROBLEM,
1470 "unexpected ! flag before --table");
1471 *table = argv[optind-1];
1472 break;
1473
1474 case 'x':
1475 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1476 invert);
1477 break;
1478
1479 case 'V':
1480 if (invert)
1481 printf("Not %s ;-)\n", program_version);
1482 else
1483 printf("%s v%s\n",
1484 program_name, program_version);
1485 exit(0);
1486
1487 case '0':
1488 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1489 invert);
1490 break;
1491
Harald Welte43ac1912002-03-03 09:43:07 +00001492 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001493 modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001494 break;
1495
1496 case 'c':
1497
1498 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1499 invert);
1500 pcnt = optarg;
1501 if (optind < argc && argv[optind][0] != '-'
1502 && argv[optind][0] != '!')
1503 bcnt = argv[optind++];
1504 else
1505 exit_error(PARAMETER_PROBLEM,
1506 "-%c requires packet and byte counter",
1507 opt2char(OPT_COUNTERS));
1508
Patrick McHardy875441e2007-10-17 08:48:58 +00001509 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001510 exit_error(PARAMETER_PROBLEM,
1511 "-%c packet counter not numeric",
1512 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001513 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001514
Patrick McHardy875441e2007-10-17 08:48:58 +00001515 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001516 exit_error(PARAMETER_PROBLEM,
1517 "-%c byte counter not numeric",
1518 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001519 fw.counters.bcnt = cnt;
1520
Harald Welte43ac1912002-03-03 09:43:07 +00001521 break;
1522
1523
Rusty Russell5eed48a2000-06-02 20:12:24 +00001524 case 1: /* non option */
1525 if (optarg[0] == '!' && optarg[1] == '\0') {
1526 if (invert)
1527 exit_error(PARAMETER_PROBLEM,
1528 "multiple consecutive ! not"
1529 " allowed");
1530 invert = TRUE;
1531 optarg[0] = '\0';
1532 continue;
1533 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001534 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001535 exit_tryhelp(2);
1536
1537 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 if (!target
1539 || !(target->parse(c - target->option_offset,
1540 argv, invert,
1541 &target->tflags,
1542 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001543 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001544 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001545 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001546 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001548 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001550 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001551 break;
1552 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001553 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001554
1555 /* If you listen carefully, you can
1556 actually hear this code suck. */
1557
1558 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001559 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001560 * parameter, that has not been parsed yet,
1561 * it's not an option of an explicitly loaded
1562 * match or a target. However, we support
1563 * implicit loading of the protocol match
1564 * extension. '-p tcp' means 'l4 proto 6' and
1565 * at the same time 'load tcp protocol match on
1566 * demand if we specify --dport'.
1567 *
1568 * To make this work, we need to make sure:
1569 * - the parameter has not been parsed by
1570 * a match (m above)
1571 * - a protocol has been specified
1572 * - the protocol extension has not been
1573 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001574 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001575 * - the protocol extension can be successively
1576 * loaded
1577 */
1578 if (m == NULL
1579 && protocol
1580 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001581 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001582 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001583 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001584 && (proto_used == 0))
1585 )
1586 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001587 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001588 /* Try loading protocol */
1589 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001590
Harald Welte00f5a9c2002-08-26 14:37:35 +00001591 proto_used = 1;
1592
1593 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1594 + m->size;
1595
1596 m->m = fw_calloc(1, size);
1597 m->m->u.match_size = size;
1598 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001599 set_revision(m->m->u.user.name,
1600 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001601 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001602 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001603
1604 opts = merge_options(opts,
1605 m->extra_opts, &m->option_offset);
1606
1607 optind--;
1608 continue;
1609 }
1610
Rusty Russell5eed48a2000-06-02 20:12:24 +00001611 if (!m)
1612 exit_error(PARAMETER_PROBLEM,
1613 "Unknown arg `%s'",
1614 argv[optind-1]);
1615 }
1616 }
1617 invert = FALSE;
1618 }
1619
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001620 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001621 if (matchp->match->final_check != NULL)
1622 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623
Jan Engelhardt830132a2007-10-04 16:24:50 +00001624 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625 target->final_check(target->tflags);
1626
1627 /* Fix me: must put inverse options checking here --MN */
1628
1629 if (optind < argc)
1630 exit_error(PARAMETER_PROBLEM,
1631 "unknown arguments found on commandline");
1632 if (!command)
1633 exit_error(PARAMETER_PROBLEM, "no command specified");
1634 if (invert)
1635 exit_error(PARAMETER_PROBLEM,
1636 "nothing appropriate following !");
1637
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001638 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001639 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001640 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001641 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001642 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 }
1644
1645 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001646 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1647 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648
1649 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001650 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1651 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001652
1653 if ((nsaddrs > 1 || ndaddrs > 1) &&
1654 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1655 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1656 " source or destination IP addresses");
1657
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1659 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1660 "specify a unique address");
1661
1662 generic_opt_check(command, options);
1663
1664 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1665 exit_error(PARAMETER_PROBLEM,
1666 "chain name `%s' too long (must be under %i chars)",
1667 chain, IP6T_FUNCTION_MAXNAMELEN);
1668
Harald Welte43ac1912002-03-03 09:43:07 +00001669 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001670 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001671 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001672
Rusty Russell8beb0492004-12-22 00:37:10 +00001673 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001674 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001675 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001676
Harald Welte43ac1912002-03-03 09:43:07 +00001677 if (!*handle)
1678 exit_error(VERSION_PROBLEM,
1679 "can't initialize ip6tables table `%s': %s",
1680 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001681
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001682 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683 || command == CMD_DELETE
1684 || command == CMD_INSERT
1685 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001686 if (strcmp(chain, "PREROUTING") == 0
1687 || strcmp(chain, "INPUT") == 0) {
1688 /* -o not valid with incoming packets. */
1689 if (options & OPT_VIANAMEOUT)
1690 exit_error(PARAMETER_PROBLEM,
1691 "Can't use -%c with %s\n",
1692 opt2char(OPT_VIANAMEOUT),
1693 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001694 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001695
Harald Welte43ac1912002-03-03 09:43:07 +00001696 if (strcmp(chain, "POSTROUTING") == 0
1697 || strcmp(chain, "OUTPUT") == 0) {
1698 /* -i not valid with outgoing packets */
1699 if (options & OPT_VIANAMEIN)
1700 exit_error(PARAMETER_PROBLEM,
1701 "Can't use -%c with %s\n",
1702 opt2char(OPT_VIANAMEIN),
1703 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001704 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001705
1706 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001707 fprintf(stderr,
1708 "Warning: using chain %s, not extension\n",
1709 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001711 if (target->t)
1712 free(target->t);
1713
Rusty Russell5eed48a2000-06-02 20:12:24 +00001714 target = NULL;
1715 }
1716
1717 /* If they didn't specify a target, or it's a chain
1718 name, use standard. */
1719 if (!target
1720 && (strlen(jumpto) == 0
1721 || ip6tc_is_chain(jumpto, *handle))) {
1722 size_t size;
1723
1724 target = find_target(IP6T_STANDARD_TARGET,
1725 LOAD_MUST_SUCCEED);
1726
1727 size = sizeof(struct ip6t_entry_target)
1728 + target->size;
1729 target->t = fw_calloc(1, size);
1730 target->t->u.target_size = size;
1731 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001732 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001733 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001734 }
1735
1736 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001737 /* it is no chain, and we can't load a plugin.
1738 * We cannot know if the plugin is corrupt, non
1739 * existant OR if the user just misspelled a
1740 * chain. */
1741 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001742 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001743 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001744 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001745 }
1746 }
1747
1748 switch (command) {
1749 case CMD_APPEND:
1750 ret = append_entry(chain, e,
1751 nsaddrs, saddrs, ndaddrs, daddrs,
1752 options&OPT_VERBOSE,
1753 handle);
1754 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001755 case CMD_DELETE:
1756 ret = delete_entry(chain, e,
1757 nsaddrs, saddrs, ndaddrs, daddrs,
1758 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001759 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001760 break;
1761 case CMD_DELETE_NUM:
1762 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1763 break;
1764 case CMD_REPLACE:
1765 ret = replace_entry(chain, e, rulenum - 1,
1766 saddrs, daddrs, options&OPT_VERBOSE,
1767 handle);
1768 break;
1769 case CMD_INSERT:
1770 ret = insert_entry(chain, e, rulenum - 1,
1771 nsaddrs, saddrs, ndaddrs, daddrs,
1772 options&OPT_VERBOSE,
1773 handle);
1774 break;
1775 case CMD_LIST:
1776 ret = list_entries(chain,
1777 options&OPT_VERBOSE,
1778 options&OPT_NUMERIC,
1779 options&OPT_EXPANDED,
1780 options&OPT_LINENUMBERS,
1781 handle);
1782 break;
1783 case CMD_FLUSH:
1784 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1785 break;
1786 case CMD_ZERO:
1787 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1788 break;
1789 case CMD_LIST|CMD_ZERO:
1790 ret = list_entries(chain,
1791 options&OPT_VERBOSE,
1792 options&OPT_NUMERIC,
1793 options&OPT_EXPANDED,
1794 options&OPT_LINENUMBERS,
1795 handle);
1796 if (ret)
1797 ret = zero_entries(chain,
1798 options&OPT_VERBOSE, handle);
1799 break;
1800 case CMD_NEW_CHAIN:
1801 ret = ip6tc_create_chain(chain, handle);
1802 break;
1803 case CMD_DELETE_CHAIN:
1804 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1805 break;
1806 case CMD_RENAME_CHAIN:
1807 ret = ip6tc_rename_chain(chain, newname, handle);
1808 break;
1809 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001810 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001811 break;
1812 default:
1813 /* We should never reach this... */
1814 exit_tryhelp(2);
1815 }
1816
1817 if (verbose > 1)
1818 dump_entries6(*handle);
1819
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001820 clear_rule_matches(&matches);
1821
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001822 if (e != NULL) {
1823 free(e);
1824 e = NULL;
1825 }
1826
Max Kellermann9ee386a2008-01-29 13:48:05 +00001827 for (i = 0; i < nsaddrs; i++)
1828 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001829
Max Kellermann9ee386a2008-01-29 13:48:05 +00001830 for (i = 0; i < ndaddrs; i++)
1831 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001832
Pablo Neiradfdcd642005-05-29 19:05:23 +00001833 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001834
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835 return ret;
1836}