blob: b3346b140893a84df8094686975dfd63d3bfda30 [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
Rusty Russell5eed48a2000-06-02 20:12:24 +00001164 if (!ip6tc_is_chain(targname, handle))
1165 target = find_target(targname, TRY_LOAD);
1166 else
1167 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1168
1169 t = ip6t_get_target((struct ip6t_entry *)fw);
1170 flags = fw->ipv6.flags;
1171
1172 if (format & FMT_LINENUMBERS)
1173 printf(FMT("%-4u ", "%u "), num+1);
1174
1175 if (!(format & FMT_NOCOUNTS)) {
1176 print_num(fw->counters.pcnt, format);
1177 print_num(fw->counters.bcnt, format);
1178 }
1179
1180 if (!(format & FMT_NOTARGET))
1181 printf(FMT("%-9s ", "%s "), targname);
1182
1183 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1184 {
Harald Welte43ac1912002-03-03 09:43:07 +00001185 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001186 if (pname)
1187 printf(FMT("%-5s", "%s "), pname);
1188 else
1189 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1190 }
1191
1192 if (format & FMT_OPTIONS) {
1193 if (format & FMT_NOTABLE)
1194 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001195 fputc(' ', stdout); /* Invert flag of FRAG */
1196 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001197 fputc(' ', stdout);
1198 }
1199
1200 if (format & FMT_VIA) {
1201 char iface[IFNAMSIZ+2];
1202
1203 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1204 iface[0] = '!';
1205 iface[1] = '\0';
1206 }
1207 else iface[0] = '\0';
1208
1209 if (fw->ipv6.iniface[0] != '\0') {
1210 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001211 }
1212 else if (format & FMT_NUMERIC) strcat(iface, "*");
1213 else strcat(iface, "any");
1214 printf(FMT(" %-6s ","in %s "), iface);
1215
1216 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1217 iface[0] = '!';
1218 iface[1] = '\0';
1219 }
1220 else iface[0] = '\0';
1221
1222 if (fw->ipv6.outiface[0] != '\0') {
1223 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001224 }
1225 else if (format & FMT_NUMERIC) strcat(iface, "*");
1226 else strcat(iface, "any");
1227 printf(FMT("%-6s ","out %s "), iface);
1228 }
1229
1230 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1231 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1232 && !(format & FMT_NUMERIC))
1233 printf(FMT("%-19s ","%s "), "anywhere");
1234 else {
1235 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001236 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001237 else
Harald Welte43ac1912002-03-03 09:43:07 +00001238 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001239 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1240 printf(FMT("%-19s ","%s "), buf);
1241 }
1242
1243 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1244 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1245 && !(format & FMT_NUMERIC))
1246 printf(FMT("%-19s","-> %s"), "anywhere");
1247 else {
1248 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001249 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001250 else
Harald Welte43ac1912002-03-03 09:43:07 +00001251 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001252 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1253 printf(FMT("%-19s","-> %s"), buf);
1254 }
1255
1256 if (format & FMT_NOTABLE)
1257 fputs(" ", stdout);
1258
1259 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1260
1261 if (target) {
1262 if (target->print)
1263 /* Print the target information. */
1264 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1265 } else if (t->u.target_size != sizeof(*t))
1266 printf("[%u bytes of unknown target data] ",
1267 t->u.target_size - sizeof(*t));
1268
1269 if (!(format & FMT_NONEWLINE))
1270 fputc('\n', stdout);
1271}
1272
1273static void
1274print_firewall_line(const struct ip6t_entry *fw,
1275 const ip6tc_handle_t h)
1276{
1277 struct ip6t_entry_target *t;
1278
1279 t = ip6t_get_target((struct ip6t_entry *)fw);
1280 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1281}
1282
1283static int
1284append_entry(const ip6t_chainlabel chain,
1285 struct ip6t_entry *fw,
1286 unsigned int nsaddrs,
1287 const struct in6_addr saddrs[],
1288 unsigned int ndaddrs,
1289 const struct in6_addr daddrs[],
1290 int verbose,
1291 ip6tc_handle_t *handle)
1292{
1293 unsigned int i, j;
1294 int ret = 1;
1295
1296 for (i = 0; i < nsaddrs; i++) {
1297 fw->ipv6.src = saddrs[i];
1298 for (j = 0; j < ndaddrs; j++) {
1299 fw->ipv6.dst = daddrs[j];
1300 if (verbose)
1301 print_firewall_line(fw, *handle);
1302 ret &= ip6tc_append_entry(chain, fw, handle);
1303 }
1304 }
1305
1306 return ret;
1307}
1308
1309static int
1310replace_entry(const ip6t_chainlabel chain,
1311 struct ip6t_entry *fw,
1312 unsigned int rulenum,
1313 const struct in6_addr *saddr,
1314 const struct in6_addr *daddr,
1315 int verbose,
1316 ip6tc_handle_t *handle)
1317{
1318 fw->ipv6.src = *saddr;
1319 fw->ipv6.dst = *daddr;
1320
1321 if (verbose)
1322 print_firewall_line(fw, *handle);
1323 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1324}
1325
1326static int
1327insert_entry(const ip6t_chainlabel chain,
1328 struct ip6t_entry *fw,
1329 unsigned int rulenum,
1330 unsigned int nsaddrs,
1331 const struct in6_addr saddrs[],
1332 unsigned int ndaddrs,
1333 const struct in6_addr daddrs[],
1334 int verbose,
1335 ip6tc_handle_t *handle)
1336{
1337 unsigned int i, j;
1338 int ret = 1;
1339
1340 for (i = 0; i < nsaddrs; i++) {
1341 fw->ipv6.src = saddrs[i];
1342 for (j = 0; j < ndaddrs; j++) {
1343 fw->ipv6.dst = daddrs[j];
1344 if (verbose)
1345 print_firewall_line(fw, *handle);
1346 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1347 }
1348 }
1349
1350 return ret;
1351}
1352
1353static unsigned char *
1354make_delete_mask(struct ip6t_entry *fw)
1355{
1356 /* Establish mask for comparison */
1357 unsigned int size;
1358 struct ip6tables_match *m;
1359 unsigned char *mask, *mptr;
1360
1361 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001362 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001363 if (!m->used)
1364 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001365
1366 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1367 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001368
1369 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001370 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001371 + ip6tables_targets->size);
1372
1373 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1374 mptr = mask + sizeof(struct ip6t_entry);
1375
1376 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001377 if (!m->used)
1378 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001379
Rusty Russell5eed48a2000-06-02 20:12:24 +00001380 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001381 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1382 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001383 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 }
1385
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001386 memset(mptr, 0xFF,
1387 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1388 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001389
1390 return mask;
1391}
1392
1393static int
1394delete_entry(const ip6t_chainlabel chain,
1395 struct ip6t_entry *fw,
1396 unsigned int nsaddrs,
1397 const struct in6_addr saddrs[],
1398 unsigned int ndaddrs,
1399 const struct in6_addr daddrs[],
1400 int verbose,
1401 ip6tc_handle_t *handle)
1402{
1403 unsigned int i, j;
1404 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001405 unsigned char *mask;
1406
1407 mask = make_delete_mask(fw);
1408 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001409 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001410 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001411 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001412 if (verbose)
1413 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001414 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001415 }
1416 }
1417 return ret;
1418}
1419
1420static int
1421check_packet(const ip6t_chainlabel chain,
1422 struct ip6t_entry *fw,
1423 unsigned int nsaddrs,
1424 const struct in6_addr saddrs[],
1425 unsigned int ndaddrs,
1426 const struct in6_addr daddrs[],
1427 int verbose,
1428 ip6tc_handle_t *handle)
1429{
1430 int ret = 1;
1431 unsigned int i, j;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432 const char *msg;
1433
1434 for (i = 0; i < nsaddrs; i++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001435 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001436 for (j = 0; j < ndaddrs; j++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001437 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001438 if (verbose)
1439 print_firewall_line(fw, *handle);
Harald Welte43ac1912002-03-03 09:43:07 +00001440 msg = ip6tc_check_packet(chain, fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001441 if (!msg) ret = 0;
1442 else printf("%s\n", msg);
1443 }
1444 }
1445
1446 return ret;
1447}
1448
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001449/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001450int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001451for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1452 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1453{
1454 int ret = 1;
1455 const char *chain;
1456 char *chains;
1457 unsigned int i, chaincount = 0;
1458
1459 chain = ip6tc_first_chain(handle);
1460 while (chain) {
1461 chaincount++;
1462 chain = ip6tc_next_chain(handle);
1463 }
1464
1465 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1466 i = 0;
1467 chain = ip6tc_first_chain(handle);
1468 while (chain) {
1469 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1470 i++;
1471 chain = ip6tc_next_chain(handle);
1472 }
1473
1474 for (i = 0; i < chaincount; i++) {
1475 if (!builtinstoo
1476 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1477 *handle))
1478 continue;
1479 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1480 }
1481
1482 free(chains);
1483 return ret;
1484}
1485
András Kis-Szabó764316a2001-02-26 17:31:20 +00001486int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001487flush_entries(const ip6t_chainlabel chain, int verbose,
1488 ip6tc_handle_t *handle)
1489{
1490 if (!chain)
1491 return for_each_chain(flush_entries, verbose, 1, handle);
1492
1493 if (verbose)
1494 fprintf(stdout, "Flushing chain `%s'\n", chain);
1495 return ip6tc_flush_entries(chain, handle);
1496}
1497
1498static int
1499zero_entries(const ip6t_chainlabel chain, int verbose,
1500 ip6tc_handle_t *handle)
1501{
1502 if (!chain)
1503 return for_each_chain(zero_entries, verbose, 1, handle);
1504
1505 if (verbose)
1506 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1507 return ip6tc_zero_entries(chain, handle);
1508}
1509
András Kis-Szabó764316a2001-02-26 17:31:20 +00001510int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001511delete_chain(const ip6t_chainlabel chain, int verbose,
1512 ip6tc_handle_t *handle)
1513{
1514 if (!chain)
1515 return for_each_chain(delete_chain, verbose, 0, handle);
1516
1517 if (verbose)
1518 fprintf(stdout, "Deleting chain `%s'\n", chain);
1519 return ip6tc_delete_chain(chain, handle);
1520}
1521
1522static int
1523list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1524 int expanded, int linenumbers, ip6tc_handle_t *handle)
1525{
1526 int found = 0;
1527 unsigned int format;
1528 const char *this;
1529
1530 format = FMT_OPTIONS;
1531 if (!verbose)
1532 format |= FMT_NOCOUNTS;
1533 else
1534 format |= FMT_VIA;
1535
1536 if (numeric)
1537 format |= FMT_NUMERIC;
1538
1539 if (!expanded)
1540 format |= FMT_KILOMEGAGIGA;
1541
1542 if (linenumbers)
1543 format |= FMT_LINENUMBERS;
1544
1545 for (this = ip6tc_first_chain(handle);
1546 this;
1547 this = ip6tc_next_chain(handle)) {
1548 const struct ip6t_entry *i;
1549 unsigned int num;
1550
1551 if (chain && strcmp(chain, this) != 0)
1552 continue;
1553
1554 if (found) printf("\n");
1555
1556 print_header(format, this, handle);
1557 i = ip6tc_first_rule(this, handle);
1558
1559 num = 0;
1560 while (i) {
1561 print_firewall(i,
1562 ip6tc_get_target(i, handle),
1563 num++,
1564 format,
1565 *handle);
1566 i = ip6tc_next_rule(i, handle);
1567 }
1568 found = 1;
1569 }
1570
1571 errno = ENOENT;
1572 return found;
1573}
1574
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001575static char *get_modprobe(void)
1576{
Harald Welte43ac1912002-03-03 09:43:07 +00001577 int procfile;
1578 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001579
Harald Welte43ac1912002-03-03 09:43:07 +00001580 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1581 if (procfile < 0)
1582 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001583
Harald Welte43ac1912002-03-03 09:43:07 +00001584 ret = malloc(1024);
1585 if (ret) {
1586 switch (read(procfile, ret, 1024)) {
1587 case -1: goto fail;
1588 case 1024: goto fail; /* Partial read. Wierd */
1589 }
1590 if (ret[strlen(ret)-1]=='\n')
1591 ret[strlen(ret)-1]=0;
1592 close(procfile);
1593 return ret;
1594 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001595 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001596 free(ret);
1597 close(procfile);
1598 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001599}
1600
Harald Welte58918652001-06-16 18:25:25 +00001601int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602{
Harald Welte43ac1912002-03-03 09:43:07 +00001603 char *buf = NULL;
1604 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001605
Harald Welte43ac1912002-03-03 09:43:07 +00001606 /* If they don't explicitly set it, read out of kernel */
1607 if (!modprobe) {
1608 buf = get_modprobe();
1609 if (!buf)
1610 return -1;
1611 modprobe = buf;
1612 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001613
Harald Welte43ac1912002-03-03 09:43:07 +00001614 switch (fork()) {
1615 case 0:
1616 argv[0] = (char *)modprobe;
1617 argv[1] = (char *)modname;
1618 argv[2] = NULL;
1619 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001620
Harald Welte43ac1912002-03-03 09:43:07 +00001621 /* not usually reached */
1622 exit(0);
1623 case -1:
1624 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001625
Harald Welte43ac1912002-03-03 09:43:07 +00001626 default: /* parent */
1627 wait(NULL);
1628 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001629
Harald Welte43ac1912002-03-03 09:43:07 +00001630 free(buf);
1631 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001632}
1633
Rusty Russell5eed48a2000-06-02 20:12:24 +00001634static struct ip6t_entry *
1635generate_entry(const struct ip6t_entry *fw,
1636 struct ip6tables_match *matches,
1637 struct ip6t_entry_target *target)
1638{
1639 unsigned int size;
1640 struct ip6tables_match *m;
1641 struct ip6t_entry *e;
1642
1643 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001644 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001645 if (!m->used)
1646 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001647
Rusty Russell5eed48a2000-06-02 20:12:24 +00001648 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001649 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650
1651 e = fw_malloc(size + target->u.target_size);
1652 *e = *fw;
1653 e->target_offset = size;
1654 e->next_offset = size + target->u.target_size;
1655
1656 size = 0;
1657 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001658 if (!m->used)
1659 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001660
Rusty Russell5eed48a2000-06-02 20:12:24 +00001661 memcpy(e->elems + size, m->m, m->m->u.match_size);
1662 size += m->m->u.match_size;
1663 }
1664 memcpy(e->elems + size, target, target->u.target_size);
1665
1666 return e;
1667}
1668
1669int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1670{
1671 struct ip6t_entry fw, *e = NULL;
1672 int invert = 0;
1673 unsigned int nsaddrs = 0, ndaddrs = 0;
1674 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1675
1676 int c, verbose = 0;
1677 const char *chain = NULL;
1678 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1679 const char *policy = NULL, *newname = NULL;
1680 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001681 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001682 int ret = 1;
1683 struct ip6tables_match *m;
1684 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001685 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001686 const char *jumpto = "";
1687 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001688 const char *modprobe = NULL;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001689 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001690
1691 memset(&fw, 0, sizeof(fw));
1692
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001693 opts = original_opts;
1694 global_option_offset = 0;
1695
Harald Welte43ac1912002-03-03 09:43:07 +00001696 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001697 * a second time */
1698 optind = 0;
1699
Harald Welte43ac1912002-03-03 09:43:07 +00001700 /* clear mflags in case do_command gets called a second time
1701 * (we clear the global list of all matches for security)*/
1702 for (m = ip6tables_matches; m; m = m->next) {
1703 m->mflags = 0;
1704 m->used = 0;
1705 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001706
Harald Welte43ac1912002-03-03 09:43:07 +00001707 for (t = ip6tables_targets; t; t = t->next) {
1708 t->tflags = 0;
1709 t->used = 0;
1710 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001711
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712 /* Suppress error messages: we may add new options if we
1713 demand-load a protocol. */
1714 opterr = 0;
1715
1716 while ((c = getopt_long(argc, argv,
Harald Welte43ac1912002-03-03 09:43:07 +00001717 "-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 +00001718 opts, NULL)) != -1) {
1719 switch (c) {
1720 /*
1721 * Command selection
1722 */
1723 case 'A':
1724 add_command(&command, CMD_APPEND, CMD_NONE,
1725 invert);
1726 chain = optarg;
1727 break;
1728
1729 case 'D':
1730 add_command(&command, CMD_DELETE, CMD_NONE,
1731 invert);
1732 chain = optarg;
1733 if (optind < argc && argv[optind][0] != '-'
1734 && argv[optind][0] != '!') {
1735 rulenum = parse_rulenumber(argv[optind++]);
1736 command = CMD_DELETE_NUM;
1737 }
1738 break;
1739
1740 case 'C':
1741 add_command(&command, CMD_CHECK, CMD_NONE,
1742 invert);
1743 chain = optarg;
1744 break;
1745
1746 case 'R':
1747 add_command(&command, CMD_REPLACE, CMD_NONE,
1748 invert);
1749 chain = optarg;
1750 if (optind < argc && argv[optind][0] != '-'
1751 && argv[optind][0] != '!')
1752 rulenum = parse_rulenumber(argv[optind++]);
1753 else
1754 exit_error(PARAMETER_PROBLEM,
1755 "-%c requires a rule number",
1756 cmd2char(CMD_REPLACE));
1757 break;
1758
1759 case 'I':
1760 add_command(&command, CMD_INSERT, CMD_NONE,
1761 invert);
1762 chain = optarg;
1763 if (optind < argc && argv[optind][0] != '-'
1764 && argv[optind][0] != '!')
1765 rulenum = parse_rulenumber(argv[optind++]);
1766 else rulenum = 1;
1767 break;
1768
1769 case 'L':
1770 add_command(&command, CMD_LIST, CMD_ZERO,
1771 invert);
1772 if (optarg) chain = optarg;
1773 else if (optind < argc && argv[optind][0] != '-'
1774 && argv[optind][0] != '!')
1775 chain = argv[optind++];
1776 break;
1777
1778 case 'F':
1779 add_command(&command, CMD_FLUSH, CMD_NONE,
1780 invert);
1781 if (optarg) chain = optarg;
1782 else if (optind < argc && argv[optind][0] != '-'
1783 && argv[optind][0] != '!')
1784 chain = argv[optind++];
1785 break;
1786
1787 case 'Z':
1788 add_command(&command, CMD_ZERO, CMD_LIST,
1789 invert);
1790 if (optarg) chain = optarg;
1791 else if (optind < argc && argv[optind][0] != '-'
1792 && argv[optind][0] != '!')
1793 chain = argv[optind++];
1794 break;
1795
1796 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001797 if (optarg && *optarg == '-')
1798 exit_error(PARAMETER_PROBLEM,
1799 "chain name not allowed to start "
1800 "with `-'\n");
1801 if (find_target(optarg, TRY_LOAD))
1802 exit_error(PARAMETER_PROBLEM,
1803 "chain name may not clash "
1804 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001805 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1806 invert);
1807 chain = optarg;
1808 break;
1809
1810 case 'X':
1811 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1812 invert);
1813 if (optarg) chain = optarg;
1814 else if (optind < argc && argv[optind][0] != '-'
1815 && argv[optind][0] != '!')
1816 chain = argv[optind++];
1817 break;
1818
1819 case 'E':
1820 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1821 invert);
1822 chain = optarg;
1823 if (optind < argc && argv[optind][0] != '-'
1824 && argv[optind][0] != '!')
1825 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001826 else
1827 exit_error(PARAMETER_PROBLEM,
1828 "-%c requires old-chain-name and "
1829 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001830 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001831 break;
1832
1833 case 'P':
1834 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1835 invert);
1836 chain = optarg;
1837 if (optind < argc && argv[optind][0] != '-'
1838 && argv[optind][0] != '!')
1839 policy = argv[optind++];
1840 else
1841 exit_error(PARAMETER_PROBLEM,
1842 "-%c requires a chain and a policy",
1843 cmd2char(CMD_SET_POLICY));
1844 break;
1845
1846 case 'h':
1847 if (!optarg)
1848 optarg = argv[optind];
1849
1850 /* iptables -p icmp -h */
1851 if (!ip6tables_matches && protocol)
1852 find_match(protocol, TRY_LOAD);
1853
1854 exit_printhelp();
1855
1856 /*
1857 * Option selection
1858 */
1859 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001860 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001861 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1862 invert);
1863
1864 /* Canonicalize into lower case */
1865 for (protocol = argv[optind-1]; *protocol; protocol++)
1866 *protocol = tolower(*protocol);
1867
1868 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001869 if ( strcmp(protocol,"ipv6-icmp") == 0)
1870 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001871 fw.ipv6.proto = parse_protocol(protocol);
1872 fw.ipv6.flags |= IP6T_F_PROTO;
1873
1874 if (fw.ipv6.proto == 0
1875 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1876 exit_error(PARAMETER_PROBLEM,
1877 "rule would never match protocol");
1878 fw.nfcache |= NFC_IP6_PROTO;
1879 break;
1880
1881 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001882 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001883 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1884 invert);
1885 shostnetworkmask = argv[optind-1];
1886 fw.nfcache |= NFC_IP6_SRC;
1887 break;
1888
1889 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001890 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1892 invert);
1893 dhostnetworkmask = argv[optind-1];
1894 fw.nfcache |= NFC_IP6_DST;
1895 break;
1896
1897 case 'j':
1898 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1899 invert);
1900 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001901 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001902 target = find_target(jumpto, TRY_LOAD);
1903
1904 if (target) {
1905 size_t size;
1906
Harald Welte43ac1912002-03-03 09:43:07 +00001907 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1908 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909
1910 target->t = fw_calloc(1, size);
1911 target->t->u.target_size = size;
1912 strcpy(target->t->u.user.name, jumpto);
1913 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001914 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001915 }
1916 break;
1917
1918
1919 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001920 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1922 invert);
1923 parse_interface(argv[optind-1],
1924 fw.ipv6.iniface,
1925 fw.ipv6.iniface_mask);
1926 fw.nfcache |= NFC_IP6_IF_IN;
1927 break;
1928
1929 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001930 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001931 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1932 invert);
1933 parse_interface(argv[optind-1],
1934 fw.ipv6.outiface,
1935 fw.ipv6.outiface_mask);
1936 fw.nfcache |= NFC_IP6_IF_OUT;
1937 break;
1938
1939 case 'v':
1940 if (!verbose)
1941 set_option(&options, OPT_VERBOSE,
1942 &fw.ipv6.invflags, invert);
1943 verbose++;
1944 break;
1945
1946 case 'm': {
1947 size_t size;
1948
1949 if (invert)
1950 exit_error(PARAMETER_PROBLEM,
1951 "unexpected ! flag before --match");
1952
1953 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001954 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1955 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001956 m->m = fw_calloc(1, size);
1957 m->m->u.match_size = size;
1958 strcpy(m->m->u.user.name, m->name);
1959 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001960 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001961 }
1962 break;
1963
1964 case 'n':
1965 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1966 invert);
1967 break;
1968
1969 case 't':
1970 if (invert)
1971 exit_error(PARAMETER_PROBLEM,
1972 "unexpected ! flag before --table");
1973 *table = argv[optind-1];
1974 break;
1975
1976 case 'x':
1977 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1978 invert);
1979 break;
1980
1981 case 'V':
1982 if (invert)
1983 printf("Not %s ;-)\n", program_version);
1984 else
1985 printf("%s v%s\n",
1986 program_name, program_version);
1987 exit(0);
1988
1989 case '0':
1990 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1991 invert);
1992 break;
1993
Harald Welte43ac1912002-03-03 09:43:07 +00001994 case 'M':
1995 modprobe = optarg;
1996 break;
1997
1998 case 'c':
1999
2000 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2001 invert);
2002 pcnt = optarg;
2003 if (optind < argc && argv[optind][0] != '-'
2004 && argv[optind][0] != '!')
2005 bcnt = argv[optind++];
2006 else
2007 exit_error(PARAMETER_PROBLEM,
2008 "-%c requires packet and byte counter",
2009 opt2char(OPT_COUNTERS));
2010
2011 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2012 exit_error(PARAMETER_PROBLEM,
2013 "-%c packet counter not numeric",
2014 opt2char(OPT_COUNTERS));
2015
2016 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2017 exit_error(PARAMETER_PROBLEM,
2018 "-%c byte counter not numeric",
2019 opt2char(OPT_COUNTERS));
2020
2021 break;
2022
2023
Rusty Russell5eed48a2000-06-02 20:12:24 +00002024 case 1: /* non option */
2025 if (optarg[0] == '!' && optarg[1] == '\0') {
2026 if (invert)
2027 exit_error(PARAMETER_PROBLEM,
2028 "multiple consecutive ! not"
2029 " allowed");
2030 invert = TRUE;
2031 optarg[0] = '\0';
2032 continue;
2033 }
2034 printf("Bad argument `%s'\n", optarg);
2035 exit_tryhelp(2);
2036
2037 default:
2038 /* FIXME: This scheme doesn't allow two of the same
2039 matches --RR */
2040 if (!target
2041 || !(target->parse(c - target->option_offset,
2042 argv, invert,
2043 &target->tflags,
2044 &fw, &target->t))) {
2045 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002046 if (!m->used)
2047 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002048
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 if (m->parse(c - m->option_offset,
2050 argv, invert,
2051 &m->mflags,
2052 &fw,
2053 &fw.nfcache,
2054 &m->m))
2055 break;
2056 }
2057
2058 /* If you listen carefully, you can
2059 actually hear this code suck. */
2060 if (m == NULL
2061 && protocol
2062 && !find_proto(protocol, DONT_LOAD,
2063 options&OPT_NUMERIC)
2064 && (m = find_proto(protocol, TRY_LOAD,
2065 options&OPT_NUMERIC))) {
2066 /* Try loading protocol */
2067 size_t size;
2068
Harald Welte43ac1912002-03-03 09:43:07 +00002069 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2070 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002071
2072 m->m = fw_calloc(1, size);
2073 m->m->u.match_size = size;
2074 strcpy(m->m->u.user.name, m->name);
2075 m->init(m->m, &fw.nfcache);
2076
Harald Welte43ac1912002-03-03 09:43:07 +00002077 opts = merge_options(opts,
2078 m->extra_opts, &m->option_offset);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002079
Rusty Russell5eed48a2000-06-02 20:12:24 +00002080 optind--;
2081 continue;
2082 }
2083 if (!m)
2084 exit_error(PARAMETER_PROBLEM,
2085 "Unknown arg `%s'",
2086 argv[optind-1]);
2087 }
2088 }
2089 invert = FALSE;
2090 }
2091
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002092 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002093 if (!m->used)
2094 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002095
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002096 m->final_check(m->mflags);
2097 }
2098
Harald Welte43ac1912002-03-03 09:43:07 +00002099 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002100 target->final_check(target->tflags);
2101
2102 /* Fix me: must put inverse options checking here --MN */
2103
2104 if (optind < argc)
2105 exit_error(PARAMETER_PROBLEM,
2106 "unknown arguments found on commandline");
2107 if (!command)
2108 exit_error(PARAMETER_PROBLEM, "no command specified");
2109 if (invert)
2110 exit_error(PARAMETER_PROBLEM,
2111 "nothing appropriate following !");
2112
2113 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2114 CMD_CHECK)) {
2115 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002116 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002117 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002118 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002119 }
2120
2121 if (shostnetworkmask)
2122 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2123 &(fw.ipv6.smsk), &nsaddrs);
2124
2125 if (dhostnetworkmask)
2126 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2127 &(fw.ipv6.dmsk), &ndaddrs);
2128
2129 if ((nsaddrs > 1 || ndaddrs > 1) &&
2130 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2131 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2132 " source or destination IP addresses");
2133
2134 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2135 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2136 cmd2char(CMD_CHECK));
2137
2138 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2139 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2140 "specify a unique address");
2141
2142 generic_opt_check(command, options);
2143
2144 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2145 exit_error(PARAMETER_PROBLEM,
2146 "chain name `%s' too long (must be under %i chars)",
2147 chain, IP6T_FUNCTION_MAXNAMELEN);
2148
Harald Welte43ac1912002-03-03 09:43:07 +00002149 /* only allocate handle if we weren't called with a handle */
2150 if (!*handle)
2151 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002152
Harald Welte43ac1912002-03-03 09:43:07 +00002153 if (!*handle) {
2154 /* try to insmod the module if iptc_init failed */
2155 ip6tables_insmod("ip6_tables", modprobe);
2156 *handle = ip6tc_init(*table);
2157 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002158
Harald Welte43ac1912002-03-03 09:43:07 +00002159 if (!*handle)
2160 exit_error(VERSION_PROBLEM,
2161 "can't initialize ip6tables table `%s': %s",
2162 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002163
2164 if (command == CMD_CHECK
2165 || command == CMD_APPEND
2166 || command == CMD_DELETE
2167 || command == CMD_INSERT
2168 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002169 if (strcmp(chain, "PREROUTING") == 0
2170 || strcmp(chain, "INPUT") == 0) {
2171 /* -o not valid with incoming packets. */
2172 if (options & OPT_VIANAMEOUT)
2173 exit_error(PARAMETER_PROBLEM,
2174 "Can't use -%c with %s\n",
2175 opt2char(OPT_VIANAMEOUT),
2176 chain);
2177 /* -i required with -C */
2178 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2179 exit_error(PARAMETER_PROBLEM,
2180 "Need -%c with %s\n",
2181 opt2char(OPT_VIANAMEIN),
2182 chain);
2183 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002184
Harald Welte43ac1912002-03-03 09:43:07 +00002185 if (strcmp(chain, "POSTROUTING") == 0
2186 || strcmp(chain, "OUTPUT") == 0) {
2187 /* -i not valid with outgoing packets */
2188 if (options & OPT_VIANAMEIN)
2189 exit_error(PARAMETER_PROBLEM,
2190 "Can't use -%c with %s\n",
2191 opt2char(OPT_VIANAMEIN),
2192 chain);
2193 /* -o required with -C */
2194 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2195 exit_error(PARAMETER_PROBLEM,
2196 "Need -%c with %s\n",
2197 opt2char(OPT_VIANAMEOUT),
2198 chain);
2199 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002200
2201 if (target && ip6tc_is_chain(jumpto, *handle)) {
2202 printf("Warning: using chain %s, not extension\n",
2203 jumpto);
2204
2205 target = NULL;
2206 }
2207
2208 /* If they didn't specify a target, or it's a chain
2209 name, use standard. */
2210 if (!target
2211 && (strlen(jumpto) == 0
2212 || ip6tc_is_chain(jumpto, *handle))) {
2213 size_t size;
2214
2215 target = find_target(IP6T_STANDARD_TARGET,
2216 LOAD_MUST_SUCCEED);
2217
2218 size = sizeof(struct ip6t_entry_target)
2219 + target->size;
2220 target->t = fw_calloc(1, size);
2221 target->t->u.target_size = size;
2222 strcpy(target->t->u.user.name, jumpto);
2223 target->init(target->t, &fw.nfcache);
2224 }
2225
2226 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002227 /* it is no chain, and we can't load a plugin.
2228 * We cannot know if the plugin is corrupt, non
2229 * existant OR if the user just misspelled a
2230 * chain. */
2231 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002232 } else {
2233 e = generate_entry(&fw, ip6tables_matches, target->t);
2234 }
2235 }
2236
2237 switch (command) {
2238 case CMD_APPEND:
2239 ret = append_entry(chain, e,
2240 nsaddrs, saddrs, ndaddrs, daddrs,
2241 options&OPT_VERBOSE,
2242 handle);
2243 break;
2244 case CMD_CHECK:
2245 ret = check_packet(chain, e,
2246 nsaddrs, saddrs, ndaddrs, daddrs,
2247 options&OPT_VERBOSE, handle);
2248 break;
2249 case CMD_DELETE:
2250 ret = delete_entry(chain, e,
2251 nsaddrs, saddrs, ndaddrs, daddrs,
2252 options&OPT_VERBOSE,
2253 handle);
2254 break;
2255 case CMD_DELETE_NUM:
2256 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2257 break;
2258 case CMD_REPLACE:
2259 ret = replace_entry(chain, e, rulenum - 1,
2260 saddrs, daddrs, options&OPT_VERBOSE,
2261 handle);
2262 break;
2263 case CMD_INSERT:
2264 ret = insert_entry(chain, e, rulenum - 1,
2265 nsaddrs, saddrs, ndaddrs, daddrs,
2266 options&OPT_VERBOSE,
2267 handle);
2268 break;
2269 case CMD_LIST:
2270 ret = list_entries(chain,
2271 options&OPT_VERBOSE,
2272 options&OPT_NUMERIC,
2273 options&OPT_EXPANDED,
2274 options&OPT_LINENUMBERS,
2275 handle);
2276 break;
2277 case CMD_FLUSH:
2278 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2279 break;
2280 case CMD_ZERO:
2281 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2282 break;
2283 case CMD_LIST|CMD_ZERO:
2284 ret = list_entries(chain,
2285 options&OPT_VERBOSE,
2286 options&OPT_NUMERIC,
2287 options&OPT_EXPANDED,
2288 options&OPT_LINENUMBERS,
2289 handle);
2290 if (ret)
2291 ret = zero_entries(chain,
2292 options&OPT_VERBOSE, handle);
2293 break;
2294 case CMD_NEW_CHAIN:
2295 ret = ip6tc_create_chain(chain, handle);
2296 break;
2297 case CMD_DELETE_CHAIN:
2298 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2299 break;
2300 case CMD_RENAME_CHAIN:
2301 ret = ip6tc_rename_chain(chain, newname, handle);
2302 break;
2303 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002304 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002305 break;
2306 default:
2307 /* We should never reach this... */
2308 exit_tryhelp(2);
2309 }
2310
2311 if (verbose > 1)
2312 dump_entries6(*handle);
2313
2314 return ret;
2315}