blob: a32d418cf2fc2437c6273d40a65186891f75fd2a [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>
33
34#ifndef TRUE
35#define TRUE 1
36#endif
37#ifndef FALSE
38#define FALSE 0
39#endif
40
41#ifndef IP6T_LIB_DIR
42#define IP6T_LIB_DIR "/usr/local/lib/iptables"
43#endif
44
45#define FMT_NUMERIC 0x0001
46#define FMT_NOCOUNTS 0x0002
47#define FMT_KILOMEGAGIGA 0x0004
48#define FMT_OPTIONS 0x0008
49#define FMT_NOTABLE 0x0010
50#define FMT_NOTARGET 0x0020
51#define FMT_VIA 0x0040
52#define FMT_NONEWLINE 0x0080
53#define FMT_LINENUMBERS 0x0100
54
55#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
56 | FMT_NUMERIC | FMT_NOTABLE)
57#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
58
59
60#define CMD_NONE 0x0000U
61#define CMD_INSERT 0x0001U
62#define CMD_DELETE 0x0002U
63#define CMD_DELETE_NUM 0x0004U
64#define CMD_REPLACE 0x0008U
65#define CMD_APPEND 0x0010U
66#define CMD_LIST 0x0020U
67#define CMD_FLUSH 0x0040U
68#define CMD_ZERO 0x0080U
69#define CMD_NEW_CHAIN 0x0100U
70#define CMD_DELETE_CHAIN 0x0200U
71#define CMD_SET_POLICY 0x0400U
72#define CMD_CHECK 0x0800U
73#define CMD_RENAME_CHAIN 0x1000U
74#define NUMBER_OF_CMD 13
75static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
76 'N', 'X', 'P', 'C', 'E' };
77
78#define OPTION_OFFSET 256
79
80#define OPT_NONE 0x00000U
81#define OPT_NUMERIC 0x00001U
82#define OPT_SOURCE 0x00002U
83#define OPT_DESTINATION 0x00004U
84#define OPT_PROTOCOL 0x00008U
85#define OPT_JUMP 0x00010U
86#define OPT_VERBOSE 0x00020U
87#define OPT_EXPANDED 0x00040U
88#define OPT_VIANAMEIN 0x00080U
89#define OPT_VIANAMEOUT 0x00100U
90#define OPT_LINENUMBERS 0x00200U
91#define NUMBER_OF_OPT 10
92static const char optflags[NUMBER_OF_OPT]
93= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3'};
94
95static struct option original_opts[] = {
96 { "append", 1, 0, 'A' },
97 { "delete", 1, 0, 'D' },
98 { "insert", 1, 0, 'I' },
99 { "replace", 1, 0, 'R' },
100 { "list", 2, 0, 'L' },
101 { "flush", 2, 0, 'F' },
102 { "zero", 2, 0, 'Z' },
103 { "check", 1, 0, 'C' },
104 { "new-chain", 1, 0, 'N' },
105 { "delete-chain", 2, 0, 'X' },
106 { "rename-chain", 2, 0, 'E' },
107 { "policy", 1, 0, 'P' },
108 { "source", 1, 0, 's' },
109 { "destination", 1, 0, 'd' },
110 { "src", 1, 0, 's' }, /* synonym */
111 { "dst", 1, 0, 'd' }, /* synonym */
112 { "protocol", 1, 0, 'p' },
113 { "in-interface", 1, 0, 'i' },
114 { "jump", 1, 0, 'j' },
115 { "table", 1, 0, 't' },
116 { "match", 1, 0, 'm' },
117 { "numeric", 0, 0, 'n' },
118 { "out-interface", 1, 0, 'o' },
119 { "verbose", 0, 0, 'v' },
120 { "exact", 0, 0, 'x' },
121 { "version", 0, 0, 'V' },
122 { "help", 2, 0, 'h' },
123 { "line-numbers", 0, 0, '0' },
124 { 0 }
125};
126
127static struct option *opts = original_opts;
128static unsigned int global_option_offset = 0;
129
130/* Table of legal combinations of commands and options. If any of the
131 * given commands make an option legal, that option is legal (applies to
132 * CMD_LIST and CMD_ZERO only).
133 * Key:
134 * + compulsory
135 * x illegal
136 * optional
137 */
138
139static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
140/* Well, it's better than "Re: Linux vs FreeBSD" */
141{
142 /* -n -s -d -p -j -v -x -i -o --line */
143/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
144/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
145/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
146/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
147/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
148/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
149/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
150/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
151/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
152/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
153/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
154/*CHECK*/ {'x','+','+','+','x',' ','x','+','+','x'},
155/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
156};
157
158static int inverse_for_options[NUMBER_OF_OPT] =
159{
160/* -n */ 0,
161/* -s */ IP6T_INV_SRCIP,
162/* -d */ IP6T_INV_DSTIP,
163/* -p */ IP6T_INV_PROTO,
164/* -j */ 0,
165/* -v */ 0,
166/* -x */ 0,
167/* -i */ IP6T_INV_VIA_IN,
168/* -o */ IP6T_INV_VIA_OUT,
169/*--line*/ 0
170};
171
172const char *program_version;
173const char *program_name;
174
175/* Keeping track of external matches and targets: linked lists. */
176struct ip6tables_match *ip6tables_matches = NULL;
177struct ip6tables_target *ip6tables_targets = NULL;
178
179/* Extra debugging from libiptc */
180extern void dump_entries6(const ip6tc_handle_t handle);
181
182/* A few hardcoded protocols for 'all' and in case the user has no
183 /etc/protocols */
184struct pprot {
185 char *name;
186 u_int8_t num;
187};
188
189static const struct pprot chain_protos[] = {
190 { "tcp", IPPROTO_TCP },
191 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000192 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000193 { "esp", IPPROTO_ESP },
194 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000195 { "all", 0 },
196};
197
198static char *
199proto_to_name(u_int8_t proto, int nolookup)
200{
201 unsigned int i;
202
203 if (proto && !nolookup) {
204 struct protoent *pent = getprotobynumber(proto);
205 if (pent)
206 return pent->p_name;
207 }
208
209 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
210 if (chain_protos[i].num == proto)
211 return chain_protos[i].name;
212
213 return NULL;
214}
215
216static void
217in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
218{
219 memcpy(dst, src, sizeof(struct in6_addr));
220}
221
222void
223exit_error(enum exittype status, char *msg, ...)
224{
225 va_list args;
226
227 va_start(args, msg);
228 fprintf(stderr, "%s v%s: ", program_name, program_version);
229 vfprintf(stderr, msg, args);
230 va_end(args);
231 fprintf(stderr, "\n");
232 if (status == PARAMETER_PROBLEM)
233 exit_tryhelp(status);
234 if (status == VERSION_PROBLEM)
235 fprintf(stderr,
236 "Perhaps iptables or your kernel needs to be upgraded.\n");
237 exit(status);
238}
239
240void
241exit_tryhelp(int status)
242{
243 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
244 program_name, program_name );
245 exit(status);
246}
247
248void
249exit_printhelp(void)
250{
251 struct ip6tables_match *m = NULL;
252 struct ip6tables_target *t = NULL;
253
254 printf("%s v%s\n\n"
255"Usage: %s -[ADC] chain rule-specification [options]\n"
256" %s -[RI] chain rulenum rule-specification [options]\n"
257" %s -D chain rulenum [options]\n"
258" %s -[LFZ] [chain] [options]\n"
259" %s -[NX] chain\n"
260" %s -E old-chain-name new-chain-name\n"
261" %s -P chain target [options]\n"
262" %s -h (print this help information)\n\n",
263 program_name, program_version, program_name, program_name,
264 program_name, program_name, program_name, program_name,
265 program_name, program_name);
266
267 printf(
268"Commands:\n"
269"Either long or short options are allowed.\n"
270" --append -A chain Append to chain\n"
271" --delete -D chain Delete matching rule from chain\n"
272" --delete -D chain rulenum\n"
273" Delete rule rulenum (1 = first) from chain\n"
274" --insert -I chain [rulenum]\n"
275" Insert in chain as rulenum (default 1=first)\n"
276" --replace -R chain rulenum\n"
277" Replace rule rulenum (1 = first) in chain\n"
278" --list -L [chain] List the rules in a chain or all chains\n"
279" --flush -F [chain] Delete all rules in chain or all chains\n"
280" --zero -Z [chain] Zero counters in chain or all chains\n"
281" --check -C chain Test this packet on chain\n"
282" --new -N chain Create a new user-defined chain\n"
283" --delete-chain\n"
284" -X [chain] Delete a user-defined chain\n"
285" --policy -P chain target\n"
286" Change policy on chain to target\n"
287" --rename-chain\n"
288" -E old-chain new-chain\n"
289" Change chain name, (moving any references)\n"
290
291"Options:\n"
292" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
293" --source -s [!] address[/mask]\n"
294" source specification\n"
295" --destination -d [!] address[/mask]\n"
296" destination specification\n"
297" --in-interface -i [!] input name[+]\n"
298" network interface name ([+] for wildcard)\n"
299" --jump -j target\n"
300" target for rule\n"
301" --numeric -n numeric output of addresses and ports\n"
302" --out-interface -o [!] output name[+]\n"
303" network interface name ([+] for wildcard)\n"
304" --table -t table table to manipulate (default: `filter')\n"
305" --verbose -v verbose mode\n"
306" --exact -x expand numbers (display exact values)\n"
307"[!] --fragment -f match second or further fragments only\n"
308"[!] --version -V print package version.\n");
309
310 /* Print out any special helps. A user might like to be able to add a --help
311 to the commandline, and see expected results. So we call help for all
312 matches & targets */
313 for (t=ip6tables_targets;t;t=t->next) {
314 printf("\n");
315 t->help();
316 }
317 for (m=ip6tables_matches;m;m=m->next) {
318 printf("\n");
319 m->help();
320 }
321 exit(0);
322}
323
324static void
325generic_opt_check(int command, int options)
326{
327 int i, j, legal = 0;
328
329 /* Check that commands are valid with options. Complicated by the
330 * fact that if an option is legal with *any* command given, it is
331 * legal overall (ie. -z and -l).
332 */
333 for (i = 0; i < NUMBER_OF_OPT; i++) {
334 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
335
336 for (j = 0; j < NUMBER_OF_CMD; j++) {
337 if (!(command & (1<<j)))
338 continue;
339
340 if (!(options & (1<<i))) {
341 if (commands_v_options[j][i] == '+')
342 exit_error(PARAMETER_PROBLEM,
343 "You need to supply the `-%c' "
344 "option for this command\n",
345 optflags[i]);
346 } else {
347 if (commands_v_options[j][i] != 'x')
348 legal = 1;
349 else if (legal == 0)
350 legal = -1;
351 }
352 }
353 if (legal == -1)
354 exit_error(PARAMETER_PROBLEM,
355 "Illegal option `-%c' with this command\n",
356 optflags[i]);
357 }
358}
359
360static char
361opt2char(int option)
362{
363 const char *ptr;
364 for (ptr = optflags; option > 1; option >>= 1, ptr++);
365
366 return *ptr;
367}
368
369static char
370cmd2char(int option)
371{
372 const char *ptr;
373 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
374
375 return *ptr;
376}
377
378static void
379add_command(int *cmd, const int newcmd, const int othercmds, int invert)
380{
381 if (invert)
382 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
383 if (*cmd & (~othercmds))
384 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
385 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
386 *cmd |= newcmd;
387}
388
389int
390check_inverse(const char option[], int *invert)
391{
392 if (option && strcmp(option, "!") == 0) {
393 if (*invert)
394 exit_error(PARAMETER_PROBLEM,
395 "Multiple `!' flags not allowed");
396
397 *invert = TRUE;
398 return TRUE;
399 }
400 return FALSE;
401}
402
403static void *
404fw_calloc(size_t count, size_t size)
405{
406 void *p;
407
408 if ((p = calloc(count, size)) == NULL) {
409 perror("iptables: calloc failed");
410 exit(1);
411 }
412 return p;
413}
414
415static void *
416fw_malloc(size_t size)
417{
418 void *p;
419
420 if ((p = malloc(size)) == NULL) {
421 perror("iptables: malloc failed");
422 exit(1);
423 }
424 return p;
425}
426
427static struct in6_addr *
428host_to_addr(const char *name, unsigned int *naddr)
429{
430 struct hostent *host;
431 struct in6_addr *addr;
432 unsigned int i;
433
434 *naddr = 0;
435 if ((host = gethostbyname2(name, AF_INET6)) != NULL) {
436 if (host->h_addrtype != AF_INET6 ||
437 host->h_length != sizeof(struct in6_addr))
438 return (struct in6_addr *) NULL;
439
440 while (host->h_addr_list[*naddr] != (char *) NULL)
441 (*naddr)++;
442 addr = fw_calloc(*naddr, sizeof(struct in6_addr));
443 for (i = 0; i < *naddr; i++)
444 in6addrcpy(&(addr[i]),
445 (struct in6_addr *) host->h_addr_list[i]);
446 return addr;
447 }
448
449 return (struct in6_addr *) NULL;
450}
451
452static char *
453addr_to_host(const struct in6_addr *addr)
454{
455 struct hostent *host;
456
András Kis-Szabó764316a2001-02-26 17:31:20 +0000457 /* TODO: gets errno 96 (EPFNOSUPPORT) on Debian/Sid. Got only AF_INET! */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000458 if ((host = gethostbyaddr((char *) addr,
459 sizeof(struct in_addr), AF_INET6)) != NULL)
460 return (char *) host->h_name;
461
462 return (char *) NULL;
463}
464
465static char *
466addr_to_numeric(const struct in6_addr *addrp)
467{
András Kis-Szabó764316a2001-02-26 17:31:20 +0000468 // static char buf[20];
469 // ADDR tags + sep. + \0
470 // 0000:0000:0000:0000:0000:0000:0000:000.000.000.000
471 // (but inet_ntop() supports only
472 // 0000:0000:0000:0000:0000:0000:0000:0000 format now.)
473 static char buf[50+1];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof buf);
475}
476
477static struct in6_addr *
478numeric_to_addr(const char *num)
479{
480 static struct in6_addr ap;
481 if (inet_pton(AF_INET6, num, &ap) == 1)
482 return &ap;
483 return (struct in6_addr *)NULL;
484}
485
486static char *
487mask_to_numeric(const struct in6_addr *addrp)
488{
489 static char buf[20];
490 int l = ipv6_prefix_length(addrp);
491 if (l == -1)
492 return addr_to_numeric(addrp);
493 sprintf(buf, "%d", l);
494 return buf;
495}
496
497static struct in6_addr *
498network_to_addr(const char *name)
499{
500 abort();
501}
502
503static char *
504addr_to_anyname(const struct in6_addr *addr)
505{
506 char *name;
507
508 if ((name = addr_to_host(addr)) != NULL)
509 return name;
510
511 return addr_to_numeric(addr);
512}
513
514/*
515 * All functions starting with "parse" should succeed, otherwise
516 * the program fails.
517 * Most routines return pointers to static data that may change
518 * between calls to the same or other routines with a few exceptions:
519 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
520 * return global static data.
521*/
522
523static struct in6_addr *
524parse_hostnetwork(const char *name, unsigned int *naddrs)
525{
526 struct in6_addr *addrp, *addrptmp;
527
528 if ((addrptmp = numeric_to_addr(name)) != NULL ||
529 (addrptmp = network_to_addr(name)) != NULL) {
530 addrp = fw_malloc(sizeof(struct in6_addr));
531 in6addrcpy(addrp, addrptmp);
532 *naddrs = 1;
533 return addrp;
534 }
535 if ((addrp = host_to_addr(name, naddrs)) != NULL)
536 return addrp;
537
538 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
539}
540
541static struct in6_addr *
542parse_mask(char *mask)
543{
544 static struct in6_addr maskaddr;
545 struct in6_addr *addrp;
546 int bits;
547
548 if (mask == NULL) {
549 /* no mask at all defaults to 128 bits */
550 memset(&maskaddr, 0xff, sizeof maskaddr);
551 return &maskaddr;
552 }
553 if ((addrp = numeric_to_addr(mask)) != NULL)
554 return addrp;
555 if ((bits = string_to_number(mask, 0, 128)) == -1)
556 exit_error(PARAMETER_PROBLEM,
557 "invalid mask `%s' specified", mask);
558 if (bits != 0) {
559 char *p = (char *)&maskaddr;
560 memset(p, 0xff, bits / 8);
561 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
562 p[bits / 8] = 0xff << (8 - (bits & 7));
563 return &maskaddr;
564 }
565
566 memset(&maskaddr, 0, sizeof maskaddr);
567 return &maskaddr;
568}
569
570static void
571parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
572 struct in6_addr *maskp, unsigned int *naddrs)
573{
574 struct in6_addr *addrp;
575 char buf[256];
576 char *p;
577 int i, j, n;
578
579 strncpy(buf, name, sizeof(buf) - 1);
580 if ((p = strrchr(buf, '/')) != NULL) {
581 *p = '\0';
582 addrp = parse_mask(p + 1);
583 } else
584 addrp = parse_mask(NULL);
585 in6addrcpy(maskp, addrp);
586
587 /* if a null mask is given, the name is ignored, like in "any/0" */
588 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
589 strcpy(buf, "::");
590
591 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
592 n = *naddrs;
593 for (i = 0, j = 0; i < n; i++) {
594 int k;
595 for (k = 0; k < 4; k++)
596 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
597 j++;
598 for (k = 0; k < j - 1; k++) {
599 if (!memcmp(&addrp[k], &addrp[j - 1], sizeof(struct in6_addr))) {
600 (*naddrs)--;
601 j--;
602 break;
603 }
604 }
605 }
606}
607
608struct ip6tables_match *
609find_match(const char *name, enum ip6t_tryload tryload)
610{
611 struct ip6tables_match *ptr;
612
613 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
614 if (strcmp(name, ptr->name) == 0)
615 break;
616 }
617
618 if (!ptr && tryload != DONT_LOAD) {
619 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
620 + strlen(name)];
621 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
622 if (dlopen(path, RTLD_NOW)) {
623 /* Found library. If it didn't register itself,
624 maybe they specified target as match. */
625 ptr = find_match(name, DONT_LOAD);
626
627 if (!ptr)
628 exit_error(PARAMETER_PROBLEM,
629 "Couldn't load match `%s'\n",
630 name);
631 } else if (tryload == LOAD_MUST_SUCCEED)
632 exit_error(PARAMETER_PROBLEM,
633 "Couldn't load match `%s'\n", name);
634 }
635
636 return ptr;
637}
638
639/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
640static struct ip6tables_match *
641find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
642{
643 int proto;
644
645 proto = string_to_number(pname, 0, 255);
646 if (proto != -1)
647 return find_match(proto_to_name(proto, nolookup), tryload);
648
649 return find_match(pname, tryload);
650}
651
652static u_int16_t
653parse_protocol(const char *s)
654{
655 int proto = string_to_number(s, 0, 255);
656
657 if (proto == -1) {
658 struct protoent *pent;
659
660 if ((pent = getprotobyname(s)))
661 proto = pent->p_proto;
662 else {
663 unsigned int i;
664 for (i = 0;
665 i < sizeof(chain_protos)/sizeof(struct pprot);
666 i++) {
667 if (strcmp(s, chain_protos[i].name) == 0) {
668 proto = chain_protos[i].num;
669 break;
670 }
671 }
672 if (i == sizeof(chain_protos)/sizeof(struct pprot))
673 exit_error(PARAMETER_PROBLEM,
674 "unknown protocol `%s' specified",
675 s);
676 }
677 }
678
679 return (u_int16_t)proto;
680}
681
682static void
683parse_interface(const char *arg, char *vianame, unsigned char *mask)
684{
685 int vialen = strlen(arg);
686 unsigned int i;
687
688 memset(mask, 0, IFNAMSIZ);
689 memset(vianame, 0, IFNAMSIZ);
690
691 if (vialen + 1 > IFNAMSIZ)
692 exit_error(PARAMETER_PROBLEM,
693 "interface name `%s' must be shorter than IFNAMSIZ"
694 " (%i)", arg, IFNAMSIZ-1);
695
696 strcpy(vianame, arg);
697 if (vialen == 0)
698 memset(mask, 0, IFNAMSIZ);
699 else if (vianame[vialen - 1] == '+') {
700 memset(mask, 0xFF, vialen - 1);
701 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
702 /* Remove `+' */
703 vianame[vialen - 1] = '\0';
704 } else {
705 /* Include nul-terminator in match */
706 memset(mask, 0xFF, vialen + 1);
707 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
708 }
709 for (i = 0; vianame[i]; i++) {
710 if (!isalnum(vianame[i])) {
711 printf("Warning: wierd character in interface"
712 " `%s' (No aliases, :, ! or *).\n",
713 vianame);
714 break;
715 }
716 }
717}
718
719/* Can't be zero. */
720static int
721parse_rulenumber(const char *rule)
722{
723 int rulenum = string_to_number(rule, 1, INT_MAX);
724
725 if (rulenum == -1)
726 exit_error(PARAMETER_PROBLEM,
727 "Invalid rule number `%s'", rule);
728
729 return rulenum;
730}
731
732static const char *
733parse_target(const char *targetname)
734{
735 const char *ptr;
736
737 if (strlen(targetname) < 1)
738 exit_error(PARAMETER_PROBLEM,
739 "Invalid target name (too short)");
740
741 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
742 exit_error(PARAMETER_PROBLEM,
743 "Invalid target name `%s' (%i chars max)",
744 targetname, sizeof(ip6t_chainlabel)-1);
745
746 for (ptr = targetname; *ptr; ptr++)
747 if (isspace(*ptr))
748 exit_error(PARAMETER_PROBLEM,
749 "Invalid target name `%s'", targetname);
750 return targetname;
751}
752
753int
754string_to_number(const char *s, int min, int max)
755{
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000756 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000757 char *end;
758
759 /* Handle hex, octal, etc. */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000760 errno = 0;
761 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000762 if (*end == '\0' && end != s) {
763 /* we parsed a number, let's see if we want this */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000764 if (errno != ERANGE && min <= number && number <= max)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765 return number;
766 }
767 return -1;
768}
769
770static void
771set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
772 int invert)
773{
774 if (*options & option)
775 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
776 opt2char(option));
777 *options |= option;
778
779 if (invert) {
780 unsigned int i;
781 for (i = 0; 1 << i != option; i++);
782
783 if (!inverse_for_options[i])
784 exit_error(PARAMETER_PROBLEM,
785 "cannot have ! before -%c",
786 opt2char(option));
787 *invflg |= inverse_for_options[i];
788 }
789}
790
791struct ip6tables_target *
792find_target(const char *name, enum ip6t_tryload tryload)
793{
794 struct ip6tables_target *ptr;
795
796 /* Standard target? */
797 if (strcmp(name, "") == 0
798 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
799 || strcmp(name, IP6TC_LABEL_DROP) == 0
800 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
801 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
802 name = "standard";
803
804 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
805 if (strcmp(name, ptr->name) == 0)
806 break;
807 }
808
809 if (!ptr && tryload != DONT_LOAD) {
810 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
811 + strlen(name)];
812 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
813 if (dlopen(path, RTLD_NOW)) {
814 /* Found library. If it didn't register itself,
815 maybe they specified match as a target. */
816 ptr = find_target(name, DONT_LOAD);
817 if (!ptr)
818 exit_error(PARAMETER_PROBLEM,
819 "Couldn't load target `%s'\n",
820 name);
821 } else if (tryload == LOAD_MUST_SUCCEED)
822 exit_error(PARAMETER_PROBLEM,
823 "Couldn't load target `%s'\n", name);
824 }
825
826 return ptr;
827}
828
829static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000830merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831 unsigned int *option_offset)
832{
833 unsigned int num_old, num_new, i;
834 struct option *merge;
835
836 for (num_old = 0; oldopts[num_old].name; num_old++);
837 for (num_new = 0; newopts[num_new].name; num_new++);
838
839 global_option_offset += OPTION_OFFSET;
840 *option_offset = global_option_offset;
841
842 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
843 memcpy(merge, oldopts, num_old * sizeof(struct option));
844 for (i = 0; i < num_new; i++) {
845 merge[num_old + i] = newopts[i];
846 merge[num_old + i].val += *option_offset;
847 }
848 memset(merge + num_old + num_new, 0, sizeof(struct option));
849
850 return merge;
851}
852
853void
854register_match6(struct ip6tables_match *me)
855{
856 if (strcmp(me->version, program_version) != 0) {
857 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
858 program_name, me->name, me->version, program_version);
859 exit(1);
860 }
861
862 if (find_match(me->name, DONT_LOAD)) {
863 fprintf(stderr, "%s: match `%s' already registered.\n",
864 program_name, me->name);
865 exit(1);
866 }
867
868 /* Prepend to list. */
869 me->next = ip6tables_matches;
870 ip6tables_matches = me;
871 me->m = NULL;
872 me->mflags = 0;
873
874 opts = merge_options(opts, me->extra_opts, &me->option_offset);
875}
876
877void
878register_target6(struct ip6tables_target *me)
879{
880 if (strcmp(me->version, program_version) != 0) {
881 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
882 program_name, me->name, me->version, program_version);
883 exit(1);
884 }
885
886 if (find_target(me->name, DONT_LOAD)) {
887 fprintf(stderr, "%s: target `%s' already registered.\n",
888 program_name, me->name);
889 exit(1);
890 }
891
892 /* Prepend to list. */
893 me->next = ip6tables_targets;
894 ip6tables_targets = me;
895 me->t = NULL;
896 me->tflags = 0;
897
898 opts = merge_options(opts, me->extra_opts, &me->option_offset);
899}
900
901static void
902print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
903{
904 struct ip6t_counters counters;
905 const char *pol = ip6tc_get_policy(chain, &counters, handle);
906 printf("Chain %s", chain);
907 if (pol) {
908 printf(" (policy %s", pol);
909 if (!(format & FMT_NOCOUNTS))
910 printf(" %llu packets, %llu bytes",
911 counters.pcnt, counters.bcnt);
912 printf(")\n");
913 } else {
914 unsigned int refs;
915 if (!ip6tc_get_references(&refs, chain, handle))
916 printf(" (ERROR obtaining refs)\n");
917 else
918 printf(" (%u references)\n", refs);
919 }
920
921 if (format & FMT_LINENUMBERS)
922 printf(FMT("%-4s ", "%s "), "num");
923 if (!(format & FMT_NOCOUNTS)) {
924 if (format & FMT_KILOMEGAGIGA) {
925 printf(FMT("%5s ","%s "), "pkts");
926 printf(FMT("%5s ","%s "), "bytes");
927 } else {
928 printf(FMT("%8s ","%s "), "pkts");
929 printf(FMT("%10s ","%s "), "bytes");
930 }
931 }
932 if (!(format & FMT_NOTARGET))
933 printf(FMT("%-9s ","%s "), "target");
934 fputs(" prot ", stdout);
935 if (format & FMT_OPTIONS)
936 fputs("opt", stdout);
937 if (format & FMT_VIA) {
938 printf(FMT(" %-6s ","%s "), "in");
939 printf(FMT("%-6s ","%s "), "out");
940 }
941 printf(FMT(" %-19s ","%s "), "source");
942 printf(FMT(" %-19s "," %s "), "destination");
943 printf("\n");
944}
945
946static void
947print_num(u_int64_t number, unsigned int format)
948{
949 if (format & FMT_KILOMEGAGIGA) {
950 if (number > 99999) {
951 number = (number + 500) / 1000;
952 if (number > 9999) {
953 number = (number + 500) / 1000;
954 if (number > 9999) {
955 number = (number + 500) / 1000;
956 printf(FMT("%4lluG ","%lluG "),number);
957 }
958 else printf(FMT("%4lluM ","%lluM "), number);
959 } else
960 printf(FMT("%4lluK ","%lluK "), number);
961 } else
962 printf(FMT("%5llu ","%llu "), number);
963 } else
964 printf(FMT("%8llu ","%llu "), number);
965}
966
967static int
968print_match(const struct ip6t_entry_match *m,
969 const struct ip6t_ip6 *ip,
970 int numeric)
971{
972 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
973
974 if (match) {
975 if (match->print)
976 match->print(ip, m, numeric);
977 } else {
978 if (m->u.user.name[0])
979 printf("UNKNOWN match `%s' ", m->u.user.name);
980 }
981 /* Don't stop iterating. */
982 return 0;
983}
984
985/* e is called `fw' here for hysterical raisins */
986static void
987print_firewall(const struct ip6t_entry *fw,
988 const char *targname,
989 unsigned int num,
990 unsigned int format,
991 const ip6tc_handle_t handle)
992{
993 struct ip6tables_target *target = NULL;
994 const struct ip6t_entry_target *t;
995 u_int8_t flags;
996 char buf[BUFSIZ];
997
998 /* User creates a chain called "REJECT": this overrides the
999 `REJECT' target module. Keep feeding them rope until the
1000 revolution... Bwahahahahah */
1001 if (!ip6tc_is_chain(targname, handle))
1002 target = find_target(targname, TRY_LOAD);
1003 else
1004 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1005
1006 t = ip6t_get_target((struct ip6t_entry *)fw);
1007 flags = fw->ipv6.flags;
1008
1009 if (format & FMT_LINENUMBERS)
1010 printf(FMT("%-4u ", "%u "), num+1);
1011
1012 if (!(format & FMT_NOCOUNTS)) {
1013 print_num(fw->counters.pcnt, format);
1014 print_num(fw->counters.bcnt, format);
1015 }
1016
1017 if (!(format & FMT_NOTARGET))
1018 printf(FMT("%-9s ", "%s "), targname);
1019
1020 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1021 {
1022 char *pname = proto_to_name(fw->ipv6.proto,
1023 format&FMT_NUMERIC);
1024 if (pname)
1025 printf(FMT("%-5s", "%s "), pname);
1026 else
1027 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1028 }
1029
1030 if (format & FMT_OPTIONS) {
1031 if (format & FMT_NOTABLE)
1032 fputs("opt ", stdout);
1033 fputc(' ', stdout);
1034 fputc(' ', stdout);
1035 fputc(' ', stdout);
1036 }
1037
1038 if (format & FMT_VIA) {
1039 char iface[IFNAMSIZ+2];
1040
1041 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1042 iface[0] = '!';
1043 iface[1] = '\0';
1044 }
1045 else iface[0] = '\0';
1046
1047 if (fw->ipv6.iniface[0] != '\0') {
1048 strcat(iface, fw->ipv6.iniface);
1049 /* If it doesn't compare the nul-term, it's a
1050 wildcard. */
1051 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1052 strcat(iface, "+");
1053 }
1054 else if (format & FMT_NUMERIC) strcat(iface, "*");
1055 else strcat(iface, "any");
1056 printf(FMT(" %-6s ","in %s "), iface);
1057
1058 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1059 iface[0] = '!';
1060 iface[1] = '\0';
1061 }
1062 else iface[0] = '\0';
1063
1064 if (fw->ipv6.outiface[0] != '\0') {
1065 strcat(iface, fw->ipv6.outiface);
1066 /* If it doesn't compare the nul-term, it's a
1067 wildcard. */
1068 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1069 strcat(iface, "+");
1070 }
1071 else if (format & FMT_NUMERIC) strcat(iface, "*");
1072 else strcat(iface, "any");
1073 printf(FMT("%-6s ","out %s "), iface);
1074 }
1075
1076 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1077 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1078 && !(format & FMT_NUMERIC))
1079 printf(FMT("%-19s ","%s "), "anywhere");
1080 else {
1081 if (format & FMT_NUMERIC)
1082 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1083 else
1084 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1085 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1086 printf(FMT("%-19s ","%s "), buf);
1087 }
1088
1089 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1090 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1091 && !(format & FMT_NUMERIC))
1092 printf(FMT("%-19s","-> %s"), "anywhere");
1093 else {
1094 if (format & FMT_NUMERIC)
1095 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1096 else
1097 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1098 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1099 printf(FMT("%-19s","-> %s"), buf);
1100 }
1101
1102 if (format & FMT_NOTABLE)
1103 fputs(" ", stdout);
1104
1105 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1106
1107 if (target) {
1108 if (target->print)
1109 /* Print the target information. */
1110 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1111 } else if (t->u.target_size != sizeof(*t))
1112 printf("[%u bytes of unknown target data] ",
1113 t->u.target_size - sizeof(*t));
1114
1115 if (!(format & FMT_NONEWLINE))
1116 fputc('\n', stdout);
1117}
1118
1119static void
1120print_firewall_line(const struct ip6t_entry *fw,
1121 const ip6tc_handle_t h)
1122{
1123 struct ip6t_entry_target *t;
1124
1125 t = ip6t_get_target((struct ip6t_entry *)fw);
1126 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1127}
1128
1129static int
1130append_entry(const ip6t_chainlabel chain,
1131 struct ip6t_entry *fw,
1132 unsigned int nsaddrs,
1133 const struct in6_addr saddrs[],
1134 unsigned int ndaddrs,
1135 const struct in6_addr daddrs[],
1136 int verbose,
1137 ip6tc_handle_t *handle)
1138{
1139 unsigned int i, j;
1140 int ret = 1;
1141
1142 for (i = 0; i < nsaddrs; i++) {
1143 fw->ipv6.src = saddrs[i];
1144 for (j = 0; j < ndaddrs; j++) {
1145 fw->ipv6.dst = daddrs[j];
1146 if (verbose)
1147 print_firewall_line(fw, *handle);
1148 ret &= ip6tc_append_entry(chain, fw, handle);
1149 }
1150 }
1151
1152 return ret;
1153}
1154
1155static int
1156replace_entry(const ip6t_chainlabel chain,
1157 struct ip6t_entry *fw,
1158 unsigned int rulenum,
1159 const struct in6_addr *saddr,
1160 const struct in6_addr *daddr,
1161 int verbose,
1162 ip6tc_handle_t *handle)
1163{
1164 fw->ipv6.src = *saddr;
1165 fw->ipv6.dst = *daddr;
1166
1167 if (verbose)
1168 print_firewall_line(fw, *handle);
1169 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1170}
1171
1172static int
1173insert_entry(const ip6t_chainlabel chain,
1174 struct ip6t_entry *fw,
1175 unsigned int rulenum,
1176 unsigned int nsaddrs,
1177 const struct in6_addr saddrs[],
1178 unsigned int ndaddrs,
1179 const struct in6_addr daddrs[],
1180 int verbose,
1181 ip6tc_handle_t *handle)
1182{
1183 unsigned int i, j;
1184 int ret = 1;
1185
1186 for (i = 0; i < nsaddrs; i++) {
1187 fw->ipv6.src = saddrs[i];
1188 for (j = 0; j < ndaddrs; j++) {
1189 fw->ipv6.dst = daddrs[j];
1190 if (verbose)
1191 print_firewall_line(fw, *handle);
1192 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1193 }
1194 }
1195
1196 return ret;
1197}
1198
1199static unsigned char *
1200make_delete_mask(struct ip6t_entry *fw)
1201{
1202 /* Establish mask for comparison */
1203 unsigned int size;
1204 struct ip6tables_match *m;
1205 unsigned char *mask, *mptr;
1206
1207 size = sizeof(struct ip6t_entry);
1208 for (m = ip6tables_matches; m; m = m->next)
1209 size += sizeof(struct ip6t_entry_match) + m->size;
1210
1211 mask = fw_calloc(1, size
1212 + sizeof(struct ip6t_entry_target)
1213 + ip6tables_targets->size);
1214
1215 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1216 mptr = mask + sizeof(struct ip6t_entry);
1217
1218 for (m = ip6tables_matches; m; m = m->next) {
1219 memset(mptr, 0xFF,
1220 sizeof(struct ip6t_entry_match) + m->userspacesize);
1221 mptr += sizeof(struct ip6t_entry_match) + m->size;
1222 }
1223
1224 memset(mptr, 0xFF, sizeof(struct ip6t_entry_target));
1225 mptr += sizeof(struct ip6t_entry_target);
1226 memset(mptr, 0xFF, ip6tables_targets->userspacesize);
1227
1228 return mask;
1229}
1230
1231static int
1232delete_entry(const ip6t_chainlabel chain,
1233 struct ip6t_entry *fw,
1234 unsigned int nsaddrs,
1235 const struct in6_addr saddrs[],
1236 unsigned int ndaddrs,
1237 const struct in6_addr daddrs[],
1238 int verbose,
1239 ip6tc_handle_t *handle)
1240{
1241 unsigned int i, j;
1242 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001243 unsigned char *mask;
1244
1245 mask = make_delete_mask(fw);
1246 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001247 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001248 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001249 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001250 if (verbose)
1251 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001252 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001253 }
1254 }
1255 return ret;
1256}
1257
1258static int
1259check_packet(const ip6t_chainlabel chain,
1260 struct ip6t_entry *fw,
1261 unsigned int nsaddrs,
1262 const struct in6_addr saddrs[],
1263 unsigned int ndaddrs,
1264 const struct in6_addr daddrs[],
1265 int verbose,
1266 ip6tc_handle_t *handle)
1267{
1268 int ret = 1;
1269 unsigned int i, j;
1270 struct ip6t_entry ipfw = *fw;
1271 const char *msg;
1272
1273 for (i = 0; i < nsaddrs; i++) {
1274 ipfw.ipv6.src = saddrs[i];
1275 for (j = 0; j < ndaddrs; j++) {
1276 ipfw.ipv6.dst = daddrs[j];
1277 if (verbose)
1278 print_firewall_line(fw, *handle);
1279 msg = ip6tc_check_packet(chain, &ipfw, handle);
1280 if (!msg) ret = 0;
1281 else printf("%s\n", msg);
1282 }
1283 }
1284
1285 return ret;
1286}
1287
András Kis-Szabó764316a2001-02-26 17:31:20 +00001288//static int
1289int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001290for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1291 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1292{
1293 int ret = 1;
1294 const char *chain;
1295 char *chains;
1296 unsigned int i, chaincount = 0;
1297
1298 chain = ip6tc_first_chain(handle);
1299 while (chain) {
1300 chaincount++;
1301 chain = ip6tc_next_chain(handle);
1302 }
1303
1304 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1305 i = 0;
1306 chain = ip6tc_first_chain(handle);
1307 while (chain) {
1308 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1309 i++;
1310 chain = ip6tc_next_chain(handle);
1311 }
1312
1313 for (i = 0; i < chaincount; i++) {
1314 if (!builtinstoo
1315 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1316 *handle))
1317 continue;
1318 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1319 }
1320
1321 free(chains);
1322 return ret;
1323}
1324
András Kis-Szabó764316a2001-02-26 17:31:20 +00001325//static int
1326int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001327flush_entries(const ip6t_chainlabel chain, int verbose,
1328 ip6tc_handle_t *handle)
1329{
1330 if (!chain)
1331 return for_each_chain(flush_entries, verbose, 1, handle);
1332
1333 if (verbose)
1334 fprintf(stdout, "Flushing chain `%s'\n", chain);
1335 return ip6tc_flush_entries(chain, handle);
1336}
1337
1338static int
1339zero_entries(const ip6t_chainlabel chain, int verbose,
1340 ip6tc_handle_t *handle)
1341{
1342 if (!chain)
1343 return for_each_chain(zero_entries, verbose, 1, handle);
1344
1345 if (verbose)
1346 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1347 return ip6tc_zero_entries(chain, handle);
1348}
1349
András Kis-Szabó764316a2001-02-26 17:31:20 +00001350//static int
1351int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001352delete_chain(const ip6t_chainlabel chain, int verbose,
1353 ip6tc_handle_t *handle)
1354{
1355 if (!chain)
1356 return for_each_chain(delete_chain, verbose, 0, handle);
1357
1358 if (verbose)
1359 fprintf(stdout, "Deleting chain `%s'\n", chain);
1360 return ip6tc_delete_chain(chain, handle);
1361}
1362
1363static int
1364list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1365 int expanded, int linenumbers, ip6tc_handle_t *handle)
1366{
1367 int found = 0;
1368 unsigned int format;
1369 const char *this;
1370
1371 format = FMT_OPTIONS;
1372 if (!verbose)
1373 format |= FMT_NOCOUNTS;
1374 else
1375 format |= FMT_VIA;
1376
1377 if (numeric)
1378 format |= FMT_NUMERIC;
1379
1380 if (!expanded)
1381 format |= FMT_KILOMEGAGIGA;
1382
1383 if (linenumbers)
1384 format |= FMT_LINENUMBERS;
1385
1386 for (this = ip6tc_first_chain(handle);
1387 this;
1388 this = ip6tc_next_chain(handle)) {
1389 const struct ip6t_entry *i;
1390 unsigned int num;
1391
1392 if (chain && strcmp(chain, this) != 0)
1393 continue;
1394
1395 if (found) printf("\n");
1396
1397 print_header(format, this, handle);
1398 i = ip6tc_first_rule(this, handle);
1399
1400 num = 0;
1401 while (i) {
1402 print_firewall(i,
1403 ip6tc_get_target(i, handle),
1404 num++,
1405 format,
1406 *handle);
1407 i = ip6tc_next_rule(i, handle);
1408 }
1409 found = 1;
1410 }
1411
1412 errno = ENOENT;
1413 return found;
1414}
1415
1416static struct ip6t_entry *
1417generate_entry(const struct ip6t_entry *fw,
1418 struct ip6tables_match *matches,
1419 struct ip6t_entry_target *target)
1420{
1421 unsigned int size;
1422 struct ip6tables_match *m;
1423 struct ip6t_entry *e;
1424
1425 size = sizeof(struct ip6t_entry);
1426 for (m = matches; m; m = m->next)
1427 size += m->m->u.match_size;
1428
1429 e = fw_malloc(size + target->u.target_size);
1430 *e = *fw;
1431 e->target_offset = size;
1432 e->next_offset = size + target->u.target_size;
1433
1434 size = 0;
1435 for (m = matches; m; m = m->next) {
1436 memcpy(e->elems + size, m->m, m->m->u.match_size);
1437 size += m->m->u.match_size;
1438 }
1439 memcpy(e->elems + size, target, target->u.target_size);
1440
1441 return e;
1442}
1443
1444int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1445{
1446 struct ip6t_entry fw, *e = NULL;
1447 int invert = 0;
1448 unsigned int nsaddrs = 0, ndaddrs = 0;
1449 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1450
1451 int c, verbose = 0;
1452 const char *chain = NULL;
1453 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1454 const char *policy = NULL, *newname = NULL;
1455 unsigned int rulenum = 0, options = 0, command = 0;
1456 int ret = 1;
1457 struct ip6tables_match *m;
1458 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001459 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001460 const char *jumpto = "";
1461 char *protocol = NULL;
1462
1463 memset(&fw, 0, sizeof(fw));
1464
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001465 opts = original_opts;
1466 global_option_offset = 0;
1467
1468 /* re-set optind to 0 in case do_command gets called
1469 * a second time */
1470 optind = 0;
1471
1472 /* clear mflags in case do_command gets called a second time
1473 * (we clear the global list of all matches for security)*/
1474 for (m = ip6tables_matches; m; m = m->next) {
1475 m->mflags = 0;
1476 }
1477
1478 for (t = ip6tables_targets; t; t = t->next) {
1479 t->tflags = 0;
1480 }
1481
Rusty Russell5eed48a2000-06-02 20:12:24 +00001482 /* Suppress error messages: we may add new options if we
1483 demand-load a protocol. */
1484 opterr = 0;
1485
1486 while ((c = getopt_long(argc, argv,
1487 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1488 opts, NULL)) != -1) {
1489 switch (c) {
1490 /*
1491 * Command selection
1492 */
1493 case 'A':
1494 add_command(&command, CMD_APPEND, CMD_NONE,
1495 invert);
1496 chain = optarg;
1497 break;
1498
1499 case 'D':
1500 add_command(&command, CMD_DELETE, CMD_NONE,
1501 invert);
1502 chain = optarg;
1503 if (optind < argc && argv[optind][0] != '-'
1504 && argv[optind][0] != '!') {
1505 rulenum = parse_rulenumber(argv[optind++]);
1506 command = CMD_DELETE_NUM;
1507 }
1508 break;
1509
1510 case 'C':
1511 add_command(&command, CMD_CHECK, CMD_NONE,
1512 invert);
1513 chain = optarg;
1514 break;
1515
1516 case 'R':
1517 add_command(&command, CMD_REPLACE, CMD_NONE,
1518 invert);
1519 chain = optarg;
1520 if (optind < argc && argv[optind][0] != '-'
1521 && argv[optind][0] != '!')
1522 rulenum = parse_rulenumber(argv[optind++]);
1523 else
1524 exit_error(PARAMETER_PROBLEM,
1525 "-%c requires a rule number",
1526 cmd2char(CMD_REPLACE));
1527 break;
1528
1529 case 'I':
1530 add_command(&command, CMD_INSERT, CMD_NONE,
1531 invert);
1532 chain = optarg;
1533 if (optind < argc && argv[optind][0] != '-'
1534 && argv[optind][0] != '!')
1535 rulenum = parse_rulenumber(argv[optind++]);
1536 else rulenum = 1;
1537 break;
1538
1539 case 'L':
1540 add_command(&command, CMD_LIST, CMD_ZERO,
1541 invert);
1542 if (optarg) chain = optarg;
1543 else if (optind < argc && argv[optind][0] != '-'
1544 && argv[optind][0] != '!')
1545 chain = argv[optind++];
1546 break;
1547
1548 case 'F':
1549 add_command(&command, CMD_FLUSH, CMD_NONE,
1550 invert);
1551 if (optarg) chain = optarg;
1552 else if (optind < argc && argv[optind][0] != '-'
1553 && argv[optind][0] != '!')
1554 chain = argv[optind++];
1555 break;
1556
1557 case 'Z':
1558 add_command(&command, CMD_ZERO, CMD_LIST,
1559 invert);
1560 if (optarg) chain = optarg;
1561 else if (optind < argc && argv[optind][0] != '-'
1562 && argv[optind][0] != '!')
1563 chain = argv[optind++];
1564 break;
1565
1566 case 'N':
1567 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1568 invert);
1569 chain = optarg;
1570 break;
1571
1572 case 'X':
1573 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1574 invert);
1575 if (optarg) chain = optarg;
1576 else if (optind < argc && argv[optind][0] != '-'
1577 && argv[optind][0] != '!')
1578 chain = argv[optind++];
1579 break;
1580
1581 case 'E':
1582 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1583 invert);
1584 chain = optarg;
1585 if (optind < argc && argv[optind][0] != '-'
1586 && argv[optind][0] != '!')
1587 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001588 else
1589 exit_error(PARAMETER_PROBLEM,
1590 "-%c requires old-chain-name and "
1591 "new-chain-name",
1592 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001593 break;
1594
1595 case 'P':
1596 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1597 invert);
1598 chain = optarg;
1599 if (optind < argc && argv[optind][0] != '-'
1600 && argv[optind][0] != '!')
1601 policy = argv[optind++];
1602 else
1603 exit_error(PARAMETER_PROBLEM,
1604 "-%c requires a chain and a policy",
1605 cmd2char(CMD_SET_POLICY));
1606 break;
1607
1608 case 'h':
1609 if (!optarg)
1610 optarg = argv[optind];
1611
1612 /* iptables -p icmp -h */
1613 if (!ip6tables_matches && protocol)
1614 find_match(protocol, TRY_LOAD);
1615
1616 exit_printhelp();
1617
1618 /*
1619 * Option selection
1620 */
1621 case 'p':
1622 if (check_inverse(optarg, &invert))
1623 optind++;
1624 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1625 invert);
1626
1627 /* Canonicalize into lower case */
1628 for (protocol = argv[optind-1]; *protocol; protocol++)
1629 *protocol = tolower(*protocol);
1630
1631 protocol = argv[optind-1];
1632 fw.ipv6.proto = parse_protocol(protocol);
1633 fw.ipv6.flags |= IP6T_F_PROTO;
1634
1635 if (fw.ipv6.proto == 0
1636 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1637 exit_error(PARAMETER_PROBLEM,
1638 "rule would never match protocol");
1639 fw.nfcache |= NFC_IP6_PROTO;
1640 break;
1641
1642 case 's':
1643 if (check_inverse(optarg, &invert))
1644 optind++;
1645 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1646 invert);
1647 shostnetworkmask = argv[optind-1];
1648 fw.nfcache |= NFC_IP6_SRC;
1649 break;
1650
1651 case 'd':
1652 if (check_inverse(optarg, &invert))
1653 optind++;
1654 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1655 invert);
1656 dhostnetworkmask = argv[optind-1];
1657 fw.nfcache |= NFC_IP6_DST;
1658 break;
1659
1660 case 'j':
1661 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1662 invert);
1663 jumpto = parse_target(optarg);
1664 target = find_target(jumpto, TRY_LOAD);
1665
1666 if (target) {
1667 size_t size;
1668
1669 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1670 + target->size);
1671
1672 target->t = fw_calloc(1, size);
1673 target->t->u.target_size = size;
1674 strcpy(target->t->u.user.name, jumpto);
1675 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001676 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001677 }
1678 break;
1679
1680
1681 case 'i':
1682 if (check_inverse(optarg, &invert))
1683 optind++;
1684 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1685 invert);
1686 parse_interface(argv[optind-1],
1687 fw.ipv6.iniface,
1688 fw.ipv6.iniface_mask);
1689 fw.nfcache |= NFC_IP6_IF_IN;
1690 break;
1691
1692 case 'o':
1693 if (check_inverse(optarg, &invert))
1694 optind++;
1695 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1696 invert);
1697 parse_interface(argv[optind-1],
1698 fw.ipv6.outiface,
1699 fw.ipv6.outiface_mask);
1700 fw.nfcache |= NFC_IP6_IF_OUT;
1701 break;
1702
1703 case 'v':
1704 if (!verbose)
1705 set_option(&options, OPT_VERBOSE,
1706 &fw.ipv6.invflags, invert);
1707 verbose++;
1708 break;
1709
1710 case 'm': {
1711 size_t size;
1712
1713 if (invert)
1714 exit_error(PARAMETER_PROBLEM,
1715 "unexpected ! flag before --match");
1716
1717 m = find_match(optarg, LOAD_MUST_SUCCEED);
1718 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1719 + m->size);
1720 m->m = fw_calloc(1, size);
1721 m->m->u.match_size = size;
1722 strcpy(m->m->u.user.name, m->name);
1723 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001724 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001725 }
1726 break;
1727
1728 case 'n':
1729 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1730 invert);
1731 break;
1732
1733 case 't':
1734 if (invert)
1735 exit_error(PARAMETER_PROBLEM,
1736 "unexpected ! flag before --table");
1737 *table = argv[optind-1];
1738 break;
1739
1740 case 'x':
1741 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1742 invert);
1743 break;
1744
1745 case 'V':
1746 if (invert)
1747 printf("Not %s ;-)\n", program_version);
1748 else
1749 printf("%s v%s\n",
1750 program_name, program_version);
1751 exit(0);
1752
1753 case '0':
1754 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1755 invert);
1756 break;
1757
1758 case 1: /* non option */
1759 if (optarg[0] == '!' && optarg[1] == '\0') {
1760 if (invert)
1761 exit_error(PARAMETER_PROBLEM,
1762 "multiple consecutive ! not"
1763 " allowed");
1764 invert = TRUE;
1765 optarg[0] = '\0';
1766 continue;
1767 }
1768 printf("Bad argument `%s'\n", optarg);
1769 exit_tryhelp(2);
1770
1771 default:
1772 /* FIXME: This scheme doesn't allow two of the same
1773 matches --RR */
1774 if (!target
1775 || !(target->parse(c - target->option_offset,
1776 argv, invert,
1777 &target->tflags,
1778 &fw, &target->t))) {
1779 for (m = ip6tables_matches; m; m = m->next) {
1780 if (m->parse(c - m->option_offset,
1781 argv, invert,
1782 &m->mflags,
1783 &fw,
1784 &fw.nfcache,
1785 &m->m))
1786 break;
1787 }
1788
1789 /* If you listen carefully, you can
1790 actually hear this code suck. */
1791 if (m == NULL
1792 && protocol
1793 && !find_proto(protocol, DONT_LOAD,
1794 options&OPT_NUMERIC)
1795 && (m = find_proto(protocol, TRY_LOAD,
1796 options&OPT_NUMERIC))) {
1797 /* Try loading protocol */
1798 size_t size;
1799
1800 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1801 + m->size);
1802
1803 m->m = fw_calloc(1, size);
1804 m->m->u.match_size = size;
1805 strcpy(m->m->u.user.name, m->name);
1806 m->init(m->m, &fw.nfcache);
1807
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001808 opts = merge_options(opts,
1809 m->extra_opts, &m->option_offset);
1810
Rusty Russell5eed48a2000-06-02 20:12:24 +00001811 optind--;
1812 continue;
1813 }
1814 if (!m)
1815 exit_error(PARAMETER_PROBLEM,
1816 "Unknown arg `%s'",
1817 argv[optind-1]);
1818 }
1819 }
1820 invert = FALSE;
1821 }
1822
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001823 for (m = ip6tables_matches; m; m = m->next)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001824 m->final_check(m->mflags);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001825
Rusty Russell5eed48a2000-06-02 20:12:24 +00001826 if (target)
1827 target->final_check(target->tflags);
1828
1829 /* Fix me: must put inverse options checking here --MN */
1830
1831 if (optind < argc)
1832 exit_error(PARAMETER_PROBLEM,
1833 "unknown arguments found on commandline");
1834 if (!command)
1835 exit_error(PARAMETER_PROBLEM, "no command specified");
1836 if (invert)
1837 exit_error(PARAMETER_PROBLEM,
1838 "nothing appropriate following !");
1839
1840 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1841 CMD_CHECK)) {
1842 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001843 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001844 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001845 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001846 }
1847
1848 if (shostnetworkmask)
1849 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1850 &(fw.ipv6.smsk), &nsaddrs);
1851
1852 if (dhostnetworkmask)
1853 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1854 &(fw.ipv6.dmsk), &ndaddrs);
1855
1856 if ((nsaddrs > 1 || ndaddrs > 1) &&
1857 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1858 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1859 " source or destination IP addresses");
1860
1861 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
1862 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1863 cmd2char(CMD_CHECK));
1864
1865 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1866 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1867 "specify a unique address");
1868
1869 generic_opt_check(command, options);
1870
1871 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1872 exit_error(PARAMETER_PROBLEM,
1873 "chain name `%s' too long (must be under %i chars)",
1874 chain, IP6T_FUNCTION_MAXNAMELEN);
1875
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001876 /* only allocate handle if we weren't called with a handle */
1877 if (!*handle)
1878 *handle = ip6tc_init(*table);
1879
1880 if (!*handle)
1881 exit_error(VERSION_PROBLEM,
1882 "can't initialize ip6tables table `%s': %s",
1883 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001884
1885 if (command == CMD_CHECK
1886 || command == CMD_APPEND
1887 || command == CMD_DELETE
1888 || command == CMD_INSERT
1889 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001890 if (strcmp(chain, "PREROUTING") == 0
1891 || strcmp(chain, "INPUT") == 0) {
1892 /* -o not valid with incoming packets. */
1893 if (options & OPT_VIANAMEOUT)
1894 exit_error(PARAMETER_PROBLEM,
1895 "Can't use -%c with %s\n",
1896 opt2char(OPT_VIANAMEOUT),
1897 chain);
1898 /* -i required with -C */
1899 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
1900 exit_error(PARAMETER_PROBLEM,
1901 "Need -%c with %s\n",
1902 opt2char(OPT_VIANAMEIN),
1903 chain);
1904 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001906 if (strcmp(chain, "POSTROUTING") == 0
1907 || strcmp(chain, "OUTPUT") == 0) {
1908 /* -i not valid with outgoing packets */
1909 if (options & OPT_VIANAMEIN)
1910 exit_error(PARAMETER_PROBLEM,
1911 "Can't use -%c with %s\n",
1912 opt2char(OPT_VIANAMEIN),
1913 chain);
1914 /* -o required with -C */
1915 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
1916 exit_error(PARAMETER_PROBLEM,
1917 "Need -%c with %s\n",
1918 opt2char(OPT_VIANAMEOUT),
1919 chain);
1920 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921
1922 if (target && ip6tc_is_chain(jumpto, *handle)) {
1923 printf("Warning: using chain %s, not extension\n",
1924 jumpto);
1925
1926 target = NULL;
1927 }
1928
1929 /* If they didn't specify a target, or it's a chain
1930 name, use standard. */
1931 if (!target
1932 && (strlen(jumpto) == 0
1933 || ip6tc_is_chain(jumpto, *handle))) {
1934 size_t size;
1935
1936 target = find_target(IP6T_STANDARD_TARGET,
1937 LOAD_MUST_SUCCEED);
1938
1939 size = sizeof(struct ip6t_entry_target)
1940 + target->size;
1941 target->t = fw_calloc(1, size);
1942 target->t->u.target_size = size;
1943 strcpy(target->t->u.user.name, jumpto);
1944 target->init(target->t, &fw.nfcache);
1945 }
1946
1947 if (!target) {
1948 struct ip6t_entry_target unknown_target;
1949
1950 /* Don't know it. Must be extension with no
1951 options? */
1952 unknown_target.u.target_size = sizeof(unknown_target);
1953 strcpy(unknown_target.u.user.name, jumpto);
1954
1955 e = generate_entry(&fw, ip6tables_matches,
1956 &unknown_target);
1957 } else {
1958 e = generate_entry(&fw, ip6tables_matches, target->t);
1959 }
1960 }
1961
1962 switch (command) {
1963 case CMD_APPEND:
1964 ret = append_entry(chain, e,
1965 nsaddrs, saddrs, ndaddrs, daddrs,
1966 options&OPT_VERBOSE,
1967 handle);
1968 break;
1969 case CMD_CHECK:
1970 ret = check_packet(chain, e,
1971 nsaddrs, saddrs, ndaddrs, daddrs,
1972 options&OPT_VERBOSE, handle);
1973 break;
1974 case CMD_DELETE:
1975 ret = delete_entry(chain, e,
1976 nsaddrs, saddrs, ndaddrs, daddrs,
1977 options&OPT_VERBOSE,
1978 handle);
1979 break;
1980 case CMD_DELETE_NUM:
1981 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1982 break;
1983 case CMD_REPLACE:
1984 ret = replace_entry(chain, e, rulenum - 1,
1985 saddrs, daddrs, options&OPT_VERBOSE,
1986 handle);
1987 break;
1988 case CMD_INSERT:
1989 ret = insert_entry(chain, e, rulenum - 1,
1990 nsaddrs, saddrs, ndaddrs, daddrs,
1991 options&OPT_VERBOSE,
1992 handle);
1993 break;
1994 case CMD_LIST:
1995 ret = list_entries(chain,
1996 options&OPT_VERBOSE,
1997 options&OPT_NUMERIC,
1998 options&OPT_EXPANDED,
1999 options&OPT_LINENUMBERS,
2000 handle);
2001 break;
2002 case CMD_FLUSH:
2003 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2004 break;
2005 case CMD_ZERO:
2006 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2007 break;
2008 case CMD_LIST|CMD_ZERO:
2009 ret = list_entries(chain,
2010 options&OPT_VERBOSE,
2011 options&OPT_NUMERIC,
2012 options&OPT_EXPANDED,
2013 options&OPT_LINENUMBERS,
2014 handle);
2015 if (ret)
2016 ret = zero_entries(chain,
2017 options&OPT_VERBOSE, handle);
2018 break;
2019 case CMD_NEW_CHAIN:
2020 ret = ip6tc_create_chain(chain, handle);
2021 break;
2022 case CMD_DELETE_CHAIN:
2023 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2024 break;
2025 case CMD_RENAME_CHAIN:
2026 ret = ip6tc_rename_chain(chain, newname, handle);
2027 break;
2028 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002029 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002030 break;
2031 default:
2032 /* We should never reach this... */
2033 exit_tryhelp(2);
2034 }
2035
2036 if (verbose > 1)
2037 dump_entries6(*handle);
2038
2039 return ret;
2040}