blob: fd7327631f45cc9cd0d563a1525664d967d19438 [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>
Jan Engelhardtc021c3c2009-01-27 15:10:05 +010036#include <stdbool.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000037#include <limits.h>
38#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000039#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000040#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000041#include <unistd.h>
42#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000043#include <sys/types.h>
44#include <sys/socket.h>
Jan Engelhardt33690a12008-02-11 00:54:00 +010045#include "ip6tables-multi.h"
Rusty Russell5eed48a2000-06-02 20:12:24 +000046
47#ifndef TRUE
48#define TRUE 1
49#endif
50#ifndef FALSE
51#define FALSE 0
52#endif
53
Rusty Russell5eed48a2000-06-02 20:12:24 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000081#define CMD_RENAME_CHAIN 0x0800U
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020082#define CMD_LIST_RULES 0x1000U
83#define NUMBER_OF_CMD 14
Rusty Russell5eed48a2000-06-02 20:12:24 +000084static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
Henrik Nordstrom96296cf2008-05-13 13:08:26 +020085 'N', 'X', 'P', 'E', 'S' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000086
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100106 {.name = "append", .has_arg = 1, .val = 'A'},
107 {.name = "delete", .has_arg = 1, .val = 'D'},
108 {.name = "insert", .has_arg = 1, .val = 'I'},
109 {.name = "replace", .has_arg = 1, .val = 'R'},
110 {.name = "list", .has_arg = 2, .val = 'L'},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200111 {.name = "list-rules", .has_arg = 2, .val = 'S'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100112 {.name = "flush", .has_arg = 2, .val = 'F'},
113 {.name = "zero", .has_arg = 2, .val = 'Z'},
114 {.name = "new-chain", .has_arg = 1, .val = 'N'},
115 {.name = "delete-chain", .has_arg = 2, .val = 'X'},
116 {.name = "rename-chain", .has_arg = 1, .val = 'E'},
117 {.name = "policy", .has_arg = 1, .val = 'P'},
118 {.name = "source", .has_arg = 1, .val = 's'},
119 {.name = "destination", .has_arg = 1, .val = 'd'},
120 {.name = "src", .has_arg = 1, .val = 's'}, /* synonym */
121 {.name = "dst", .has_arg = 1, .val = 'd'}, /* synonym */
122 {.name = "protocol", .has_arg = 1, .val = 'p'},
123 {.name = "in-interface", .has_arg = 1, .val = 'i'},
124 {.name = "jump", .has_arg = 1, .val = 'j'},
125 {.name = "table", .has_arg = 1, .val = 't'},
126 {.name = "match", .has_arg = 1, .val = 'm'},
127 {.name = "numeric", .has_arg = 0, .val = 'n'},
128 {.name = "out-interface", .has_arg = 1, .val = 'o'},
129 {.name = "verbose", .has_arg = 0, .val = 'v'},
130 {.name = "exact", .has_arg = 0, .val = 'x'},
131 {.name = "version", .has_arg = 0, .val = 'V'},
132 {.name = "help", .has_arg = 2, .val = 'h'},
133 {.name = "line-numbers", .has_arg = 0, .val = '0'},
134 {.name = "modprobe", .has_arg = 1, .val = 'M'},
135 {.name = "set-counters", .has_arg = 1, .val = 'c'},
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200136 {.name = "goto", .has_arg = 1, .val = 'g'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +0100137 {NULL},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Rusty Russell5eed48a2000-06-02 20:12:24 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200161 /* -n -s -d -p -j -v -x -i -o --line -c */
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
Henrik Nordstrom48c1bc62008-05-12 20:53:16 +0200172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x',' '},
Henrik Nordstrom96296cf2008-05-13 13:08:26 +0200173/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
174/*LIST_RULES*/{'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IP6T_INV_SRCIP,
181/* -d */ IP6T_INV_DSTIP,
182/* -p */ IP6T_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IP6T_INV_VIA_IN,
187/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190};
191
192const char *program_version;
193const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000194
Rusty Russell5eed48a2000-06-02 20:12:24 +0000195/* A few hardcoded protocols for 'all' and in case the user has no
196 /etc/protocols */
197struct pprot {
198 char *name;
199 u_int8_t num;
200};
201
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000202struct afinfo afinfo = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100203 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000204 .libprefix = "libip6t_",
205 .ipproto = IPPROTO_IPV6,
206 .kmod = "ip6_tables",
207 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
208 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
209};
210
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000211/* Primitive headers... */
212/* defined in netinet/in.h */
213#if 0
214#ifndef IPPROTO_ESP
215#define IPPROTO_ESP 50
216#endif
217#ifndef IPPROTO_AH
218#define IPPROTO_AH 51
219#endif
220#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000221#ifndef IPPROTO_MH
222#define IPPROTO_MH 135
223#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000224
Rusty Russell5eed48a2000-06-02 20:12:24 +0000225static const struct pprot chain_protos[] = {
226 { "tcp", IPPROTO_TCP },
227 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000228 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000229 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000230 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000231 { "esp", IPPROTO_ESP },
232 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000233 { "ipv6-mh", IPPROTO_MH },
234 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000235 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000236};
237
238static char *
239proto_to_name(u_int8_t proto, int nolookup)
240{
241 unsigned int i;
242
243 if (proto && !nolookup) {
244 struct protoent *pent = getprotobynumber(proto);
245 if (pent)
246 return pent->p_name;
247 }
248
249 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
250 if (chain_protos[i].num == proto)
251 return chain_protos[i].name;
252
253 return NULL;
254}
255
Pablo Neiradfdcd642005-05-29 19:05:23 +0000256static void free_opts(int reset_offset)
257{
258 if (opts != original_opts) {
259 free(opts);
260 opts = original_opts;
261 if (reset_offset)
262 global_option_offset = 0;
263 }
264}
265
Patrick McHardy0b639362007-09-08 16:00:01 +0000266static void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000267exit_tryhelp(int status)
268{
Harald Weltea8658ca2003-03-05 07:46:15 +0000269 if (line != -1)
270 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000271 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
272 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000273 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000274 exit(status);
275}
276
Patrick McHardy0b639362007-09-08 16:00:01 +0000277static void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000278exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000279{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000280 struct ip6tables_rule_match *matchp = NULL;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200281 struct xtables_target *t = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282
283 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000284"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000285" %s -[RI] chain rulenum rule-specification [options]\n"
286" %s -D chain rulenum [options]\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200287" %s -[LS] [chain [rulenum]] [options]\n"
288" %s -[FZ] [chain] [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289" %s -[NX] chain\n"
290" %s -E old-chain-name new-chain-name\n"
291" %s -P chain target [options]\n"
292" %s -h (print this help information)\n\n",
293 program_name, program_version, program_name, program_name,
294 program_name, program_name, program_name, program_name,
Henrik Nordstrombb340822008-05-13 13:09:23 +0200295 program_name, program_name, program_name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000296
297 printf(
298"Commands:\n"
299"Either long or short options are allowed.\n"
300" --append -A chain Append to chain\n"
301" --delete -D chain Delete matching rule from chain\n"
302" --delete -D chain rulenum\n"
303" Delete rule rulenum (1 = first) from chain\n"
304" --insert -I chain [rulenum]\n"
305" Insert in chain as rulenum (default 1=first)\n"
306" --replace -R chain rulenum\n"
307" Replace rule rulenum (1 = first) in chain\n"
Henrik Nordstrombb340822008-05-13 13:09:23 +0200308" --list -L [chain [rulenum]]\n"
309" List the rules in a chain or all chains\n"
310" --list-rules -S [chain [rulenum]]\n"
311" Print the rules in a chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000312" --flush -F [chain] Delete all rules in chain or all chains\n"
313" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314" --new -N chain Create a new user-defined chain\n"
315" --delete-chain\n"
316" -X [chain] Delete a user-defined chain\n"
317" --policy -P chain target\n"
318" Change policy on chain to target\n"
319" --rename-chain\n"
320" -E old-chain new-chain\n"
321" Change chain name, (moving any references)\n"
322
323"Options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200324"[!] --proto -p proto protocol: by number or name, eg. `tcp'\n"
325"[!] --source -s address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000326" source specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200327"[!] --destination -d address[/mask]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000328" destination specification\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200329"[!] --in-interface -i input name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000330" network interface name ([+] for wildcard)\n"
331" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000332" target for rule (may load target extension)\n"
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200333#ifdef IP6T_F_GOTO
334" --goto -g chain\n"
335" jump to chain with no return\n"
336#endif
Harald Welte43ac1912002-03-03 09:43:07 +0000337" --match -m match\n"
338" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000339" --numeric -n numeric output of addresses and ports\n"
Jan Engelhardt96727922008-08-13 14:42:41 +0200340"[!] --out-interface -o output name[+]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000341" network interface name ([+] for wildcard)\n"
342" --table -t table table to manipulate (default: `filter')\n"
343" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000344" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000345" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000346/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000347" --modprobe=<command> try to insert modules using this command\n"
348" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000349"[!] --version -V print package version.\n");
350
Max Kellermann5b76f682008-01-29 13:42:48 +0000351 /* Print out any special helps. A user might like to be able to add a --help
352 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000353 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000354 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000355 if (t->used) {
356 printf("\n");
357 t->help();
358 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000359 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000360 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000361 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000362 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000363 }
364 exit(0);
365}
366
Patrick McHardy0b639362007-09-08 16:00:01 +0000367void
Jan Engelhardta41545c2009-01-27 21:27:19 +0100368exit_error(enum xtables_exittype status, const char *msg, ...)
Patrick McHardy0b639362007-09-08 16:00:01 +0000369{
370 va_list args;
371
372 va_start(args, msg);
373 fprintf(stderr, "%s v%s: ", program_name, program_version);
374 vfprintf(stderr, msg, args);
375 va_end(args);
376 fprintf(stderr, "\n");
377 if (status == PARAMETER_PROBLEM)
378 exit_tryhelp(status);
379 if (status == VERSION_PROBLEM)
380 fprintf(stderr,
381 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
382 /* On error paths, make sure that we don't leak memory */
383 free_opts(1);
384 exit(status);
385}
386
Rusty Russell5eed48a2000-06-02 20:12:24 +0000387static void
388generic_opt_check(int command, int options)
389{
390 int i, j, legal = 0;
391
392 /* Check that commands are valid with options. Complicated by the
393 * fact that if an option is legal with *any* command given, it is
394 * legal overall (ie. -z and -l).
395 */
396 for (i = 0; i < NUMBER_OF_OPT; i++) {
397 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
398
399 for (j = 0; j < NUMBER_OF_CMD; j++) {
400 if (!(command & (1<<j)))
401 continue;
402
403 if (!(options & (1<<i))) {
404 if (commands_v_options[j][i] == '+')
405 exit_error(PARAMETER_PROBLEM,
406 "You need to supply the `-%c' "
407 "option for this command\n",
408 optflags[i]);
409 } else {
410 if (commands_v_options[j][i] != 'x')
411 legal = 1;
412 else if (legal == 0)
413 legal = -1;
414 }
415 }
416 if (legal == -1)
417 exit_error(PARAMETER_PROBLEM,
418 "Illegal option `-%c' with this command\n",
419 optflags[i]);
420 }
421}
422
423static char
424opt2char(int option)
425{
426 const char *ptr;
427 for (ptr = optflags; option > 1; option >>= 1, ptr++);
428
429 return *ptr;
430}
431
432static char
433cmd2char(int option)
434{
435 const char *ptr;
436 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
437
438 return *ptr;
439}
440
441static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000442add_command(unsigned int *cmd, const int newcmd, const int othercmds,
443 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000444{
445 if (invert)
446 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
447 if (*cmd & (~othercmds))
448 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
449 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
450 *cmd |= newcmd;
451}
452
453int
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100454check_inverse(const char option[], int *invert, int *my_optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000455{
456 if (option && strcmp(option, "!") == 0) {
457 if (*invert)
458 exit_error(PARAMETER_PROBLEM,
459 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460 *invert = TRUE;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100461 if (my_optind != NULL) {
462 ++*my_optind;
463 if (argc && *my_optind > argc)
Harald Welteb77f1da2002-03-14 11:35:58 +0000464 exit_error(PARAMETER_PROBLEM,
465 "no argument following `!'");
466 }
467
Rusty Russell5eed48a2000-06-02 20:12:24 +0000468 return TRUE;
469 }
470 return FALSE;
471}
472
Rusty Russell5eed48a2000-06-02 20:12:24 +0000473/*
474 * All functions starting with "parse" should succeed, otherwise
475 * the program fails.
476 * Most routines return pointers to static data that may change
477 * between calls to the same or other routines with a few exceptions:
478 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
479 * return global static data.
480*/
481
Rusty Russell5eed48a2000-06-02 20:12:24 +0000482/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200483static struct xtables_match *
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100484find_proto(const char *pname, enum xtables_tryload tryload,
485 int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000486{
Harald Welteed498492001-07-23 01:24:22 +0000487 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100489 if (xtables_strtoui(pname, NULL, &proto, 0, UINT8_MAX)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000490 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000491
Harald Welte43ac1912002-03-03 09:43:07 +0000492 if (protoname)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100493 return xtables_find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000494 } else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100495 return xtables_find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000496
497 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498}
499
Harald Welte43ac1912002-03-03 09:43:07 +0000500u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000501parse_protocol(const char *s)
502{
Harald Welteed498492001-07-23 01:24:22 +0000503 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000504
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100505 if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000506 struct protoent *pent;
507
Harald Weltecbe1ec72006-02-11 09:50:11 +0000508 /* first deal with the special case of 'all' to prevent
509 * people from being able to redefine 'all' in nsswitch
Max Kellermann5b76f682008-01-29 13:42:48 +0000510 * and/or provoke expensive [not working] ldap/nis/...
Harald Weltecbe1ec72006-02-11 09:50:11 +0000511 * lookups */
512 if (!strcmp(s, "all"))
513 return 0;
514
Rusty Russell5eed48a2000-06-02 20:12:24 +0000515 if ((pent = getprotobyname(s)))
516 proto = pent->p_proto;
517 else {
518 unsigned int i;
519 for (i = 0;
520 i < sizeof(chain_protos)/sizeof(struct pprot);
521 i++) {
522 if (strcmp(s, chain_protos[i].name) == 0) {
523 proto = chain_protos[i].num;
524 break;
525 }
526 }
527 if (i == sizeof(chain_protos)/sizeof(struct pprot))
528 exit_error(PARAMETER_PROBLEM,
529 "unknown protocol `%s' specified",
530 s);
531 }
532 }
533
534 return (u_int16_t)proto;
535}
536
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000537/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000538static int is_exthdr(u_int16_t proto)
539{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000540 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000541 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000542 proto == IPPROTO_AH ||
543 proto == IPPROTO_DSTOPTS);
544}
545
Rusty Russell5eed48a2000-06-02 20:12:24 +0000546/* Can't be zero. */
547static int
548parse_rulenumber(const char *rule)
549{
Harald Welte43ac1912002-03-03 09:43:07 +0000550 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100552 if (!xtables_strtoui(rule, NULL, &rulenum, 1, INT_MAX))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000553 exit_error(PARAMETER_PROBLEM,
554 "Invalid rule number `%s'", rule);
555
556 return rulenum;
557}
558
559static const char *
560parse_target(const char *targetname)
561{
562 const char *ptr;
563
564 if (strlen(targetname) < 1)
565 exit_error(PARAMETER_PROBLEM,
566 "Invalid target name (too short)");
567
568 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
569 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000570 "Invalid target name `%s' (%u chars max)",
571 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000572
573 for (ptr = targetname; *ptr; ptr++)
574 if (isspace(*ptr))
575 exit_error(PARAMETER_PROBLEM,
576 "Invalid target name `%s'", targetname);
577 return targetname;
578}
579
Rusty Russell5eed48a2000-06-02 20:12:24 +0000580static void
581set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
582 int invert)
583{
584 if (*options & option)
585 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
586 opt2char(option));
587 *options |= option;
588
589 if (invert) {
590 unsigned int i;
591 for (i = 0; 1 << i != option; i++);
592
593 if (!inverse_for_options[i])
594 exit_error(PARAMETER_PROBLEM,
595 "cannot have ! before -%c",
596 opt2char(option));
597 *invflg |= inverse_for_options[i];
598 }
599}
600
Rusty Russell5eed48a2000-06-02 20:12:24 +0000601static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000602merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000603 unsigned int *option_offset)
604{
605 unsigned int num_old, num_new, i;
606 struct option *merge;
607
Jan Engelhardtd0145402007-07-30 14:32:26 +0000608 if (newopts == NULL)
609 return oldopts;
610
Rusty Russell5eed48a2000-06-02 20:12:24 +0000611 for (num_old = 0; oldopts[num_old].name; num_old++);
612 for (num_new = 0; newopts[num_new].name; num_new++);
613
614 global_option_offset += OPTION_OFFSET;
615 *option_offset = global_option_offset;
616
617 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
618 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000619 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000620 for (i = 0; i < num_new; i++) {
621 merge[num_old + i] = newopts[i];
622 merge[num_old + i].val += *option_offset;
623 }
624 memset(merge + num_old + num_new, 0, sizeof(struct option));
625
626 return merge;
627}
628
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000629static void
630print_num(u_int64_t number, unsigned int format)
631{
Harald Welte43ac1912002-03-03 09:43:07 +0000632 if (format & FMT_KILOMEGAGIGA) {
633 if (number > 99999) {
634 number = (number + 500) / 1000;
635 if (number > 9999) {
636 number = (number + 500) / 1000;
637 if (number > 9999) {
638 number = (number + 500) / 1000;
639 if (number > 9999) {
640 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000641 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000642 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000643 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000644 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000645 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000646 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000647 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000648 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000649 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000650 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000651 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000652}
653
Harald Welte43ac1912002-03-03 09:43:07 +0000654
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655static void
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100656print_header(unsigned int format, const char *chain, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000657{
658 struct ip6t_counters counters;
659 const char *pol = ip6tc_get_policy(chain, &counters, handle);
660 printf("Chain %s", chain);
661 if (pol) {
662 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000663 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000664 fputc(' ', stdout);
665 print_num(counters.pcnt, (format|FMT_NOTABLE));
666 fputs("packets, ", stdout);
667 print_num(counters.bcnt, (format|FMT_NOTABLE));
668 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000669 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000670 printf(")\n");
671 } else {
672 unsigned int refs;
673 if (!ip6tc_get_references(&refs, chain, handle))
674 printf(" (ERROR obtaining refs)\n");
675 else
676 printf(" (%u references)\n", refs);
677 }
678
679 if (format & FMT_LINENUMBERS)
680 printf(FMT("%-4s ", "%s "), "num");
681 if (!(format & FMT_NOCOUNTS)) {
682 if (format & FMT_KILOMEGAGIGA) {
683 printf(FMT("%5s ","%s "), "pkts");
684 printf(FMT("%5s ","%s "), "bytes");
685 } else {
686 printf(FMT("%8s ","%s "), "pkts");
687 printf(FMT("%10s ","%s "), "bytes");
688 }
689 }
690 if (!(format & FMT_NOTARGET))
691 printf(FMT("%-9s ","%s "), "target");
692 fputs(" prot ", stdout);
693 if (format & FMT_OPTIONS)
694 fputs("opt", stdout);
695 if (format & FMT_VIA) {
696 printf(FMT(" %-6s ","%s "), "in");
697 printf(FMT("%-6s ","%s "), "out");
698 }
699 printf(FMT(" %-19s ","%s "), "source");
700 printf(FMT(" %-19s "," %s "), "destination");
701 printf("\n");
702}
703
Harald Welte43ac1912002-03-03 09:43:07 +0000704
Rusty Russell5eed48a2000-06-02 20:12:24 +0000705static int
706print_match(const struct ip6t_entry_match *m,
707 const struct ip6t_ip6 *ip,
708 int numeric)
709{
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100710 struct xtables_match *match =
711 xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000712
713 if (match) {
714 if (match->print)
715 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +0000716 else
717 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000718 } else {
719 if (m->u.user.name[0])
720 printf("UNKNOWN match `%s' ", m->u.user.name);
721 }
722 /* Don't stop iterating. */
723 return 0;
724}
725
Jan Engelhardt6cf172e2008-03-10 17:48:59 +0100726/* e is called `fw' here for historical reasons */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000727static void
728print_firewall(const struct ip6t_entry *fw,
729 const char *targname,
730 unsigned int num,
731 unsigned int format,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100732 struct ip6tc_handle *const handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000733{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200734 struct xtables_target *target = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735 const struct ip6t_entry_target *t;
736 u_int8_t flags;
737 char buf[BUFSIZ];
738
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739 if (!ip6tc_is_chain(targname, handle))
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100740 target = xtables_find_target(targname, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000741 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100742 target = xtables_find_target(IP6T_STANDARD_TARGET,
743 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000744
745 t = ip6t_get_target((struct ip6t_entry *)fw);
746 flags = fw->ipv6.flags;
747
748 if (format & FMT_LINENUMBERS)
Henrik Nordstrom15641892008-06-13 17:57:35 +0200749 printf(FMT("%-4u ", "%u "), num);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000750
751 if (!(format & FMT_NOCOUNTS)) {
752 print_num(fw->counters.pcnt, format);
753 print_num(fw->counters.bcnt, format);
754 }
755
756 if (!(format & FMT_NOTARGET))
757 printf(FMT("%-9s ", "%s "), targname);
758
759 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
760 {
Harald Welte43ac1912002-03-03 09:43:07 +0000761 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000762 if (pname)
763 printf(FMT("%-5s", "%s "), pname);
764 else
765 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
766 }
767
768 if (format & FMT_OPTIONS) {
769 if (format & FMT_NOTABLE)
770 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +0000771 fputc(' ', stdout); /* Invert flag of FRAG */
772 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000773 fputc(' ', stdout);
774 }
775
776 if (format & FMT_VIA) {
777 char iface[IFNAMSIZ+2];
778
779 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
780 iface[0] = '!';
781 iface[1] = '\0';
782 }
783 else iface[0] = '\0';
784
785 if (fw->ipv6.iniface[0] != '\0') {
786 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000787 }
788 else if (format & FMT_NUMERIC) strcat(iface, "*");
789 else strcat(iface, "any");
790 printf(FMT(" %-6s ","in %s "), iface);
791
792 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
793 iface[0] = '!';
794 iface[1] = '\0';
795 }
796 else iface[0] = '\0';
797
798 if (fw->ipv6.outiface[0] != '\0') {
799 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 }
801 else if (format & FMT_NUMERIC) strcat(iface, "*");
802 else strcat(iface, "any");
803 printf(FMT("%-6s ","out %s "), iface);
804 }
805
806 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
Max Kellermann5b76f682008-01-29 13:42:48 +0000807 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808 && !(format & FMT_NUMERIC))
809 printf(FMT("%-19s ","%s "), "anywhere");
810 else {
811 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100812 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.src));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100814 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.src));
815 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.smsk));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000816 printf(FMT("%-19s ","%s "), buf);
817 }
818
819 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
820 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
821 && !(format & FMT_NUMERIC))
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200822 printf(FMT("%-19s ","-> %s"), "anywhere");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000823 else {
824 if (format & FMT_NUMERIC)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100825 strcpy(buf, xtables_ip6addr_to_numeric(&fw->ipv6.dst));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100827 strcpy(buf, xtables_ip6addr_to_anyname(&fw->ipv6.dst));
828 strcat(buf, xtables_ip6mask_to_numeric(&fw->ipv6.dmsk));
Jamie Strandboge69ceee52008-05-16 14:52:12 +0200829 printf(FMT("%-19s ","-> %s"), buf);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830 }
831
832 if (format & FMT_NOTABLE)
833 fputs(" ", stdout);
834
Thomas Jacobeaf831e2008-06-23 11:35:29 +0200835#ifdef IP6T_F_GOTO
836 if(fw->ipv6.flags & IP6T_F_GOTO)
837 printf("[goto] ");
838#endif
839
Rusty Russell5eed48a2000-06-02 20:12:24 +0000840 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
841
842 if (target) {
843 if (target->print)
844 /* Print the target information. */
845 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
846 } else if (t->u.target_size != sizeof(*t))
847 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +0000848 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849
850 if (!(format & FMT_NONEWLINE))
851 fputc('\n', stdout);
852}
853
854static void
855print_firewall_line(const struct ip6t_entry *fw,
Jan Engelhardtfd187312008-11-10 16:59:27 +0100856 struct ip6tc_handle *const h)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000857{
858 struct ip6t_entry_target *t;
859
860 t = ip6t_get_target((struct ip6t_entry *)fw);
861 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
862}
863
864static int
865append_entry(const ip6t_chainlabel chain,
866 struct ip6t_entry *fw,
867 unsigned int nsaddrs,
868 const struct in6_addr saddrs[],
869 unsigned int ndaddrs,
870 const struct in6_addr daddrs[],
871 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100872 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873{
874 unsigned int i, j;
875 int ret = 1;
876
877 for (i = 0; i < nsaddrs; i++) {
878 fw->ipv6.src = saddrs[i];
879 for (j = 0; j < ndaddrs; j++) {
880 fw->ipv6.dst = daddrs[j];
881 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100882 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000883 ret &= ip6tc_append_entry(chain, fw, handle);
884 }
885 }
886
887 return ret;
888}
889
890static int
891replace_entry(const ip6t_chainlabel chain,
892 struct ip6t_entry *fw,
893 unsigned int rulenum,
894 const struct in6_addr *saddr,
895 const struct in6_addr *daddr,
896 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100897 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898{
899 fw->ipv6.src = *saddr;
900 fw->ipv6.dst = *daddr;
901
902 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100903 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000904 return ip6tc_replace_entry(chain, fw, rulenum, handle);
905}
906
907static int
908insert_entry(const ip6t_chainlabel chain,
909 struct ip6t_entry *fw,
910 unsigned int rulenum,
911 unsigned int nsaddrs,
912 const struct in6_addr saddrs[],
913 unsigned int ndaddrs,
914 const struct in6_addr daddrs[],
915 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100916 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000917{
918 unsigned int i, j;
919 int ret = 1;
920
921 for (i = 0; i < nsaddrs; i++) {
922 fw->ipv6.src = saddrs[i];
923 for (j = 0; j < ndaddrs; j++) {
924 fw->ipv6.dst = daddrs[j];
925 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100926 print_firewall_line(fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000927 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
928 }
929 }
930
931 return ret;
932}
933
934static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000935make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000936{
937 /* Establish mask for comparison */
938 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000939 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000940 unsigned char *mask, *mptr;
941
942 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000943 for (matchp = matches; matchp; matchp = matchp->next)
944 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000945
Jan Engelhardt630ef482009-01-27 14:58:41 +0100946 mask = xtables_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +0000947 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000948 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000949
950 memset(mask, 0xFF, sizeof(struct ip6t_entry));
951 mptr = mask + sizeof(struct ip6t_entry);
952
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000953 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000954 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +0000955 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000956 + matchp->match->userspacesize);
957 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000958 }
959
Max Kellermann5b76f682008-01-29 13:42:48 +0000960 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000961 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000962 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000963
964 return mask;
965}
966
967static int
968delete_entry(const ip6t_chainlabel chain,
969 struct ip6t_entry *fw,
970 unsigned int nsaddrs,
971 const struct in6_addr saddrs[],
972 unsigned int ndaddrs,
973 const struct in6_addr daddrs[],
974 int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100975 struct ip6tc_handle *handle,
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000976 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977{
978 unsigned int i, j;
979 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 unsigned char *mask;
981
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000982 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000983 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000984 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000985 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +0000986 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000987 if (verbose)
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100988 print_firewall_line(fw, handle);
Philip Blundell57e07af2000-06-04 17:25:33 +0000989 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000990 }
991 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +0000992 free(mask);
993
Rusty Russell5eed48a2000-06-02 20:12:24 +0000994 return ret;
995}
996
András Kis-Szabó764316a2001-02-26 17:31:20 +0000997int
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100998for_each_chain(int (*fn)(const ip6t_chainlabel, int, struct ip6tc_handle *),
999 int verbose, int builtinstoo, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001000{
Max Kellermann5b76f682008-01-29 13:42:48 +00001001 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001002 const char *chain;
1003 char *chains;
1004 unsigned int i, chaincount = 0;
1005
1006 chain = ip6tc_first_chain(handle);
1007 while (chain) {
1008 chaincount++;
1009 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001010 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001011
Jan Engelhardt630ef482009-01-27 14:58:41 +01001012 chains = xtables_malloc(sizeof(ip6t_chainlabel) * chaincount);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001013 i = 0;
1014 chain = ip6tc_first_chain(handle);
1015 while (chain) {
1016 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1017 i++;
1018 chain = ip6tc_next_chain(handle);
Max Kellermann5b76f682008-01-29 13:42:48 +00001019 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001020
1021 for (i = 0; i < chaincount; i++) {
1022 if (!builtinstoo
1023 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001024 handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001025 continue;
Max Kellermann5b76f682008-01-29 13:42:48 +00001026 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027 }
1028
1029 free(chains);
Max Kellermann5b76f682008-01-29 13:42:48 +00001030 return ret;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001031}
1032
András Kis-Szabó764316a2001-02-26 17:31:20 +00001033int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001034flush_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001035 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001036{
1037 if (!chain)
1038 return for_each_chain(flush_entries, verbose, 1, handle);
1039
1040 if (verbose)
1041 fprintf(stdout, "Flushing chain `%s'\n", chain);
1042 return ip6tc_flush_entries(chain, handle);
1043}
1044
1045static int
1046zero_entries(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001047 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048{
1049 if (!chain)
1050 return for_each_chain(zero_entries, verbose, 1, handle);
1051
1052 if (verbose)
1053 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1054 return ip6tc_zero_entries(chain, handle);
1055}
1056
András Kis-Szabó764316a2001-02-26 17:31:20 +00001057int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001058delete_chain(const ip6t_chainlabel chain, int verbose,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001059 struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001060{
1061 if (!chain)
1062 return for_each_chain(delete_chain, verbose, 0, handle);
1063
1064 if (verbose)
Max Kellermann5b76f682008-01-29 13:42:48 +00001065 fprintf(stdout, "Deleting chain `%s'\n", chain);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001066 return ip6tc_delete_chain(chain, handle);
1067}
1068
1069static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001070list_entries(const ip6t_chainlabel chain, int rulenum, int verbose, int numeric,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001071 int expanded, int linenumbers, struct ip6tc_handle *handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001072{
1073 int found = 0;
1074 unsigned int format;
1075 const char *this;
1076
1077 format = FMT_OPTIONS;
1078 if (!verbose)
1079 format |= FMT_NOCOUNTS;
1080 else
1081 format |= FMT_VIA;
1082
1083 if (numeric)
1084 format |= FMT_NUMERIC;
1085
1086 if (!expanded)
1087 format |= FMT_KILOMEGAGIGA;
1088
1089 if (linenumbers)
1090 format |= FMT_LINENUMBERS;
1091
1092 for (this = ip6tc_first_chain(handle);
1093 this;
1094 this = ip6tc_next_chain(handle)) {
1095 const struct ip6t_entry *i;
1096 unsigned int num;
1097
1098 if (chain && strcmp(chain, this) != 0)
1099 continue;
1100
1101 if (found) printf("\n");
1102
Henrik Nordstrombb340822008-05-13 13:09:23 +02001103 if (!rulenum)
1104 print_header(format, this, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001105 i = ip6tc_first_rule(this, handle);
1106
1107 num = 0;
1108 while (i) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001109 num++;
1110 if (!rulenum || num == rulenum)
1111 print_firewall(i,
1112 ip6tc_get_target(i, handle),
1113 num,
1114 format,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001115 handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001116 i = ip6tc_next_rule(i, handle);
1117 }
1118 found = 1;
1119 }
1120
1121 errno = ENOENT;
1122 return found;
1123}
1124
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001125/* This assumes that mask is contiguous, and byte-bounded. */
1126static void
1127print_iface(char letter, const char *iface, const unsigned char *mask,
1128 int invert)
1129{
1130 unsigned int i;
1131
1132 if (mask[0] == 0)
1133 return;
1134
1135 printf("-%c %s", letter, invert ? "! " : "");
1136
1137 for (i = 0; i < IFNAMSIZ; i++) {
1138 if (mask[i] != 0) {
1139 if (iface[i] != '\0')
1140 printf("%c", iface[i]);
1141 } else {
1142 /* we can access iface[i-1] here, because
1143 * a few lines above we make sure that mask[0] != 0 */
1144 if (iface[i-1] != '\0')
1145 printf("+");
1146 break;
1147 }
1148 }
1149
1150 printf(" ");
1151}
1152
1153/* The ip6tables looks up the /etc/protocols. */
1154static void print_proto(u_int16_t proto, int invert)
1155{
1156 if (proto) {
1157 unsigned int i;
1158 const char *invertstr = invert ? "! " : "";
1159
1160 struct protoent *pent = getprotobynumber(proto);
1161 if (pent) {
1162 printf("-p %s%s ",
1163 invertstr, pent->p_name);
1164 return;
1165 }
1166
1167 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
1168 if (chain_protos[i].num == proto) {
1169 printf("-p %s%s ",
1170 invertstr, chain_protos[i].name);
1171 return;
1172 }
1173
1174 printf("-p %s%u ", invertstr, proto);
1175 }
1176}
1177
1178static int print_match_save(const struct ip6t_entry_match *e,
1179 const struct ip6t_ip6 *ip)
1180{
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001181 struct xtables_match *match =
1182 xtables_find_match(e->u.user.name, XTF_TRY_LOAD, NULL);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001183
1184 if (match) {
1185 printf("-m %s ", e->u.user.name);
1186
1187 /* some matches don't provide a save function */
1188 if (match->save)
1189 match->save(ip, e);
1190 } else {
1191 if (e->u.match_size) {
1192 fprintf(stderr,
1193 "Can't find library for match `%s'\n",
1194 e->u.user.name);
1195 exit(1);
1196 }
1197 }
1198 return 0;
1199}
1200
1201/* print a given ip including mask if neccessary */
1202static void print_ip(char *prefix, const struct in6_addr *ip, const struct in6_addr *mask, int invert)
1203{
1204 char buf[51];
1205 int l = ipv6_prefix_length(mask);
1206
1207 if (l == 0 && !invert)
1208 return;
1209
1210 printf("%s %s%s",
1211 prefix,
1212 invert ? "! " : "",
1213 inet_ntop(AF_INET6, ip, buf, sizeof buf));
1214
1215 if (l == -1)
1216 printf("/%s ", inet_ntop(AF_INET6, mask, buf, sizeof buf));
1217 else
1218 printf("/%d ", l);
1219}
1220
1221/* We want this to be readable, so only print out neccessary fields.
1222 * Because that's the kind of world I want to live in. */
1223void print_rule(const struct ip6t_entry *e,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001224 struct ip6tc_handle *h, const char *chain, int counters)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001225{
1226 struct ip6t_entry_target *t;
1227 const char *target_name;
1228
1229 /* print counters for iptables-save */
1230 if (counters > 0)
1231 printf("[%llu:%llu] ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1232
1233 /* print chain name */
1234 printf("-A %s ", chain);
1235
1236 /* Print IP part. */
1237 print_ip("-s", &(e->ipv6.src), &(e->ipv6.smsk),
1238 e->ipv6.invflags & IP6T_INV_SRCIP);
1239
1240 print_ip("-d", &(e->ipv6.dst), &(e->ipv6.dmsk),
1241 e->ipv6.invflags & IP6T_INV_DSTIP);
1242
1243 print_iface('i', e->ipv6.iniface, e->ipv6.iniface_mask,
1244 e->ipv6.invflags & IP6T_INV_VIA_IN);
1245
1246 print_iface('o', e->ipv6.outiface, e->ipv6.outiface_mask,
1247 e->ipv6.invflags & IP6T_INV_VIA_OUT);
1248
1249 print_proto(e->ipv6.proto, e->ipv6.invflags & IP6T_INV_PROTO);
1250
1251#if 0
1252 /* not definied in ipv6
1253 * FIXME: linux/netfilter_ipv6/ip6_tables: IP6T_INV_FRAG why definied? */
1254 if (e->ipv6.flags & IPT_F_FRAG)
1255 printf("%s-f ",
1256 e->ipv6.invflags & IP6T_INV_FRAG ? "! " : "");
1257#endif
1258
1259 if (e->ipv6.flags & IP6T_F_TOS)
1260 printf("%s-? %d ",
1261 e->ipv6.invflags & IP6T_INV_TOS ? "! " : "",
1262 e->ipv6.tos);
1263
1264 /* Print matchinfo part */
1265 if (e->target_offset) {
1266 IP6T_MATCH_ITERATE(e, print_match_save, &e->ipv6);
1267 }
1268
1269 /* print counters for iptables -R */
1270 if (counters < 0)
1271 printf("-c %llu %llu ", (unsigned long long)e->counters.pcnt, (unsigned long long)e->counters.bcnt);
1272
1273 /* Print target name */
1274 target_name = ip6tc_get_target(e, h);
1275 if (target_name && (*target_name != '\0'))
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001276#ifdef IP6T_F_GOTO
1277 printf("-%c %s ", e->ipv6.flags & IP6T_F_GOTO ? 'g' : 'j', target_name);
1278#else
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001279 printf("-j %s ", target_name);
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001280#endif
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001281
1282 /* Print targinfo part */
1283 t = ip6t_get_target((struct ip6t_entry *)e);
1284 if (t->u.user.name[0]) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001285 struct xtables_target *target =
1286 xtables_find_target(t->u.user.name, XTF_TRY_LOAD);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001287
1288 if (!target) {
1289 fprintf(stderr, "Can't find library for target `%s'\n",
1290 t->u.user.name);
1291 exit(1);
1292 }
1293
1294 if (target->save)
1295 target->save(&e->ipv6, t);
1296 else {
1297 /* If the target size is greater than ip6t_entry_target
1298 * there is something to be saved, we just don't know
1299 * how to print it */
1300 if (t->u.target_size !=
1301 sizeof(struct ip6t_entry_target)) {
1302 fprintf(stderr, "Target `%s' is missing "
1303 "save function\n",
1304 t->u.user.name);
1305 exit(1);
1306 }
1307 }
1308 }
1309 printf("\n");
1310}
1311
1312static int
Henrik Nordstrombb340822008-05-13 13:09:23 +02001313list_rules(const ip6t_chainlabel chain, int rulenum, int counters,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001314 struct ip6tc_handle *handle)
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001315{
1316 const char *this = NULL;
1317 int found = 0;
1318
1319 if (counters)
1320 counters = -1; /* iptables -c format */
1321
1322 /* Dump out chain names first,
1323 * thereby preventing dependency conflicts */
Henrik Nordstrombb340822008-05-13 13:09:23 +02001324 if (!rulenum) for (this = ip6tc_first_chain(handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001325 this;
1326 this = ip6tc_next_chain(handle)) {
1327 if (chain && strcmp(this, chain) != 0)
1328 continue;
1329
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01001330 if (ip6tc_builtin(this, handle)) {
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001331 struct ip6t_counters count;
1332 printf("-P %s %s", this, ip6tc_get_policy(this, &count, handle));
1333 if (counters)
1334 printf(" -c %llu %llu", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
1335 printf("\n");
1336 } else {
1337 printf("-N %s\n", this);
1338 }
1339 }
1340
1341 for (this = ip6tc_first_chain(handle);
1342 this;
1343 this = ip6tc_next_chain(handle)) {
1344 const struct ip6t_entry *e;
Henrik Nordstrombb340822008-05-13 13:09:23 +02001345 int num = 0;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001346
1347 if (chain && strcmp(this, chain) != 0)
1348 continue;
1349
1350 /* Dump out rules */
1351 e = ip6tc_first_rule(this, handle);
1352 while(e) {
Henrik Nordstrombb340822008-05-13 13:09:23 +02001353 num++;
1354 if (!rulenum || num == rulenum)
1355 print_rule(e, handle, this, counters);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001356 e = ip6tc_next_rule(e, handle);
1357 }
1358 found = 1;
1359 }
1360
1361 errno = ENOENT;
1362 return found;
1363}
1364
Rusty Russell5eed48a2000-06-02 20:12:24 +00001365static struct ip6t_entry *
1366generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001367 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001368 struct ip6t_entry_target *target)
1369{
1370 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001371 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001372 struct ip6t_entry *e;
1373
1374 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001375 for (matchp = matches; matchp; matchp = matchp->next)
1376 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001377
Jan Engelhardt630ef482009-01-27 14:58:41 +01001378 e = xtables_malloc(size + target->u.target_size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001379 *e = *fw;
1380 e->target_offset = size;
1381 e->next_offset = size + target->u.target_size;
1382
1383 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001384 for (matchp = matches; matchp; matchp = matchp->next) {
1385 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1386 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387 }
1388 memcpy(e->elems + size, target, target->u.target_size);
1389
1390 return e;
1391}
1392
Jan Engelhardt33690a12008-02-11 00:54:00 +01001393static void clear_rule_matches(struct ip6tables_rule_match **matches)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001394{
1395 struct ip6tables_rule_match *matchp, *tmp;
1396
1397 for (matchp = *matches; matchp;) {
1398 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001399 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001400 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001401 matchp->match->m = NULL;
1402 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001403 if (matchp->match == matchp->match->next) {
1404 free(matchp->match);
1405 matchp->match = NULL;
1406 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001407 free(matchp);
1408 matchp = tmp;
1409 }
1410
1411 *matches = NULL;
1412}
1413
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001414static void set_revision(char *name, u_int8_t revision)
1415{
1416 /* Old kernel sources don't have ".revision" field,
1417 but we stole a byte from name. */
1418 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1419 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1420}
1421
Jan Engelhardtfd187312008-11-10 16:59:27 +01001422int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423{
1424 struct ip6t_entry fw, *e = NULL;
1425 int invert = 0;
1426 unsigned int nsaddrs = 0, ndaddrs = 0;
1427 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1428
1429 int c, verbose = 0;
Max Kellermann9ee386a2008-01-29 13:48:05 +00001430 unsigned i;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 const char *chain = NULL;
1432 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1433 const char *policy = NULL, *newname = NULL;
1434 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001435 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001436 int ret = 1;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001437 struct xtables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001438 struct ip6tables_rule_match *matches = NULL;
1439 struct ip6tables_rule_match *matchp;
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +02001440 struct xtables_target *target = NULL;
1441 struct xtables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001442 const char *jumpto = "";
1443 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001444 int proto_used = 0;
Patrick McHardy875441e2007-10-17 08:48:58 +00001445 unsigned long long cnt;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001446
1447 memset(&fw, 0, sizeof(fw));
1448
Harald Welte43ac1912002-03-03 09:43:07 +00001449 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001450 * a second time */
1451 optind = 0;
1452
Harald Welte43ac1912002-03-03 09:43:07 +00001453 /* clear mflags in case do_command gets called a second time
1454 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001455 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001456 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001457
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001458 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001459 t->tflags = 0;
1460 t->used = 0;
1461 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001462
Rusty Russell5eed48a2000-06-02 20:12:24 +00001463 /* Suppress error messages: we may add new options if we
1464 demand-load a protocol. */
1465 opterr = 0;
1466
1467 while ((c = getopt_long(argc, argv,
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001468 "-A:D:R:I:L::S::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469 opts, NULL)) != -1) {
1470 switch (c) {
1471 /*
1472 * Command selection
1473 */
1474 case 'A':
1475 add_command(&command, CMD_APPEND, CMD_NONE,
1476 invert);
1477 chain = optarg;
1478 break;
1479
1480 case 'D':
1481 add_command(&command, CMD_DELETE, CMD_NONE,
1482 invert);
1483 chain = optarg;
1484 if (optind < argc && argv[optind][0] != '-'
1485 && argv[optind][0] != '!') {
1486 rulenum = parse_rulenumber(argv[optind++]);
1487 command = CMD_DELETE_NUM;
1488 }
1489 break;
1490
Rusty Russell5eed48a2000-06-02 20:12:24 +00001491 case 'R':
1492 add_command(&command, CMD_REPLACE, CMD_NONE,
1493 invert);
1494 chain = optarg;
1495 if (optind < argc && argv[optind][0] != '-'
1496 && argv[optind][0] != '!')
1497 rulenum = parse_rulenumber(argv[optind++]);
1498 else
1499 exit_error(PARAMETER_PROBLEM,
1500 "-%c requires a rule number",
1501 cmd2char(CMD_REPLACE));
1502 break;
1503
1504 case 'I':
1505 add_command(&command, CMD_INSERT, CMD_NONE,
1506 invert);
1507 chain = optarg;
1508 if (optind < argc && argv[optind][0] != '-'
1509 && argv[optind][0] != '!')
1510 rulenum = parse_rulenumber(argv[optind++]);
1511 else rulenum = 1;
1512 break;
1513
1514 case 'L':
1515 add_command(&command, CMD_LIST, CMD_ZERO,
1516 invert);
1517 if (optarg) chain = optarg;
1518 else if (optind < argc && argv[optind][0] != '-'
1519 && argv[optind][0] != '!')
1520 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001521 if (optind < argc && argv[optind][0] != '-'
1522 && argv[optind][0] != '!')
1523 rulenum = parse_rulenumber(argv[optind++]);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001524 break;
1525
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001526 case 'S':
1527 add_command(&command, CMD_LIST_RULES, CMD_ZERO,
1528 invert);
1529 if (optarg) chain = optarg;
1530 else if (optind < argc && argv[optind][0] != '-'
1531 && argv[optind][0] != '!')
1532 chain = argv[optind++];
Henrik Nordstrombb340822008-05-13 13:09:23 +02001533 if (optind < argc && argv[optind][0] != '-'
1534 && argv[optind][0] != '!')
1535 rulenum = parse_rulenumber(argv[optind++]);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001536 break;
1537
Rusty Russell5eed48a2000-06-02 20:12:24 +00001538 case 'F':
1539 add_command(&command, CMD_FLUSH, CMD_NONE,
1540 invert);
1541 if (optarg) chain = optarg;
1542 else if (optind < argc && argv[optind][0] != '-'
1543 && argv[optind][0] != '!')
1544 chain = argv[optind++];
1545 break;
1546
1547 case 'Z':
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02001548 add_command(&command, CMD_ZERO, CMD_LIST|CMD_LIST_RULES,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001549 invert);
1550 if (optarg) chain = optarg;
1551 else if (optind < argc && argv[optind][0] != '-'
1552 && argv[optind][0] != '!')
1553 chain = argv[optind++];
1554 break;
1555
1556 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001557 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001558 exit_error(PARAMETER_PROBLEM,
1559 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001560 "with `%c'\n", *optarg);
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001561 if (xtables_find_target(optarg, XTF_TRY_LOAD))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001562 exit_error(PARAMETER_PROBLEM,
1563 "chain name may not clash "
1564 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001565 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1566 invert);
1567 chain = optarg;
1568 break;
1569
1570 case 'X':
1571 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1572 invert);
1573 if (optarg) chain = optarg;
1574 else if (optind < argc && argv[optind][0] != '-'
1575 && argv[optind][0] != '!')
1576 chain = argv[optind++];
1577 break;
1578
1579 case 'E':
1580 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1581 invert);
1582 chain = optarg;
1583 if (optind < argc && argv[optind][0] != '-'
1584 && argv[optind][0] != '!')
1585 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001586 else
1587 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001588 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001589 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001590 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001591 break;
1592
1593 case 'P':
1594 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1595 invert);
1596 chain = optarg;
1597 if (optind < argc && argv[optind][0] != '-'
1598 && argv[optind][0] != '!')
1599 policy = argv[optind++];
1600 else
1601 exit_error(PARAMETER_PROBLEM,
1602 "-%c requires a chain and a policy",
1603 cmd2char(CMD_SET_POLICY));
1604 break;
1605
1606 case 'h':
1607 if (!optarg)
1608 optarg = argv[optind];
1609
Jonas Berlin1b91e592005-04-01 06:58:38 +00001610 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001611 if (!matches && protocol)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001612 xtables_find_match(protocol, XTF_TRY_LOAD,
1613 &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001614
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001615 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001616
1617 /*
1618 * Option selection
1619 */
1620 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001621 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1623 invert);
1624
1625 /* Canonicalize into lower case */
1626 for (protocol = argv[optind-1]; *protocol; protocol++)
1627 *protocol = tolower(*protocol);
1628
1629 protocol = argv[optind-1];
1630 fw.ipv6.proto = parse_protocol(protocol);
1631 fw.ipv6.flags |= IP6T_F_PROTO;
1632
1633 if (fw.ipv6.proto == 0
1634 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1635 exit_error(PARAMETER_PROBLEM,
1636 "rule would never match protocol");
Max Kellermann5b76f682008-01-29 13:42:48 +00001637
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001638 if (is_exthdr(fw.ipv6.proto)
1639 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Max Kellermannaae4f822007-10-17 16:36:49 +00001640 fprintf(stderr,
1641 "Warning: never matched protocol: %s. "
1642 "use extension match instead.\n",
1643 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001644 break;
1645
1646 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001647 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1649 invert);
1650 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001651 break;
1652
1653 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001654 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001655 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1656 invert);
1657 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 break;
1659
Thomas Jacobeaf831e2008-06-23 11:35:29 +02001660#ifdef IP6T_F_GOTO
1661 case 'g':
1662 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1663 invert);
1664 fw.ipv6.flags |= IP6T_F_GOTO;
1665 jumpto = parse_target(optarg);
1666 break;
1667#endif
1668
Rusty Russell5eed48a2000-06-02 20:12:24 +00001669 case 'j':
1670 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1671 invert);
1672 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001673 /* TRY_LOAD (may be chain name) */
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001674 target = xtables_find_target(jumpto, XTF_TRY_LOAD);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001675
1676 if (target) {
1677 size_t size;
1678
Harald Welte43ac1912002-03-03 09:43:07 +00001679 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1680 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001681
Jan Engelhardt630ef482009-01-27 14:58:41 +01001682 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683 target->t->u.target_size = size;
1684 strcpy(target->t->u.user.name, jumpto);
Patrick McHardy455559b2008-04-15 15:51:19 +02001685 set_revision(target->t->u.user.name,
1686 target->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001687 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001688 target->init(target->t);
Patrick McHardy455559b2008-04-15 15:51:19 +02001689 opts = merge_options(opts,
1690 target->extra_opts,
1691 &target->option_offset);
1692 if (opts == NULL)
1693 exit_error(OTHER_PROBLEM,
1694 "can't alloc memory!");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001695 }
1696 break;
1697
1698
1699 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001700 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001701 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1702 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001703 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704 fw.ipv6.iniface,
1705 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001706 break;
1707
1708 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001709 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001710 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1711 invert);
Jan Engelhardtaae6be92009-01-30 04:24:47 +01001712 xtables_parse_interface(argv[optind-1],
Rusty Russell5eed48a2000-06-02 20:12:24 +00001713 fw.ipv6.outiface,
1714 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 break;
1716
1717 case 'v':
1718 if (!verbose)
1719 set_option(&options, OPT_VERBOSE,
1720 &fw.ipv6.invflags, invert);
1721 verbose++;
1722 break;
1723
1724 case 'm': {
1725 size_t size;
1726
1727 if (invert)
1728 exit_error(PARAMETER_PROBLEM,
1729 "unexpected ! flag before --match");
1730
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001731 m = xtables_find_match(optarg, XTF_LOAD_MUST_SUCCEED,
1732 &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001733 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1734 + m->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01001735 m->m = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001736 m->m->u.match_size = size;
1737 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001738 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001739 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001740 m->init(m->m);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001741 if (m != m->next)
1742 /* Merge options for non-cloned matches */
1743 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001744 }
1745 break;
1746
1747 case 'n':
1748 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1749 invert);
1750 break;
1751
1752 case 't':
1753 if (invert)
1754 exit_error(PARAMETER_PROBLEM,
1755 "unexpected ! flag before --table");
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001756 *table = optarg;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001757 break;
1758
1759 case 'x':
1760 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1761 invert);
1762 break;
1763
1764 case 'V':
1765 if (invert)
1766 printf("Not %s ;-)\n", program_version);
1767 else
1768 printf("%s v%s\n",
1769 program_name, program_version);
1770 exit(0);
1771
1772 case '0':
1773 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1774 invert);
1775 break;
1776
Harald Welte43ac1912002-03-03 09:43:07 +00001777 case 'M':
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001778 xtables_modprobe_program = optarg;
Harald Welte43ac1912002-03-03 09:43:07 +00001779 break;
1780
1781 case 'c':
1782
1783 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1784 invert);
1785 pcnt = optarg;
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001786 bcnt = strchr(pcnt + 1, ',');
1787 if (bcnt)
1788 bcnt++;
1789 if (!bcnt && optind < argc && argv[optind][0] != '-'
Harald Welte43ac1912002-03-03 09:43:07 +00001790 && argv[optind][0] != '!')
1791 bcnt = argv[optind++];
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001792 if (!bcnt)
Harald Welte43ac1912002-03-03 09:43:07 +00001793 exit_error(PARAMETER_PROBLEM,
1794 "-%c requires packet and byte counter",
1795 opt2char(OPT_COUNTERS));
1796
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001797 if (sscanf(pcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001798 exit_error(PARAMETER_PROBLEM,
1799 "-%c packet counter not numeric",
1800 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001801 fw.counters.pcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001802
Henrik Nordstrom60a60732008-05-13 13:10:38 +02001803 if (sscanf(bcnt, "%llu", &cnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001804 exit_error(PARAMETER_PROBLEM,
1805 "-%c byte counter not numeric",
1806 opt2char(OPT_COUNTERS));
Patrick McHardy875441e2007-10-17 08:48:58 +00001807 fw.counters.bcnt = cnt;
Harald Welte43ac1912002-03-03 09:43:07 +00001808 break;
1809
Rusty Russell5eed48a2000-06-02 20:12:24 +00001810 case 1: /* non option */
1811 if (optarg[0] == '!' && optarg[1] == '\0') {
1812 if (invert)
1813 exit_error(PARAMETER_PROBLEM,
1814 "multiple consecutive ! not"
1815 " allowed");
1816 invert = TRUE;
1817 optarg[0] = '\0';
1818 continue;
1819 }
Max Kellermannaae4f822007-10-17 16:36:49 +00001820 fprintf(stderr, "Bad argument `%s'\n", optarg);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001821 exit_tryhelp(2);
1822
1823 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001824 if (!target
1825 || !(target->parse(c - target->option_offset,
1826 argv, invert,
1827 &target->tflags,
1828 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001829 for (matchp = matches; matchp; matchp = matchp->next) {
Max Kellermann5b76f682008-01-29 13:42:48 +00001830 if (matchp->completed)
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001831 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001832 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001833 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001834 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001835 &fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001836 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001837 break;
1838 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001839 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001840
1841 /* If you listen carefully, you can
1842 actually hear this code suck. */
1843
1844 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001845 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001846 * parameter, that has not been parsed yet,
1847 * it's not an option of an explicitly loaded
1848 * match or a target. However, we support
1849 * implicit loading of the protocol match
1850 * extension. '-p tcp' means 'l4 proto 6' and
1851 * at the same time 'load tcp protocol match on
1852 * demand if we specify --dport'.
1853 *
1854 * To make this work, we need to make sure:
1855 * - the parameter has not been parsed by
1856 * a match (m above)
1857 * - a protocol has been specified
1858 * - the protocol extension has not been
1859 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001860 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001861 * - the protocol extension can be successively
1862 * loaded
1863 */
1864 if (m == NULL
1865 && protocol
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001866 && (!find_proto(protocol, XTF_DONT_LOAD,
Max Kellermann5b76f682008-01-29 13:42:48 +00001867 options&OPT_NUMERIC, NULL)
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001868 || (find_proto(protocol, XTF_DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001869 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001870 && (proto_used == 0))
1871 )
Jan Engelhardt2338efd2009-01-27 15:23:01 +01001872 && (m = find_proto(protocol, XTF_TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001873 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001874 /* Try loading protocol */
1875 size_t size;
Max Kellermann5b76f682008-01-29 13:42:48 +00001876
Harald Welte00f5a9c2002-08-26 14:37:35 +00001877 proto_used = 1;
1878
1879 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1880 + m->size;
1881
Jan Engelhardt630ef482009-01-27 14:58:41 +01001882 m->m = xtables_calloc(1, size);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001883 m->m->u.match_size = size;
1884 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001885 set_revision(m->m->u.user.name,
1886 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001887 if (m->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00001888 m->init(m->m);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001889
1890 opts = merge_options(opts,
1891 m->extra_opts, &m->option_offset);
1892
1893 optind--;
1894 continue;
1895 }
1896
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001897 if (!m) {
1898 if (c == '?') {
1899 if (optopt) {
1900 exit_error(
1901 PARAMETER_PROBLEM,
1902 "option `%s' "
1903 "requires an "
1904 "argument",
1905 argv[optind-1]);
1906 } else {
1907 exit_error(
1908 PARAMETER_PROBLEM,
1909 "unknown option "
1910 "`%s'",
1911 argv[optind-1]);
1912 }
1913 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001914 exit_error(PARAMETER_PROBLEM,
Jan Engelhardtd0cbf5f2008-08-04 12:51:01 +02001915 "Unknown arg `%s'", optarg);
Pablo Neira Ayuso0e6b7d32008-11-19 19:01:26 +01001916 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001917 }
1918 }
1919 invert = FALSE;
1920 }
1921
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001922 for (matchp = matches; matchp; matchp = matchp->next)
Jan Engelhardt830132a2007-10-04 16:24:50 +00001923 if (matchp->match->final_check != NULL)
1924 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001925
Jan Engelhardt830132a2007-10-04 16:24:50 +00001926 if (target != NULL && target->final_check != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001927 target->final_check(target->tflags);
1928
1929 /* Fix me: must put inverse options checking here --MN */
1930
1931 if (optind < argc)
1932 exit_error(PARAMETER_PROBLEM,
1933 "unknown arguments found on commandline");
1934 if (!command)
1935 exit_error(PARAMETER_PROBLEM, "no command specified");
1936 if (invert)
1937 exit_error(PARAMETER_PROBLEM,
1938 "nothing appropriate following !");
1939
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001940 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001942 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001943 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001944 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001945 }
1946
1947 if (shostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001948 ip6parse_hostnetworkmask(shostnetworkmask, &saddrs,
1949 &fw.ipv6.smsk, &nsaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001950
1951 if (dhostnetworkmask)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001952 ip6parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1953 &fw.ipv6.dmsk, &ndaddrs);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001954
1955 if ((nsaddrs > 1 || ndaddrs > 1) &&
1956 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1957 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1958 " source or destination IP addresses");
1959
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1961 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1962 "specify a unique address");
1963
1964 generic_opt_check(command, options);
1965
1966 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1967 exit_error(PARAMETER_PROBLEM,
1968 "chain name `%s' too long (must be under %i chars)",
1969 chain, IP6T_FUNCTION_MAXNAMELEN);
1970
Harald Welte43ac1912002-03-03 09:43:07 +00001971 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001972 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001973 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001974
Rusty Russell8beb0492004-12-22 00:37:10 +00001975 /* try to insmod the module if iptc_init failed */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +01001976 if (!*handle && xtables_load_ko(xtables_modprobe_program, false) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001977 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001978
Harald Welte43ac1912002-03-03 09:43:07 +00001979 if (!*handle)
1980 exit_error(VERSION_PROBLEM,
1981 "can't initialize ip6tables table `%s': %s",
1982 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001983
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001984 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985 || command == CMD_DELETE
1986 || command == CMD_INSERT
1987 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00001988 if (strcmp(chain, "PREROUTING") == 0
1989 || strcmp(chain, "INPUT") == 0) {
1990 /* -o not valid with incoming packets. */
1991 if (options & OPT_VIANAMEOUT)
1992 exit_error(PARAMETER_PROBLEM,
1993 "Can't use -%c with %s\n",
1994 opt2char(OPT_VIANAMEOUT),
1995 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00001996 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001997
Harald Welte43ac1912002-03-03 09:43:07 +00001998 if (strcmp(chain, "POSTROUTING") == 0
1999 || strcmp(chain, "OUTPUT") == 0) {
2000 /* -i not valid with outgoing packets */
2001 if (options & OPT_VIANAMEIN)
2002 exit_error(PARAMETER_PROBLEM,
2003 "Can't use -%c with %s\n",
2004 opt2char(OPT_VIANAMEIN),
2005 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002006 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002007
2008 if (target && ip6tc_is_chain(jumpto, *handle)) {
Max Kellermannaae4f822007-10-17 16:36:49 +00002009 fprintf(stderr,
2010 "Warning: using chain %s, not extension\n",
2011 jumpto);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002012
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002013 if (target->t)
2014 free(target->t);
2015
Rusty Russell5eed48a2000-06-02 20:12:24 +00002016 target = NULL;
2017 }
2018
2019 /* If they didn't specify a target, or it's a chain
2020 name, use standard. */
2021 if (!target
2022 && (strlen(jumpto) == 0
2023 || ip6tc_is_chain(jumpto, *handle))) {
2024 size_t size;
2025
Jan Engelhardt2338efd2009-01-27 15:23:01 +01002026 target = xtables_find_target(IP6T_STANDARD_TARGET,
2027 XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002028
2029 size = sizeof(struct ip6t_entry_target)
2030 + target->size;
Jan Engelhardt630ef482009-01-27 14:58:41 +01002031 target->t = xtables_calloc(1, size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002032 target->t->u.target_size = size;
2033 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002034 if (target->init != NULL)
Peter Rileyea146a92007-09-02 13:09:07 +00002035 target->init(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002036 }
2037
2038 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002039 /* it is no chain, and we can't load a plugin.
2040 * We cannot know if the plugin is corrupt, non
2041 * existant OR if the user just misspelled a
2042 * chain. */
Thomas Jacobeaf831e2008-06-23 11:35:29 +02002043#ifdef IP6T_F_GOTO
2044 if (fw.ipv6.flags & IP6T_F_GOTO)
2045 exit_error(PARAMETER_PROBLEM,
2046 "goto '%s' is not a chain\n", jumpto);
2047#endif
Jan Engelhardt2338efd2009-01-27 15:23:01 +01002048 xtables_find_target(jumpto, XTF_LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002050 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002051 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002052 }
2053 }
2054
2055 switch (command) {
2056 case CMD_APPEND:
2057 ret = append_entry(chain, e,
2058 nsaddrs, saddrs, ndaddrs, daddrs,
2059 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002060 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002061 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002062 case CMD_DELETE:
2063 ret = delete_entry(chain, e,
2064 nsaddrs, saddrs, ndaddrs, daddrs,
2065 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002066 *handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002067 break;
2068 case CMD_DELETE_NUM:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002069 ret = ip6tc_delete_num_entry(chain, rulenum - 1, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002070 break;
2071 case CMD_REPLACE:
2072 ret = replace_entry(chain, e, rulenum - 1,
2073 saddrs, daddrs, options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002074 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002075 break;
2076 case CMD_INSERT:
2077 ret = insert_entry(chain, e, rulenum - 1,
2078 nsaddrs, saddrs, ndaddrs, daddrs,
2079 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002080 *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002081 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002082 case CMD_FLUSH:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002083 ret = flush_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002084 break;
2085 case CMD_ZERO:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002086 ret = zero_entries(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002087 break;
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002088 case CMD_LIST:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002089 case CMD_LIST|CMD_ZERO:
2090 ret = list_entries(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002091 rulenum,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002092 options&OPT_VERBOSE,
2093 options&OPT_NUMERIC,
2094 options&OPT_EXPANDED,
2095 options&OPT_LINENUMBERS,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002096 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002097 if (ret && (command & CMD_ZERO))
2098 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002099 options&OPT_VERBOSE, *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002100 break;
2101 case CMD_LIST_RULES:
2102 case CMD_LIST_RULES|CMD_ZERO:
2103 ret = list_rules(chain,
Henrik Nordstrombb340822008-05-13 13:09:23 +02002104 rulenum,
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002105 options&OPT_VERBOSE,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002106 *handle);
Henrik Nordstrom96296cf2008-05-13 13:08:26 +02002107 if (ret && (command & CMD_ZERO))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002108 ret = zero_entries(chain,
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002109 options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002110 break;
2111 case CMD_NEW_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002112 ret = ip6tc_create_chain(chain, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002113 break;
2114 case CMD_DELETE_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002115 ret = delete_chain(chain, options&OPT_VERBOSE, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002116 break;
2117 case CMD_RENAME_CHAIN:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002118 ret = ip6tc_rename_chain(chain, newname, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002119 break;
2120 case CMD_SET_POLICY:
Jan Engelhardt1c9015b2008-11-10 17:00:41 +01002121 ret = ip6tc_set_policy(chain, policy, options&OPT_COUNTERS ? &fw.counters : NULL, *handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002122 break;
2123 default:
2124 /* We should never reach this... */
2125 exit_tryhelp(2);
2126 }
2127
2128 if (verbose > 1)
2129 dump_entries6(*handle);
2130
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002131 clear_rule_matches(&matches);
2132
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002133 if (e != NULL) {
2134 free(e);
2135 e = NULL;
2136 }
2137
Max Kellermann9ee386a2008-01-29 13:48:05 +00002138 for (i = 0; i < nsaddrs; i++)
2139 free(&saddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002140
Max Kellermann9ee386a2008-01-29 13:48:05 +00002141 for (i = 0; i < ndaddrs; i++)
2142 free(&daddrs[i]);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002143
Pablo Neiradfdcd642005-05-29 19:05:23 +00002144 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002145
Rusty Russell5eed48a2000-06-02 20:12:24 +00002146 return ret;
2147}