blob: bc65012fb4f905332eeeede7e8afbc9b432f8c4f [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;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200276 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000277
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'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200470static struct xtables_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
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000615static void
616print_num(u_int64_t number, unsigned int format)
617{
Harald Welte43ac1912002-03-03 09:43:07 +0000618 if (format & FMT_KILOMEGAGIGA) {
619 if (number > 99999) {
620 number = (number + 500) / 1000;
621 if (number > 9999) {
622 number = (number + 500) / 1000;
623 if (number > 9999) {
624 number = (number + 500) / 1000;
625 if (number > 9999) {
626 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000627 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000628 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000629 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000630 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000631 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000632 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000633 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000634 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000635 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000636 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000637 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000638}
639
Harald Welte43ac1912002-03-03 09:43:07 +0000640
Rusty Russell5eed48a2000-06-02 20:12:24 +0000641static void
642print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
643{
644 struct ip6t_counters counters;
645 const char *pol = ip6tc_get_policy(chain, &counters, handle);
646 printf("Chain %s", chain);
647 if (pol) {
648 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000649 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000650 fputc(' ', stdout);
651 print_num(counters.pcnt, (format|FMT_NOTABLE));
652 fputs("packets, ", stdout);
653 print_num(counters.bcnt, (format|FMT_NOTABLE));
654 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000655 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000656 printf(")\n");
657 } else {
658 unsigned int refs;
659 if (!ip6tc_get_references(&refs, chain, handle))
660 printf(" (ERROR obtaining refs)\n");
661 else
662 printf(" (%u references)\n", refs);
663 }
664
665 if (format & FMT_LINENUMBERS)
666 printf(FMT("%-4s ", "%s "), "num");
667 if (!(format & FMT_NOCOUNTS)) {
668 if (format & FMT_KILOMEGAGIGA) {
669 printf(FMT("%5s ","%s "), "pkts");
670 printf(FMT("%5s ","%s "), "bytes");
671 } else {
672 printf(FMT("%8s ","%s "), "pkts");
673 printf(FMT("%10s ","%s "), "bytes");
674 }
675 }
676 if (!(format & FMT_NOTARGET))
677 printf(FMT("%-9s ","%s "), "target");
678 fputs(" prot ", stdout);
679 if (format & FMT_OPTIONS)
680 fputs("opt", stdout);
681 if (format & FMT_VIA) {
682 printf(FMT(" %-6s ","%s "), "in");
683 printf(FMT("%-6s ","%s "), "out");
684 }
685 printf(FMT(" %-19s ","%s "), "source");
686 printf(FMT(" %-19s "," %s "), "destination");
687 printf("\n");
688}
689
Harald Welte43ac1912002-03-03 09:43:07 +0000690
Rusty Russell5eed48a2000-06-02 20:12:24 +0000691static int
692print_match(const struct ip6t_entry_match *m,
693 const struct ip6t_ip6 *ip,
694 int numeric)
695{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200696 struct xtables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000697
698 if (match) {
699 if (match->print)
700 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000701 else
702 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 } else {
704 if (m->u.user.name[0])
705 printf("UNKNOWN match `%s' ", m->u.user.name);
706 }
707 /* Don't stop iterating. */
708 return 0;
709}
710
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100711/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000712static void
713print_firewall(const struct ip6t_entry *fw,
714 const char *targname,
715 unsigned int num,
716 unsigned int format,
717 const ip6tc_handle_t handle)
718{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200719 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720 const struct ip6t_entry_target *t;
721 u_int8_t flags;
722 char buf[BUFSIZ];
723
Rusty Russell5eed48a2000-06-02 20:12:24 +0000724 if (!ip6tc_is_chain(targname, handle))
725 target = find_target(targname, TRY_LOAD);
726 else
727 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
728
729 t = ip6t_get_target((struct ip6t_entry *)fw);
730 flags = fw->ipv6.flags;
731
732 if (format & FMT_LINENUMBERS)
733 printf(FMT("%-4u ", "%u "), num+1);
734
735 if (!(format & FMT_NOCOUNTS)) {
736 print_num(fw->counters.pcnt, format);
737 print_num(fw->counters.bcnt, format);
738 }
739
740 if (!(format & FMT_NOTARGET))
741 printf(FMT("%-9s ", "%s "), targname);
742
743 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
744 {
Harald Welte43ac1912002-03-03 09:43:07 +0000745 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000746 if (pname)
747 printf(FMT("%-5s", "%s "), pname);
748 else
749 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
750 }
751
752 if (format & FMT_OPTIONS) {
753 if (format & FMT_NOTABLE)
754 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000755 fputc(' ', stdout); /* Invert flag of FRAG */
756 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000757 fputc(' ', stdout);
758 }
759
760 if (format & FMT_VIA) {
761 char iface[IFNAMSIZ+2];
762
763 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
764 iface[0] = '!';
765 iface[1] = '\0';
766 }
767 else iface[0] = '\0';
768
769 if (fw->ipv6.iniface[0] != '\0') {
770 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771 }
772 else if (format & FMT_NUMERIC) strcat(iface, "*");
773 else strcat(iface, "any");
774 printf(FMT(" %-6s ","in %s "), iface);
775
776 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
777 iface[0] = '!';
778 iface[1] = '\0';
779 }
780 else iface[0] = '\0';
781
782 if (fw->ipv6.outiface[0] != '\0') {
783 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784 }
785 else if (format & FMT_NUMERIC) strcat(iface, "*");
786 else strcat(iface, "any");
787 printf(FMT("%-6s ","out %s "), iface);
788 }
789
790 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000791 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792 && !(format & FMT_NUMERIC))
793 printf(FMT("%-19s ","%s "), "anywhere");
794 else {
795 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000796 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000797 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000798 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.src));
799 strcat(buf, ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 printf(FMT("%-19s ","%s "), buf);
801 }
802
803 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
804 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
805 && !(format & FMT_NUMERIC))
806 printf(FMT("%-19s","-> %s"), "anywhere");
807 else {
808 if (format & FMT_NUMERIC)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000809 sprintf(buf, "%s", ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000810 else
Jan Engelhardt08b16162008-01-20 13:36:08 +0000811 sprintf(buf, "%s", ip6addr_to_anyname(&fw->ipv6.dst));
812 strcat(buf, ip6mask_to_numeric(&fw->ipv6.dmsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 printf(FMT("%-19s","-> %s"), buf);
814 }
815
816 if (format & FMT_NOTABLE)
817 fputs(" ", stdout);
818
819 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
820
821 if (target) {
822 if (target->print)
823 /* Print the target information. */
824 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
825 } else if (t->u.target_size != sizeof(*t))
826 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000827 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828
829 if (!(format & FMT_NONEWLINE))
830 fputc('\n', stdout);
831}
832
833static void
834print_firewall_line(const struct ip6t_entry *fw,
835 const ip6tc_handle_t h)
836{
837 struct ip6t_entry_target *t;
838
839 t = ip6t_get_target((struct ip6t_entry *)fw);
840 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
841}
842
843static int
844append_entry(const ip6t_chainlabel chain,
845 struct ip6t_entry *fw,
846 unsigned int nsaddrs,
847 const struct in6_addr saddrs[],
848 unsigned int ndaddrs,
849 const struct in6_addr daddrs[],
850 int verbose,
851 ip6tc_handle_t *handle)
852{
853 unsigned int i, j;
854 int ret = 1;
855
856 for (i = 0; i < nsaddrs; i++) {
857 fw->ipv6.src = saddrs[i];
858 for (j = 0; j < ndaddrs; j++) {
859 fw->ipv6.dst = daddrs[j];
860 if (verbose)
861 print_firewall_line(fw, *handle);
862 ret &= ip6tc_append_entry(chain, fw, handle);
863 }
864 }
865
866 return ret;
867}
868
869static int
870replace_entry(const ip6t_chainlabel chain,
871 struct ip6t_entry *fw,
872 unsigned int rulenum,
873 const struct in6_addr *saddr,
874 const struct in6_addr *daddr,
875 int verbose,
876 ip6tc_handle_t *handle)
877{
878 fw->ipv6.src = *saddr;
879 fw->ipv6.dst = *daddr;
880
881 if (verbose)
882 print_firewall_line(fw, *handle);
883 return ip6tc_replace_entry(chain, fw, rulenum, handle);
884}
885
886static int
887insert_entry(const ip6t_chainlabel chain,
888 struct ip6t_entry *fw,
889 unsigned int rulenum,
890 unsigned int nsaddrs,
891 const struct in6_addr saddrs[],
892 unsigned int ndaddrs,
893 const struct in6_addr daddrs[],
894 int verbose,
895 ip6tc_handle_t *handle)
896{
897 unsigned int i, j;
898 int ret = 1;
899
900 for (i = 0; i < nsaddrs; i++) {
901 fw->ipv6.src = saddrs[i];
902 for (j = 0; j < ndaddrs; j++) {
903 fw->ipv6.dst = daddrs[j];
904 if (verbose)
905 print_firewall_line(fw, *handle);
906 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
907 }
908 }
909
910 return ret;
911}
912
913static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000914make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000915{
916 /* Establish mask for comparison */
917 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000918 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000919 unsigned char *mask, *mptr;
920
921 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000922 for (matchp = matches; matchp; matchp = matchp->next)
923 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000924
925 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000926 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000927 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000928
929 memset(mask, 0xFF, sizeof(struct ip6t_entry));
930 mptr = mask + sizeof(struct ip6t_entry);
931
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000932 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000934 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000935 + matchp->match->userspacesize);
936 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000937 }
938
Max Kellermann5b76f682008-01-29 13:42:48 +0000939 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000940 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000941 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000942
943 return mask;
944}
945
946static int
947delete_entry(const ip6t_chainlabel chain,
948 struct ip6t_entry *fw,
949 unsigned int nsaddrs,
950 const struct in6_addr saddrs[],
951 unsigned int ndaddrs,
952 const struct in6_addr daddrs[],
953 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000954 ip6tc_handle_t *handle,
955 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000956{
957 unsigned int i, j;
958 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000959 unsigned char *mask;
960
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000961 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000962 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000963 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000965 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000966 if (verbose)
967 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000968 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000969 }
970 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000971 free(mask);
972
Rusty Russell5eed48a2000-06-02 20:12:24 +0000973 return ret;
974}
975
András Kis-Szabó764316a2001-02-26 17:31:20 +0000976int
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
978 int verbose, int builtinstoo, ip6tc_handle_t *handle)
979{
Max Kellermann5b76f682008-01-29 13:42:48 +0000980 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000981 const char *chain;
982 char *chains;
983 unsigned int i, chaincount = 0;
984
985 chain = ip6tc_first_chain(handle);
986 while (chain) {
987 chaincount++;
988 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000989 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000990
991 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
992 i = 0;
993 chain = ip6tc_first_chain(handle);
994 while (chain) {
995 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
996 i++;
997 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +0000998 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000999
1000 for (i = 0; i < chaincount; i++) {
1001 if (!builtinstoo
1002 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001003 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +00001005 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001006 }
1007
1008 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001009 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001010}
1011
András Kis-Szabó764316a2001-02-26 17:31:20 +00001012int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001013flush_entries(const ip6t_chainlabel chain, int verbose,
1014 ip6tc_handle_t *handle)
1015{
1016 if (!chain)
1017 return for_each_chain(flush_entries, verbose, 1, handle);
1018
1019 if (verbose)
1020 fprintf(stdout, "Flushing chain `%s'\n", chain);
1021 return ip6tc_flush_entries(chain, handle);
1022}
1023
1024static int
1025zero_entries(const ip6t_chainlabel chain, int verbose,
1026 ip6tc_handle_t *handle)
1027{
1028 if (!chain)
1029 return for_each_chain(zero_entries, verbose, 1, handle);
1030
1031 if (verbose)
1032 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1033 return ip6tc_zero_entries(chain, handle);
1034}
1035
András Kis-Szabó764316a2001-02-26 17:31:20 +00001036int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001037delete_chain(const ip6t_chainlabel chain, int verbose,
1038 ip6tc_handle_t *handle)
1039{
1040 if (!chain)
1041 return for_each_chain(delete_chain, verbose, 0, handle);
1042
1043 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001044 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001045 return ip6tc_delete_chain(chain, handle);
1046}
1047
1048static int
1049list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1050 int expanded, int linenumbers, ip6tc_handle_t *handle)
1051{
1052 int found = 0;
1053 unsigned int format;
1054 const char *this;
1055
1056 format = FMT_OPTIONS;
1057 if (!verbose)
1058 format |= FMT_NOCOUNTS;
1059 else
1060 format |= FMT_VIA;
1061
1062 if (numeric)
1063 format |= FMT_NUMERIC;
1064
1065 if (!expanded)
1066 format |= FMT_KILOMEGAGIGA;
1067
1068 if (linenumbers)
1069 format |= FMT_LINENUMBERS;
1070
1071 for (this = ip6tc_first_chain(handle);
1072 this;
1073 this = ip6tc_next_chain(handle)) {
1074 const struct ip6t_entry *i;
1075 unsigned int num;
1076
1077 if (chain && strcmp(chain, this) != 0)
1078 continue;
1079
1080 if (found) printf("\n");
1081
1082 print_header(format, this, handle);
1083 i = ip6tc_first_rule(this, handle);
1084
1085 num = 0;
1086 while (i) {
1087 print_firewall(i,
1088 ip6tc_get_target(i, handle),
1089 num++,
1090 format,
1091 *handle);
1092 i = ip6tc_next_rule(i, handle);
1093 }
1094 found = 1;
1095 }
1096
1097 errno = ENOENT;
1098 return found;
1099}
1100
1101static struct ip6t_entry *
1102generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001103 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001104 struct ip6t_entry_target *target)
1105{
1106 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001107 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001108 struct ip6t_entry *e;
1109
1110 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001111 for (matchp = matches; matchp; matchp = matchp->next)
1112 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001113
1114 e = fw_malloc(size + target->u.target_size);
1115 *e = *fw;
1116 e->target_offset = size;
1117 e->next_offset = size + target->u.target_size;
1118
1119 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001120 for (matchp = matches; matchp; matchp = matchp->next) {
1121 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1122 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001123 }
1124 memcpy(e->elems + size, target, target->u.target_size);
1125
1126 return e;
1127}
1128
Jan Engelhardt33690a12008-02-11 00:54:00 +01001129static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001130{
1131 struct ip6tables_rule_match *matchp, *tmp;
1132
1133 for (matchp = *matches; matchp;) {
1134 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001135 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001136 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001137 matchp->match->m = NULL;
1138 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001139 if (matchp->match == matchp->match->next) {
1140 free(matchp->match);
1141 matchp->match = NULL;
1142 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001143 free(matchp);
1144 matchp = tmp;
1145 }
1146
1147 *matches = NULL;
1148}
1149
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001150static void set_revision(char *name, u_int8_t revision)
1151{
1152 /* Old kernel sources don't have ".revision" field,
1153 but we stole a byte from name. */
1154 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1155 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1156}
1157
Rusty Russell5eed48a2000-06-02 20:12:24 +00001158int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1159{
1160 struct ip6t_entry fw, *e = NULL;
1161 int invert = 0;
1162 unsigned int nsaddrs = 0, ndaddrs = 0;
1163 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1164
1165 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001166 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001167 const char *chain = NULL;
1168 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1169 const char *policy = NULL, *newname = NULL;
1170 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001171 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001172 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001173 struct xtables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001174 struct ip6tables_rule_match *matches = NULL;
1175 struct ip6tables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001176 struct xtables_target *target = NULL;
1177 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001178 const char *jumpto = "";
1179 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001180 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001181 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001182
1183 memset(&fw, 0, sizeof(fw));
1184
Harald Welte43ac1912002-03-03 09:43:07 +00001185 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001186 * a second time */
1187 optind = 0;
1188
Harald Welte43ac1912002-03-03 09:43:07 +00001189 /* clear mflags in case do_command gets called a second time
1190 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001191 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001192 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001193
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001194 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001195 t->tflags = 0;
1196 t->used = 0;
1197 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001198
Rusty Russell5eed48a2000-06-02 20:12:24 +00001199 /* Suppress error messages: we may add new options if we
1200 demand-load a protocol. */
1201 opterr = 0;
1202
1203 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001204 "-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 +00001205 opts, NULL)) != -1) {
1206 switch (c) {
1207 /*
1208 * Command selection
1209 */
1210 case 'A':
1211 add_command(&command, CMD_APPEND, CMD_NONE,
1212 invert);
1213 chain = optarg;
1214 break;
1215
1216 case 'D':
1217 add_command(&command, CMD_DELETE, CMD_NONE,
1218 invert);
1219 chain = optarg;
1220 if (optind < argc && argv[optind][0] != '-'
1221 && argv[optind][0] != '!') {
1222 rulenum = parse_rulenumber(argv[optind++]);
1223 command = CMD_DELETE_NUM;
1224 }
1225 break;
1226
Rusty Russell5eed48a2000-06-02 20:12:24 +00001227 case 'R':
1228 add_command(&command, CMD_REPLACE, CMD_NONE,
1229 invert);
1230 chain = optarg;
1231 if (optind < argc && argv[optind][0] != '-'
1232 && argv[optind][0] != '!')
1233 rulenum = parse_rulenumber(argv[optind++]);
1234 else
1235 exit_error(PARAMETER_PROBLEM,
1236 "-%c requires a rule number",
1237 cmd2char(CMD_REPLACE));
1238 break;
1239
1240 case 'I':
1241 add_command(&command, CMD_INSERT, CMD_NONE,
1242 invert);
1243 chain = optarg;
1244 if (optind < argc && argv[optind][0] != '-'
1245 && argv[optind][0] != '!')
1246 rulenum = parse_rulenumber(argv[optind++]);
1247 else rulenum = 1;
1248 break;
1249
1250 case 'L':
1251 add_command(&command, CMD_LIST, CMD_ZERO,
1252 invert);
1253 if (optarg) chain = optarg;
1254 else if (optind < argc && argv[optind][0] != '-'
1255 && argv[optind][0] != '!')
1256 chain = argv[optind++];
1257 break;
1258
1259 case 'F':
1260 add_command(&command, CMD_FLUSH, CMD_NONE,
1261 invert);
1262 if (optarg) chain = optarg;
1263 else if (optind < argc && argv[optind][0] != '-'
1264 && argv[optind][0] != '!')
1265 chain = argv[optind++];
1266 break;
1267
1268 case 'Z':
1269 add_command(&command, CMD_ZERO, CMD_LIST,
1270 invert);
1271 if (optarg) chain = optarg;
1272 else if (optind < argc && argv[optind][0] != '-'
1273 && argv[optind][0] != '!')
1274 chain = argv[optind++];
1275 break;
1276
1277 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001278 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001279 exit_error(PARAMETER_PROBLEM,
1280 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001281 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001282 if (find_target(optarg, TRY_LOAD))
1283 exit_error(PARAMETER_PROBLEM,
1284 "chain name may not clash "
1285 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001286 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1287 invert);
1288 chain = optarg;
1289 break;
1290
1291 case 'X':
1292 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1293 invert);
1294 if (optarg) chain = optarg;
1295 else if (optind < argc && argv[optind][0] != '-'
1296 && argv[optind][0] != '!')
1297 chain = argv[optind++];
1298 break;
1299
1300 case 'E':
1301 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1302 invert);
1303 chain = optarg;
1304 if (optind < argc && argv[optind][0] != '-'
1305 && argv[optind][0] != '!')
1306 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001307 else
1308 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001309 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001310 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001311 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001312 break;
1313
1314 case 'P':
1315 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1316 invert);
1317 chain = optarg;
1318 if (optind < argc && argv[optind][0] != '-'
1319 && argv[optind][0] != '!')
1320 policy = argv[optind++];
1321 else
1322 exit_error(PARAMETER_PROBLEM,
1323 "-%c requires a chain and a policy",
1324 cmd2char(CMD_SET_POLICY));
1325 break;
1326
1327 case 'h':
1328 if (!optarg)
1329 optarg = argv[optind];
1330
Jonas Berlin1b91e592005-04-01 06:58:38 +00001331 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001332 if (!matches && protocol)
1333 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001334
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001335 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001336
1337 /*
1338 * Option selection
1339 */
1340 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001341 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001342 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1343 invert);
1344
1345 /* Canonicalize into lower case */
1346 for (protocol = argv[optind-1]; *protocol; protocol++)
1347 *protocol = tolower(*protocol);
1348
1349 protocol = argv[optind-1];
1350 fw.ipv6.proto = parse_protocol(protocol);
1351 fw.ipv6.flags |= IP6T_F_PROTO;
1352
1353 if (fw.ipv6.proto == 0
1354 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1355 exit_error(PARAMETER_PROBLEM,
1356 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001357
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001358 if (is_exthdr(fw.ipv6.proto)
1359 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001360 fprintf(stderr,
1361 "Warning: never matched protocol: %s. "
1362 "use extension match instead.\n",
1363 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001364 break;
1365
1366 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001367 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001368 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1369 invert);
1370 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001371 break;
1372
1373 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001374 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1376 invert);
1377 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378 break;
1379
1380 case 'j':
1381 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1382 invert);
1383 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001384 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001385 target = find_target(jumpto, TRY_LOAD);
1386
1387 if (target) {
1388 size_t size;
1389
Harald Welte43ac1912002-03-03 09:43:07 +00001390 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1391 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392
1393 target->t = fw_calloc(1, size);
1394 target->t->u.target_size = size;
1395 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001396 set_revision(target->t->u.user.name,
1397 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001398 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001399 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001400 opts = merge_options(opts,
1401 target->extra_opts,
1402 &target->option_offset);
1403 if (opts == NULL)
1404 exit_error(OTHER_PROBLEM,
1405 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001406 }
1407 break;
1408
1409
1410 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001411 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001412 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1413 invert);
1414 parse_interface(argv[optind-1],
1415 fw.ipv6.iniface,
1416 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 break;
1418
1419 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001420 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001421 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1422 invert);
1423 parse_interface(argv[optind-1],
1424 fw.ipv6.outiface,
1425 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001426 break;
1427
1428 case 'v':
1429 if (!verbose)
1430 set_option(&options, OPT_VERBOSE,
1431 &fw.ipv6.invflags, invert);
1432 verbose++;
1433 break;
1434
1435 case 'm': {
1436 size_t size;
1437
1438 if (invert)
1439 exit_error(PARAMETER_PROBLEM,
1440 "unexpected ! flag before --match");
1441
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001442 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001443 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1444 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001445 m->m = fw_calloc(1, size);
1446 m->m->u.match_size = size;
1447 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001448 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001449 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001450 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001451 if (m != m->next)
1452 /* Merge options for non-cloned matches */
1453 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454 }
1455 break;
1456
1457 case 'n':
1458 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1459 invert);
1460 break;
1461
1462 case 't':
1463 if (invert)
1464 exit_error(PARAMETER_PROBLEM,
1465 "unexpected ! flag before --table");
1466 *table = argv[optind-1];
1467 break;
1468
1469 case 'x':
1470 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1471 invert);
1472 break;
1473
1474 case 'V':
1475 if (invert)
1476 printf("Not %s ;-)\n", program_version);
1477 else
1478 printf("%s v%s\n",
1479 program_name, program_version);
1480 exit(0);
1481
1482 case '0':
1483 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1484 invert);
1485 break;
1486
Harald Welte43ac1912002-03-03 09:43:07 +00001487 case 'M':
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001488 modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001489 break;
1490
1491 case 'c':
1492
1493 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1494 invert);
1495 pcnt = optarg;
1496 if (optind < argc && argv[optind][0] != '-'
1497 && argv[optind][0] != '!')
1498 bcnt = argv[optind++];
1499 else
1500 exit_error(PARAMETER_PROBLEM,
1501 "-%c requires packet and byte counter",
1502 opt2char(OPT_COUNTERS));
1503
Patrick McHardy875441e2007-10-17 08:48:58 +00001504 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001505 exit_error(PARAMETER_PROBLEM,
1506 "-%c packet counter not numeric",
1507 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001508 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001509
Patrick McHardy875441e2007-10-17 08:48:58 +00001510 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001511 exit_error(PARAMETER_PROBLEM,
1512 "-%c byte counter not numeric",
1513 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001514 fw.counters.bcnt = cnt;
1515
Harald Welte43ac1912002-03-03 09:43:07 +00001516 break;
1517
1518
Rusty Russell5eed48a2000-06-02 20:12:24 +00001519 case 1: /* non option */
1520 if (optarg[0] == '!' && optarg[1] == '\0') {
1521 if (invert)
1522 exit_error(PARAMETER_PROBLEM,
1523 "multiple consecutive ! not"
1524 " allowed");
1525 invert = TRUE;
1526 optarg[0] = '\0';
1527 continue;
1528 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001529 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001530 exit_tryhelp(2);
1531
1532 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533 if (!target
1534 || !(target->parse(c - target->option_offset,
1535 argv, invert,
1536 &target->tflags,
1537 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001538 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001539 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001540 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001541 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001542 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001543 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001544 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001545 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001546 break;
1547 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001548 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001549
1550 /* If you listen carefully, you can
1551 actually hear this code suck. */
1552
1553 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001554 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001555 * parameter, that has not been parsed yet,
1556 * it's not an option of an explicitly loaded
1557 * match or a target. However, we support
1558 * implicit loading of the protocol match
1559 * extension. '-p tcp' means 'l4 proto 6' and
1560 * at the same time 'load tcp protocol match on
1561 * demand if we specify --dport'.
1562 *
1563 * To make this work, we need to make sure:
1564 * - the parameter has not been parsed by
1565 * a match (m above)
1566 * - a protocol has been specified
1567 * - the protocol extension has not been
1568 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001569 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001570 * - the protocol extension can be successively
1571 * loaded
1572 */
1573 if (m == NULL
1574 && protocol
1575 && (!find_proto(protocol, DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001576 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001577 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001578 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001579 && (proto_used == 0))
1580 )
1581 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001582 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001583 /* Try loading protocol */
1584 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001585
Harald Welte00f5a9c2002-08-26 14:37:35 +00001586 proto_used = 1;
1587
1588 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1589 + m->size;
1590
1591 m->m = fw_calloc(1, size);
1592 m->m->u.match_size = size;
1593 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001594 set_revision(m->m->u.user.name,
1595 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001596 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001597 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001598
1599 opts = merge_options(opts,
1600 m->extra_opts, &m->option_offset);
1601
1602 optind--;
1603 continue;
1604 }
1605
Rusty Russell5eed48a2000-06-02 20:12:24 +00001606 if (!m)
1607 exit_error(PARAMETER_PROBLEM,
1608 "Unknown arg `%s'",
1609 argv[optind-1]);
1610 }
1611 }
1612 invert = FALSE;
1613 }
1614
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001615 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001616 if (matchp->match->final_check != NULL)
1617 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001618
Jan Engelhardt830132a2007-10-04 16:24:50 +00001619 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001620 target->final_check(target->tflags);
1621
1622 /* Fix me: must put inverse options checking here --MN */
1623
1624 if (optind < argc)
1625 exit_error(PARAMETER_PROBLEM,
1626 "unknown arguments found on commandline");
1627 if (!command)
1628 exit_error(PARAMETER_PROBLEM, "no command specified");
1629 if (invert)
1630 exit_error(PARAMETER_PROBLEM,
1631 "nothing appropriate following !");
1632
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001633 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001635 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001636 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001637 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001638 }
1639
1640 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001641 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1642 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643
1644 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001645 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1646 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001647
1648 if ((nsaddrs > 1 || ndaddrs > 1) &&
1649 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1650 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1651 " source or destination IP addresses");
1652
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1654 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1655 "specify a unique address");
1656
1657 generic_opt_check(command, options);
1658
1659 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1660 exit_error(PARAMETER_PROBLEM,
1661 "chain name `%s' too long (must be under %i chars)",
1662 chain, IP6T_FUNCTION_MAXNAMELEN);
1663
Harald Welte43ac1912002-03-03 09:43:07 +00001664 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001665 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001666 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001667
Rusty Russell8beb0492004-12-22 00:37:10 +00001668 /* try to insmod the module if iptc_init failed */
Jan Engelhardtdbb77542008-02-11 00:33:30 +01001669 if (!*handle && load_xtables_ko(modprobe_program, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001670 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001671
Harald Welte43ac1912002-03-03 09:43:07 +00001672 if (!*handle)
1673 exit_error(VERSION_PROBLEM,
1674 "can't initialize ip6tables table `%s': %s",
1675 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001676
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001677 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001678 || command == CMD_DELETE
1679 || command == CMD_INSERT
1680 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001681 if (strcmp(chain, "PREROUTING") == 0
1682 || strcmp(chain, "INPUT") == 0) {
1683 /* -o not valid with incoming packets. */
1684 if (options & OPT_VIANAMEOUT)
1685 exit_error(PARAMETER_PROBLEM,
1686 "Can't use -%c with %s\n",
1687 opt2char(OPT_VIANAMEOUT),
1688 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001689 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001690
Harald Welte43ac1912002-03-03 09:43:07 +00001691 if (strcmp(chain, "POSTROUTING") == 0
1692 || strcmp(chain, "OUTPUT") == 0) {
1693 /* -i not valid with outgoing packets */
1694 if (options & OPT_VIANAMEIN)
1695 exit_error(PARAMETER_PROBLEM,
1696 "Can't use -%c with %s\n",
1697 opt2char(OPT_VIANAMEIN),
1698 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001699 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001700
1701 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00001702 fprintf(stderr,
1703 "Warning: using chain %s, not extension\n",
1704 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001705
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001706 if (target->t)
1707 free(target->t);
1708
Rusty Russell5eed48a2000-06-02 20:12:24 +00001709 target = NULL;
1710 }
1711
1712 /* If they didn't specify a target, or it's a chain
1713 name, use standard. */
1714 if (!target
1715 && (strlen(jumpto) == 0
1716 || ip6tc_is_chain(jumpto, *handle))) {
1717 size_t size;
1718
1719 target = find_target(IP6T_STANDARD_TARGET,
1720 LOAD_MUST_SUCCEED);
1721
1722 size = sizeof(struct ip6t_entry_target)
1723 + target->size;
1724 target->t = fw_calloc(1, size);
1725 target->t->u.target_size = size;
1726 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001727 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001728 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001729 }
1730
1731 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00001732 /* it is no chain, and we can't load a plugin.
1733 * We cannot know if the plugin is corrupt, non
1734 * existant OR if the user just misspelled a
1735 * chain. */
1736 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001737 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001738 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001739 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001740 }
1741 }
1742
1743 switch (command) {
1744 case CMD_APPEND:
1745 ret = append_entry(chain, e,
1746 nsaddrs, saddrs, ndaddrs, daddrs,
1747 options&OPT_VERBOSE,
1748 handle);
1749 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001750 case CMD_DELETE:
1751 ret = delete_entry(chain, e,
1752 nsaddrs, saddrs, ndaddrs, daddrs,
1753 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001754 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001755 break;
1756 case CMD_DELETE_NUM:
1757 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1758 break;
1759 case CMD_REPLACE:
1760 ret = replace_entry(chain, e, rulenum - 1,
1761 saddrs, daddrs, options&OPT_VERBOSE,
1762 handle);
1763 break;
1764 case CMD_INSERT:
1765 ret = insert_entry(chain, e, rulenum - 1,
1766 nsaddrs, saddrs, ndaddrs, daddrs,
1767 options&OPT_VERBOSE,
1768 handle);
1769 break;
1770 case CMD_LIST:
1771 ret = list_entries(chain,
1772 options&OPT_VERBOSE,
1773 options&OPT_NUMERIC,
1774 options&OPT_EXPANDED,
1775 options&OPT_LINENUMBERS,
1776 handle);
1777 break;
1778 case CMD_FLUSH:
1779 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1780 break;
1781 case CMD_ZERO:
1782 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1783 break;
1784 case CMD_LIST|CMD_ZERO:
1785 ret = list_entries(chain,
1786 options&OPT_VERBOSE,
1787 options&OPT_NUMERIC,
1788 options&OPT_EXPANDED,
1789 options&OPT_LINENUMBERS,
1790 handle);
1791 if (ret)
1792 ret = zero_entries(chain,
1793 options&OPT_VERBOSE, handle);
1794 break;
1795 case CMD_NEW_CHAIN:
1796 ret = ip6tc_create_chain(chain, handle);
1797 break;
1798 case CMD_DELETE_CHAIN:
1799 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1800 break;
1801 case CMD_RENAME_CHAIN:
1802 ret = ip6tc_rename_chain(chain, newname, handle);
1803 break;
1804 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001805 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001806 break;
1807 default:
1808 /* We should never reach this... */
1809 exit_tryhelp(2);
1810 }
1811
1812 if (verbose > 1)
1813 dump_entries6(*handle);
1814
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001815 clear_rule_matches(&matches);
1816
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001817 if (e != NULL) {
1818 free(e);
1819 e = NULL;
1820 }
1821
Max Kellermann9ee386a2008-01-29 13:48:05 +00001822 for (i = 0; i < nsaddrs; i++)
1823 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001824
Max Kellermann9ee386a2008-01-29 13:48:05 +00001825 for (i = 0; i < ndaddrs; i++)
1826 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001827
Pablo Neiradfdcd642005-05-29 19:05:23 +00001828 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001829
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 return ret;
1831}