blob: dad9052d74f423970c47618a6b8ae7d9221fb239 [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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
31#include <ip6tables.h>
32#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000033#include <unistd.h>
34#include <fcntl.h>
35#include <sys/wait.h>
36#include <sys/types.h>
37#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000038
39#ifndef TRUE
40#define TRUE 1
41#endif
42#ifndef FALSE
43#define FALSE 0
44#endif
45
46#ifndef IP6T_LIB_DIR
47#define IP6T_LIB_DIR "/usr/local/lib/iptables"
48#endif
49
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000050#ifndef PROC_SYS_MODPROBE
51#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
52#endif
53
Rusty Russell5eed48a2000-06-02 20:12:24 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
81#define CMD_CHECK 0x0800U
82#define CMD_RENAME_CHAIN 0x1000U
83#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
85 'N', 'X', 'P', 'C', 'E' };
86
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
106 { "append", 1, 0, 'A' },
107 { "delete", 1, 0, 'D' },
108 { "insert", 1, 0, 'I' },
109 { "replace", 1, 0, 'R' },
110 { "list", 2, 0, 'L' },
111 { "flush", 2, 0, 'F' },
112 { "zero", 2, 0, 'Z' },
113 { "check", 1, 0, 'C' },
114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
116 { "rename-chain", 2, 0, 'E' },
117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
122 { "protocol", 1, 0, 'p' },
123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "version", 0, 0, 'V' },
132 { "help", 2, 0, 'h' },
133 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000134 { "modprobe", 1, 0, 'M' },
135 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000136 { 0 }
137};
138
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000139#ifndef __OPTIMIZE__
140struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000141ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000142{
143 return (void *)e + e->target_offset;
144}
145#endif
146
Rusty Russell5eed48a2000-06-02 20:12:24 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
150/* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
162 /* -n -s -d -p -j -v -x -i -o --line */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
165/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
166/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
167/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
168/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
176};
177
178static int inverse_for_options[NUMBER_OF_OPT] =
179{
180/* -n */ 0,
181/* -s */ IP6T_INV_SRCIP,
182/* -d */ IP6T_INV_DSTIP,
183/* -p */ IP6T_INV_PROTO,
184/* -j */ 0,
185/* -v */ 0,
186/* -x */ 0,
187/* -i */ IP6T_INV_VIA_IN,
188/* -o */ IP6T_INV_VIA_OUT,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
195/* Keeping track of external matches and targets: linked lists. */
196struct ip6tables_match *ip6tables_matches = NULL;
197struct ip6tables_target *ip6tables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries6(const ip6tc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000209/* Primitive headers... */
210/* defined in netinet/in.h */
211#if 0
212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
218#endif
219
Rusty Russell5eed48a2000-06-02 20:12:24 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000223 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226 { "all", 0 },
227};
228
229static char *
230proto_to_name(u_int8_t proto, int nolookup)
231{
232 unsigned int i;
233
234 if (proto && !nolookup) {
235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247static void
248in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
249{
250 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000251 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000252}
253
254void
255exit_error(enum exittype status, char *msg, ...)
256{
257 va_list args;
258
259 va_start(args, msg);
260 fprintf(stderr, "%s v%s: ", program_name, program_version);
261 vfprintf(stderr, msg, args);
262 va_end(args);
263 fprintf(stderr, "\n");
264 if (status == PARAMETER_PROBLEM)
265 exit_tryhelp(status);
266 if (status == VERSION_PROBLEM)
267 fprintf(stderr,
268 "Perhaps iptables or your kernel needs to be upgraded.\n");
269 exit(status);
270}
271
272void
273exit_tryhelp(int status)
274{
275 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
276 program_name, program_name );
277 exit(status);
278}
279
280void
281exit_printhelp(void)
282{
283 struct ip6tables_match *m = NULL;
284 struct ip6tables_target *t = NULL;
285
286 printf("%s v%s\n\n"
287"Usage: %s -[ADC] chain rule-specification [options]\n"
288" %s -[RI] chain rulenum rule-specification [options]\n"
289" %s -D chain rulenum [options]\n"
290" %s -[LFZ] [chain] [options]\n"
291" %s -[NX] chain\n"
292" %s -E old-chain-name new-chain-name\n"
293" %s -P chain target [options]\n"
294" %s -h (print this help information)\n\n",
295 program_name, program_version, program_name, program_name,
296 program_name, program_name, program_name, program_name,
297 program_name, program_name);
298
299 printf(
300"Commands:\n"
301"Either long or short options are allowed.\n"
302" --append -A chain Append to chain\n"
303" --delete -D chain Delete matching rule from chain\n"
304" --delete -D chain rulenum\n"
305" Delete rule rulenum (1 = first) from chain\n"
306" --insert -I chain [rulenum]\n"
307" Insert in chain as rulenum (default 1=first)\n"
308" --replace -R chain rulenum\n"
309" Replace rule rulenum (1 = first) in chain\n"
310" --list -L [chain] List the rules in a chain or all chains\n"
311" --flush -F [chain] Delete all rules in chain or all chains\n"
312" --zero -Z [chain] Zero counters in chain or all chains\n"
313" --check -C chain Test this packet on chain\n"
314" --new -N chain Create a new user-defined chain\n"
315" --delete-chain\n"
316" -X [chain] Delete a user-defined chain\n"
317" --policy -P chain target\n"
318" Change policy on chain to target\n"
319" --rename-chain\n"
320" -E old-chain new-chain\n"
321" Change chain name, (moving any references)\n"
322
323"Options:\n"
324" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
325" --source -s [!] address[/mask]\n"
326" source specification\n"
327" --destination -d [!] address[/mask]\n"
328" destination specification\n"
329" --in-interface -i [!] input name[+]\n"
330" network interface name ([+] for wildcard)\n"
331" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000332" target for rule (may load target extension)\n"
333" --match -m match\n"
334" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335" --numeric -n numeric output of addresses and ports\n"
336" --out-interface -o [!] output name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --table -t table table to manipulate (default: `filter')\n"
339" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000340" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000341" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000342/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000343" --modprobe=<command> try to insert modules using this command\n"
344" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000345"[!] --version -V print package version.\n");
346
347 /* Print out any special helps. A user might like to be able to add a --help
348 to the commandline, and see expected results. So we call help for all
349 matches & targets */
350 for (t=ip6tables_targets;t;t=t->next) {
351 printf("\n");
352 t->help();
353 }
354 for (m=ip6tables_matches;m;m=m->next) {
355 printf("\n");
356 m->help();
357 }
358 exit(0);
359}
360
361static void
362generic_opt_check(int command, int options)
363{
364 int i, j, legal = 0;
365
366 /* Check that commands are valid with options. Complicated by the
367 * fact that if an option is legal with *any* command given, it is
368 * legal overall (ie. -z and -l).
369 */
370 for (i = 0; i < NUMBER_OF_OPT; i++) {
371 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
372
373 for (j = 0; j < NUMBER_OF_CMD; j++) {
374 if (!(command & (1<<j)))
375 continue;
376
377 if (!(options & (1<<i))) {
378 if (commands_v_options[j][i] == '+')
379 exit_error(PARAMETER_PROBLEM,
380 "You need to supply the `-%c' "
381 "option for this command\n",
382 optflags[i]);
383 } else {
384 if (commands_v_options[j][i] != 'x')
385 legal = 1;
386 else if (legal == 0)
387 legal = -1;
388 }
389 }
390 if (legal == -1)
391 exit_error(PARAMETER_PROBLEM,
392 "Illegal option `-%c' with this command\n",
393 optflags[i]);
394 }
395}
396
397static char
398opt2char(int option)
399{
400 const char *ptr;
401 for (ptr = optflags; option > 1; option >>= 1, ptr++);
402
403 return *ptr;
404}
405
406static char
407cmd2char(int option)
408{
409 const char *ptr;
410 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
411
412 return *ptr;
413}
414
415static void
416add_command(int *cmd, const int newcmd, const int othercmds, int invert)
417{
418 if (invert)
419 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
420 if (*cmd & (~othercmds))
421 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
422 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
423 *cmd |= newcmd;
424}
425
426int
Harald Welteb77f1da2002-03-14 11:35:58 +0000427check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000428{
429 if (option && strcmp(option, "!") == 0) {
430 if (*invert)
431 exit_error(PARAMETER_PROBLEM,
432 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000433 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000434 if (optind) {
435 *optind = *optind+1;
436 if (argc && *optind > argc)
437 exit_error(PARAMETER_PROBLEM,
438 "no argument following `!'");
439 }
440
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441 return TRUE;
442 }
443 return FALSE;
444}
445
446static void *
447fw_calloc(size_t count, size_t size)
448{
449 void *p;
450
451 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000452 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000453 exit(1);
454 }
455 return p;
456}
457
458static void *
459fw_malloc(size_t size)
460{
461 void *p;
462
463 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000464 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000465 exit(1);
466 }
467 return p;
468}
469
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470static char *
471addr_to_numeric(const struct in6_addr *addrp)
472{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000473 /* 0000:0000:0000:0000:0000:000.000.000.000
474 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000475 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000476 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477}
478
479static struct in6_addr *
480numeric_to_addr(const char *num)
481{
482 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000483 int err;
484 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000485 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000486#ifdef DEBUG
487 fprintf(stderr, "\nnumeric2addr: %d\n", err);
488#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000489 return (struct in6_addr *)NULL;
490}
491
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000492
493static struct in6_addr *
494host_to_addr(const char *name, unsigned int *naddr)
495{
496 struct addrinfo hints;
497 struct addrinfo *res;
498 static struct in6_addr *addr;
499 int err;
500
501 memset(&hints, 0, sizeof(hints));
502 hints.ai_flags=AI_CANONNAME;
503 hints.ai_family=AF_INET6;
504 hints.ai_socktype=SOCK_RAW;
505 hints.ai_protocol=41;
506 hints.ai_next=NULL;
507
508 *naddr = 0;
509 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
510#ifdef DEBUG
511 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
512#endif
513 return (struct in6_addr *) NULL;
514 } else {
515 if (res->ai_family != AF_INET6 ||
516 res->ai_addrlen != sizeof(struct sockaddr_in6))
517 return (struct in6_addr *) NULL;
518
519#ifdef DEBUG
520 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
521 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
522#endif
523 /* Get the first element of the address-chain */
524 addr = fw_calloc(1, sizeof(struct in6_addr));
525 in6addrcpy(addr, (struct in6_addr *)
526 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
527 freeaddrinfo(res);
528 *naddr = 1;
529 return addr;
530 }
531
532 return (struct in6_addr *) NULL;
533}
534
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000535static char *
536addr_to_host(const struct in6_addr *addr)
537{
538 struct sockaddr_in6 saddr;
539 int err;
540 static char hostname[NI_MAXHOST];
541
542 memset(&saddr, 0, sizeof(struct sockaddr_in6));
543 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000544 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000545
546 if ( (err=getnameinfo((struct sockaddr *)&saddr,
547 sizeof(struct sockaddr_in6),
548 hostname, sizeof(hostname)-1,
549 NULL, 0, 0)) != 0 ){
550#ifdef DEBUG
551 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
552#endif
553 return (char *) NULL;
554 } else {
555#ifdef DEBUG
556 fprintf (stderr, "\naddr2host: %s\n", hostname);
557#endif
558
559 return hostname;
560 }
561
562 return (char *) NULL;
563}
564
Rusty Russell5eed48a2000-06-02 20:12:24 +0000565static char *
566mask_to_numeric(const struct in6_addr *addrp)
567{
568 static char buf[20];
569 int l = ipv6_prefix_length(addrp);
570 if (l == -1)
571 return addr_to_numeric(addrp);
Harald Welte43ac1912002-03-03 09:43:07 +0000572 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000573 return buf;
574}
575
576static struct in6_addr *
577network_to_addr(const char *name)
578{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000579 /* abort();*/
580 /* TODO: not implemented yet, but the exception breaks the
581 * name resolvation */
582 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000583}
584
585static char *
586addr_to_anyname(const struct in6_addr *addr)
587{
588 char *name;
589
590 if ((name = addr_to_host(addr)) != NULL)
591 return name;
592
593 return addr_to_numeric(addr);
594}
595
596/*
597 * All functions starting with "parse" should succeed, otherwise
598 * the program fails.
599 * Most routines return pointers to static data that may change
600 * between calls to the same or other routines with a few exceptions:
601 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
602 * return global static data.
603*/
604
605static struct in6_addr *
606parse_hostnetwork(const char *name, unsigned int *naddrs)
607{
608 struct in6_addr *addrp, *addrptmp;
609
610 if ((addrptmp = numeric_to_addr(name)) != NULL ||
611 (addrptmp = network_to_addr(name)) != NULL) {
612 addrp = fw_malloc(sizeof(struct in6_addr));
613 in6addrcpy(addrp, addrptmp);
614 *naddrs = 1;
615 return addrp;
616 }
617 if ((addrp = host_to_addr(name, naddrs)) != NULL)
618 return addrp;
619
620 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
621}
622
623static struct in6_addr *
624parse_mask(char *mask)
625{
626 static struct in6_addr maskaddr;
627 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000628 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000629
630 if (mask == NULL) {
631 /* no mask at all defaults to 128 bits */
632 memset(&maskaddr, 0xff, sizeof maskaddr);
633 return &maskaddr;
634 }
635 if ((addrp = numeric_to_addr(mask)) != NULL)
636 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000637 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000638 exit_error(PARAMETER_PROBLEM,
639 "invalid mask `%s' specified", mask);
640 if (bits != 0) {
641 char *p = (char *)&maskaddr;
642 memset(p, 0xff, bits / 8);
643 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
644 p[bits / 8] = 0xff << (8 - (bits & 7));
645 return &maskaddr;
646 }
647
648 memset(&maskaddr, 0, sizeof maskaddr);
649 return &maskaddr;
650}
651
Harald Welte43ac1912002-03-03 09:43:07 +0000652void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000653parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
654 struct in6_addr *maskp, unsigned int *naddrs)
655{
656 struct in6_addr *addrp;
657 char buf[256];
658 char *p;
659 int i, j, n;
660
661 strncpy(buf, name, sizeof(buf) - 1);
662 if ((p = strrchr(buf, '/')) != NULL) {
663 *p = '\0';
664 addrp = parse_mask(p + 1);
665 } else
666 addrp = parse_mask(NULL);
667 in6addrcpy(maskp, addrp);
668
669 /* if a null mask is given, the name is ignored, like in "any/0" */
670 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
671 strcpy(buf, "::");
672
673 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
674 n = *naddrs;
675 for (i = 0, j = 0; i < n; i++) {
676 int k;
677 for (k = 0; k < 4; k++)
678 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
679 j++;
680 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000681 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000682 (*naddrs)--;
683 j--;
684 break;
685 }
686 }
687 }
688}
689
690struct ip6tables_match *
691find_match(const char *name, enum ip6t_tryload tryload)
692{
693 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000694 int icmphack = 0;
695
696 /* This is ugly as hell. Nonetheless, there is no way of changing
697 * this without hurting backwards compatibility */
698 if ( (strcmp(name,"icmpv6") == 0) ||
699 (strcmp(name,"ipv6-icmp") == 0) ||
700 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
701
702 if (!icmphack) {
703 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
704 if (strcmp(name, ptr->name) == 0)
705 break;
706 }
707 } else {
708 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
709 if (strcmp("icmp6", ptr->name) == 0)
710 break;
711 }
712 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000713
Harald Welte3efb6ea2001-08-06 18:50:21 +0000714#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000715 if (!ptr && tryload != DONT_LOAD) {
716 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
717 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000718 if (!icmphack)
719 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
720 else
721 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000722 if (dlopen(path, RTLD_NOW)) {
723 /* Found library. If it didn't register itself,
724 maybe they specified target as match. */
725 ptr = find_match(name, DONT_LOAD);
726
727 if (!ptr)
728 exit_error(PARAMETER_PROBLEM,
729 "Couldn't load match `%s'\n",
730 name);
731 } else if (tryload == LOAD_MUST_SUCCEED)
732 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000733 "Couldn't load match `%s':%s\n",
734 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000735 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000736#else
737 if (ptr && !ptr->loaded) {
738 if (tryload != DONT_LOAD)
739 ptr->loaded = 1;
740 else
741 ptr = NULL;
742 }
Marc Boucher067477b2002-03-24 15:09:31 +0000743 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
744 exit_error(PARAMETER_PROBLEM,
745 "Couldn't find match `%s'\n", name);
746 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000747#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000748
Harald Welte43ac1912002-03-03 09:43:07 +0000749 if (ptr)
750 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000751
752
Rusty Russell5eed48a2000-06-02 20:12:24 +0000753 return ptr;
754}
755
756/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
757static struct ip6tables_match *
758find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
759{
Harald Welteed498492001-07-23 01:24:22 +0000760 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761
Harald Welte43ac1912002-03-03 09:43:07 +0000762 if (string_to_number(pname, 0, 255, &proto) != -1) {
763 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000764
Harald Welte43ac1912002-03-03 09:43:07 +0000765 if (protoname)
766 return find_match(protoname, tryload);
767 } else
768 return find_match(pname, tryload);
769
770 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771}
772
Harald Welte43ac1912002-03-03 09:43:07 +0000773u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000774parse_protocol(const char *s)
775{
Harald Welteed498492001-07-23 01:24:22 +0000776 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777
Harald Welteed498492001-07-23 01:24:22 +0000778 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000779 struct protoent *pent;
780
781 if ((pent = getprotobyname(s)))
782 proto = pent->p_proto;
783 else {
784 unsigned int i;
785 for (i = 0;
786 i < sizeof(chain_protos)/sizeof(struct pprot);
787 i++) {
788 if (strcmp(s, chain_protos[i].name) == 0) {
789 proto = chain_protos[i].num;
790 break;
791 }
792 }
793 if (i == sizeof(chain_protos)/sizeof(struct pprot))
794 exit_error(PARAMETER_PROBLEM,
795 "unknown protocol `%s' specified",
796 s);
797 }
798 }
799
800 return (u_int16_t)proto;
801}
802
803static void
804parse_interface(const char *arg, char *vianame, unsigned char *mask)
805{
806 int vialen = strlen(arg);
807 unsigned int i;
808
809 memset(mask, 0, IFNAMSIZ);
810 memset(vianame, 0, IFNAMSIZ);
811
812 if (vialen + 1 > IFNAMSIZ)
813 exit_error(PARAMETER_PROBLEM,
814 "interface name `%s' must be shorter than IFNAMSIZ"
815 " (%i)", arg, IFNAMSIZ-1);
816
817 strcpy(vianame, arg);
818 if (vialen == 0)
819 memset(mask, 0, IFNAMSIZ);
820 else if (vianame[vialen - 1] == '+') {
821 memset(mask, 0xFF, vialen - 1);
822 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000823 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000824 } else {
825 /* Include nul-terminator in match */
826 memset(mask, 0xFF, vialen + 1);
827 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000828 for (i = 0; vianame[i]; i++) {
829 if (!isalnum(vianame[i])
830 && vianame[i] != '_'
831 && vianame[i] != '.') {
832 printf("Warning: wierd character in interface"
833 " `%s' (No aliases, :, ! or *).\n",
834 vianame);
835 break;
836 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000837 }
838 }
839}
840
841/* Can't be zero. */
842static int
843parse_rulenumber(const char *rule)
844{
Harald Welte43ac1912002-03-03 09:43:07 +0000845 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846
Harald Welte43ac1912002-03-03 09:43:07 +0000847 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000848 exit_error(PARAMETER_PROBLEM,
849 "Invalid rule number `%s'", rule);
850
851 return rulenum;
852}
853
854static const char *
855parse_target(const char *targetname)
856{
857 const char *ptr;
858
859 if (strlen(targetname) < 1)
860 exit_error(PARAMETER_PROBLEM,
861 "Invalid target name (too short)");
862
863 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
864 exit_error(PARAMETER_PROBLEM,
865 "Invalid target name `%s' (%i chars max)",
866 targetname, sizeof(ip6t_chainlabel)-1);
867
868 for (ptr = targetname; *ptr; ptr++)
869 if (isspace(*ptr))
870 exit_error(PARAMETER_PROBLEM,
871 "Invalid target name `%s'", targetname);
872 return targetname;
873}
874
875int
Harald Welteed498492001-07-23 01:24:22 +0000876string_to_number(const char *s, unsigned int min, unsigned int max,
877 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000878{
Harald Welteed498492001-07-23 01:24:22 +0000879 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000880 char *end;
881
882 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000883 errno = 0;
884 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000885 if (*end == '\0' && end != s) {
886 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000887 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000888 *ret = number;
889 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000890 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000891 }
892 return -1;
893}
894
895static void
896set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
897 int invert)
898{
899 if (*options & option)
900 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
901 opt2char(option));
902 *options |= option;
903
904 if (invert) {
905 unsigned int i;
906 for (i = 0; 1 << i != option; i++);
907
908 if (!inverse_for_options[i])
909 exit_error(PARAMETER_PROBLEM,
910 "cannot have ! before -%c",
911 opt2char(option));
912 *invflg |= inverse_for_options[i];
913 }
914}
915
916struct ip6tables_target *
917find_target(const char *name, enum ip6t_tryload tryload)
918{
919 struct ip6tables_target *ptr;
920
921 /* Standard target? */
922 if (strcmp(name, "") == 0
923 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
924 || strcmp(name, IP6TC_LABEL_DROP) == 0
925 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
926 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
927 name = "standard";
928
929 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
930 if (strcmp(name, ptr->name) == 0)
931 break;
932 }
933
Harald Welte3efb6ea2001-08-06 18:50:21 +0000934#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000935 if (!ptr && tryload != DONT_LOAD) {
936 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
937 + strlen(name)];
938 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
939 if (dlopen(path, RTLD_NOW)) {
940 /* Found library. If it didn't register itself,
941 maybe they specified match as a target. */
942 ptr = find_target(name, DONT_LOAD);
943 if (!ptr)
944 exit_error(PARAMETER_PROBLEM,
945 "Couldn't load target `%s'\n",
946 name);
947 } else if (tryload == LOAD_MUST_SUCCEED)
948 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000949 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000950 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000951 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000952#else
953 if (ptr && !ptr->loaded) {
954 if (tryload != DONT_LOAD)
955 ptr->loaded = 1;
956 else
957 ptr = NULL;
958 }
Marc Boucher067477b2002-03-24 15:09:31 +0000959 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
960 exit_error(PARAMETER_PROBLEM,
961 "Couldn't find target `%s'\n", name);
962 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000963#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964
Harald Welte43ac1912002-03-03 09:43:07 +0000965 if (ptr)
966 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000967
Rusty Russell5eed48a2000-06-02 20:12:24 +0000968 return ptr;
969}
970
971static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000972merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000973 unsigned int *option_offset)
974{
975 unsigned int num_old, num_new, i;
976 struct option *merge;
977
978 for (num_old = 0; oldopts[num_old].name; num_old++);
979 for (num_new = 0; newopts[num_new].name; num_new++);
980
981 global_option_offset += OPTION_OFFSET;
982 *option_offset = global_option_offset;
983
984 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
985 memcpy(merge, oldopts, num_old * sizeof(struct option));
986 for (i = 0; i < num_new; i++) {
987 merge[num_old + i] = newopts[i];
988 merge[num_old + i].val += *option_offset;
989 }
990 memset(merge + num_old + num_new, 0, sizeof(struct option));
991
992 return merge;
993}
994
995void
996register_match6(struct ip6tables_match *me)
997{
Harald Welte43ac1912002-03-03 09:43:07 +0000998 struct ip6tables_match **i;
999
Rusty Russell5eed48a2000-06-02 20:12:24 +00001000 if (strcmp(me->version, program_version) != 0) {
1001 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1002 program_name, me->name, me->version, program_version);
1003 exit(1);
1004 }
1005
1006 if (find_match(me->name, DONT_LOAD)) {
1007 fprintf(stderr, "%s: match `%s' already registered.\n",
1008 program_name, me->name);
1009 exit(1);
1010 }
1011
Harald Welte43ac1912002-03-03 09:43:07 +00001012 if (me->size != IP6T_ALIGN(me->size)) {
1013 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1014 program_name, me->name, me->size);
1015 exit(1);
1016 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001017
Harald Welte43ac1912002-03-03 09:43:07 +00001018 /* Append to list. */
1019 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1020 me->next = NULL;
1021 *i = me;
1022
Rusty Russell5eed48a2000-06-02 20:12:24 +00001023 me->m = NULL;
1024 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001025}
1026
1027void
1028register_target6(struct ip6tables_target *me)
1029{
1030 if (strcmp(me->version, program_version) != 0) {
1031 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1032 program_name, me->name, me->version, program_version);
1033 exit(1);
1034 }
1035
1036 if (find_target(me->name, DONT_LOAD)) {
1037 fprintf(stderr, "%s: target `%s' already registered.\n",
1038 program_name, me->name);
1039 exit(1);
1040 }
1041
Harald Welte43ac1912002-03-03 09:43:07 +00001042 if (me->size != IP6T_ALIGN(me->size)) {
1043 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1044 program_name, me->name, me->size);
1045 exit(1);
1046 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001047
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 /* Prepend to list. */
1049 me->next = ip6tables_targets;
1050 ip6tables_targets = me;
1051 me->t = NULL;
1052 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001053}
1054
1055static void
1056print_num(u_int64_t number, unsigned int format)
1057{
Harald Welte43ac1912002-03-03 09:43:07 +00001058 if (format & FMT_KILOMEGAGIGA) {
1059 if (number > 99999) {
1060 number = (number + 500) / 1000;
1061 if (number > 9999) {
1062 number = (number + 500) / 1000;
1063 if (number > 9999) {
1064 number = (number + 500) / 1000;
1065 if (number > 9999) {
1066 number = (number + 500) / 1000;
1067 printf(FMT("%4lluT ","%lluT "), number);
1068 }
1069 else printf(FMT("%4lluG ","%lluG "), number);
1070 }
1071 else printf(FMT("%4lluM ","%lluM "), number);
1072 } else
1073 printf(FMT("%4lluK ","%lluK "), number);
1074 } else
1075 printf(FMT("%5llu ","%llu "), number);
1076 } else
1077 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001078}
1079
Harald Welte43ac1912002-03-03 09:43:07 +00001080
Rusty Russell5eed48a2000-06-02 20:12:24 +00001081static void
1082print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1083{
1084 struct ip6t_counters counters;
1085 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1086 printf("Chain %s", chain);
1087 if (pol) {
1088 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001089 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001090 fputc(' ', stdout);
1091 print_num(counters.pcnt, (format|FMT_NOTABLE));
1092 fputs("packets, ", stdout);
1093 print_num(counters.bcnt, (format|FMT_NOTABLE));
1094 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001095 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001096 printf(")\n");
1097 } else {
1098 unsigned int refs;
1099 if (!ip6tc_get_references(&refs, chain, handle))
1100 printf(" (ERROR obtaining refs)\n");
1101 else
1102 printf(" (%u references)\n", refs);
1103 }
1104
1105 if (format & FMT_LINENUMBERS)
1106 printf(FMT("%-4s ", "%s "), "num");
1107 if (!(format & FMT_NOCOUNTS)) {
1108 if (format & FMT_KILOMEGAGIGA) {
1109 printf(FMT("%5s ","%s "), "pkts");
1110 printf(FMT("%5s ","%s "), "bytes");
1111 } else {
1112 printf(FMT("%8s ","%s "), "pkts");
1113 printf(FMT("%10s ","%s "), "bytes");
1114 }
1115 }
1116 if (!(format & FMT_NOTARGET))
1117 printf(FMT("%-9s ","%s "), "target");
1118 fputs(" prot ", stdout);
1119 if (format & FMT_OPTIONS)
1120 fputs("opt", stdout);
1121 if (format & FMT_VIA) {
1122 printf(FMT(" %-6s ","%s "), "in");
1123 printf(FMT("%-6s ","%s "), "out");
1124 }
1125 printf(FMT(" %-19s ","%s "), "source");
1126 printf(FMT(" %-19s "," %s "), "destination");
1127 printf("\n");
1128}
1129
Harald Welte43ac1912002-03-03 09:43:07 +00001130
Rusty Russell5eed48a2000-06-02 20:12:24 +00001131static int
1132print_match(const struct ip6t_entry_match *m,
1133 const struct ip6t_ip6 *ip,
1134 int numeric)
1135{
1136 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1137
1138 if (match) {
1139 if (match->print)
1140 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001141 else
1142 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001143 } else {
1144 if (m->u.user.name[0])
1145 printf("UNKNOWN match `%s' ", m->u.user.name);
1146 }
1147 /* Don't stop iterating. */
1148 return 0;
1149}
1150
1151/* e is called `fw' here for hysterical raisins */
1152static void
1153print_firewall(const struct ip6t_entry *fw,
1154 const char *targname,
1155 unsigned int num,
1156 unsigned int format,
1157 const ip6tc_handle_t handle)
1158{
1159 struct ip6tables_target *target = NULL;
1160 const struct ip6t_entry_target *t;
1161 u_int8_t flags;
1162 char buf[BUFSIZ];
1163
1164 /* User creates a chain called "REJECT": this overrides the
1165 `REJECT' target module. Keep feeding them rope until the
1166 revolution... Bwahahahahah */
1167 if (!ip6tc_is_chain(targname, handle))
1168 target = find_target(targname, TRY_LOAD);
1169 else
1170 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1171
1172 t = ip6t_get_target((struct ip6t_entry *)fw);
1173 flags = fw->ipv6.flags;
1174
1175 if (format & FMT_LINENUMBERS)
1176 printf(FMT("%-4u ", "%u "), num+1);
1177
1178 if (!(format & FMT_NOCOUNTS)) {
1179 print_num(fw->counters.pcnt, format);
1180 print_num(fw->counters.bcnt, format);
1181 }
1182
1183 if (!(format & FMT_NOTARGET))
1184 printf(FMT("%-9s ", "%s "), targname);
1185
1186 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1187 {
Harald Welte43ac1912002-03-03 09:43:07 +00001188 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189 if (pname)
1190 printf(FMT("%-5s", "%s "), pname);
1191 else
1192 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1193 }
1194
1195 if (format & FMT_OPTIONS) {
1196 if (format & FMT_NOTABLE)
1197 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001198 fputc(' ', stdout); /* Invert flag of FRAG */
1199 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001200 fputc(' ', stdout);
1201 }
1202
1203 if (format & FMT_VIA) {
1204 char iface[IFNAMSIZ+2];
1205
1206 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1207 iface[0] = '!';
1208 iface[1] = '\0';
1209 }
1210 else iface[0] = '\0';
1211
1212 if (fw->ipv6.iniface[0] != '\0') {
1213 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001214 }
1215 else if (format & FMT_NUMERIC) strcat(iface, "*");
1216 else strcat(iface, "any");
1217 printf(FMT(" %-6s ","in %s "), iface);
1218
1219 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1220 iface[0] = '!';
1221 iface[1] = '\0';
1222 }
1223 else iface[0] = '\0';
1224
1225 if (fw->ipv6.outiface[0] != '\0') {
1226 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001227 }
1228 else if (format & FMT_NUMERIC) strcat(iface, "*");
1229 else strcat(iface, "any");
1230 printf(FMT("%-6s ","out %s "), iface);
1231 }
1232
1233 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1234 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1235 && !(format & FMT_NUMERIC))
1236 printf(FMT("%-19s ","%s "), "anywhere");
1237 else {
1238 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001239 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 else
Harald Welte43ac1912002-03-03 09:43:07 +00001241 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1243 printf(FMT("%-19s ","%s "), buf);
1244 }
1245
1246 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1247 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1248 && !(format & FMT_NUMERIC))
1249 printf(FMT("%-19s","-> %s"), "anywhere");
1250 else {
1251 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001252 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001253 else
Harald Welte43ac1912002-03-03 09:43:07 +00001254 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001255 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1256 printf(FMT("%-19s","-> %s"), buf);
1257 }
1258
1259 if (format & FMT_NOTABLE)
1260 fputs(" ", stdout);
1261
1262 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1263
1264 if (target) {
1265 if (target->print)
1266 /* Print the target information. */
1267 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1268 } else if (t->u.target_size != sizeof(*t))
1269 printf("[%u bytes of unknown target data] ",
1270 t->u.target_size - sizeof(*t));
1271
1272 if (!(format & FMT_NONEWLINE))
1273 fputc('\n', stdout);
1274}
1275
1276static void
1277print_firewall_line(const struct ip6t_entry *fw,
1278 const ip6tc_handle_t h)
1279{
1280 struct ip6t_entry_target *t;
1281
1282 t = ip6t_get_target((struct ip6t_entry *)fw);
1283 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1284}
1285
1286static int
1287append_entry(const ip6t_chainlabel chain,
1288 struct ip6t_entry *fw,
1289 unsigned int nsaddrs,
1290 const struct in6_addr saddrs[],
1291 unsigned int ndaddrs,
1292 const struct in6_addr daddrs[],
1293 int verbose,
1294 ip6tc_handle_t *handle)
1295{
1296 unsigned int i, j;
1297 int ret = 1;
1298
1299 for (i = 0; i < nsaddrs; i++) {
1300 fw->ipv6.src = saddrs[i];
1301 for (j = 0; j < ndaddrs; j++) {
1302 fw->ipv6.dst = daddrs[j];
1303 if (verbose)
1304 print_firewall_line(fw, *handle);
1305 ret &= ip6tc_append_entry(chain, fw, handle);
1306 }
1307 }
1308
1309 return ret;
1310}
1311
1312static int
1313replace_entry(const ip6t_chainlabel chain,
1314 struct ip6t_entry *fw,
1315 unsigned int rulenum,
1316 const struct in6_addr *saddr,
1317 const struct in6_addr *daddr,
1318 int verbose,
1319 ip6tc_handle_t *handle)
1320{
1321 fw->ipv6.src = *saddr;
1322 fw->ipv6.dst = *daddr;
1323
1324 if (verbose)
1325 print_firewall_line(fw, *handle);
1326 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1327}
1328
1329static int
1330insert_entry(const ip6t_chainlabel chain,
1331 struct ip6t_entry *fw,
1332 unsigned int rulenum,
1333 unsigned int nsaddrs,
1334 const struct in6_addr saddrs[],
1335 unsigned int ndaddrs,
1336 const struct in6_addr daddrs[],
1337 int verbose,
1338 ip6tc_handle_t *handle)
1339{
1340 unsigned int i, j;
1341 int ret = 1;
1342
1343 for (i = 0; i < nsaddrs; i++) {
1344 fw->ipv6.src = saddrs[i];
1345 for (j = 0; j < ndaddrs; j++) {
1346 fw->ipv6.dst = daddrs[j];
1347 if (verbose)
1348 print_firewall_line(fw, *handle);
1349 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1350 }
1351 }
1352
1353 return ret;
1354}
1355
1356static unsigned char *
1357make_delete_mask(struct ip6t_entry *fw)
1358{
1359 /* Establish mask for comparison */
1360 unsigned int size;
1361 struct ip6tables_match *m;
1362 unsigned char *mask, *mptr;
1363
1364 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001365 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001366 if (!m->used)
1367 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001368
1369 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1370 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001371
1372 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001373 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001374 + ip6tables_targets->size);
1375
1376 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1377 mptr = mask + sizeof(struct ip6t_entry);
1378
1379 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001380 if (!m->used)
1381 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001382
Rusty Russell5eed48a2000-06-02 20:12:24 +00001383 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001384 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1385 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001386 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387 }
1388
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001389 memset(mptr, 0xFF,
1390 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1391 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392
1393 return mask;
1394}
1395
1396static int
1397delete_entry(const ip6t_chainlabel chain,
1398 struct ip6t_entry *fw,
1399 unsigned int nsaddrs,
1400 const struct in6_addr saddrs[],
1401 unsigned int ndaddrs,
1402 const struct in6_addr daddrs[],
1403 int verbose,
1404 ip6tc_handle_t *handle)
1405{
1406 unsigned int i, j;
1407 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001408 unsigned char *mask;
1409
1410 mask = make_delete_mask(fw);
1411 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001412 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001414 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001415 if (verbose)
1416 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001417 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418 }
1419 }
1420 return ret;
1421}
1422
1423static int
1424check_packet(const ip6t_chainlabel chain,
1425 struct ip6t_entry *fw,
1426 unsigned int nsaddrs,
1427 const struct in6_addr saddrs[],
1428 unsigned int ndaddrs,
1429 const struct in6_addr daddrs[],
1430 int verbose,
1431 ip6tc_handle_t *handle)
1432{
1433 int ret = 1;
1434 unsigned int i, j;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001435 const char *msg;
1436
1437 for (i = 0; i < nsaddrs; i++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001438 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001439 for (j = 0; j < ndaddrs; j++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001440 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001441 if (verbose)
1442 print_firewall_line(fw, *handle);
Harald Welte43ac1912002-03-03 09:43:07 +00001443 msg = ip6tc_check_packet(chain, fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001444 if (!msg) ret = 0;
1445 else printf("%s\n", msg);
1446 }
1447 }
1448
1449 return ret;
1450}
1451
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001452/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001453int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1455 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1456{
1457 int ret = 1;
1458 const char *chain;
1459 char *chains;
1460 unsigned int i, chaincount = 0;
1461
1462 chain = ip6tc_first_chain(handle);
1463 while (chain) {
1464 chaincount++;
1465 chain = ip6tc_next_chain(handle);
1466 }
1467
1468 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1469 i = 0;
1470 chain = ip6tc_first_chain(handle);
1471 while (chain) {
1472 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1473 i++;
1474 chain = ip6tc_next_chain(handle);
1475 }
1476
1477 for (i = 0; i < chaincount; i++) {
1478 if (!builtinstoo
1479 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1480 *handle))
1481 continue;
1482 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1483 }
1484
1485 free(chains);
1486 return ret;
1487}
1488
András Kis-Szabó764316a2001-02-26 17:31:20 +00001489int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001490flush_entries(const ip6t_chainlabel chain, int verbose,
1491 ip6tc_handle_t *handle)
1492{
1493 if (!chain)
1494 return for_each_chain(flush_entries, verbose, 1, handle);
1495
1496 if (verbose)
1497 fprintf(stdout, "Flushing chain `%s'\n", chain);
1498 return ip6tc_flush_entries(chain, handle);
1499}
1500
1501static int
1502zero_entries(const ip6t_chainlabel chain, int verbose,
1503 ip6tc_handle_t *handle)
1504{
1505 if (!chain)
1506 return for_each_chain(zero_entries, verbose, 1, handle);
1507
1508 if (verbose)
1509 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1510 return ip6tc_zero_entries(chain, handle);
1511}
1512
András Kis-Szabó764316a2001-02-26 17:31:20 +00001513int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514delete_chain(const ip6t_chainlabel chain, int verbose,
1515 ip6tc_handle_t *handle)
1516{
1517 if (!chain)
1518 return for_each_chain(delete_chain, verbose, 0, handle);
1519
1520 if (verbose)
1521 fprintf(stdout, "Deleting chain `%s'\n", chain);
1522 return ip6tc_delete_chain(chain, handle);
1523}
1524
1525static int
1526list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1527 int expanded, int linenumbers, ip6tc_handle_t *handle)
1528{
1529 int found = 0;
1530 unsigned int format;
1531 const char *this;
1532
1533 format = FMT_OPTIONS;
1534 if (!verbose)
1535 format |= FMT_NOCOUNTS;
1536 else
1537 format |= FMT_VIA;
1538
1539 if (numeric)
1540 format |= FMT_NUMERIC;
1541
1542 if (!expanded)
1543 format |= FMT_KILOMEGAGIGA;
1544
1545 if (linenumbers)
1546 format |= FMT_LINENUMBERS;
1547
1548 for (this = ip6tc_first_chain(handle);
1549 this;
1550 this = ip6tc_next_chain(handle)) {
1551 const struct ip6t_entry *i;
1552 unsigned int num;
1553
1554 if (chain && strcmp(chain, this) != 0)
1555 continue;
1556
1557 if (found) printf("\n");
1558
1559 print_header(format, this, handle);
1560 i = ip6tc_first_rule(this, handle);
1561
1562 num = 0;
1563 while (i) {
1564 print_firewall(i,
1565 ip6tc_get_target(i, handle),
1566 num++,
1567 format,
1568 *handle);
1569 i = ip6tc_next_rule(i, handle);
1570 }
1571 found = 1;
1572 }
1573
1574 errno = ENOENT;
1575 return found;
1576}
1577
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001578static char *get_modprobe(void)
1579{
Harald Welte43ac1912002-03-03 09:43:07 +00001580 int procfile;
1581 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001582
Harald Welte43ac1912002-03-03 09:43:07 +00001583 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1584 if (procfile < 0)
1585 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001586
Harald Welte43ac1912002-03-03 09:43:07 +00001587 ret = malloc(1024);
1588 if (ret) {
1589 switch (read(procfile, ret, 1024)) {
1590 case -1: goto fail;
1591 case 1024: goto fail; /* Partial read. Wierd */
1592 }
1593 if (ret[strlen(ret)-1]=='\n')
1594 ret[strlen(ret)-1]=0;
1595 close(procfile);
1596 return ret;
1597 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001598 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001599 free(ret);
1600 close(procfile);
1601 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602}
1603
Harald Welte58918652001-06-16 18:25:25 +00001604int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001605{
Harald Welte43ac1912002-03-03 09:43:07 +00001606 char *buf = NULL;
1607 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001608
Harald Welte43ac1912002-03-03 09:43:07 +00001609 /* If they don't explicitly set it, read out of kernel */
1610 if (!modprobe) {
1611 buf = get_modprobe();
1612 if (!buf)
1613 return -1;
1614 modprobe = buf;
1615 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001616
Harald Welte43ac1912002-03-03 09:43:07 +00001617 switch (fork()) {
1618 case 0:
1619 argv[0] = (char *)modprobe;
1620 argv[1] = (char *)modname;
1621 argv[2] = NULL;
1622 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623
Harald Welte43ac1912002-03-03 09:43:07 +00001624 /* not usually reached */
1625 exit(0);
1626 case -1:
1627 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001628
Harald Welte43ac1912002-03-03 09:43:07 +00001629 default: /* parent */
1630 wait(NULL);
1631 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001632
Harald Welte43ac1912002-03-03 09:43:07 +00001633 free(buf);
1634 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001635}
1636
Rusty Russell5eed48a2000-06-02 20:12:24 +00001637static struct ip6t_entry *
1638generate_entry(const struct ip6t_entry *fw,
1639 struct ip6tables_match *matches,
1640 struct ip6t_entry_target *target)
1641{
1642 unsigned int size;
1643 struct ip6tables_match *m;
1644 struct ip6t_entry *e;
1645
1646 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001647 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001648 if (!m->used)
1649 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001650
Rusty Russell5eed48a2000-06-02 20:12:24 +00001651 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001652 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653
1654 e = fw_malloc(size + target->u.target_size);
1655 *e = *fw;
1656 e->target_offset = size;
1657 e->next_offset = size + target->u.target_size;
1658
1659 size = 0;
1660 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001661 if (!m->used)
1662 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001663
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 memcpy(e->elems + size, m->m, m->m->u.match_size);
1665 size += m->m->u.match_size;
1666 }
1667 memcpy(e->elems + size, target, target->u.target_size);
1668
1669 return e;
1670}
1671
1672int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1673{
1674 struct ip6t_entry fw, *e = NULL;
1675 int invert = 0;
1676 unsigned int nsaddrs = 0, ndaddrs = 0;
1677 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1678
1679 int c, verbose = 0;
1680 const char *chain = NULL;
1681 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1682 const char *policy = NULL, *newname = NULL;
1683 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001684 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001685 int ret = 1;
1686 struct ip6tables_match *m;
1687 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001688 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001689 const char *jumpto = "";
1690 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001691 const char *modprobe = NULL;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001692 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693
1694 memset(&fw, 0, sizeof(fw));
1695
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001696 opts = original_opts;
1697 global_option_offset = 0;
1698
Harald Welte43ac1912002-03-03 09:43:07 +00001699 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001700 * a second time */
1701 optind = 0;
1702
Harald Welte43ac1912002-03-03 09:43:07 +00001703 /* clear mflags in case do_command gets called a second time
1704 * (we clear the global list of all matches for security)*/
1705 for (m = ip6tables_matches; m; m = m->next) {
1706 m->mflags = 0;
1707 m->used = 0;
1708 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001709
Harald Welte43ac1912002-03-03 09:43:07 +00001710 for (t = ip6tables_targets; t; t = t->next) {
1711 t->tflags = 0;
1712 t->used = 0;
1713 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001714
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 /* Suppress error messages: we may add new options if we
1716 demand-load a protocol. */
1717 opterr = 0;
1718
1719 while ((c = getopt_long(argc, argv,
Harald Welte43ac1912002-03-03 09:43:07 +00001720 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001721 opts, NULL)) != -1) {
1722 switch (c) {
1723 /*
1724 * Command selection
1725 */
1726 case 'A':
1727 add_command(&command, CMD_APPEND, CMD_NONE,
1728 invert);
1729 chain = optarg;
1730 break;
1731
1732 case 'D':
1733 add_command(&command, CMD_DELETE, CMD_NONE,
1734 invert);
1735 chain = optarg;
1736 if (optind < argc && argv[optind][0] != '-'
1737 && argv[optind][0] != '!') {
1738 rulenum = parse_rulenumber(argv[optind++]);
1739 command = CMD_DELETE_NUM;
1740 }
1741 break;
1742
1743 case 'C':
1744 add_command(&command, CMD_CHECK, CMD_NONE,
1745 invert);
1746 chain = optarg;
1747 break;
1748
1749 case 'R':
1750 add_command(&command, CMD_REPLACE, CMD_NONE,
1751 invert);
1752 chain = optarg;
1753 if (optind < argc && argv[optind][0] != '-'
1754 && argv[optind][0] != '!')
1755 rulenum = parse_rulenumber(argv[optind++]);
1756 else
1757 exit_error(PARAMETER_PROBLEM,
1758 "-%c requires a rule number",
1759 cmd2char(CMD_REPLACE));
1760 break;
1761
1762 case 'I':
1763 add_command(&command, CMD_INSERT, CMD_NONE,
1764 invert);
1765 chain = optarg;
1766 if (optind < argc && argv[optind][0] != '-'
1767 && argv[optind][0] != '!')
1768 rulenum = parse_rulenumber(argv[optind++]);
1769 else rulenum = 1;
1770 break;
1771
1772 case 'L':
1773 add_command(&command, CMD_LIST, CMD_ZERO,
1774 invert);
1775 if (optarg) chain = optarg;
1776 else if (optind < argc && argv[optind][0] != '-'
1777 && argv[optind][0] != '!')
1778 chain = argv[optind++];
1779 break;
1780
1781 case 'F':
1782 add_command(&command, CMD_FLUSH, CMD_NONE,
1783 invert);
1784 if (optarg) chain = optarg;
1785 else if (optind < argc && argv[optind][0] != '-'
1786 && argv[optind][0] != '!')
1787 chain = argv[optind++];
1788 break;
1789
1790 case 'Z':
1791 add_command(&command, CMD_ZERO, CMD_LIST,
1792 invert);
1793 if (optarg) chain = optarg;
1794 else if (optind < argc && argv[optind][0] != '-'
1795 && argv[optind][0] != '!')
1796 chain = argv[optind++];
1797 break;
1798
1799 case 'N':
1800 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1801 invert);
1802 chain = optarg;
1803 break;
1804
1805 case 'X':
1806 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1807 invert);
1808 if (optarg) chain = optarg;
1809 else if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 chain = argv[optind++];
1812 break;
1813
1814 case 'E':
1815 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1816 invert);
1817 chain = optarg;
1818 if (optind < argc && argv[optind][0] != '-'
1819 && argv[optind][0] != '!')
1820 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001821 else
1822 exit_error(PARAMETER_PROBLEM,
1823 "-%c requires old-chain-name and "
1824 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001825 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001826 break;
1827
1828 case 'P':
1829 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1830 invert);
1831 chain = optarg;
1832 if (optind < argc && argv[optind][0] != '-'
1833 && argv[optind][0] != '!')
1834 policy = argv[optind++];
1835 else
1836 exit_error(PARAMETER_PROBLEM,
1837 "-%c requires a chain and a policy",
1838 cmd2char(CMD_SET_POLICY));
1839 break;
1840
1841 case 'h':
1842 if (!optarg)
1843 optarg = argv[optind];
1844
1845 /* iptables -p icmp -h */
1846 if (!ip6tables_matches && protocol)
1847 find_match(protocol, TRY_LOAD);
1848
1849 exit_printhelp();
1850
1851 /*
1852 * Option selection
1853 */
1854 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001855 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001856 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1857 invert);
1858
1859 /* Canonicalize into lower case */
1860 for (protocol = argv[optind-1]; *protocol; protocol++)
1861 *protocol = tolower(*protocol);
1862
1863 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001864 if ( strcmp(protocol,"ipv6-icmp") == 0)
1865 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866 fw.ipv6.proto = parse_protocol(protocol);
1867 fw.ipv6.flags |= IP6T_F_PROTO;
1868
1869 if (fw.ipv6.proto == 0
1870 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1871 exit_error(PARAMETER_PROBLEM,
1872 "rule would never match protocol");
1873 fw.nfcache |= NFC_IP6_PROTO;
1874 break;
1875
1876 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001877 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1879 invert);
1880 shostnetworkmask = argv[optind-1];
1881 fw.nfcache |= NFC_IP6_SRC;
1882 break;
1883
1884 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001885 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1887 invert);
1888 dhostnetworkmask = argv[optind-1];
1889 fw.nfcache |= NFC_IP6_DST;
1890 break;
1891
1892 case 'j':
1893 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1894 invert);
1895 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001896 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897 target = find_target(jumpto, TRY_LOAD);
1898
1899 if (target) {
1900 size_t size;
1901
Harald Welte43ac1912002-03-03 09:43:07 +00001902 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1903 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001904
1905 target->t = fw_calloc(1, size);
1906 target->t->u.target_size = size;
1907 strcpy(target->t->u.user.name, jumpto);
1908 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001909 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001910 }
1911 break;
1912
1913
1914 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001915 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001916 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1917 invert);
1918 parse_interface(argv[optind-1],
1919 fw.ipv6.iniface,
1920 fw.ipv6.iniface_mask);
1921 fw.nfcache |= NFC_IP6_IF_IN;
1922 break;
1923
1924 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001925 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001926 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1927 invert);
1928 parse_interface(argv[optind-1],
1929 fw.ipv6.outiface,
1930 fw.ipv6.outiface_mask);
1931 fw.nfcache |= NFC_IP6_IF_OUT;
1932 break;
1933
1934 case 'v':
1935 if (!verbose)
1936 set_option(&options, OPT_VERBOSE,
1937 &fw.ipv6.invflags, invert);
1938 verbose++;
1939 break;
1940
1941 case 'm': {
1942 size_t size;
1943
1944 if (invert)
1945 exit_error(PARAMETER_PROBLEM,
1946 "unexpected ! flag before --match");
1947
1948 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001949 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1950 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 m->m = fw_calloc(1, size);
1952 m->m->u.match_size = size;
1953 strcpy(m->m->u.user.name, m->name);
1954 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001955 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001956 }
1957 break;
1958
1959 case 'n':
1960 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1961 invert);
1962 break;
1963
1964 case 't':
1965 if (invert)
1966 exit_error(PARAMETER_PROBLEM,
1967 "unexpected ! flag before --table");
1968 *table = argv[optind-1];
1969 break;
1970
1971 case 'x':
1972 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1973 invert);
1974 break;
1975
1976 case 'V':
1977 if (invert)
1978 printf("Not %s ;-)\n", program_version);
1979 else
1980 printf("%s v%s\n",
1981 program_name, program_version);
1982 exit(0);
1983
1984 case '0':
1985 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1986 invert);
1987 break;
1988
Harald Welte43ac1912002-03-03 09:43:07 +00001989 case 'M':
1990 modprobe = optarg;
1991 break;
1992
1993 case 'c':
1994
1995 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1996 invert);
1997 pcnt = optarg;
1998 if (optind < argc && argv[optind][0] != '-'
1999 && argv[optind][0] != '!')
2000 bcnt = argv[optind++];
2001 else
2002 exit_error(PARAMETER_PROBLEM,
2003 "-%c requires packet and byte counter",
2004 opt2char(OPT_COUNTERS));
2005
2006 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2007 exit_error(PARAMETER_PROBLEM,
2008 "-%c packet counter not numeric",
2009 opt2char(OPT_COUNTERS));
2010
2011 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2012 exit_error(PARAMETER_PROBLEM,
2013 "-%c byte counter not numeric",
2014 opt2char(OPT_COUNTERS));
2015
2016 break;
2017
2018
Rusty Russell5eed48a2000-06-02 20:12:24 +00002019 case 1: /* non option */
2020 if (optarg[0] == '!' && optarg[1] == '\0') {
2021 if (invert)
2022 exit_error(PARAMETER_PROBLEM,
2023 "multiple consecutive ! not"
2024 " allowed");
2025 invert = TRUE;
2026 optarg[0] = '\0';
2027 continue;
2028 }
2029 printf("Bad argument `%s'\n", optarg);
2030 exit_tryhelp(2);
2031
2032 default:
2033 /* FIXME: This scheme doesn't allow two of the same
2034 matches --RR */
2035 if (!target
2036 || !(target->parse(c - target->option_offset,
2037 argv, invert,
2038 &target->tflags,
2039 &fw, &target->t))) {
2040 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002041 if (!m->used)
2042 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002043
Rusty Russell5eed48a2000-06-02 20:12:24 +00002044 if (m->parse(c - m->option_offset,
2045 argv, invert,
2046 &m->mflags,
2047 &fw,
2048 &fw.nfcache,
2049 &m->m))
2050 break;
2051 }
2052
2053 /* If you listen carefully, you can
2054 actually hear this code suck. */
2055 if (m == NULL
2056 && protocol
2057 && !find_proto(protocol, DONT_LOAD,
2058 options&OPT_NUMERIC)
2059 && (m = find_proto(protocol, TRY_LOAD,
2060 options&OPT_NUMERIC))) {
2061 /* Try loading protocol */
2062 size_t size;
2063
Harald Welte43ac1912002-03-03 09:43:07 +00002064 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2065 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002066
2067 m->m = fw_calloc(1, size);
2068 m->m->u.match_size = size;
2069 strcpy(m->m->u.user.name, m->name);
2070 m->init(m->m, &fw.nfcache);
2071
Harald Welte43ac1912002-03-03 09:43:07 +00002072 opts = merge_options(opts,
2073 m->extra_opts, &m->option_offset);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002074
Rusty Russell5eed48a2000-06-02 20:12:24 +00002075 optind--;
2076 continue;
2077 }
2078 if (!m)
2079 exit_error(PARAMETER_PROBLEM,
2080 "Unknown arg `%s'",
2081 argv[optind-1]);
2082 }
2083 }
2084 invert = FALSE;
2085 }
2086
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002087 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002088 if (!m->used)
2089 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002090
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002091 m->final_check(m->mflags);
2092 }
2093
Harald Welte43ac1912002-03-03 09:43:07 +00002094 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002095 target->final_check(target->tflags);
2096
2097 /* Fix me: must put inverse options checking here --MN */
2098
2099 if (optind < argc)
2100 exit_error(PARAMETER_PROBLEM,
2101 "unknown arguments found on commandline");
2102 if (!command)
2103 exit_error(PARAMETER_PROBLEM, "no command specified");
2104 if (invert)
2105 exit_error(PARAMETER_PROBLEM,
2106 "nothing appropriate following !");
2107
2108 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2109 CMD_CHECK)) {
2110 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002111 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002113 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002114 }
2115
2116 if (shostnetworkmask)
2117 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2118 &(fw.ipv6.smsk), &nsaddrs);
2119
2120 if (dhostnetworkmask)
2121 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2122 &(fw.ipv6.dmsk), &ndaddrs);
2123
2124 if ((nsaddrs > 1 || ndaddrs > 1) &&
2125 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2126 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2127 " source or destination IP addresses");
2128
2129 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2130 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2131 cmd2char(CMD_CHECK));
2132
2133 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2134 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2135 "specify a unique address");
2136
2137 generic_opt_check(command, options);
2138
2139 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2140 exit_error(PARAMETER_PROBLEM,
2141 "chain name `%s' too long (must be under %i chars)",
2142 chain, IP6T_FUNCTION_MAXNAMELEN);
2143
Harald Welte43ac1912002-03-03 09:43:07 +00002144 /* only allocate handle if we weren't called with a handle */
2145 if (!*handle)
2146 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002147
Harald Welte43ac1912002-03-03 09:43:07 +00002148 if (!*handle) {
2149 /* try to insmod the module if iptc_init failed */
2150 ip6tables_insmod("ip6_tables", modprobe);
2151 *handle = ip6tc_init(*table);
2152 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002153
Harald Welte43ac1912002-03-03 09:43:07 +00002154 if (!*handle)
2155 exit_error(VERSION_PROBLEM,
2156 "can't initialize ip6tables table `%s': %s",
2157 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002158
2159 if (command == CMD_CHECK
2160 || command == CMD_APPEND
2161 || command == CMD_DELETE
2162 || command == CMD_INSERT
2163 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002164 if (strcmp(chain, "PREROUTING") == 0
2165 || strcmp(chain, "INPUT") == 0) {
2166 /* -o not valid with incoming packets. */
2167 if (options & OPT_VIANAMEOUT)
2168 exit_error(PARAMETER_PROBLEM,
2169 "Can't use -%c with %s\n",
2170 opt2char(OPT_VIANAMEOUT),
2171 chain);
2172 /* -i required with -C */
2173 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2174 exit_error(PARAMETER_PROBLEM,
2175 "Need -%c with %s\n",
2176 opt2char(OPT_VIANAMEIN),
2177 chain);
2178 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002179
Harald Welte43ac1912002-03-03 09:43:07 +00002180 if (strcmp(chain, "POSTROUTING") == 0
2181 || strcmp(chain, "OUTPUT") == 0) {
2182 /* -i not valid with outgoing packets */
2183 if (options & OPT_VIANAMEIN)
2184 exit_error(PARAMETER_PROBLEM,
2185 "Can't use -%c with %s\n",
2186 opt2char(OPT_VIANAMEIN),
2187 chain);
2188 /* -o required with -C */
2189 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2190 exit_error(PARAMETER_PROBLEM,
2191 "Need -%c with %s\n",
2192 opt2char(OPT_VIANAMEOUT),
2193 chain);
2194 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002195
2196 if (target && ip6tc_is_chain(jumpto, *handle)) {
2197 printf("Warning: using chain %s, not extension\n",
2198 jumpto);
2199
2200 target = NULL;
2201 }
2202
2203 /* If they didn't specify a target, or it's a chain
2204 name, use standard. */
2205 if (!target
2206 && (strlen(jumpto) == 0
2207 || ip6tc_is_chain(jumpto, *handle))) {
2208 size_t size;
2209
2210 target = find_target(IP6T_STANDARD_TARGET,
2211 LOAD_MUST_SUCCEED);
2212
2213 size = sizeof(struct ip6t_entry_target)
2214 + target->size;
2215 target->t = fw_calloc(1, size);
2216 target->t->u.target_size = size;
2217 strcpy(target->t->u.user.name, jumpto);
2218 target->init(target->t, &fw.nfcache);
2219 }
2220
2221 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002222 /* it is no chain, and we can't load a plugin.
2223 * We cannot know if the plugin is corrupt, non
2224 * existant OR if the user just misspelled a
2225 * chain. */
2226 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002227 } else {
2228 e = generate_entry(&fw, ip6tables_matches, target->t);
2229 }
2230 }
2231
2232 switch (command) {
2233 case CMD_APPEND:
2234 ret = append_entry(chain, e,
2235 nsaddrs, saddrs, ndaddrs, daddrs,
2236 options&OPT_VERBOSE,
2237 handle);
2238 break;
2239 case CMD_CHECK:
2240 ret = check_packet(chain, e,
2241 nsaddrs, saddrs, ndaddrs, daddrs,
2242 options&OPT_VERBOSE, handle);
2243 break;
2244 case CMD_DELETE:
2245 ret = delete_entry(chain, e,
2246 nsaddrs, saddrs, ndaddrs, daddrs,
2247 options&OPT_VERBOSE,
2248 handle);
2249 break;
2250 case CMD_DELETE_NUM:
2251 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2252 break;
2253 case CMD_REPLACE:
2254 ret = replace_entry(chain, e, rulenum - 1,
2255 saddrs, daddrs, options&OPT_VERBOSE,
2256 handle);
2257 break;
2258 case CMD_INSERT:
2259 ret = insert_entry(chain, e, rulenum - 1,
2260 nsaddrs, saddrs, ndaddrs, daddrs,
2261 options&OPT_VERBOSE,
2262 handle);
2263 break;
2264 case CMD_LIST:
2265 ret = list_entries(chain,
2266 options&OPT_VERBOSE,
2267 options&OPT_NUMERIC,
2268 options&OPT_EXPANDED,
2269 options&OPT_LINENUMBERS,
2270 handle);
2271 break;
2272 case CMD_FLUSH:
2273 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2274 break;
2275 case CMD_ZERO:
2276 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2277 break;
2278 case CMD_LIST|CMD_ZERO:
2279 ret = list_entries(chain,
2280 options&OPT_VERBOSE,
2281 options&OPT_NUMERIC,
2282 options&OPT_EXPANDED,
2283 options&OPT_LINENUMBERS,
2284 handle);
2285 if (ret)
2286 ret = zero_entries(chain,
2287 options&OPT_VERBOSE, handle);
2288 break;
2289 case CMD_NEW_CHAIN:
2290 ret = ip6tc_create_chain(chain, handle);
2291 break;
2292 case CMD_DELETE_CHAIN:
2293 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2294 break;
2295 case CMD_RENAME_CHAIN:
2296 ret = ip6tc_rename_chain(chain, newname, handle);
2297 break;
2298 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002299 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002300 break;
2301 default:
2302 /* We should never reach this... */
2303 exit_tryhelp(2);
2304 }
2305
2306 if (verbose > 1)
2307 dump_entries6(*handle);
2308
2309 return ret;
2310}