blob: 17bdb4e517d8127bef371e1b8f28a42374f541cb [file] [log] [blame]
Rusty Russell5eed48a2000-06-02 20:12:24 +00001/* Code to take an iptables-style command line and do it. */
2
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>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
38#include <ip6tables.h>
39#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
42#include <sys/wait.h>
43#include <sys/types.h>
44#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
53#ifndef IP6T_LIB_DIR
54#define IP6T_LIB_DIR "/usr/local/lib/iptables"
55#endif
56
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000057#ifndef PROC_SYS_MODPROBE
58#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
59#endif
60
Rusty Russell5eed48a2000-06-02 20:12:24 +000061#define FMT_NUMERIC 0x0001
62#define FMT_NOCOUNTS 0x0002
63#define FMT_KILOMEGAGIGA 0x0004
64#define FMT_OPTIONS 0x0008
65#define FMT_NOTABLE 0x0010
66#define FMT_NOTARGET 0x0020
67#define FMT_VIA 0x0040
68#define FMT_NONEWLINE 0x0080
69#define FMT_LINENUMBERS 0x0100
70
71#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
72 | FMT_NUMERIC | FMT_NOTABLE)
73#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
74
75
76#define CMD_NONE 0x0000U
77#define CMD_INSERT 0x0001U
78#define CMD_DELETE 0x0002U
79#define CMD_DELETE_NUM 0x0004U
80#define CMD_REPLACE 0x0008U
81#define CMD_APPEND 0x0010U
82#define CMD_LIST 0x0020U
83#define CMD_FLUSH 0x0040U
84#define CMD_ZERO 0x0080U
85#define CMD_NEW_CHAIN 0x0100U
86#define CMD_DELETE_CHAIN 0x0200U
87#define CMD_SET_POLICY 0x0400U
88#define CMD_CHECK 0x0800U
89#define CMD_RENAME_CHAIN 0x1000U
90#define NUMBER_OF_CMD 13
91static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000092 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94#define OPTION_OFFSET 256
95
96#define OPT_NONE 0x00000U
97#define OPT_NUMERIC 0x00001U
98#define OPT_SOURCE 0x00002U
99#define OPT_DESTINATION 0x00004U
100#define OPT_PROTOCOL 0x00008U
101#define OPT_JUMP 0x00010U
102#define OPT_VERBOSE 0x00020U
103#define OPT_EXPANDED 0x00040U
104#define OPT_VIANAMEIN 0x00080U
105#define OPT_VIANAMEOUT 0x00100U
106#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000107#define OPT_COUNTERS 0x00400U
108#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000109static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000110= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000111
112static struct option original_opts[] = {
113 { "append", 1, 0, 'A' },
114 { "delete", 1, 0, 'D' },
115 { "insert", 1, 0, 'I' },
116 { "replace", 1, 0, 'R' },
117 { "list", 2, 0, 'L' },
118 { "flush", 2, 0, 'F' },
119 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000120 { "new-chain", 1, 0, 'N' },
121 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000122 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000123 { "policy", 1, 0, 'P' },
124 { "source", 1, 0, 's' },
125 { "destination", 1, 0, 'd' },
126 { "src", 1, 0, 's' }, /* synonym */
127 { "dst", 1, 0, 'd' }, /* synonym */
128 { "protocol", 1, 0, 'p' },
129 { "in-interface", 1, 0, 'i' },
130 { "jump", 1, 0, 'j' },
131 { "table", 1, 0, 't' },
132 { "match", 1, 0, 'm' },
133 { "numeric", 0, 0, 'n' },
134 { "out-interface", 1, 0, 'o' },
135 { "verbose", 0, 0, 'v' },
136 { "exact", 0, 0, 'x' },
137 { "version", 0, 0, 'V' },
138 { "help", 2, 0, 'h' },
139 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000140 { "modprobe", 1, 0, 'M' },
141 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000142 { 0 }
143};
144
Harald Weltea8658ca2003-03-05 07:46:15 +0000145/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
146 * current line of the input file, in order to give a more precise error
147 * message. ip6tables itself doesn't need this, so it is initialized to the
148 * magic number of -1 */
149int line = -1;
150
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000151#ifndef __OPTIMIZE__
152struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000153ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000154{
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000155 return (void *)e + e->target_offset;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000156}
157#endif
158
Rusty Russell5eed48a2000-06-02 20:12:24 +0000159static struct option *opts = original_opts;
160static unsigned int global_option_offset = 0;
161
162/* Table of legal combinations of commands and options. If any of the
163 * given commands make an option legal, that option is legal (applies to
164 * CMD_LIST and CMD_ZERO only).
165 * Key:
166 * + compulsory
167 * x illegal
168 * optional
169 */
170
171static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
172/* Well, it's better than "Re: Linux vs FreeBSD" */
173{
174 /* -n -s -d -p -j -v -x -i -o --line */
175/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
176/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
177/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
178/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
179/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
180/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
181/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
182/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
183/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
184/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
185/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000186/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000187/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
188};
189
190static int inverse_for_options[NUMBER_OF_OPT] =
191{
192/* -n */ 0,
193/* -s */ IP6T_INV_SRCIP,
194/* -d */ IP6T_INV_DSTIP,
195/* -p */ IP6T_INV_PROTO,
196/* -j */ 0,
197/* -v */ 0,
198/* -x */ 0,
199/* -i */ IP6T_INV_VIA_IN,
200/* -o */ IP6T_INV_VIA_OUT,
201/*--line*/ 0
202};
203
204const char *program_version;
205const char *program_name;
206
207/* Keeping track of external matches and targets: linked lists. */
208struct ip6tables_match *ip6tables_matches = NULL;
209struct ip6tables_target *ip6tables_targets = NULL;
210
211/* Extra debugging from libiptc */
212extern void dump_entries6(const ip6tc_handle_t handle);
213
214/* A few hardcoded protocols for 'all' and in case the user has no
215 /etc/protocols */
216struct pprot {
217 char *name;
218 u_int8_t num;
219};
220
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000221/* Primitive headers... */
222/* defined in netinet/in.h */
223#if 0
224#ifndef IPPROTO_ESP
225#define IPPROTO_ESP 50
226#endif
227#ifndef IPPROTO_AH
228#define IPPROTO_AH 51
229#endif
230#endif
231
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232static const struct pprot chain_protos[] = {
233 { "tcp", IPPROTO_TCP },
234 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000235 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000236 { "esp", IPPROTO_ESP },
237 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000238 { "all", 0 },
239};
240
241static char *
242proto_to_name(u_int8_t proto, int nolookup)
243{
244 unsigned int i;
245
246 if (proto && !nolookup) {
247 struct protoent *pent = getprotobynumber(proto);
248 if (pent)
249 return pent->p_name;
250 }
251
252 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
253 if (chain_protos[i].num == proto)
254 return chain_protos[i].name;
255
256 return NULL;
257}
258
259static void
260in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
261{
262 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000263 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000264}
265
266void
267exit_error(enum exittype status, char *msg, ...)
268{
269 va_list args;
270
271 va_start(args, msg);
272 fprintf(stderr, "%s v%s: ", program_name, program_version);
273 vfprintf(stderr, msg, args);
274 va_end(args);
275 fprintf(stderr, "\n");
276 if (status == PARAMETER_PROBLEM)
277 exit_tryhelp(status);
278 if (status == VERSION_PROBLEM)
279 fprintf(stderr,
280 "Perhaps iptables or your kernel needs to be upgraded.\n");
281 exit(status);
282}
283
284void
285exit_tryhelp(int status)
286{
Harald Weltea8658ca2003-03-05 07:46:15 +0000287 if (line != -1)
288 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
290 program_name, program_name );
291 exit(status);
292}
293
294void
295exit_printhelp(void)
296{
297 struct ip6tables_match *m = NULL;
298 struct ip6tables_target *t = NULL;
299
300 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000301"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000302" %s -[RI] chain rulenum rule-specification [options]\n"
303" %s -D chain rulenum [options]\n"
304" %s -[LFZ] [chain] [options]\n"
305" %s -[NX] chain\n"
306" %s -E old-chain-name new-chain-name\n"
307" %s -P chain target [options]\n"
308" %s -h (print this help information)\n\n",
309 program_name, program_version, program_name, program_name,
310 program_name, program_name, program_name, program_name,
311 program_name, program_name);
312
313 printf(
314"Commands:\n"
315"Either long or short options are allowed.\n"
316" --append -A chain Append to chain\n"
317" --delete -D chain Delete matching rule from chain\n"
318" --delete -D chain rulenum\n"
319" Delete rule rulenum (1 = first) from chain\n"
320" --insert -I chain [rulenum]\n"
321" Insert in chain as rulenum (default 1=first)\n"
322" --replace -R chain rulenum\n"
323" Replace rule rulenum (1 = first) in chain\n"
324" --list -L [chain] List the rules in a chain or all chains\n"
325" --flush -F [chain] Delete all rules in chain or all chains\n"
326" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327" --new -N chain Create a new user-defined chain\n"
328" --delete-chain\n"
329" -X [chain] Delete a user-defined chain\n"
330" --policy -P chain target\n"
331" Change policy on chain to target\n"
332" --rename-chain\n"
333" -E old-chain new-chain\n"
334" Change chain name, (moving any references)\n"
335
336"Options:\n"
337" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
338" --source -s [!] address[/mask]\n"
339" source specification\n"
340" --destination -d [!] address[/mask]\n"
341" destination specification\n"
342" --in-interface -i [!] input name[+]\n"
343" network interface name ([+] for wildcard)\n"
344" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000345" target for rule (may load target extension)\n"
346" --match -m match\n"
347" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348" --numeric -n numeric output of addresses and ports\n"
349" --out-interface -o [!] output name[+]\n"
350" network interface name ([+] for wildcard)\n"
351" --table -t table table to manipulate (default: `filter')\n"
352" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000353" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000354" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000355/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000356" --modprobe=<command> try to insert modules using this command\n"
357" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000358"[!] --version -V print package version.\n");
359
360 /* Print out any special helps. A user might like to be able to add a --help
361 to the commandline, and see expected results. So we call help for all
362 matches & targets */
363 for (t=ip6tables_targets;t;t=t->next) {
364 printf("\n");
365 t->help();
366 }
367 for (m=ip6tables_matches;m;m=m->next) {
368 printf("\n");
369 m->help();
370 }
371 exit(0);
372}
373
374static void
375generic_opt_check(int command, int options)
376{
377 int i, j, legal = 0;
378
379 /* Check that commands are valid with options. Complicated by the
380 * fact that if an option is legal with *any* command given, it is
381 * legal overall (ie. -z and -l).
382 */
383 for (i = 0; i < NUMBER_OF_OPT; i++) {
384 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
385
386 for (j = 0; j < NUMBER_OF_CMD; j++) {
387 if (!(command & (1<<j)))
388 continue;
389
390 if (!(options & (1<<i))) {
391 if (commands_v_options[j][i] == '+')
392 exit_error(PARAMETER_PROBLEM,
393 "You need to supply the `-%c' "
394 "option for this command\n",
395 optflags[i]);
396 } else {
397 if (commands_v_options[j][i] != 'x')
398 legal = 1;
399 else if (legal == 0)
400 legal = -1;
401 }
402 }
403 if (legal == -1)
404 exit_error(PARAMETER_PROBLEM,
405 "Illegal option `-%c' with this command\n",
406 optflags[i]);
407 }
408}
409
410static char
411opt2char(int option)
412{
413 const char *ptr;
414 for (ptr = optflags; option > 1; option >>= 1, ptr++);
415
416 return *ptr;
417}
418
419static char
420cmd2char(int option)
421{
422 const char *ptr;
423 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
424
425 return *ptr;
426}
427
428static void
429add_command(int *cmd, const int newcmd, const int othercmds, int invert)
430{
431 if (invert)
432 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
433 if (*cmd & (~othercmds))
434 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
435 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
436 *cmd |= newcmd;
437}
438
439int
Harald Welteb77f1da2002-03-14 11:35:58 +0000440check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441{
442 if (option && strcmp(option, "!") == 0) {
443 if (*invert)
444 exit_error(PARAMETER_PROBLEM,
445 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000446 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000447 if (optind) {
448 *optind = *optind+1;
449 if (argc && *optind > argc)
450 exit_error(PARAMETER_PROBLEM,
451 "no argument following `!'");
452 }
453
Rusty Russell5eed48a2000-06-02 20:12:24 +0000454 return TRUE;
455 }
456 return FALSE;
457}
458
459static void *
460fw_calloc(size_t count, size_t size)
461{
462 void *p;
463
464 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000465 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000466 exit(1);
467 }
468 return p;
469}
470
471static void *
472fw_malloc(size_t size)
473{
474 void *p;
475
476 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000477 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478 exit(1);
479 }
480 return p;
481}
482
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483static char *
484addr_to_numeric(const struct in6_addr *addrp)
485{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000486 /* 0000:0000:0000:0000:0000:000.000.000.000
487 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000488 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000489 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000490}
491
492static struct in6_addr *
493numeric_to_addr(const char *num)
494{
495 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000496 int err;
497 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000499#ifdef DEBUG
500 fprintf(stderr, "\nnumeric2addr: %d\n", err);
501#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000502 return (struct in6_addr *)NULL;
503}
504
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000505
506static struct in6_addr *
507host_to_addr(const char *name, unsigned int *naddr)
508{
509 struct addrinfo hints;
510 struct addrinfo *res;
511 static struct in6_addr *addr;
512 int err;
513
514 memset(&hints, 0, sizeof(hints));
515 hints.ai_flags=AI_CANONNAME;
516 hints.ai_family=AF_INET6;
517 hints.ai_socktype=SOCK_RAW;
518 hints.ai_protocol=41;
519 hints.ai_next=NULL;
520
521 *naddr = 0;
522 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
523#ifdef DEBUG
524 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
525#endif
526 return (struct in6_addr *) NULL;
527 } else {
528 if (res->ai_family != AF_INET6 ||
529 res->ai_addrlen != sizeof(struct sockaddr_in6))
530 return (struct in6_addr *) NULL;
531
532#ifdef DEBUG
533 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
534 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
535#endif
536 /* Get the first element of the address-chain */
537 addr = fw_calloc(1, sizeof(struct in6_addr));
538 in6addrcpy(addr, (struct in6_addr *)
539 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
540 freeaddrinfo(res);
541 *naddr = 1;
542 return addr;
543 }
544
545 return (struct in6_addr *) NULL;
546}
547
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000548static char *
549addr_to_host(const struct in6_addr *addr)
550{
551 struct sockaddr_in6 saddr;
552 int err;
553 static char hostname[NI_MAXHOST];
554
555 memset(&saddr, 0, sizeof(struct sockaddr_in6));
556 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000557 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000558
559 if ( (err=getnameinfo((struct sockaddr *)&saddr,
560 sizeof(struct sockaddr_in6),
561 hostname, sizeof(hostname)-1,
562 NULL, 0, 0)) != 0 ){
563#ifdef DEBUG
564 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
565#endif
566 return (char *) NULL;
567 } else {
568#ifdef DEBUG
569 fprintf (stderr, "\naddr2host: %s\n", hostname);
570#endif
571
572 return hostname;
573 }
574
575 return (char *) NULL;
576}
577
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578static char *
579mask_to_numeric(const struct in6_addr *addrp)
580{
581 static char buf[20];
582 int l = ipv6_prefix_length(addrp);
583 if (l == -1)
584 return addr_to_numeric(addrp);
Harald Welte43ac1912002-03-03 09:43:07 +0000585 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000586 return buf;
587}
588
589static struct in6_addr *
590network_to_addr(const char *name)
591{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000592 /* abort();*/
593 /* TODO: not implemented yet, but the exception breaks the
594 * name resolvation */
595 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000596}
597
598static char *
599addr_to_anyname(const struct in6_addr *addr)
600{
601 char *name;
602
603 if ((name = addr_to_host(addr)) != NULL)
604 return name;
605
606 return addr_to_numeric(addr);
607}
608
609/*
610 * All functions starting with "parse" should succeed, otherwise
611 * the program fails.
612 * Most routines return pointers to static data that may change
613 * between calls to the same or other routines with a few exceptions:
614 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
615 * return global static data.
616*/
617
618static struct in6_addr *
619parse_hostnetwork(const char *name, unsigned int *naddrs)
620{
621 struct in6_addr *addrp, *addrptmp;
622
623 if ((addrptmp = numeric_to_addr(name)) != NULL ||
624 (addrptmp = network_to_addr(name)) != NULL) {
625 addrp = fw_malloc(sizeof(struct in6_addr));
626 in6addrcpy(addrp, addrptmp);
627 *naddrs = 1;
628 return addrp;
629 }
630 if ((addrp = host_to_addr(name, naddrs)) != NULL)
631 return addrp;
632
633 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
634}
635
636static struct in6_addr *
637parse_mask(char *mask)
638{
639 static struct in6_addr maskaddr;
640 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000641 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000642
643 if (mask == NULL) {
644 /* no mask at all defaults to 128 bits */
645 memset(&maskaddr, 0xff, sizeof maskaddr);
646 return &maskaddr;
647 }
648 if ((addrp = numeric_to_addr(mask)) != NULL)
649 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000650 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000651 exit_error(PARAMETER_PROBLEM,
652 "invalid mask `%s' specified", mask);
653 if (bits != 0) {
654 char *p = (char *)&maskaddr;
655 memset(p, 0xff, bits / 8);
656 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
657 p[bits / 8] = 0xff << (8 - (bits & 7));
658 return &maskaddr;
659 }
660
661 memset(&maskaddr, 0, sizeof maskaddr);
662 return &maskaddr;
663}
664
Harald Welte43ac1912002-03-03 09:43:07 +0000665void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000666parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
667 struct in6_addr *maskp, unsigned int *naddrs)
668{
669 struct in6_addr *addrp;
670 char buf[256];
671 char *p;
672 int i, j, n;
673
674 strncpy(buf, name, sizeof(buf) - 1);
675 if ((p = strrchr(buf, '/')) != NULL) {
676 *p = '\0';
677 addrp = parse_mask(p + 1);
678 } else
679 addrp = parse_mask(NULL);
680 in6addrcpy(maskp, addrp);
681
682 /* if a null mask is given, the name is ignored, like in "any/0" */
683 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
684 strcpy(buf, "::");
685
686 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
687 n = *naddrs;
688 for (i = 0, j = 0; i < n; i++) {
689 int k;
690 for (k = 0; k < 4; k++)
691 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
692 j++;
693 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000694 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000695 (*naddrs)--;
696 j--;
697 break;
698 }
699 }
700 }
701}
702
703struct ip6tables_match *
704find_match(const char *name, enum ip6t_tryload tryload)
705{
706 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000707 int icmphack = 0;
708
709 /* This is ugly as hell. Nonetheless, there is no way of changing
710 * this without hurting backwards compatibility */
711 if ( (strcmp(name,"icmpv6") == 0) ||
712 (strcmp(name,"ipv6-icmp") == 0) ||
713 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
714
715 if (!icmphack) {
716 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
717 if (strcmp(name, ptr->name) == 0)
718 break;
719 }
720 } else {
721 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
722 if (strcmp("icmp6", ptr->name) == 0)
723 break;
724 }
725 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000726
Harald Welte3efb6ea2001-08-06 18:50:21 +0000727#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 if (!ptr && tryload != DONT_LOAD) {
729 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
730 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000731 if (!icmphack)
732 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
733 else
734 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735 if (dlopen(path, RTLD_NOW)) {
736 /* Found library. If it didn't register itself,
737 maybe they specified target as match. */
738 ptr = find_match(name, DONT_LOAD);
739
740 if (!ptr)
741 exit_error(PARAMETER_PROBLEM,
742 "Couldn't load match `%s'\n",
743 name);
744 } else if (tryload == LOAD_MUST_SUCCEED)
745 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000746 "Couldn't load match `%s':%s\n",
747 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000749#else
750 if (ptr && !ptr->loaded) {
751 if (tryload != DONT_LOAD)
752 ptr->loaded = 1;
753 else
754 ptr = NULL;
755 }
Marc Boucher067477b2002-03-24 15:09:31 +0000756 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
757 exit_error(PARAMETER_PROBLEM,
758 "Couldn't find match `%s'\n", name);
759 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000760#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761
Harald Welte43ac1912002-03-03 09:43:07 +0000762 if (ptr)
763 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000764
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765 return ptr;
766}
767
768/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
769static struct ip6tables_match *
770find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
771{
Harald Welteed498492001-07-23 01:24:22 +0000772 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000773
Harald Welte43ac1912002-03-03 09:43:07 +0000774 if (string_to_number(pname, 0, 255, &proto) != -1) {
775 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000776
Harald Welte43ac1912002-03-03 09:43:07 +0000777 if (protoname)
778 return find_match(protoname, tryload);
779 } else
780 return find_match(pname, tryload);
781
782 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783}
784
Harald Welte43ac1912002-03-03 09:43:07 +0000785u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000786parse_protocol(const char *s)
787{
Harald Welteed498492001-07-23 01:24:22 +0000788 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789
Harald Welteed498492001-07-23 01:24:22 +0000790 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000791 struct protoent *pent;
792
793 if ((pent = getprotobyname(s)))
794 proto = pent->p_proto;
795 else {
796 unsigned int i;
797 for (i = 0;
798 i < sizeof(chain_protos)/sizeof(struct pprot);
799 i++) {
800 if (strcmp(s, chain_protos[i].name) == 0) {
801 proto = chain_protos[i].num;
802 break;
803 }
804 }
805 if (i == sizeof(chain_protos)/sizeof(struct pprot))
806 exit_error(PARAMETER_PROBLEM,
807 "unknown protocol `%s' specified",
808 s);
809 }
810 }
811
812 return (u_int16_t)proto;
813}
814
815static void
816parse_interface(const char *arg, char *vianame, unsigned char *mask)
817{
818 int vialen = strlen(arg);
819 unsigned int i;
820
821 memset(mask, 0, IFNAMSIZ);
822 memset(vianame, 0, IFNAMSIZ);
823
824 if (vialen + 1 > IFNAMSIZ)
825 exit_error(PARAMETER_PROBLEM,
826 "interface name `%s' must be shorter than IFNAMSIZ"
827 " (%i)", arg, IFNAMSIZ-1);
828
829 strcpy(vianame, arg);
830 if (vialen == 0)
831 memset(mask, 0, IFNAMSIZ);
832 else if (vianame[vialen - 1] == '+') {
833 memset(mask, 0xFF, vialen - 1);
834 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000835 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836 } else {
837 /* Include nul-terminator in match */
838 memset(mask, 0xFF, vialen + 1);
839 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000840 for (i = 0; vianame[i]; i++) {
841 if (!isalnum(vianame[i])
842 && vianame[i] != '_'
843 && vianame[i] != '.') {
844 printf("Warning: wierd character in interface"
845 " `%s' (No aliases, :, ! or *).\n",
846 vianame);
847 break;
848 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849 }
850 }
851}
852
853/* Can't be zero. */
854static int
855parse_rulenumber(const char *rule)
856{
Harald Welte43ac1912002-03-03 09:43:07 +0000857 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000858
Harald Welte43ac1912002-03-03 09:43:07 +0000859 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000860 exit_error(PARAMETER_PROBLEM,
861 "Invalid rule number `%s'", rule);
862
863 return rulenum;
864}
865
866static const char *
867parse_target(const char *targetname)
868{
869 const char *ptr;
870
871 if (strlen(targetname) < 1)
872 exit_error(PARAMETER_PROBLEM,
873 "Invalid target name (too short)");
874
875 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
876 exit_error(PARAMETER_PROBLEM,
877 "Invalid target name `%s' (%i chars max)",
878 targetname, sizeof(ip6t_chainlabel)-1);
879
880 for (ptr = targetname; *ptr; ptr++)
881 if (isspace(*ptr))
882 exit_error(PARAMETER_PROBLEM,
883 "Invalid target name `%s'", targetname);
884 return targetname;
885}
886
887int
Harald Welteed498492001-07-23 01:24:22 +0000888string_to_number(const char *s, unsigned int min, unsigned int max,
889 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000890{
Harald Welteed498492001-07-23 01:24:22 +0000891 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892 char *end;
893
894 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000895 errno = 0;
896 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897 if (*end == '\0' && end != s) {
898 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000899 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000900 *ret = number;
901 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000902 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 }
904 return -1;
905}
906
907static void
908set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
909 int invert)
910{
911 if (*options & option)
912 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
913 opt2char(option));
914 *options |= option;
915
916 if (invert) {
917 unsigned int i;
918 for (i = 0; 1 << i != option; i++);
919
920 if (!inverse_for_options[i])
921 exit_error(PARAMETER_PROBLEM,
922 "cannot have ! before -%c",
923 opt2char(option));
924 *invflg |= inverse_for_options[i];
925 }
926}
927
928struct ip6tables_target *
929find_target(const char *name, enum ip6t_tryload tryload)
930{
931 struct ip6tables_target *ptr;
932
933 /* Standard target? */
934 if (strcmp(name, "") == 0
935 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
936 || strcmp(name, IP6TC_LABEL_DROP) == 0
937 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
938 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
939 name = "standard";
940
941 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
942 if (strcmp(name, ptr->name) == 0)
943 break;
944 }
945
Harald Welte3efb6ea2001-08-06 18:50:21 +0000946#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000947 if (!ptr && tryload != DONT_LOAD) {
948 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
949 + strlen(name)];
950 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
951 if (dlopen(path, RTLD_NOW)) {
952 /* Found library. If it didn't register itself,
953 maybe they specified match as a target. */
954 ptr = find_target(name, DONT_LOAD);
955 if (!ptr)
956 exit_error(PARAMETER_PROBLEM,
957 "Couldn't load target `%s'\n",
958 name);
959 } else if (tryload == LOAD_MUST_SUCCEED)
960 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000961 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000962 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000963 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000964#else
965 if (ptr && !ptr->loaded) {
966 if (tryload != DONT_LOAD)
967 ptr->loaded = 1;
968 else
969 ptr = NULL;
970 }
Marc Boucher067477b2002-03-24 15:09:31 +0000971 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
972 exit_error(PARAMETER_PROBLEM,
973 "Couldn't find target `%s'\n", name);
974 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000975#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976
Harald Welte43ac1912002-03-03 09:43:07 +0000977 if (ptr)
978 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000979
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 return ptr;
981}
982
983static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000984merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000985 unsigned int *option_offset)
986{
987 unsigned int num_old, num_new, i;
988 struct option *merge;
989
990 for (num_old = 0; oldopts[num_old].name; num_old++);
991 for (num_new = 0; newopts[num_new].name; num_new++);
992
993 global_option_offset += OPTION_OFFSET;
994 *option_offset = global_option_offset;
995
996 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
997 memcpy(merge, oldopts, num_old * sizeof(struct option));
998 for (i = 0; i < num_new; i++) {
999 merge[num_old + i] = newopts[i];
1000 merge[num_old + i].val += *option_offset;
1001 }
1002 memset(merge + num_old + num_new, 0, sizeof(struct option));
1003
1004 return merge;
1005}
1006
1007void
1008register_match6(struct ip6tables_match *me)
1009{
Harald Welte43ac1912002-03-03 09:43:07 +00001010 struct ip6tables_match **i;
1011
Rusty Russell5eed48a2000-06-02 20:12:24 +00001012 if (strcmp(me->version, program_version) != 0) {
1013 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1014 program_name, me->name, me->version, program_version);
1015 exit(1);
1016 }
1017
1018 if (find_match(me->name, DONT_LOAD)) {
1019 fprintf(stderr, "%s: match `%s' already registered.\n",
1020 program_name, me->name);
1021 exit(1);
1022 }
1023
Harald Welte43ac1912002-03-03 09:43:07 +00001024 if (me->size != IP6T_ALIGN(me->size)) {
1025 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1026 program_name, me->name, me->size);
1027 exit(1);
1028 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001029
Harald Welte43ac1912002-03-03 09:43:07 +00001030 /* Append to list. */
1031 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1032 me->next = NULL;
1033 *i = me;
1034
Rusty Russell5eed48a2000-06-02 20:12:24 +00001035 me->m = NULL;
1036 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001037}
1038
1039void
1040register_target6(struct ip6tables_target *me)
1041{
1042 if (strcmp(me->version, program_version) != 0) {
1043 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1044 program_name, me->name, me->version, program_version);
1045 exit(1);
1046 }
1047
1048 if (find_target(me->name, DONT_LOAD)) {
1049 fprintf(stderr, "%s: target `%s' already registered.\n",
1050 program_name, me->name);
1051 exit(1);
1052 }
1053
Harald Welte43ac1912002-03-03 09:43:07 +00001054 if (me->size != IP6T_ALIGN(me->size)) {
1055 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1056 program_name, me->name, me->size);
1057 exit(1);
1058 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001059
Rusty Russell5eed48a2000-06-02 20:12:24 +00001060 /* Prepend to list. */
1061 me->next = ip6tables_targets;
1062 ip6tables_targets = me;
1063 me->t = NULL;
1064 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001065}
1066
1067static void
1068print_num(u_int64_t number, unsigned int format)
1069{
Harald Welte43ac1912002-03-03 09:43:07 +00001070 if (format & FMT_KILOMEGAGIGA) {
1071 if (number > 99999) {
1072 number = (number + 500) / 1000;
1073 if (number > 9999) {
1074 number = (number + 500) / 1000;
1075 if (number > 9999) {
1076 number = (number + 500) / 1000;
1077 if (number > 9999) {
1078 number = (number + 500) / 1000;
1079 printf(FMT("%4lluT ","%lluT "), number);
1080 }
1081 else printf(FMT("%4lluG ","%lluG "), number);
1082 }
1083 else printf(FMT("%4lluM ","%lluM "), number);
1084 } else
1085 printf(FMT("%4lluK ","%lluK "), number);
1086 } else
1087 printf(FMT("%5llu ","%llu "), number);
1088 } else
1089 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001090}
1091
Harald Welte43ac1912002-03-03 09:43:07 +00001092
Rusty Russell5eed48a2000-06-02 20:12:24 +00001093static void
1094print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1095{
1096 struct ip6t_counters counters;
1097 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1098 printf("Chain %s", chain);
1099 if (pol) {
1100 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001101 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001102 fputc(' ', stdout);
1103 print_num(counters.pcnt, (format|FMT_NOTABLE));
1104 fputs("packets, ", stdout);
1105 print_num(counters.bcnt, (format|FMT_NOTABLE));
1106 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001107 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001108 printf(")\n");
1109 } else {
1110 unsigned int refs;
1111 if (!ip6tc_get_references(&refs, chain, handle))
1112 printf(" (ERROR obtaining refs)\n");
1113 else
1114 printf(" (%u references)\n", refs);
1115 }
1116
1117 if (format & FMT_LINENUMBERS)
1118 printf(FMT("%-4s ", "%s "), "num");
1119 if (!(format & FMT_NOCOUNTS)) {
1120 if (format & FMT_KILOMEGAGIGA) {
1121 printf(FMT("%5s ","%s "), "pkts");
1122 printf(FMT("%5s ","%s "), "bytes");
1123 } else {
1124 printf(FMT("%8s ","%s "), "pkts");
1125 printf(FMT("%10s ","%s "), "bytes");
1126 }
1127 }
1128 if (!(format & FMT_NOTARGET))
1129 printf(FMT("%-9s ","%s "), "target");
1130 fputs(" prot ", stdout);
1131 if (format & FMT_OPTIONS)
1132 fputs("opt", stdout);
1133 if (format & FMT_VIA) {
1134 printf(FMT(" %-6s ","%s "), "in");
1135 printf(FMT("%-6s ","%s "), "out");
1136 }
1137 printf(FMT(" %-19s ","%s "), "source");
1138 printf(FMT(" %-19s "," %s "), "destination");
1139 printf("\n");
1140}
1141
Harald Welte43ac1912002-03-03 09:43:07 +00001142
Rusty Russell5eed48a2000-06-02 20:12:24 +00001143static int
1144print_match(const struct ip6t_entry_match *m,
1145 const struct ip6t_ip6 *ip,
1146 int numeric)
1147{
1148 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1149
1150 if (match) {
1151 if (match->print)
1152 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001153 else
1154 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001155 } else {
1156 if (m->u.user.name[0])
1157 printf("UNKNOWN match `%s' ", m->u.user.name);
1158 }
1159 /* Don't stop iterating. */
1160 return 0;
1161}
1162
1163/* e is called `fw' here for hysterical raisins */
1164static void
1165print_firewall(const struct ip6t_entry *fw,
1166 const char *targname,
1167 unsigned int num,
1168 unsigned int format,
1169 const ip6tc_handle_t handle)
1170{
1171 struct ip6tables_target *target = NULL;
1172 const struct ip6t_entry_target *t;
1173 u_int8_t flags;
1174 char buf[BUFSIZ];
1175
Rusty Russell5eed48a2000-06-02 20:12:24 +00001176 if (!ip6tc_is_chain(targname, handle))
1177 target = find_target(targname, TRY_LOAD);
1178 else
1179 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1180
1181 t = ip6t_get_target((struct ip6t_entry *)fw);
1182 flags = fw->ipv6.flags;
1183
1184 if (format & FMT_LINENUMBERS)
1185 printf(FMT("%-4u ", "%u "), num+1);
1186
1187 if (!(format & FMT_NOCOUNTS)) {
1188 print_num(fw->counters.pcnt, format);
1189 print_num(fw->counters.bcnt, format);
1190 }
1191
1192 if (!(format & FMT_NOTARGET))
1193 printf(FMT("%-9s ", "%s "), targname);
1194
1195 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1196 {
Harald Welte43ac1912002-03-03 09:43:07 +00001197 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001198 if (pname)
1199 printf(FMT("%-5s", "%s "), pname);
1200 else
1201 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1202 }
1203
1204 if (format & FMT_OPTIONS) {
1205 if (format & FMT_NOTABLE)
1206 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001207 fputc(' ', stdout); /* Invert flag of FRAG */
1208 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001209 fputc(' ', stdout);
1210 }
1211
1212 if (format & FMT_VIA) {
1213 char iface[IFNAMSIZ+2];
1214
1215 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1216 iface[0] = '!';
1217 iface[1] = '\0';
1218 }
1219 else iface[0] = '\0';
1220
1221 if (fw->ipv6.iniface[0] != '\0') {
1222 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001223 }
1224 else if (format & FMT_NUMERIC) strcat(iface, "*");
1225 else strcat(iface, "any");
1226 printf(FMT(" %-6s ","in %s "), iface);
1227
1228 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1229 iface[0] = '!';
1230 iface[1] = '\0';
1231 }
1232 else iface[0] = '\0';
1233
1234 if (fw->ipv6.outiface[0] != '\0') {
1235 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001236 }
1237 else if (format & FMT_NUMERIC) strcat(iface, "*");
1238 else strcat(iface, "any");
1239 printf(FMT("%-6s ","out %s "), iface);
1240 }
1241
1242 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1243 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1244 && !(format & FMT_NUMERIC))
1245 printf(FMT("%-19s ","%s "), "anywhere");
1246 else {
1247 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001248 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001249 else
Harald Welte43ac1912002-03-03 09:43:07 +00001250 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001251 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1252 printf(FMT("%-19s ","%s "), buf);
1253 }
1254
1255 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1256 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1257 && !(format & FMT_NUMERIC))
1258 printf(FMT("%-19s","-> %s"), "anywhere");
1259 else {
1260 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001261 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001262 else
Harald Welte43ac1912002-03-03 09:43:07 +00001263 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001264 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1265 printf(FMT("%-19s","-> %s"), buf);
1266 }
1267
1268 if (format & FMT_NOTABLE)
1269 fputs(" ", stdout);
1270
1271 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1272
1273 if (target) {
1274 if (target->print)
1275 /* Print the target information. */
1276 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1277 } else if (t->u.target_size != sizeof(*t))
1278 printf("[%u bytes of unknown target data] ",
1279 t->u.target_size - sizeof(*t));
1280
1281 if (!(format & FMT_NONEWLINE))
1282 fputc('\n', stdout);
1283}
1284
1285static void
1286print_firewall_line(const struct ip6t_entry *fw,
1287 const ip6tc_handle_t h)
1288{
1289 struct ip6t_entry_target *t;
1290
1291 t = ip6t_get_target((struct ip6t_entry *)fw);
1292 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1293}
1294
1295static int
1296append_entry(const ip6t_chainlabel chain,
1297 struct ip6t_entry *fw,
1298 unsigned int nsaddrs,
1299 const struct in6_addr saddrs[],
1300 unsigned int ndaddrs,
1301 const struct in6_addr daddrs[],
1302 int verbose,
1303 ip6tc_handle_t *handle)
1304{
1305 unsigned int i, j;
1306 int ret = 1;
1307
1308 for (i = 0; i < nsaddrs; i++) {
1309 fw->ipv6.src = saddrs[i];
1310 for (j = 0; j < ndaddrs; j++) {
1311 fw->ipv6.dst = daddrs[j];
1312 if (verbose)
1313 print_firewall_line(fw, *handle);
1314 ret &= ip6tc_append_entry(chain, fw, handle);
1315 }
1316 }
1317
1318 return ret;
1319}
1320
1321static int
1322replace_entry(const ip6t_chainlabel chain,
1323 struct ip6t_entry *fw,
1324 unsigned int rulenum,
1325 const struct in6_addr *saddr,
1326 const struct in6_addr *daddr,
1327 int verbose,
1328 ip6tc_handle_t *handle)
1329{
1330 fw->ipv6.src = *saddr;
1331 fw->ipv6.dst = *daddr;
1332
1333 if (verbose)
1334 print_firewall_line(fw, *handle);
1335 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1336}
1337
1338static int
1339insert_entry(const ip6t_chainlabel chain,
1340 struct ip6t_entry *fw,
1341 unsigned int rulenum,
1342 unsigned int nsaddrs,
1343 const struct in6_addr saddrs[],
1344 unsigned int ndaddrs,
1345 const struct in6_addr daddrs[],
1346 int verbose,
1347 ip6tc_handle_t *handle)
1348{
1349 unsigned int i, j;
1350 int ret = 1;
1351
1352 for (i = 0; i < nsaddrs; i++) {
1353 fw->ipv6.src = saddrs[i];
1354 for (j = 0; j < ndaddrs; j++) {
1355 fw->ipv6.dst = daddrs[j];
1356 if (verbose)
1357 print_firewall_line(fw, *handle);
1358 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1359 }
1360 }
1361
1362 return ret;
1363}
1364
1365static unsigned char *
1366make_delete_mask(struct ip6t_entry *fw)
1367{
1368 /* Establish mask for comparison */
1369 unsigned int size;
1370 struct ip6tables_match *m;
1371 unsigned char *mask, *mptr;
1372
1373 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001374 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001375 if (!m->used)
1376 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001377
1378 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1379 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001380
1381 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001382 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001383 + ip6tables_targets->size);
1384
1385 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1386 mptr = mask + sizeof(struct ip6t_entry);
1387
1388 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001389 if (!m->used)
1390 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001391
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001393 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1394 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001395 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396 }
1397
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001398 memset(mptr, 0xFF,
1399 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1400 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001401
1402 return mask;
1403}
1404
1405static int
1406delete_entry(const ip6t_chainlabel chain,
1407 struct ip6t_entry *fw,
1408 unsigned int nsaddrs,
1409 const struct in6_addr saddrs[],
1410 unsigned int ndaddrs,
1411 const struct in6_addr daddrs[],
1412 int verbose,
1413 ip6tc_handle_t *handle)
1414{
1415 unsigned int i, j;
1416 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 unsigned char *mask;
1418
1419 mask = make_delete_mask(fw);
1420 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001421 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001423 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424 if (verbose)
1425 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001426 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 }
1428 }
1429 return ret;
1430}
1431
András Kis-Szabó764316a2001-02-26 17:31:20 +00001432int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1434 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1435{
1436 int ret = 1;
1437 const char *chain;
1438 char *chains;
1439 unsigned int i, chaincount = 0;
1440
1441 chain = ip6tc_first_chain(handle);
1442 while (chain) {
1443 chaincount++;
1444 chain = ip6tc_next_chain(handle);
1445 }
1446
1447 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1448 i = 0;
1449 chain = ip6tc_first_chain(handle);
1450 while (chain) {
1451 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1452 i++;
1453 chain = ip6tc_next_chain(handle);
1454 }
1455
1456 for (i = 0; i < chaincount; i++) {
1457 if (!builtinstoo
1458 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1459 *handle))
1460 continue;
1461 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1462 }
1463
1464 free(chains);
1465 return ret;
1466}
1467
András Kis-Szabó764316a2001-02-26 17:31:20 +00001468int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469flush_entries(const ip6t_chainlabel chain, int verbose,
1470 ip6tc_handle_t *handle)
1471{
1472 if (!chain)
1473 return for_each_chain(flush_entries, verbose, 1, handle);
1474
1475 if (verbose)
1476 fprintf(stdout, "Flushing chain `%s'\n", chain);
1477 return ip6tc_flush_entries(chain, handle);
1478}
1479
1480static int
1481zero_entries(const ip6t_chainlabel chain, int verbose,
1482 ip6tc_handle_t *handle)
1483{
1484 if (!chain)
1485 return for_each_chain(zero_entries, verbose, 1, handle);
1486
1487 if (verbose)
1488 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1489 return ip6tc_zero_entries(chain, handle);
1490}
1491
András Kis-Szabó764316a2001-02-26 17:31:20 +00001492int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001493delete_chain(const ip6t_chainlabel chain, int verbose,
1494 ip6tc_handle_t *handle)
1495{
1496 if (!chain)
1497 return for_each_chain(delete_chain, verbose, 0, handle);
1498
1499 if (verbose)
1500 fprintf(stdout, "Deleting chain `%s'\n", chain);
1501 return ip6tc_delete_chain(chain, handle);
1502}
1503
1504static int
1505list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1506 int expanded, int linenumbers, ip6tc_handle_t *handle)
1507{
1508 int found = 0;
1509 unsigned int format;
1510 const char *this;
1511
1512 format = FMT_OPTIONS;
1513 if (!verbose)
1514 format |= FMT_NOCOUNTS;
1515 else
1516 format |= FMT_VIA;
1517
1518 if (numeric)
1519 format |= FMT_NUMERIC;
1520
1521 if (!expanded)
1522 format |= FMT_KILOMEGAGIGA;
1523
1524 if (linenumbers)
1525 format |= FMT_LINENUMBERS;
1526
1527 for (this = ip6tc_first_chain(handle);
1528 this;
1529 this = ip6tc_next_chain(handle)) {
1530 const struct ip6t_entry *i;
1531 unsigned int num;
1532
1533 if (chain && strcmp(chain, this) != 0)
1534 continue;
1535
1536 if (found) printf("\n");
1537
1538 print_header(format, this, handle);
1539 i = ip6tc_first_rule(this, handle);
1540
1541 num = 0;
1542 while (i) {
1543 print_firewall(i,
1544 ip6tc_get_target(i, handle),
1545 num++,
1546 format,
1547 *handle);
1548 i = ip6tc_next_rule(i, handle);
1549 }
1550 found = 1;
1551 }
1552
1553 errno = ENOENT;
1554 return found;
1555}
1556
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001557static char *get_modprobe(void)
1558{
Harald Welte43ac1912002-03-03 09:43:07 +00001559 int procfile;
1560 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001561
Harald Welte43ac1912002-03-03 09:43:07 +00001562 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1563 if (procfile < 0)
1564 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001565
Harald Welte43ac1912002-03-03 09:43:07 +00001566 ret = malloc(1024);
1567 if (ret) {
1568 switch (read(procfile, ret, 1024)) {
1569 case -1: goto fail;
1570 case 1024: goto fail; /* Partial read. Wierd */
1571 }
1572 if (ret[strlen(ret)-1]=='\n')
1573 ret[strlen(ret)-1]=0;
1574 close(procfile);
1575 return ret;
1576 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001577 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001578 free(ret);
1579 close(procfile);
1580 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001581}
1582
Harald Welte58918652001-06-16 18:25:25 +00001583int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001584{
Harald Welte43ac1912002-03-03 09:43:07 +00001585 char *buf = NULL;
1586 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001587
Harald Welte43ac1912002-03-03 09:43:07 +00001588 /* If they don't explicitly set it, read out of kernel */
1589 if (!modprobe) {
1590 buf = get_modprobe();
1591 if (!buf)
1592 return -1;
1593 modprobe = buf;
1594 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001595
Harald Welte43ac1912002-03-03 09:43:07 +00001596 switch (fork()) {
1597 case 0:
1598 argv[0] = (char *)modprobe;
1599 argv[1] = (char *)modname;
1600 argv[2] = NULL;
1601 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602
Harald Welte43ac1912002-03-03 09:43:07 +00001603 /* not usually reached */
1604 exit(0);
1605 case -1:
1606 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001607
Harald Welte43ac1912002-03-03 09:43:07 +00001608 default: /* parent */
1609 wait(NULL);
1610 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001611
Harald Welte43ac1912002-03-03 09:43:07 +00001612 free(buf);
1613 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001614}
1615
Rusty Russell5eed48a2000-06-02 20:12:24 +00001616static struct ip6t_entry *
1617generate_entry(const struct ip6t_entry *fw,
1618 struct ip6tables_match *matches,
1619 struct ip6t_entry_target *target)
1620{
1621 unsigned int size;
1622 struct ip6tables_match *m;
1623 struct ip6t_entry *e;
1624
1625 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001626 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001627 if (!m->used)
1628 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001629
Rusty Russell5eed48a2000-06-02 20:12:24 +00001630 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001631 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632
1633 e = fw_malloc(size + target->u.target_size);
1634 *e = *fw;
1635 e->target_offset = size;
1636 e->next_offset = size + target->u.target_size;
1637
1638 size = 0;
1639 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001640 if (!m->used)
1641 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001642
Rusty Russell5eed48a2000-06-02 20:12:24 +00001643 memcpy(e->elems + size, m->m, m->m->u.match_size);
1644 size += m->m->u.match_size;
1645 }
1646 memcpy(e->elems + size, target, target->u.target_size);
1647
1648 return e;
1649}
1650
1651int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1652{
1653 struct ip6t_entry fw, *e = NULL;
1654 int invert = 0;
1655 unsigned int nsaddrs = 0, ndaddrs = 0;
1656 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1657
1658 int c, verbose = 0;
1659 const char *chain = NULL;
1660 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1661 const char *policy = NULL, *newname = NULL;
1662 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001663 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 int ret = 1;
1665 struct ip6tables_match *m;
1666 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001667 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 const char *jumpto = "";
1669 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001670 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001671 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001672 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673
1674 memset(&fw, 0, sizeof(fw));
1675
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001676 opts = original_opts;
1677 global_option_offset = 0;
1678
Harald Welte43ac1912002-03-03 09:43:07 +00001679 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001680 * a second time */
1681 optind = 0;
1682
Harald Welte43ac1912002-03-03 09:43:07 +00001683 /* clear mflags in case do_command gets called a second time
1684 * (we clear the global list of all matches for security)*/
1685 for (m = ip6tables_matches; m; m = m->next) {
1686 m->mflags = 0;
1687 m->used = 0;
1688 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001689
Harald Welte43ac1912002-03-03 09:43:07 +00001690 for (t = ip6tables_targets; t; t = t->next) {
1691 t->tflags = 0;
1692 t->used = 0;
1693 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001694
Rusty Russell5eed48a2000-06-02 20:12:24 +00001695 /* Suppress error messages: we may add new options if we
1696 demand-load a protocol. */
1697 opterr = 0;
1698
1699 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001700 "-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 +00001701 opts, NULL)) != -1) {
1702 switch (c) {
1703 /*
1704 * Command selection
1705 */
1706 case 'A':
1707 add_command(&command, CMD_APPEND, CMD_NONE,
1708 invert);
1709 chain = optarg;
1710 break;
1711
1712 case 'D':
1713 add_command(&command, CMD_DELETE, CMD_NONE,
1714 invert);
1715 chain = optarg;
1716 if (optind < argc && argv[optind][0] != '-'
1717 && argv[optind][0] != '!') {
1718 rulenum = parse_rulenumber(argv[optind++]);
1719 command = CMD_DELETE_NUM;
1720 }
1721 break;
1722
Rusty Russell5eed48a2000-06-02 20:12:24 +00001723 case 'R':
1724 add_command(&command, CMD_REPLACE, CMD_NONE,
1725 invert);
1726 chain = optarg;
1727 if (optind < argc && argv[optind][0] != '-'
1728 && argv[optind][0] != '!')
1729 rulenum = parse_rulenumber(argv[optind++]);
1730 else
1731 exit_error(PARAMETER_PROBLEM,
1732 "-%c requires a rule number",
1733 cmd2char(CMD_REPLACE));
1734 break;
1735
1736 case 'I':
1737 add_command(&command, CMD_INSERT, CMD_NONE,
1738 invert);
1739 chain = optarg;
1740 if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!')
1742 rulenum = parse_rulenumber(argv[optind++]);
1743 else rulenum = 1;
1744 break;
1745
1746 case 'L':
1747 add_command(&command, CMD_LIST, CMD_ZERO,
1748 invert);
1749 if (optarg) chain = optarg;
1750 else if (optind < argc && argv[optind][0] != '-'
1751 && argv[optind][0] != '!')
1752 chain = argv[optind++];
1753 break;
1754
1755 case 'F':
1756 add_command(&command, CMD_FLUSH, CMD_NONE,
1757 invert);
1758 if (optarg) chain = optarg;
1759 else if (optind < argc && argv[optind][0] != '-'
1760 && argv[optind][0] != '!')
1761 chain = argv[optind++];
1762 break;
1763
1764 case 'Z':
1765 add_command(&command, CMD_ZERO, CMD_LIST,
1766 invert);
1767 if (optarg) chain = optarg;
1768 else if (optind < argc && argv[optind][0] != '-'
1769 && argv[optind][0] != '!')
1770 chain = argv[optind++];
1771 break;
1772
1773 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001774 if (optarg && *optarg == '-')
1775 exit_error(PARAMETER_PROBLEM,
1776 "chain name not allowed to start "
1777 "with `-'\n");
1778 if (find_target(optarg, TRY_LOAD))
1779 exit_error(PARAMETER_PROBLEM,
1780 "chain name may not clash "
1781 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001782 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1783 invert);
1784 chain = optarg;
1785 break;
1786
1787 case 'X':
1788 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1789 invert);
1790 if (optarg) chain = optarg;
1791 else if (optind < argc && argv[optind][0] != '-'
1792 && argv[optind][0] != '!')
1793 chain = argv[optind++];
1794 break;
1795
1796 case 'E':
1797 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1798 invert);
1799 chain = optarg;
1800 if (optind < argc && argv[optind][0] != '-'
1801 && argv[optind][0] != '!')
1802 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001803 else
1804 exit_error(PARAMETER_PROBLEM,
1805 "-%c requires old-chain-name and "
1806 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001807 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001808 break;
1809
1810 case 'P':
1811 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1812 invert);
1813 chain = optarg;
1814 if (optind < argc && argv[optind][0] != '-'
1815 && argv[optind][0] != '!')
1816 policy = argv[optind++];
1817 else
1818 exit_error(PARAMETER_PROBLEM,
1819 "-%c requires a chain and a policy",
1820 cmd2char(CMD_SET_POLICY));
1821 break;
1822
1823 case 'h':
1824 if (!optarg)
1825 optarg = argv[optind];
1826
1827 /* iptables -p icmp -h */
1828 if (!ip6tables_matches && protocol)
1829 find_match(protocol, TRY_LOAD);
1830
1831 exit_printhelp();
1832
1833 /*
1834 * Option selection
1835 */
1836 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001837 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001838 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1839 invert);
1840
1841 /* Canonicalize into lower case */
1842 for (protocol = argv[optind-1]; *protocol; protocol++)
1843 *protocol = tolower(*protocol);
1844
1845 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001846 if ( strcmp(protocol,"ipv6-icmp") == 0)
1847 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001848 fw.ipv6.proto = parse_protocol(protocol);
1849 fw.ipv6.flags |= IP6T_F_PROTO;
1850
1851 if (fw.ipv6.proto == 0
1852 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1853 exit_error(PARAMETER_PROBLEM,
1854 "rule would never match protocol");
1855 fw.nfcache |= NFC_IP6_PROTO;
1856 break;
1857
1858 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001859 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1861 invert);
1862 shostnetworkmask = argv[optind-1];
1863 fw.nfcache |= NFC_IP6_SRC;
1864 break;
1865
1866 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001867 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001868 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1869 invert);
1870 dhostnetworkmask = argv[optind-1];
1871 fw.nfcache |= NFC_IP6_DST;
1872 break;
1873
1874 case 'j':
1875 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1876 invert);
1877 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001878 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001879 target = find_target(jumpto, TRY_LOAD);
1880
1881 if (target) {
1882 size_t size;
1883
Harald Welte43ac1912002-03-03 09:43:07 +00001884 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1885 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886
1887 target->t = fw_calloc(1, size);
1888 target->t->u.target_size = size;
1889 strcpy(target->t->u.user.name, jumpto);
1890 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001891 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001892 }
1893 break;
1894
1895
1896 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001897 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1899 invert);
1900 parse_interface(argv[optind-1],
1901 fw.ipv6.iniface,
1902 fw.ipv6.iniface_mask);
1903 fw.nfcache |= NFC_IP6_IF_IN;
1904 break;
1905
1906 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001907 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001908 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1909 invert);
1910 parse_interface(argv[optind-1],
1911 fw.ipv6.outiface,
1912 fw.ipv6.outiface_mask);
1913 fw.nfcache |= NFC_IP6_IF_OUT;
1914 break;
1915
1916 case 'v':
1917 if (!verbose)
1918 set_option(&options, OPT_VERBOSE,
1919 &fw.ipv6.invflags, invert);
1920 verbose++;
1921 break;
1922
1923 case 'm': {
1924 size_t size;
1925
1926 if (invert)
1927 exit_error(PARAMETER_PROBLEM,
1928 "unexpected ! flag before --match");
1929
1930 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001931 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1932 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933 m->m = fw_calloc(1, size);
1934 m->m->u.match_size = size;
1935 strcpy(m->m->u.user.name, m->name);
1936 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001937 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001938 }
1939 break;
1940
1941 case 'n':
1942 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1943 invert);
1944 break;
1945
1946 case 't':
1947 if (invert)
1948 exit_error(PARAMETER_PROBLEM,
1949 "unexpected ! flag before --table");
1950 *table = argv[optind-1];
1951 break;
1952
1953 case 'x':
1954 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1955 invert);
1956 break;
1957
1958 case 'V':
1959 if (invert)
1960 printf("Not %s ;-)\n", program_version);
1961 else
1962 printf("%s v%s\n",
1963 program_name, program_version);
1964 exit(0);
1965
1966 case '0':
1967 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1968 invert);
1969 break;
1970
Harald Welte43ac1912002-03-03 09:43:07 +00001971 case 'M':
1972 modprobe = optarg;
1973 break;
1974
1975 case 'c':
1976
1977 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1978 invert);
1979 pcnt = optarg;
1980 if (optind < argc && argv[optind][0] != '-'
1981 && argv[optind][0] != '!')
1982 bcnt = argv[optind++];
1983 else
1984 exit_error(PARAMETER_PROBLEM,
1985 "-%c requires packet and byte counter",
1986 opt2char(OPT_COUNTERS));
1987
1988 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1989 exit_error(PARAMETER_PROBLEM,
1990 "-%c packet counter not numeric",
1991 opt2char(OPT_COUNTERS));
1992
1993 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1994 exit_error(PARAMETER_PROBLEM,
1995 "-%c byte counter not numeric",
1996 opt2char(OPT_COUNTERS));
1997
1998 break;
1999
2000
Rusty Russell5eed48a2000-06-02 20:12:24 +00002001 case 1: /* non option */
2002 if (optarg[0] == '!' && optarg[1] == '\0') {
2003 if (invert)
2004 exit_error(PARAMETER_PROBLEM,
2005 "multiple consecutive ! not"
2006 " allowed");
2007 invert = TRUE;
2008 optarg[0] = '\0';
2009 continue;
2010 }
2011 printf("Bad argument `%s'\n", optarg);
2012 exit_tryhelp(2);
2013
2014 default:
2015 /* FIXME: This scheme doesn't allow two of the same
2016 matches --RR */
2017 if (!target
2018 || !(target->parse(c - target->option_offset,
2019 argv, invert,
2020 &target->tflags,
2021 &fw, &target->t))) {
2022 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002023 if (!m->used)
2024 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002025
Rusty Russell5eed48a2000-06-02 20:12:24 +00002026 if (m->parse(c - m->option_offset,
2027 argv, invert,
2028 &m->mflags,
2029 &fw,
2030 &fw.nfcache,
2031 &m->m))
2032 break;
2033 }
Harald Welte00f5a9c2002-08-26 14:37:35 +00002034
2035 /* If you listen carefully, you can
2036 actually hear this code suck. */
2037
2038 /* some explanations (after four different bugs
2039 * in 3 different releases): If we encountere a
2040 * parameter, that has not been parsed yet,
2041 * it's not an option of an explicitly loaded
2042 * match or a target. However, we support
2043 * implicit loading of the protocol match
2044 * extension. '-p tcp' means 'l4 proto 6' and
2045 * at the same time 'load tcp protocol match on
2046 * demand if we specify --dport'.
2047 *
2048 * To make this work, we need to make sure:
2049 * - the parameter has not been parsed by
2050 * a match (m above)
2051 * - a protocol has been specified
2052 * - the protocol extension has not been
2053 * loaded yet, or is loaded and unused
2054 * [think of iptables-restore!]
2055 * - the protocol extension can be successively
2056 * loaded
2057 */
2058 if (m == NULL
2059 && protocol
2060 && (!find_proto(protocol, DONT_LOAD,
2061 options&OPT_NUMERIC)
2062 || (find_proto(protocol, DONT_LOAD,
2063 options&OPT_NUMERIC)
2064 && (proto_used == 0))
2065 )
2066 && (m = find_proto(protocol, TRY_LOAD,
2067 options&OPT_NUMERIC))) {
2068 /* Try loading protocol */
2069 size_t size;
2070
2071 proto_used = 1;
2072
2073 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2074 + m->size;
2075
2076 m->m = fw_calloc(1, size);
2077 m->m->u.match_size = size;
2078 strcpy(m->m->u.user.name, m->name);
2079 m->init(m->m, &fw.nfcache);
2080
2081 opts = merge_options(opts,
2082 m->extra_opts, &m->option_offset);
2083
2084 optind--;
2085 continue;
2086 }
2087
Rusty Russell5eed48a2000-06-02 20:12:24 +00002088 if (!m)
2089 exit_error(PARAMETER_PROBLEM,
2090 "Unknown arg `%s'",
2091 argv[optind-1]);
2092 }
2093 }
2094 invert = FALSE;
2095 }
2096
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002097 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002098 if (!m->used)
2099 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002100
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002101 m->final_check(m->mflags);
2102 }
2103
Harald Welte43ac1912002-03-03 09:43:07 +00002104 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002105 target->final_check(target->tflags);
2106
2107 /* Fix me: must put inverse options checking here --MN */
2108
2109 if (optind < argc)
2110 exit_error(PARAMETER_PROBLEM,
2111 "unknown arguments found on commandline");
2112 if (!command)
2113 exit_error(PARAMETER_PROBLEM, "no command specified");
2114 if (invert)
2115 exit_error(PARAMETER_PROBLEM,
2116 "nothing appropriate following !");
2117
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002118 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002119 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002120 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002121 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002122 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002123 }
2124
2125 if (shostnetworkmask)
2126 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2127 &(fw.ipv6.smsk), &nsaddrs);
2128
2129 if (dhostnetworkmask)
2130 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2131 &(fw.ipv6.dmsk), &ndaddrs);
2132
2133 if ((nsaddrs > 1 || ndaddrs > 1) &&
2134 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2135 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2136 " source or destination IP addresses");
2137
Rusty Russell5eed48a2000-06-02 20:12:24 +00002138 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2139 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2140 "specify a unique address");
2141
2142 generic_opt_check(command, options);
2143
2144 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2145 exit_error(PARAMETER_PROBLEM,
2146 "chain name `%s' too long (must be under %i chars)",
2147 chain, IP6T_FUNCTION_MAXNAMELEN);
2148
Harald Welte43ac1912002-03-03 09:43:07 +00002149 /* only allocate handle if we weren't called with a handle */
2150 if (!*handle)
2151 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002152
Harald Welte43ac1912002-03-03 09:43:07 +00002153 if (!*handle) {
2154 /* try to insmod the module if iptc_init failed */
2155 ip6tables_insmod("ip6_tables", modprobe);
2156 *handle = ip6tc_init(*table);
2157 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002158
Harald Welte43ac1912002-03-03 09:43:07 +00002159 if (!*handle)
2160 exit_error(VERSION_PROBLEM,
2161 "can't initialize ip6tables table `%s': %s",
2162 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002163
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002164 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002165 || command == CMD_DELETE
2166 || command == CMD_INSERT
2167 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002168 if (strcmp(chain, "PREROUTING") == 0
2169 || strcmp(chain, "INPUT") == 0) {
2170 /* -o not valid with incoming packets. */
2171 if (options & OPT_VIANAMEOUT)
2172 exit_error(PARAMETER_PROBLEM,
2173 "Can't use -%c with %s\n",
2174 opt2char(OPT_VIANAMEOUT),
2175 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002176 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002177
Harald Welte43ac1912002-03-03 09:43:07 +00002178 if (strcmp(chain, "POSTROUTING") == 0
2179 || strcmp(chain, "OUTPUT") == 0) {
2180 /* -i not valid with outgoing packets */
2181 if (options & OPT_VIANAMEIN)
2182 exit_error(PARAMETER_PROBLEM,
2183 "Can't use -%c with %s\n",
2184 opt2char(OPT_VIANAMEIN),
2185 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002186 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002187
2188 if (target && ip6tc_is_chain(jumpto, *handle)) {
2189 printf("Warning: using chain %s, not extension\n",
2190 jumpto);
2191
2192 target = NULL;
2193 }
2194
2195 /* If they didn't specify a target, or it's a chain
2196 name, use standard. */
2197 if (!target
2198 && (strlen(jumpto) == 0
2199 || ip6tc_is_chain(jumpto, *handle))) {
2200 size_t size;
2201
2202 target = find_target(IP6T_STANDARD_TARGET,
2203 LOAD_MUST_SUCCEED);
2204
2205 size = sizeof(struct ip6t_entry_target)
2206 + target->size;
2207 target->t = fw_calloc(1, size);
2208 target->t->u.target_size = size;
2209 strcpy(target->t->u.user.name, jumpto);
2210 target->init(target->t, &fw.nfcache);
2211 }
2212
2213 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002214 /* it is no chain, and we can't load a plugin.
2215 * We cannot know if the plugin is corrupt, non
2216 * existant OR if the user just misspelled a
2217 * chain. */
2218 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002219 } else {
2220 e = generate_entry(&fw, ip6tables_matches, target->t);
2221 }
2222 }
2223
2224 switch (command) {
2225 case CMD_APPEND:
2226 ret = append_entry(chain, e,
2227 nsaddrs, saddrs, ndaddrs, daddrs,
2228 options&OPT_VERBOSE,
2229 handle);
2230 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002231 case CMD_DELETE:
2232 ret = delete_entry(chain, e,
2233 nsaddrs, saddrs, ndaddrs, daddrs,
2234 options&OPT_VERBOSE,
2235 handle);
2236 break;
2237 case CMD_DELETE_NUM:
2238 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2239 break;
2240 case CMD_REPLACE:
2241 ret = replace_entry(chain, e, rulenum - 1,
2242 saddrs, daddrs, options&OPT_VERBOSE,
2243 handle);
2244 break;
2245 case CMD_INSERT:
2246 ret = insert_entry(chain, e, rulenum - 1,
2247 nsaddrs, saddrs, ndaddrs, daddrs,
2248 options&OPT_VERBOSE,
2249 handle);
2250 break;
2251 case CMD_LIST:
2252 ret = list_entries(chain,
2253 options&OPT_VERBOSE,
2254 options&OPT_NUMERIC,
2255 options&OPT_EXPANDED,
2256 options&OPT_LINENUMBERS,
2257 handle);
2258 break;
2259 case CMD_FLUSH:
2260 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2261 break;
2262 case CMD_ZERO:
2263 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2264 break;
2265 case CMD_LIST|CMD_ZERO:
2266 ret = list_entries(chain,
2267 options&OPT_VERBOSE,
2268 options&OPT_NUMERIC,
2269 options&OPT_EXPANDED,
2270 options&OPT_LINENUMBERS,
2271 handle);
2272 if (ret)
2273 ret = zero_entries(chain,
2274 options&OPT_VERBOSE, handle);
2275 break;
2276 case CMD_NEW_CHAIN:
2277 ret = ip6tc_create_chain(chain, handle);
2278 break;
2279 case CMD_DELETE_CHAIN:
2280 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2281 break;
2282 case CMD_RENAME_CHAIN:
2283 ret = ip6tc_rename_chain(chain, newname, handle);
2284 break;
2285 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002286 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002287 break;
2288 default:
2289 /* We should never reach this... */
2290 exit_tryhelp(2);
2291 }
2292
2293 if (verbose > 1)
2294 dump_entries6(*handle);
2295
2296 return ret;
2297}