blob: d3b80cf47879c59e631ddfbad343f1e20ba9a4d4 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000034#include <ctype.h>
35#include <stdarg.h>
36#include <limits.h>
37#include <ip6tables.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000038#include <xtables.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000039#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000042#include <sys/types.h>
43#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000044
45#ifndef TRUE
46#define TRUE 1
47#endif
48#ifndef FALSE
49#define FALSE 0
50#endif
51
Rusty Russell5eed48a2000-06-02 20:12:24 +000052#define FMT_NUMERIC 0x0001
53#define FMT_NOCOUNTS 0x0002
54#define FMT_KILOMEGAGIGA 0x0004
55#define FMT_OPTIONS 0x0008
56#define FMT_NOTABLE 0x0010
57#define FMT_NOTARGET 0x0020
58#define FMT_VIA 0x0040
59#define FMT_NONEWLINE 0x0080
60#define FMT_LINENUMBERS 0x0100
61
62#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
63 | FMT_NUMERIC | FMT_NOTABLE)
64#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
65
66
67#define CMD_NONE 0x0000U
68#define CMD_INSERT 0x0001U
69#define CMD_DELETE 0x0002U
70#define CMD_DELETE_NUM 0x0004U
71#define CMD_REPLACE 0x0008U
72#define CMD_APPEND 0x0010U
73#define CMD_LIST 0x0020U
74#define CMD_FLUSH 0x0040U
75#define CMD_ZERO 0x0080U
76#define CMD_NEW_CHAIN 0x0100U
77#define CMD_DELETE_CHAIN 0x0200U
78#define CMD_SET_POLICY 0x0400U
Harald Welte0eca33f2006-04-21 11:56:30 +000079#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000080#define NUMBER_OF_CMD 13
81static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000082 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000083
84#define OPTION_OFFSET 256
85
86#define OPT_NONE 0x00000U
87#define OPT_NUMERIC 0x00001U
88#define OPT_SOURCE 0x00002U
89#define OPT_DESTINATION 0x00004U
90#define OPT_PROTOCOL 0x00008U
91#define OPT_JUMP 0x00010U
92#define OPT_VERBOSE 0x00020U
93#define OPT_EXPANDED 0x00040U
94#define OPT_VIANAMEIN 0x00080U
95#define OPT_VIANAMEOUT 0x00100U
96#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +000097#define OPT_COUNTERS 0x00400U
98#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +000099static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000100= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000101
102static struct option original_opts[] = {
103 { "append", 1, 0, 'A' },
104 { "delete", 1, 0, 'D' },
105 { "insert", 1, 0, 'I' },
106 { "replace", 1, 0, 'R' },
107 { "list", 2, 0, 'L' },
108 { "flush", 2, 0, 'F' },
109 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000110 { "new-chain", 1, 0, 'N' },
111 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000112 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000113 { "policy", 1, 0, 'P' },
114 { "source", 1, 0, 's' },
115 { "destination", 1, 0, 'd' },
116 { "src", 1, 0, 's' }, /* synonym */
117 { "dst", 1, 0, 'd' }, /* synonym */
118 { "protocol", 1, 0, 'p' },
119 { "in-interface", 1, 0, 'i' },
120 { "jump", 1, 0, 'j' },
121 { "table", 1, 0, 't' },
122 { "match", 1, 0, 'm' },
123 { "numeric", 0, 0, 'n' },
124 { "out-interface", 1, 0, 'o' },
125 { "verbose", 0, 0, 'v' },
126 { "exact", 0, 0, 'x' },
127 { "version", 0, 0, 'V' },
128 { "help", 2, 0, 'h' },
129 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000130 { "modprobe", 1, 0, 'M' },
131 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000132 { 0 }
133};
134
Harald Weltea8658ca2003-03-05 07:46:15 +0000135/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
136 * current line of the input file, in order to give a more precise error
137 * message. ip6tables itself doesn't need this, so it is initialized to the
138 * magic number of -1 */
139int line = -1;
140
Rusty Russell5eed48a2000-06-02 20:12:24 +0000141static struct option *opts = original_opts;
142static unsigned int global_option_offset = 0;
143
144/* Table of legal combinations of commands and options. If any of the
145 * given commands make an option legal, that option is legal (applies to
146 * CMD_LIST and CMD_ZERO only).
147 * Key:
148 * + compulsory
149 * x illegal
150 * optional
151 */
152
153static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
154/* Well, it's better than "Re: Linux vs FreeBSD" */
155{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000156 /* -n -s -d -p -j -v -x -i -o --line -c */
157/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
158/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
159/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
160/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
161/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
162/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
163/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
164/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
165/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
166/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
167/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000168/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000169};
170
171static int inverse_for_options[NUMBER_OF_OPT] =
172{
173/* -n */ 0,
174/* -s */ IP6T_INV_SRCIP,
175/* -d */ IP6T_INV_DSTIP,
176/* -p */ IP6T_INV_PROTO,
177/* -j */ 0,
178/* -v */ 0,
179/* -x */ 0,
180/* -i */ IP6T_INV_VIA_IN,
181/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000182/*--line*/ 0,
183/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000184};
185
186const char *program_version;
187const char *program_name;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000188
189/* Extra debugging from libiptc */
190extern void dump_entries6(const ip6tc_handle_t handle);
191
192/* A few hardcoded protocols for 'all' and in case the user has no
193 /etc/protocols */
194struct pprot {
195 char *name;
196 u_int8_t num;
197};
198
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000199struct afinfo afinfo = {
200 .family = AF_INET6,
201 .libprefix = "libip6t_",
202 .ipproto = IPPROTO_IPV6,
203 .kmod = "ip6_tables",
204 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
205 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
206};
207
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000208/* Primitive headers... */
209/* defined in netinet/in.h */
210#if 0
211#ifndef IPPROTO_ESP
212#define IPPROTO_ESP 50
213#endif
214#ifndef IPPROTO_AH
215#define IPPROTO_AH 51
216#endif
217#endif
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000218#ifndef IPPROTO_MH
219#define IPPROTO_MH 135
220#endif
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000221
Rusty Russell5eed48a2000-06-02 20:12:24 +0000222static const struct pprot chain_protos[] = {
223 { "tcp", IPPROTO_TCP },
224 { "udp", IPPROTO_UDP },
Patrick McHardy95616062007-01-11 09:08:22 +0000225 { "udplite", IPPROTO_UDPLITE },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000226 { "icmpv6", IPPROTO_ICMPV6 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000227 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000228 { "esp", IPPROTO_ESP },
229 { "ah", IPPROTO_AH },
Masahide NAKAMURA00d46e12007-02-09 11:24:14 +0000230 { "ipv6-mh", IPPROTO_MH },
231 { "mh", IPPROTO_MH },
Phil Oesterb7408912007-04-30 00:01:39 +0000232 { "all", 0 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000233};
234
235static char *
236proto_to_name(u_int8_t proto, int nolookup)
237{
238 unsigned int i;
239
240 if (proto && !nolookup) {
241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
Phil Oester58179b12006-07-20 17:00:19 +0000253int
254service_to_port(const char *name, const char *proto)
255{
256 struct servent *service;
257
258 if ((service = getservbyname(name, proto)) != NULL)
259 return ntohs((unsigned short) service->s_port);
260
261 return -1;
262}
263
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000264u_int16_t
265parse_port(const char *port, const char *proto)
266{
267 unsigned int portnum;
268
269 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
270 (portnum = service_to_port(port, proto)) != -1)
271 return (u_int16_t)portnum;
272
273 exit_error(PARAMETER_PROBLEM,
274 "invalid port/service `%s' specified", port);
275}
276
Rusty Russell5eed48a2000-06-02 20:12:24 +0000277static void
278in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
279{
280 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000281 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282}
283
Pablo Neiradfdcd642005-05-29 19:05:23 +0000284static void free_opts(int reset_offset)
285{
286 if (opts != original_opts) {
287 free(opts);
288 opts = original_opts;
289 if (reset_offset)
290 global_option_offset = 0;
291 }
292}
293
Rusty Russell5eed48a2000-06-02 20:12:24 +0000294void
295exit_error(enum exittype status, char *msg, ...)
296{
297 va_list args;
298
299 va_start(args, msg);
300 fprintf(stderr, "%s v%s: ", program_name, program_version);
301 vfprintf(stderr, msg, args);
302 va_end(args);
303 fprintf(stderr, "\n");
304 if (status == PARAMETER_PROBLEM)
305 exit_tryhelp(status);
306 if (status == VERSION_PROBLEM)
307 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000308 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000309 /* On error paths, make sure that we don't leak memory */
310 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000311 exit(status);
312}
313
314void
315exit_tryhelp(int status)
316{
Harald Weltea8658ca2003-03-05 07:46:15 +0000317 if (line != -1)
318 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000319 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
320 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000321 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322 exit(status);
323}
324
325void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000326exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000328 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000329 struct ip6tables_target *t = NULL;
330
331 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000332"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000333" %s -[RI] chain rulenum rule-specification [options]\n"
334" %s -D chain rulenum [options]\n"
335" %s -[LFZ] [chain] [options]\n"
336" %s -[NX] chain\n"
337" %s -E old-chain-name new-chain-name\n"
338" %s -P chain target [options]\n"
339" %s -h (print this help information)\n\n",
340 program_name, program_version, program_name, program_name,
341 program_name, program_name, program_name, program_name,
342 program_name, program_name);
343
344 printf(
345"Commands:\n"
346"Either long or short options are allowed.\n"
347" --append -A chain Append to chain\n"
348" --delete -D chain Delete matching rule from chain\n"
349" --delete -D chain rulenum\n"
350" Delete rule rulenum (1 = first) from chain\n"
351" --insert -I chain [rulenum]\n"
352" Insert in chain as rulenum (default 1=first)\n"
353" --replace -R chain rulenum\n"
354" Replace rule rulenum (1 = first) in chain\n"
355" --list -L [chain] List the rules in a chain or all chains\n"
356" --flush -F [chain] Delete all rules in chain or all chains\n"
357" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000358" --new -N chain Create a new user-defined chain\n"
359" --delete-chain\n"
360" -X [chain] Delete a user-defined chain\n"
361" --policy -P chain target\n"
362" Change policy on chain to target\n"
363" --rename-chain\n"
364" -E old-chain new-chain\n"
365" Change chain name, (moving any references)\n"
366
367"Options:\n"
368" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
369" --source -s [!] address[/mask]\n"
370" source specification\n"
371" --destination -d [!] address[/mask]\n"
372" destination specification\n"
373" --in-interface -i [!] input name[+]\n"
374" network interface name ([+] for wildcard)\n"
375" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000376" target for rule (may load target extension)\n"
377" --match -m match\n"
378" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000379" --numeric -n numeric output of addresses and ports\n"
380" --out-interface -o [!] output name[+]\n"
381" network interface name ([+] for wildcard)\n"
382" --table -t table table to manipulate (default: `filter')\n"
383" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000384" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000385" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000386/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000387" --modprobe=<command> try to insert modules using this command\n"
388" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000389"[!] --version -V print package version.\n");
390
391 /* Print out any special helps. A user might like to be able to add a --help
392 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000393 specified matches & targets */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000394 for (t = xtables_targets; t; t = t->next) {
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000395 if (t->used) {
396 printf("\n");
397 t->help();
398 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000399 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000400 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000401 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000402 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000403 }
404 exit(0);
405}
406
407static void
408generic_opt_check(int command, int options)
409{
410 int i, j, legal = 0;
411
412 /* Check that commands are valid with options. Complicated by the
413 * fact that if an option is legal with *any* command given, it is
414 * legal overall (ie. -z and -l).
415 */
416 for (i = 0; i < NUMBER_OF_OPT; i++) {
417 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
418
419 for (j = 0; j < NUMBER_OF_CMD; j++) {
420 if (!(command & (1<<j)))
421 continue;
422
423 if (!(options & (1<<i))) {
424 if (commands_v_options[j][i] == '+')
425 exit_error(PARAMETER_PROBLEM,
426 "You need to supply the `-%c' "
427 "option for this command\n",
428 optflags[i]);
429 } else {
430 if (commands_v_options[j][i] != 'x')
431 legal = 1;
432 else if (legal == 0)
433 legal = -1;
434 }
435 }
436 if (legal == -1)
437 exit_error(PARAMETER_PROBLEM,
438 "Illegal option `-%c' with this command\n",
439 optflags[i]);
440 }
441}
442
443static char
444opt2char(int option)
445{
446 const char *ptr;
447 for (ptr = optflags; option > 1; option >>= 1, ptr++);
448
449 return *ptr;
450}
451
452static char
453cmd2char(int option)
454{
455 const char *ptr;
456 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
457
458 return *ptr;
459}
460
461static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000462add_command(unsigned int *cmd, const int newcmd, const int othercmds,
463 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000464{
465 if (invert)
466 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
467 if (*cmd & (~othercmds))
468 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
469 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
470 *cmd |= newcmd;
471}
472
473int
Harald Welteb77f1da2002-03-14 11:35:58 +0000474check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000475{
476 if (option && strcmp(option, "!") == 0) {
477 if (*invert)
478 exit_error(PARAMETER_PROBLEM,
479 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000480 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000481 if (optind) {
482 *optind = *optind+1;
483 if (argc && *optind > argc)
484 exit_error(PARAMETER_PROBLEM,
485 "no argument following `!'");
486 }
487
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488 return TRUE;
489 }
490 return FALSE;
491}
492
Rusty Russell5eed48a2000-06-02 20:12:24 +0000493static char *
494addr_to_numeric(const struct in6_addr *addrp)
495{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000496 /* 0000:0000:0000:0000:0000:000.000.000.000
497 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000498 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000499 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000500}
501
502static struct in6_addr *
503numeric_to_addr(const char *num)
504{
505 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000506 int err;
507 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000508 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000509#ifdef DEBUG
510 fprintf(stderr, "\nnumeric2addr: %d\n", err);
511#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000512 return (struct in6_addr *)NULL;
513}
514
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000515
516static struct in6_addr *
517host_to_addr(const char *name, unsigned int *naddr)
518{
519 struct addrinfo hints;
520 struct addrinfo *res;
521 static struct in6_addr *addr;
522 int err;
523
524 memset(&hints, 0, sizeof(hints));
525 hints.ai_flags=AI_CANONNAME;
526 hints.ai_family=AF_INET6;
527 hints.ai_socktype=SOCK_RAW;
528 hints.ai_protocol=41;
529 hints.ai_next=NULL;
530
531 *naddr = 0;
532 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
533#ifdef DEBUG
534 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
535#endif
536 return (struct in6_addr *) NULL;
537 } else {
538 if (res->ai_family != AF_INET6 ||
539 res->ai_addrlen != sizeof(struct sockaddr_in6))
540 return (struct in6_addr *) NULL;
541
542#ifdef DEBUG
543 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
544 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
545#endif
546 /* Get the first element of the address-chain */
547 addr = fw_calloc(1, sizeof(struct in6_addr));
548 in6addrcpy(addr, (struct in6_addr *)
549 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
550 freeaddrinfo(res);
551 *naddr = 1;
552 return addr;
553 }
554
555 return (struct in6_addr *) NULL;
556}
557
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000558static char *
559addr_to_host(const struct in6_addr *addr)
560{
561 struct sockaddr_in6 saddr;
562 int err;
563 static char hostname[NI_MAXHOST];
564
565 memset(&saddr, 0, sizeof(struct sockaddr_in6));
566 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000567 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000568
569 if ( (err=getnameinfo((struct sockaddr *)&saddr,
570 sizeof(struct sockaddr_in6),
571 hostname, sizeof(hostname)-1,
572 NULL, 0, 0)) != 0 ){
573#ifdef DEBUG
574 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
575#endif
576 return (char *) NULL;
577 } else {
578#ifdef DEBUG
579 fprintf (stderr, "\naddr2host: %s\n", hostname);
580#endif
581
582 return hostname;
583 }
584
585 return (char *) NULL;
586}
587
Rusty Russell5eed48a2000-06-02 20:12:24 +0000588static char *
589mask_to_numeric(const struct in6_addr *addrp)
590{
Harald Welte8a50ab82003-06-24 18:15:59 +0000591 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000592 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000593 if (l == -1) {
594 strcpy(buf, "/");
595 strcat(buf, addr_to_numeric(addrp));
596 return buf;
597 }
Harald Welte43ac1912002-03-03 09:43:07 +0000598 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599 return buf;
600}
601
602static struct in6_addr *
603network_to_addr(const char *name)
604{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000605 /* abort();*/
606 /* TODO: not implemented yet, but the exception breaks the
607 * name resolvation */
608 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000609}
610
611static char *
612addr_to_anyname(const struct in6_addr *addr)
613{
614 char *name;
615
616 if ((name = addr_to_host(addr)) != NULL)
617 return name;
618
619 return addr_to_numeric(addr);
620}
621
622/*
623 * All functions starting with "parse" should succeed, otherwise
624 * the program fails.
625 * Most routines return pointers to static data that may change
626 * between calls to the same or other routines with a few exceptions:
627 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
628 * return global static data.
629*/
630
631static struct in6_addr *
632parse_hostnetwork(const char *name, unsigned int *naddrs)
633{
634 struct in6_addr *addrp, *addrptmp;
635
636 if ((addrptmp = numeric_to_addr(name)) != NULL ||
637 (addrptmp = network_to_addr(name)) != NULL) {
638 addrp = fw_malloc(sizeof(struct in6_addr));
639 in6addrcpy(addrp, addrptmp);
640 *naddrs = 1;
641 return addrp;
642 }
643 if ((addrp = host_to_addr(name, naddrs)) != NULL)
644 return addrp;
645
646 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
647}
648
649static struct in6_addr *
650parse_mask(char *mask)
651{
652 static struct in6_addr maskaddr;
653 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000654 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000655
656 if (mask == NULL) {
657 /* no mask at all defaults to 128 bits */
658 memset(&maskaddr, 0xff, sizeof maskaddr);
659 return &maskaddr;
660 }
661 if ((addrp = numeric_to_addr(mask)) != NULL)
662 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000663 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000664 exit_error(PARAMETER_PROBLEM,
665 "invalid mask `%s' specified", mask);
666 if (bits != 0) {
667 char *p = (char *)&maskaddr;
668 memset(p, 0xff, bits / 8);
669 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
670 p[bits / 8] = 0xff << (8 - (bits & 7));
671 return &maskaddr;
672 }
673
674 memset(&maskaddr, 0, sizeof maskaddr);
675 return &maskaddr;
676}
677
Harald Welte43ac1912002-03-03 09:43:07 +0000678void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000679parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
680 struct in6_addr *maskp, unsigned int *naddrs)
681{
682 struct in6_addr *addrp;
683 char buf[256];
684 char *p;
685 int i, j, n;
686
687 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000688 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 if ((p = strrchr(buf, '/')) != NULL) {
690 *p = '\0';
691 addrp = parse_mask(p + 1);
692 } else
693 addrp = parse_mask(NULL);
694 in6addrcpy(maskp, addrp);
695
696 /* if a null mask is given, the name is ignored, like in "any/0" */
697 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
698 strcpy(buf, "::");
699
700 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
701 n = *naddrs;
702 for (i = 0, j = 0; i < n; i++) {
703 int k;
704 for (k = 0; k < 4; k++)
705 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
706 j++;
707 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000708 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709 (*naddrs)--;
710 j--;
711 break;
712 }
713 }
714 }
715}
716
Rusty Russell5eed48a2000-06-02 20:12:24 +0000717/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
718static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000719find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720{
Harald Welteed498492001-07-23 01:24:22 +0000721 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000722
Harald Welte43ac1912002-03-03 09:43:07 +0000723 if (string_to_number(pname, 0, 255, &proto) != -1) {
724 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000725
Harald Welte43ac1912002-03-03 09:43:07 +0000726 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000727 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000728 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000729 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000730
731 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000732}
733
Harald Welte43ac1912002-03-03 09:43:07 +0000734u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735parse_protocol(const char *s)
736{
Harald Welteed498492001-07-23 01:24:22 +0000737 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738
Harald Welteed498492001-07-23 01:24:22 +0000739 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000740 struct protoent *pent;
741
Harald Weltecbe1ec72006-02-11 09:50:11 +0000742 /* first deal with the special case of 'all' to prevent
743 * people from being able to redefine 'all' in nsswitch
744 * and/or provoke expensive [not working] ldap/nis/...
745 * lookups */
746 if (!strcmp(s, "all"))
747 return 0;
748
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 if ((pent = getprotobyname(s)))
750 proto = pent->p_proto;
751 else {
752 unsigned int i;
753 for (i = 0;
754 i < sizeof(chain_protos)/sizeof(struct pprot);
755 i++) {
756 if (strcmp(s, chain_protos[i].name) == 0) {
757 proto = chain_protos[i].num;
758 break;
759 }
760 }
761 if (i == sizeof(chain_protos)/sizeof(struct pprot))
762 exit_error(PARAMETER_PROBLEM,
763 "unknown protocol `%s' specified",
764 s);
765 }
766 }
767
768 return (u_int16_t)proto;
769}
770
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000771/* These are invalid numbers as upper layer protocol */
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000772static int is_exthdr(u_int16_t proto)
773{
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +0000774 return (proto == IPPROTO_ROUTING ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000775 proto == IPPROTO_FRAGMENT ||
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000776 proto == IPPROTO_AH ||
777 proto == IPPROTO_DSTOPTS);
778}
779
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000780void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781{
782 int vialen = strlen(arg);
783 unsigned int i;
784
785 memset(mask, 0, IFNAMSIZ);
786 memset(vianame, 0, IFNAMSIZ);
787
788 if (vialen + 1 > IFNAMSIZ)
789 exit_error(PARAMETER_PROBLEM,
790 "interface name `%s' must be shorter than IFNAMSIZ"
791 " (%i)", arg, IFNAMSIZ-1);
792
793 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000794 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 memset(mask, 0, IFNAMSIZ);
796 else if (vianame[vialen - 1] == '+') {
797 memset(mask, 0xFF, vialen - 1);
798 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000799 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 } else {
801 /* Include nul-terminator in match */
802 memset(mask, 0xFF, vialen + 1);
803 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000804 for (i = 0; vianame[i]; i++) {
Patrick McHardy2d1a2972006-09-20 08:32:25 +0000805 if (vianame[i] == ':' ||
806 vianame[i] == '!' ||
807 vianame[i] == '*') {
808 printf("Warning: weird character in interface"
Harald Welte43ac1912002-03-03 09:43:07 +0000809 " `%s' (No aliases, :, ! or *).\n",
810 vianame);
811 break;
812 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000813 }
814 }
815}
816
817/* Can't be zero. */
818static int
819parse_rulenumber(const char *rule)
820{
Harald Welte43ac1912002-03-03 09:43:07 +0000821 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822
Harald Welte43ac1912002-03-03 09:43:07 +0000823 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 exit_error(PARAMETER_PROBLEM,
825 "Invalid rule number `%s'", rule);
826
827 return rulenum;
828}
829
830static const char *
831parse_target(const char *targetname)
832{
833 const char *ptr;
834
835 if (strlen(targetname) < 1)
836 exit_error(PARAMETER_PROBLEM,
837 "Invalid target name (too short)");
838
839 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
840 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000841 "Invalid target name `%s' (%u chars max)",
842 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000843
844 for (ptr = targetname; *ptr; ptr++)
845 if (isspace(*ptr))
846 exit_error(PARAMETER_PROBLEM,
847 "Invalid target name `%s'", targetname);
848 return targetname;
849}
850
851int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000852string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
853 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000854{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000855 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000856 char *end;
857
858 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000859 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000860 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000861 if (*end == '\0' && end != s) {
862 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000863 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000864 *ret = number;
865 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000866 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000867 }
868 return -1;
869}
870
Martin Josefssonb105bc92004-05-26 15:54:49 +0000871int
872string_to_number_l(const char *s, unsigned long min, unsigned long max,
873 unsigned long *ret)
874{
875 int result;
876 unsigned long long number;
877
878 result = string_to_number_ll(s, min, max, &number);
879 *ret = (unsigned long)number;
880
881 return result;
882}
883
884int string_to_number(const char *s, unsigned int min, unsigned int max,
885 unsigned int *ret)
886{
887 int result;
888 unsigned long number;
889
890 result = string_to_number_l(s, min, max, &number);
891 *ret = (unsigned int)number;
892
893 return result;
894}
895
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896static void
897set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
898 int invert)
899{
900 if (*options & option)
901 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
902 opt2char(option));
903 *options |= option;
904
905 if (invert) {
906 unsigned int i;
907 for (i = 0; 1 << i != option; i++);
908
909 if (!inverse_for_options[i])
910 exit_error(PARAMETER_PROBLEM,
911 "cannot have ! before -%c",
912 opt2char(option));
913 *invflg |= inverse_for_options[i];
914 }
915}
916
Rusty Russell5eed48a2000-06-02 20:12:24 +0000917static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000918merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000919 unsigned int *option_offset)
920{
921 unsigned int num_old, num_new, i;
922 struct option *merge;
923
924 for (num_old = 0; oldopts[num_old].name; num_old++);
925 for (num_new = 0; newopts[num_new].name; num_new++);
926
927 global_option_offset += OPTION_OFFSET;
928 *option_offset = global_option_offset;
929
930 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
931 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +0000932 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000933 for (i = 0; i < num_new; i++) {
934 merge[num_old + i] = newopts[i];
935 merge[num_old + i].val += *option_offset;
936 }
937 memset(merge + num_old + num_new, 0, sizeof(struct option));
938
939 return merge;
940}
941
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000942void register_match6(struct ip6tables_match *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000943{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000944 me->family = AF_INET6;
945 xtables_register_match(me);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000946}
947
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000948void register_target6(struct ip6tables_target *me)
Rémi Denis-Courmont06652172006-10-20 12:24:34 +0000949{
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000950 me->family = AF_INET6;
951 xtables_register_target(me);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000952}
953
954static void
955print_num(u_int64_t number, unsigned int format)
956{
Harald Welte43ac1912002-03-03 09:43:07 +0000957 if (format & FMT_KILOMEGAGIGA) {
958 if (number > 99999) {
959 number = (number + 500) / 1000;
960 if (number > 9999) {
961 number = (number + 500) / 1000;
962 if (number > 9999) {
963 number = (number + 500) / 1000;
964 if (number > 9999) {
965 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +0000966 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000967 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000968 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000969 }
Martin Josefssona28d4952004-05-26 16:04:48 +0000970 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000971 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000972 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000973 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000974 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +0000975 } else
Martin Josefssona28d4952004-05-26 16:04:48 +0000976 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977}
978
Harald Welte43ac1912002-03-03 09:43:07 +0000979
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980static void
981print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
982{
983 struct ip6t_counters counters;
984 const char *pol = ip6tc_get_policy(chain, &counters, handle);
985 printf("Chain %s", chain);
986 if (pol) {
987 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000988 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +0000989 fputc(' ', stdout);
990 print_num(counters.pcnt, (format|FMT_NOTABLE));
991 fputs("packets, ", stdout);
992 print_num(counters.bcnt, (format|FMT_NOTABLE));
993 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000994 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000995 printf(")\n");
996 } else {
997 unsigned int refs;
998 if (!ip6tc_get_references(&refs, chain, handle))
999 printf(" (ERROR obtaining refs)\n");
1000 else
1001 printf(" (%u references)\n", refs);
1002 }
1003
1004 if (format & FMT_LINENUMBERS)
1005 printf(FMT("%-4s ", "%s "), "num");
1006 if (!(format & FMT_NOCOUNTS)) {
1007 if (format & FMT_KILOMEGAGIGA) {
1008 printf(FMT("%5s ","%s "), "pkts");
1009 printf(FMT("%5s ","%s "), "bytes");
1010 } else {
1011 printf(FMT("%8s ","%s "), "pkts");
1012 printf(FMT("%10s ","%s "), "bytes");
1013 }
1014 }
1015 if (!(format & FMT_NOTARGET))
1016 printf(FMT("%-9s ","%s "), "target");
1017 fputs(" prot ", stdout);
1018 if (format & FMT_OPTIONS)
1019 fputs("opt", stdout);
1020 if (format & FMT_VIA) {
1021 printf(FMT(" %-6s ","%s "), "in");
1022 printf(FMT("%-6s ","%s "), "out");
1023 }
1024 printf(FMT(" %-19s ","%s "), "source");
1025 printf(FMT(" %-19s "," %s "), "destination");
1026 printf("\n");
1027}
1028
Harald Welte43ac1912002-03-03 09:43:07 +00001029
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030static int
1031print_match(const struct ip6t_entry_match *m,
1032 const struct ip6t_ip6 *ip,
1033 int numeric)
1034{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001035 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001036
1037 if (match) {
1038 if (match->print)
1039 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001040 else
1041 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001042 } else {
1043 if (m->u.user.name[0])
1044 printf("UNKNOWN match `%s' ", m->u.user.name);
1045 }
1046 /* Don't stop iterating. */
1047 return 0;
1048}
1049
1050/* e is called `fw' here for hysterical raisins */
1051static void
1052print_firewall(const struct ip6t_entry *fw,
1053 const char *targname,
1054 unsigned int num,
1055 unsigned int format,
1056 const ip6tc_handle_t handle)
1057{
1058 struct ip6tables_target *target = NULL;
1059 const struct ip6t_entry_target *t;
1060 u_int8_t flags;
1061 char buf[BUFSIZ];
1062
Rusty Russell5eed48a2000-06-02 20:12:24 +00001063 if (!ip6tc_is_chain(targname, handle))
1064 target = find_target(targname, TRY_LOAD);
1065 else
1066 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1067
1068 t = ip6t_get_target((struct ip6t_entry *)fw);
1069 flags = fw->ipv6.flags;
1070
1071 if (format & FMT_LINENUMBERS)
1072 printf(FMT("%-4u ", "%u "), num+1);
1073
1074 if (!(format & FMT_NOCOUNTS)) {
1075 print_num(fw->counters.pcnt, format);
1076 print_num(fw->counters.bcnt, format);
1077 }
1078
1079 if (!(format & FMT_NOTARGET))
1080 printf(FMT("%-9s ", "%s "), targname);
1081
1082 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1083 {
Harald Welte43ac1912002-03-03 09:43:07 +00001084 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001085 if (pname)
1086 printf(FMT("%-5s", "%s "), pname);
1087 else
1088 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1089 }
1090
1091 if (format & FMT_OPTIONS) {
1092 if (format & FMT_NOTABLE)
1093 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001094 fputc(' ', stdout); /* Invert flag of FRAG */
1095 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001096 fputc(' ', stdout);
1097 }
1098
1099 if (format & FMT_VIA) {
1100 char iface[IFNAMSIZ+2];
1101
1102 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1103 iface[0] = '!';
1104 iface[1] = '\0';
1105 }
1106 else iface[0] = '\0';
1107
1108 if (fw->ipv6.iniface[0] != '\0') {
1109 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001110 }
1111 else if (format & FMT_NUMERIC) strcat(iface, "*");
1112 else strcat(iface, "any");
1113 printf(FMT(" %-6s ","in %s "), iface);
1114
1115 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1116 iface[0] = '!';
1117 iface[1] = '\0';
1118 }
1119 else iface[0] = '\0';
1120
1121 if (fw->ipv6.outiface[0] != '\0') {
1122 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001123 }
1124 else if (format & FMT_NUMERIC) strcat(iface, "*");
1125 else strcat(iface, "any");
1126 printf(FMT("%-6s ","out %s "), iface);
1127 }
1128
1129 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1130 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1131 && !(format & FMT_NUMERIC))
1132 printf(FMT("%-19s ","%s "), "anywhere");
1133 else {
1134 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001135 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001136 else
Harald Welte43ac1912002-03-03 09:43:07 +00001137 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001138 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1139 printf(FMT("%-19s ","%s "), buf);
1140 }
1141
1142 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1143 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1144 && !(format & FMT_NUMERIC))
1145 printf(FMT("%-19s","-> %s"), "anywhere");
1146 else {
1147 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001148 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001149 else
Harald Welte43ac1912002-03-03 09:43:07 +00001150 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001151 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1152 printf(FMT("%-19s","-> %s"), buf);
1153 }
1154
1155 if (format & FMT_NOTABLE)
1156 fputs(" ", stdout);
1157
1158 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1159
1160 if (target) {
1161 if (target->print)
1162 /* Print the target information. */
1163 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1164 } else if (t->u.target_size != sizeof(*t))
1165 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001166 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001167
1168 if (!(format & FMT_NONEWLINE))
1169 fputc('\n', stdout);
1170}
1171
1172static void
1173print_firewall_line(const struct ip6t_entry *fw,
1174 const ip6tc_handle_t h)
1175{
1176 struct ip6t_entry_target *t;
1177
1178 t = ip6t_get_target((struct ip6t_entry *)fw);
1179 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1180}
1181
1182static int
1183append_entry(const ip6t_chainlabel chain,
1184 struct ip6t_entry *fw,
1185 unsigned int nsaddrs,
1186 const struct in6_addr saddrs[],
1187 unsigned int ndaddrs,
1188 const struct in6_addr daddrs[],
1189 int verbose,
1190 ip6tc_handle_t *handle)
1191{
1192 unsigned int i, j;
1193 int ret = 1;
1194
1195 for (i = 0; i < nsaddrs; i++) {
1196 fw->ipv6.src = saddrs[i];
1197 for (j = 0; j < ndaddrs; j++) {
1198 fw->ipv6.dst = daddrs[j];
1199 if (verbose)
1200 print_firewall_line(fw, *handle);
1201 ret &= ip6tc_append_entry(chain, fw, handle);
1202 }
1203 }
1204
1205 return ret;
1206}
1207
1208static int
1209replace_entry(const ip6t_chainlabel chain,
1210 struct ip6t_entry *fw,
1211 unsigned int rulenum,
1212 const struct in6_addr *saddr,
1213 const struct in6_addr *daddr,
1214 int verbose,
1215 ip6tc_handle_t *handle)
1216{
1217 fw->ipv6.src = *saddr;
1218 fw->ipv6.dst = *daddr;
1219
1220 if (verbose)
1221 print_firewall_line(fw, *handle);
1222 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1223}
1224
1225static int
1226insert_entry(const ip6t_chainlabel chain,
1227 struct ip6t_entry *fw,
1228 unsigned int rulenum,
1229 unsigned int nsaddrs,
1230 const struct in6_addr saddrs[],
1231 unsigned int ndaddrs,
1232 const struct in6_addr daddrs[],
1233 int verbose,
1234 ip6tc_handle_t *handle)
1235{
1236 unsigned int i, j;
1237 int ret = 1;
1238
1239 for (i = 0; i < nsaddrs; i++) {
1240 fw->ipv6.src = saddrs[i];
1241 for (j = 0; j < ndaddrs; j++) {
1242 fw->ipv6.dst = daddrs[j];
1243 if (verbose)
1244 print_firewall_line(fw, *handle);
1245 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1246 }
1247 }
1248
1249 return ret;
1250}
1251
1252static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001253make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001254{
1255 /* Establish mask for comparison */
1256 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001257 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001258 unsigned char *mask, *mptr;
1259
1260 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001261 for (matchp = matches; matchp; matchp = matchp->next)
1262 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001263
1264 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001265 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001266 + xtables_targets->size);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001267
1268 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1269 mptr = mask + sizeof(struct ip6t_entry);
1270
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001271 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001272 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001273 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001274 + matchp->match->userspacesize);
1275 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001276 }
1277
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001278 memset(mptr, 0xFF,
1279 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001280 + xtables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001281
1282 return mask;
1283}
1284
1285static int
1286delete_entry(const ip6t_chainlabel chain,
1287 struct ip6t_entry *fw,
1288 unsigned int nsaddrs,
1289 const struct in6_addr saddrs[],
1290 unsigned int ndaddrs,
1291 const struct in6_addr daddrs[],
1292 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001293 ip6tc_handle_t *handle,
1294 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001295{
1296 unsigned int i, j;
1297 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001298 unsigned char *mask;
1299
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001300 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001301 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001302 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001303 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001304 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001305 if (verbose)
1306 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001307 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001308 }
1309 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001310 free(mask);
1311
Rusty Russell5eed48a2000-06-02 20:12:24 +00001312 return ret;
1313}
1314
András Kis-Szabó764316a2001-02-26 17:31:20 +00001315int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001316for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1317 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1318{
1319 int ret = 1;
1320 const char *chain;
1321 char *chains;
1322 unsigned int i, chaincount = 0;
1323
1324 chain = ip6tc_first_chain(handle);
1325 while (chain) {
1326 chaincount++;
1327 chain = ip6tc_next_chain(handle);
1328 }
1329
1330 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1331 i = 0;
1332 chain = ip6tc_first_chain(handle);
1333 while (chain) {
1334 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1335 i++;
1336 chain = ip6tc_next_chain(handle);
1337 }
1338
1339 for (i = 0; i < chaincount; i++) {
1340 if (!builtinstoo
1341 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001342 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001343 continue;
1344 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1345 }
1346
1347 free(chains);
1348 return ret;
1349}
1350
András Kis-Szabó764316a2001-02-26 17:31:20 +00001351int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001352flush_entries(const ip6t_chainlabel chain, int verbose,
1353 ip6tc_handle_t *handle)
1354{
1355 if (!chain)
1356 return for_each_chain(flush_entries, verbose, 1, handle);
1357
1358 if (verbose)
1359 fprintf(stdout, "Flushing chain `%s'\n", chain);
1360 return ip6tc_flush_entries(chain, handle);
1361}
1362
1363static int
1364zero_entries(const ip6t_chainlabel chain, int verbose,
1365 ip6tc_handle_t *handle)
1366{
1367 if (!chain)
1368 return for_each_chain(zero_entries, verbose, 1, handle);
1369
1370 if (verbose)
1371 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1372 return ip6tc_zero_entries(chain, handle);
1373}
1374
András Kis-Szabó764316a2001-02-26 17:31:20 +00001375int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001376delete_chain(const ip6t_chainlabel chain, int verbose,
1377 ip6tc_handle_t *handle)
1378{
1379 if (!chain)
1380 return for_each_chain(delete_chain, verbose, 0, handle);
1381
1382 if (verbose)
1383 fprintf(stdout, "Deleting chain `%s'\n", chain);
1384 return ip6tc_delete_chain(chain, handle);
1385}
1386
1387static int
1388list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1389 int expanded, int linenumbers, ip6tc_handle_t *handle)
1390{
1391 int found = 0;
1392 unsigned int format;
1393 const char *this;
1394
1395 format = FMT_OPTIONS;
1396 if (!verbose)
1397 format |= FMT_NOCOUNTS;
1398 else
1399 format |= FMT_VIA;
1400
1401 if (numeric)
1402 format |= FMT_NUMERIC;
1403
1404 if (!expanded)
1405 format |= FMT_KILOMEGAGIGA;
1406
1407 if (linenumbers)
1408 format |= FMT_LINENUMBERS;
1409
1410 for (this = ip6tc_first_chain(handle);
1411 this;
1412 this = ip6tc_next_chain(handle)) {
1413 const struct ip6t_entry *i;
1414 unsigned int num;
1415
1416 if (chain && strcmp(chain, this) != 0)
1417 continue;
1418
1419 if (found) printf("\n");
1420
1421 print_header(format, this, handle);
1422 i = ip6tc_first_rule(this, handle);
1423
1424 num = 0;
1425 while (i) {
1426 print_firewall(i,
1427 ip6tc_get_target(i, handle),
1428 num++,
1429 format,
1430 *handle);
1431 i = ip6tc_next_rule(i, handle);
1432 }
1433 found = 1;
1434 }
1435
1436 errno = ENOENT;
1437 return found;
1438}
1439
1440static struct ip6t_entry *
1441generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001442 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 struct ip6t_entry_target *target)
1444{
1445 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001446 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001447 struct ip6t_entry *e;
1448
1449 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001450 for (matchp = matches; matchp; matchp = matchp->next)
1451 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001452
1453 e = fw_malloc(size + target->u.target_size);
1454 *e = *fw;
1455 e->target_offset = size;
1456 e->next_offset = size + target->u.target_size;
1457
1458 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001459 for (matchp = matches; matchp; matchp = matchp->next) {
1460 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1461 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001462 }
1463 memcpy(e->elems + size, target, target->u.target_size);
1464
1465 return e;
1466}
1467
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001468void clear_rule_matches(struct ip6tables_rule_match **matches)
1469{
1470 struct ip6tables_rule_match *matchp, *tmp;
1471
1472 for (matchp = *matches; matchp;) {
1473 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001474 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001475 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001476 matchp->match->m = NULL;
1477 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001478 if (matchp->match == matchp->match->next) {
1479 free(matchp->match);
1480 matchp->match = NULL;
1481 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001482 free(matchp);
1483 matchp = tmp;
1484 }
1485
1486 *matches = NULL;
1487}
1488
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001489static void set_revision(char *name, u_int8_t revision)
1490{
1491 /* Old kernel sources don't have ".revision" field,
1492 but we stole a byte from name. */
1493 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1494 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1495}
1496
Rusty Russell5eed48a2000-06-02 20:12:24 +00001497int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1498{
1499 struct ip6t_entry fw, *e = NULL;
1500 int invert = 0;
1501 unsigned int nsaddrs = 0, ndaddrs = 0;
1502 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1503
1504 int c, verbose = 0;
1505 const char *chain = NULL;
1506 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1507 const char *policy = NULL, *newname = NULL;
1508 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001509 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001510 int ret = 1;
1511 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001512 struct ip6tables_rule_match *matches = NULL;
1513 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001515 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516 const char *jumpto = "";
1517 char *protocol = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001518 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001519
1520 memset(&fw, 0, sizeof(fw));
1521
Harald Welte43ac1912002-03-03 09:43:07 +00001522 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001523 * a second time */
1524 optind = 0;
1525
Harald Welte43ac1912002-03-03 09:43:07 +00001526 /* clear mflags in case do_command gets called a second time
1527 * (we clear the global list of all matches for security)*/
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001528 for (m = xtables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001529 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001530
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001531 for (t = xtables_targets; t; t = t->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001532 t->tflags = 0;
1533 t->used = 0;
1534 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001535
Rusty Russell5eed48a2000-06-02 20:12:24 +00001536 /* Suppress error messages: we may add new options if we
1537 demand-load a protocol. */
1538 opterr = 0;
1539
1540 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001541 "-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 +00001542 opts, NULL)) != -1) {
1543 switch (c) {
1544 /*
1545 * Command selection
1546 */
1547 case 'A':
1548 add_command(&command, CMD_APPEND, CMD_NONE,
1549 invert);
1550 chain = optarg;
1551 break;
1552
1553 case 'D':
1554 add_command(&command, CMD_DELETE, CMD_NONE,
1555 invert);
1556 chain = optarg;
1557 if (optind < argc && argv[optind][0] != '-'
1558 && argv[optind][0] != '!') {
1559 rulenum = parse_rulenumber(argv[optind++]);
1560 command = CMD_DELETE_NUM;
1561 }
1562 break;
1563
Rusty Russell5eed48a2000-06-02 20:12:24 +00001564 case 'R':
1565 add_command(&command, CMD_REPLACE, CMD_NONE,
1566 invert);
1567 chain = optarg;
1568 if (optind < argc && argv[optind][0] != '-'
1569 && argv[optind][0] != '!')
1570 rulenum = parse_rulenumber(argv[optind++]);
1571 else
1572 exit_error(PARAMETER_PROBLEM,
1573 "-%c requires a rule number",
1574 cmd2char(CMD_REPLACE));
1575 break;
1576
1577 case 'I':
1578 add_command(&command, CMD_INSERT, CMD_NONE,
1579 invert);
1580 chain = optarg;
1581 if (optind < argc && argv[optind][0] != '-'
1582 && argv[optind][0] != '!')
1583 rulenum = parse_rulenumber(argv[optind++]);
1584 else rulenum = 1;
1585 break;
1586
1587 case 'L':
1588 add_command(&command, CMD_LIST, CMD_ZERO,
1589 invert);
1590 if (optarg) chain = optarg;
1591 else if (optind < argc && argv[optind][0] != '-'
1592 && argv[optind][0] != '!')
1593 chain = argv[optind++];
1594 break;
1595
1596 case 'F':
1597 add_command(&command, CMD_FLUSH, CMD_NONE,
1598 invert);
1599 if (optarg) chain = optarg;
1600 else if (optind < argc && argv[optind][0] != '-'
1601 && argv[optind][0] != '!')
1602 chain = argv[optind++];
1603 break;
1604
1605 case 'Z':
1606 add_command(&command, CMD_ZERO, CMD_LIST,
1607 invert);
1608 if (optarg) chain = optarg;
1609 else if (optind < argc && argv[optind][0] != '-'
1610 && argv[optind][0] != '!')
1611 chain = argv[optind++];
1612 break;
1613
1614 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001615 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001616 exit_error(PARAMETER_PROBLEM,
1617 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001618 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001619 if (find_target(optarg, TRY_LOAD))
1620 exit_error(PARAMETER_PROBLEM,
1621 "chain name may not clash "
1622 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1624 invert);
1625 chain = optarg;
1626 break;
1627
1628 case 'X':
1629 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1630 invert);
1631 if (optarg) chain = optarg;
1632 else if (optind < argc && argv[optind][0] != '-'
1633 && argv[optind][0] != '!')
1634 chain = argv[optind++];
1635 break;
1636
1637 case 'E':
1638 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1639 invert);
1640 chain = optarg;
1641 if (optind < argc && argv[optind][0] != '-'
1642 && argv[optind][0] != '!')
1643 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001644 else
1645 exit_error(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI950ddc62007-06-12 01:36:26 +00001646 "-%c requires old-chain-name and "
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001647 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001648 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001649 break;
1650
1651 case 'P':
1652 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1653 invert);
1654 chain = optarg;
1655 if (optind < argc && argv[optind][0] != '-'
1656 && argv[optind][0] != '!')
1657 policy = argv[optind++];
1658 else
1659 exit_error(PARAMETER_PROBLEM,
1660 "-%c requires a chain and a policy",
1661 cmd2char(CMD_SET_POLICY));
1662 break;
1663
1664 case 'h':
1665 if (!optarg)
1666 optarg = argv[optind];
1667
Jonas Berlin1b91e592005-04-01 06:58:38 +00001668 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001669 if (!matches && protocol)
1670 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001671
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001672 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673
1674 /*
1675 * Option selection
1676 */
1677 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001678 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001679 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1680 invert);
1681
1682 /* Canonicalize into lower case */
1683 for (protocol = argv[optind-1]; *protocol; protocol++)
1684 *protocol = tolower(*protocol);
1685
1686 protocol = argv[optind-1];
1687 fw.ipv6.proto = parse_protocol(protocol);
1688 fw.ipv6.flags |= IP6T_F_PROTO;
1689
1690 if (fw.ipv6.proto == 0
1691 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1692 exit_error(PARAMETER_PROBLEM,
1693 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001694
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001695 if (is_exthdr(fw.ipv6.proto)
1696 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001697 printf("Warning: never matched protocol: %s. "
Yasuyuki KOZAKAIf69e30c2007-06-11 20:17:34 +00001698 "use extension match instead.\n",
1699 protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001700 break;
1701
1702 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001703 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1705 invert);
1706 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707 break;
1708
1709 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001710 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001711 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1712 invert);
1713 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001714 break;
1715
1716 case 'j':
1717 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1718 invert);
1719 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001720 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001721 target = find_target(jumpto, TRY_LOAD);
1722
1723 if (target) {
1724 size_t size;
1725
Harald Welte43ac1912002-03-03 09:43:07 +00001726 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1727 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001728
1729 target->t = fw_calloc(1, size);
1730 target->t->u.target_size = size;
1731 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001732 if (target->init != NULL)
1733 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001734 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001735 }
1736 break;
1737
1738
1739 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001740 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001741 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1742 invert);
1743 parse_interface(argv[optind-1],
1744 fw.ipv6.iniface,
1745 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001746 break;
1747
1748 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001749 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001750 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1751 invert);
1752 parse_interface(argv[optind-1],
1753 fw.ipv6.outiface,
1754 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001755 break;
1756
1757 case 'v':
1758 if (!verbose)
1759 set_option(&options, OPT_VERBOSE,
1760 &fw.ipv6.invflags, invert);
1761 verbose++;
1762 break;
1763
1764 case 'm': {
1765 size_t size;
1766
1767 if (invert)
1768 exit_error(PARAMETER_PROBLEM,
1769 "unexpected ! flag before --match");
1770
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001771 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001772 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1773 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001774 m->m = fw_calloc(1, size);
1775 m->m->u.match_size = size;
1776 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001777 set_revision(m->m->u.user.name, m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001778 if (m->init != NULL)
1779 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001780 if (m != m->next)
1781 /* Merge options for non-cloned matches */
1782 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001783 }
1784 break;
1785
1786 case 'n':
1787 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1788 invert);
1789 break;
1790
1791 case 't':
1792 if (invert)
1793 exit_error(PARAMETER_PROBLEM,
1794 "unexpected ! flag before --table");
1795 *table = argv[optind-1];
1796 break;
1797
1798 case 'x':
1799 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1800 invert);
1801 break;
1802
1803 case 'V':
1804 if (invert)
1805 printf("Not %s ;-)\n", program_version);
1806 else
1807 printf("%s v%s\n",
1808 program_name, program_version);
1809 exit(0);
1810
1811 case '0':
1812 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1813 invert);
1814 break;
1815
Harald Welte43ac1912002-03-03 09:43:07 +00001816 case 'M':
1817 modprobe = optarg;
1818 break;
1819
1820 case 'c':
1821
1822 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1823 invert);
1824 pcnt = optarg;
1825 if (optind < argc && argv[optind][0] != '-'
1826 && argv[optind][0] != '!')
1827 bcnt = argv[optind++];
1828 else
1829 exit_error(PARAMETER_PROBLEM,
1830 "-%c requires packet and byte counter",
1831 opt2char(OPT_COUNTERS));
1832
Martin Josefssona28d4952004-05-26 16:04:48 +00001833 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001834 exit_error(PARAMETER_PROBLEM,
1835 "-%c packet counter not numeric",
1836 opt2char(OPT_COUNTERS));
1837
Martin Josefssona28d4952004-05-26 16:04:48 +00001838 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00001839 exit_error(PARAMETER_PROBLEM,
1840 "-%c byte counter not numeric",
1841 opt2char(OPT_COUNTERS));
1842
1843 break;
1844
1845
Rusty Russell5eed48a2000-06-02 20:12:24 +00001846 case 1: /* non option */
1847 if (optarg[0] == '!' && optarg[1] == '\0') {
1848 if (invert)
1849 exit_error(PARAMETER_PROBLEM,
1850 "multiple consecutive ! not"
1851 " allowed");
1852 invert = TRUE;
1853 optarg[0] = '\0';
1854 continue;
1855 }
1856 printf("Bad argument `%s'\n", optarg);
1857 exit_tryhelp(2);
1858
1859 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860 if (!target
1861 || !(target->parse(c - target->option_offset,
1862 argv, invert,
1863 &target->tflags,
1864 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001865 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001866 if (matchp->completed)
1867 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001868 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001869 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001870 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001871 &fw,
1872 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001873 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874 break;
1875 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001876 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001877
1878 /* If you listen carefully, you can
1879 actually hear this code suck. */
1880
1881 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001882 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00001883 * parameter, that has not been parsed yet,
1884 * it's not an option of an explicitly loaded
1885 * match or a target. However, we support
1886 * implicit loading of the protocol match
1887 * extension. '-p tcp' means 'l4 proto 6' and
1888 * at the same time 'load tcp protocol match on
1889 * demand if we specify --dport'.
1890 *
1891 * To make this work, we need to make sure:
1892 * - the parameter has not been parsed by
1893 * a match (m above)
1894 * - a protocol has been specified
1895 * - the protocol extension has not been
1896 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00001897 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00001898 * - the protocol extension can be successively
1899 * loaded
1900 */
1901 if (m == NULL
1902 && protocol
1903 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001904 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001905 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001906 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00001907 && (proto_used == 0))
1908 )
1909 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001910 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00001911 /* Try loading protocol */
1912 size_t size;
1913
1914 proto_used = 1;
1915
1916 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1917 + m->size;
1918
1919 m->m = fw_calloc(1, size);
1920 m->m->u.match_size = size;
1921 strcpy(m->m->u.user.name, m->name);
Rémi Denis-Courmont06652172006-10-20 12:24:34 +00001922 set_revision(m->m->u.user.name,
1923 m->revision);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001924 if (m->init != NULL)
1925 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00001926
1927 opts = merge_options(opts,
1928 m->extra_opts, &m->option_offset);
1929
1930 optind--;
1931 continue;
1932 }
1933
Rusty Russell5eed48a2000-06-02 20:12:24 +00001934 if (!m)
1935 exit_error(PARAMETER_PROBLEM,
1936 "Unknown arg `%s'",
1937 argv[optind-1]);
1938 }
1939 }
1940 invert = FALSE;
1941 }
1942
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001943 for (matchp = matches; matchp; matchp = matchp->next)
1944 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001945
Harald Welte43ac1912002-03-03 09:43:07 +00001946 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 target->final_check(target->tflags);
1948
1949 /* Fix me: must put inverse options checking here --MN */
1950
1951 if (optind < argc)
1952 exit_error(PARAMETER_PROBLEM,
1953 "unknown arguments found on commandline");
1954 if (!command)
1955 exit_error(PARAMETER_PROBLEM, "no command specified");
1956 if (invert)
1957 exit_error(PARAMETER_PROBLEM,
1958 "nothing appropriate following !");
1959
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001960 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001961 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001962 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001964 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001965 }
1966
1967 if (shostnetworkmask)
1968 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1969 &(fw.ipv6.smsk), &nsaddrs);
1970
1971 if (dhostnetworkmask)
1972 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1973 &(fw.ipv6.dmsk), &ndaddrs);
1974
1975 if ((nsaddrs > 1 || ndaddrs > 1) &&
1976 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1977 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1978 " source or destination IP addresses");
1979
Rusty Russell5eed48a2000-06-02 20:12:24 +00001980 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1981 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1982 "specify a unique address");
1983
1984 generic_opt_check(command, options);
1985
1986 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1987 exit_error(PARAMETER_PROBLEM,
1988 "chain name `%s' too long (must be under %i chars)",
1989 chain, IP6T_FUNCTION_MAXNAMELEN);
1990
Harald Welte43ac1912002-03-03 09:43:07 +00001991 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00001992 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00001993 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001994
Rusty Russell8beb0492004-12-22 00:37:10 +00001995 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001996 if (!*handle && load_xtables_ko(modprobe, 0) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00001997 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001998
Harald Welte43ac1912002-03-03 09:43:07 +00001999 if (!*handle)
2000 exit_error(VERSION_PROBLEM,
2001 "can't initialize ip6tables table `%s': %s",
2002 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002003
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002004 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002005 || command == CMD_DELETE
2006 || command == CMD_INSERT
2007 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002008 if (strcmp(chain, "PREROUTING") == 0
2009 || strcmp(chain, "INPUT") == 0) {
2010 /* -o not valid with incoming packets. */
2011 if (options & OPT_VIANAMEOUT)
2012 exit_error(PARAMETER_PROBLEM,
2013 "Can't use -%c with %s\n",
2014 opt2char(OPT_VIANAMEOUT),
2015 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002016 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002017
Harald Welte43ac1912002-03-03 09:43:07 +00002018 if (strcmp(chain, "POSTROUTING") == 0
2019 || strcmp(chain, "OUTPUT") == 0) {
2020 /* -i not valid with outgoing packets */
2021 if (options & OPT_VIANAMEIN)
2022 exit_error(PARAMETER_PROBLEM,
2023 "Can't use -%c with %s\n",
2024 opt2char(OPT_VIANAMEIN),
2025 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002026 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002027
2028 if (target && ip6tc_is_chain(jumpto, *handle)) {
2029 printf("Warning: using chain %s, not extension\n",
2030 jumpto);
2031
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002032 if (target->t)
2033 free(target->t);
2034
Rusty Russell5eed48a2000-06-02 20:12:24 +00002035 target = NULL;
2036 }
2037
2038 /* If they didn't specify a target, or it's a chain
2039 name, use standard. */
2040 if (!target
2041 && (strlen(jumpto) == 0
2042 || ip6tc_is_chain(jumpto, *handle))) {
2043 size_t size;
2044
2045 target = find_target(IP6T_STANDARD_TARGET,
2046 LOAD_MUST_SUCCEED);
2047
2048 size = sizeof(struct ip6t_entry_target)
2049 + target->size;
2050 target->t = fw_calloc(1, size);
2051 target->t->u.target_size = size;
2052 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002053 if (target->init != NULL)
2054 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002055 }
2056
2057 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002058 /* it is no chain, and we can't load a plugin.
2059 * We cannot know if the plugin is corrupt, non
2060 * existant OR if the user just misspelled a
2061 * chain. */
2062 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002063 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002064 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002065 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002066 }
2067 }
2068
2069 switch (command) {
2070 case CMD_APPEND:
2071 ret = append_entry(chain, e,
2072 nsaddrs, saddrs, ndaddrs, daddrs,
2073 options&OPT_VERBOSE,
2074 handle);
2075 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002076 case CMD_DELETE:
2077 ret = delete_entry(chain, e,
2078 nsaddrs, saddrs, ndaddrs, daddrs,
2079 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002080 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002081 break;
2082 case CMD_DELETE_NUM:
2083 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2084 break;
2085 case CMD_REPLACE:
2086 ret = replace_entry(chain, e, rulenum - 1,
2087 saddrs, daddrs, options&OPT_VERBOSE,
2088 handle);
2089 break;
2090 case CMD_INSERT:
2091 ret = insert_entry(chain, e, rulenum - 1,
2092 nsaddrs, saddrs, ndaddrs, daddrs,
2093 options&OPT_VERBOSE,
2094 handle);
2095 break;
2096 case CMD_LIST:
2097 ret = list_entries(chain,
2098 options&OPT_VERBOSE,
2099 options&OPT_NUMERIC,
2100 options&OPT_EXPANDED,
2101 options&OPT_LINENUMBERS,
2102 handle);
2103 break;
2104 case CMD_FLUSH:
2105 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2106 break;
2107 case CMD_ZERO:
2108 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2109 break;
2110 case CMD_LIST|CMD_ZERO:
2111 ret = list_entries(chain,
2112 options&OPT_VERBOSE,
2113 options&OPT_NUMERIC,
2114 options&OPT_EXPANDED,
2115 options&OPT_LINENUMBERS,
2116 handle);
2117 if (ret)
2118 ret = zero_entries(chain,
2119 options&OPT_VERBOSE, handle);
2120 break;
2121 case CMD_NEW_CHAIN:
2122 ret = ip6tc_create_chain(chain, handle);
2123 break;
2124 case CMD_DELETE_CHAIN:
2125 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2126 break;
2127 case CMD_RENAME_CHAIN:
2128 ret = ip6tc_rename_chain(chain, newname, handle);
2129 break;
2130 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002131 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002132 break;
2133 default:
2134 /* We should never reach this... */
2135 exit_tryhelp(2);
2136 }
2137
2138 if (verbose > 1)
2139 dump_entries6(*handle);
2140
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002141 clear_rule_matches(&matches);
2142
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002143 if (e != NULL) {
2144 free(e);
2145 e = NULL;
2146 }
2147
2148 for (c = 0; c < nsaddrs; c++)
2149 free(&saddrs[c]);
2150
2151 for (c = 0; c < ndaddrs; c++)
2152 free(&daddrs[c]);
2153
Pablo Neiradfdcd642005-05-29 19:05:23 +00002154 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002155
Rusty Russell5eed48a2000-06-02 20:12:24 +00002156 return ret;
2157}