blob: 023acbf527013f13ecff33b960eb975a133fef1c [file] [log] [blame]
Rusty Russell5eed48a2000-06-02 20:12:24 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
12 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <getopt.h>
29#include <string.h>
30#include <netdb.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <dlfcn.h>
35#include <ctype.h>
36#include <stdarg.h>
37#include <limits.h>
38#include <ip6tables.h>
39#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
42#include <sys/wait.h>
43#include <sys/types.h>
44#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
53#ifndef IP6T_LIB_DIR
54#define IP6T_LIB_DIR "/usr/local/lib/iptables"
55#endif
56
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000057#ifndef PROC_SYS_MODPROBE
58#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
59#endif
60
Rusty Russell5eed48a2000-06-02 20:12:24 +000061#define FMT_NUMERIC 0x0001
62#define FMT_NOCOUNTS 0x0002
63#define FMT_KILOMEGAGIGA 0x0004
64#define FMT_OPTIONS 0x0008
65#define FMT_NOTABLE 0x0010
66#define FMT_NOTARGET 0x0020
67#define FMT_VIA 0x0040
68#define FMT_NONEWLINE 0x0080
69#define FMT_LINENUMBERS 0x0100
70
71#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
72 | FMT_NUMERIC | FMT_NOTABLE)
73#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
74
75
76#define CMD_NONE 0x0000U
77#define CMD_INSERT 0x0001U
78#define CMD_DELETE 0x0002U
79#define CMD_DELETE_NUM 0x0004U
80#define CMD_REPLACE 0x0008U
81#define CMD_APPEND 0x0010U
82#define CMD_LIST 0x0020U
83#define CMD_FLUSH 0x0040U
84#define CMD_ZERO 0x0080U
85#define CMD_NEW_CHAIN 0x0100U
86#define CMD_DELETE_CHAIN 0x0200U
87#define CMD_SET_POLICY 0x0400U
88#define CMD_CHECK 0x0800U
89#define CMD_RENAME_CHAIN 0x1000U
90#define NUMBER_OF_CMD 13
91static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000092 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94#define OPTION_OFFSET 256
95
96#define OPT_NONE 0x00000U
97#define OPT_NUMERIC 0x00001U
98#define OPT_SOURCE 0x00002U
99#define OPT_DESTINATION 0x00004U
100#define OPT_PROTOCOL 0x00008U
101#define OPT_JUMP 0x00010U
102#define OPT_VERBOSE 0x00020U
103#define OPT_EXPANDED 0x00040U
104#define OPT_VIANAMEIN 0x00080U
105#define OPT_VIANAMEOUT 0x00100U
106#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000107#define OPT_COUNTERS 0x00400U
108#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000109static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000110= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000111
112static struct option original_opts[] = {
113 { "append", 1, 0, 'A' },
114 { "delete", 1, 0, 'D' },
115 { "insert", 1, 0, 'I' },
116 { "replace", 1, 0, 'R' },
117 { "list", 2, 0, 'L' },
118 { "flush", 2, 0, 'F' },
119 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000120 { "new-chain", 1, 0, 'N' },
121 { "delete-chain", 2, 0, 'X' },
122 { "rename-chain", 2, 0, 'E' },
123 { "policy", 1, 0, 'P' },
124 { "source", 1, 0, 's' },
125 { "destination", 1, 0, 'd' },
126 { "src", 1, 0, 's' }, /* synonym */
127 { "dst", 1, 0, 'd' }, /* synonym */
128 { "protocol", 1, 0, 'p' },
129 { "in-interface", 1, 0, 'i' },
130 { "jump", 1, 0, 'j' },
131 { "table", 1, 0, 't' },
132 { "match", 1, 0, 'm' },
133 { "numeric", 0, 0, 'n' },
134 { "out-interface", 1, 0, 'o' },
135 { "verbose", 0, 0, 'v' },
136 { "exact", 0, 0, 'x' },
137 { "version", 0, 0, 'V' },
138 { "help", 2, 0, 'h' },
139 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000140 { "modprobe", 1, 0, 'M' },
141 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000142 { 0 }
143};
144
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000145#ifndef __OPTIMIZE__
146struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000147ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000148{
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000149 return (void *)e + e->target_offset;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000150}
151#endif
152
Rusty Russell5eed48a2000-06-02 20:12:24 +0000153static struct option *opts = original_opts;
154static unsigned int global_option_offset = 0;
155
156/* Table of legal combinations of commands and options. If any of the
157 * given commands make an option legal, that option is legal (applies to
158 * CMD_LIST and CMD_ZERO only).
159 * Key:
160 * + compulsory
161 * x illegal
162 * optional
163 */
164
165static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
166/* Well, it's better than "Re: Linux vs FreeBSD" */
167{
168 /* -n -s -d -p -j -v -x -i -o --line */
169/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
170/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
171/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
172/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
173/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
174/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
175/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
176/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
177/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
178/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
179/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000180/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000181/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
182};
183
184static int inverse_for_options[NUMBER_OF_OPT] =
185{
186/* -n */ 0,
187/* -s */ IP6T_INV_SRCIP,
188/* -d */ IP6T_INV_DSTIP,
189/* -p */ IP6T_INV_PROTO,
190/* -j */ 0,
191/* -v */ 0,
192/* -x */ 0,
193/* -i */ IP6T_INV_VIA_IN,
194/* -o */ IP6T_INV_VIA_OUT,
195/*--line*/ 0
196};
197
198const char *program_version;
199const char *program_name;
200
201/* Keeping track of external matches and targets: linked lists. */
202struct ip6tables_match *ip6tables_matches = NULL;
203struct ip6tables_target *ip6tables_targets = NULL;
204
205/* Extra debugging from libiptc */
206extern void dump_entries6(const ip6tc_handle_t handle);
207
208/* A few hardcoded protocols for 'all' and in case the user has no
209 /etc/protocols */
210struct pprot {
211 char *name;
212 u_int8_t num;
213};
214
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000215/* Primitive headers... */
216/* defined in netinet/in.h */
217#if 0
218#ifndef IPPROTO_ESP
219#define IPPROTO_ESP 50
220#endif
221#ifndef IPPROTO_AH
222#define IPPROTO_AH 51
223#endif
224#endif
225
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226static const struct pprot chain_protos[] = {
227 { "tcp", IPPROTO_TCP },
228 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000229 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000230 { "esp", IPPROTO_ESP },
231 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232 { "all", 0 },
233};
234
235static char *
236proto_to_name(u_int8_t proto, int nolookup)
237{
238 unsigned int i;
239
240 if (proto && !nolookup) {
241 struct protoent *pent = getprotobynumber(proto);
242 if (pent)
243 return pent->p_name;
244 }
245
246 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
247 if (chain_protos[i].num == proto)
248 return chain_protos[i].name;
249
250 return NULL;
251}
252
253static void
254in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
255{
256 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000257 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000258}
259
260void
261exit_error(enum exittype status, char *msg, ...)
262{
263 va_list args;
264
265 va_start(args, msg);
266 fprintf(stderr, "%s v%s: ", program_name, program_version);
267 vfprintf(stderr, msg, args);
268 va_end(args);
269 fprintf(stderr, "\n");
270 if (status == PARAMETER_PROBLEM)
271 exit_tryhelp(status);
272 if (status == VERSION_PROBLEM)
273 fprintf(stderr,
274 "Perhaps iptables or your kernel needs to be upgraded.\n");
275 exit(status);
276}
277
278void
279exit_tryhelp(int status)
280{
281 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
282 program_name, program_name );
283 exit(status);
284}
285
286void
287exit_printhelp(void)
288{
289 struct ip6tables_match *m = NULL;
290 struct ip6tables_target *t = NULL;
291
292 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000293"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000294" %s -[RI] chain rulenum rule-specification [options]\n"
295" %s -D chain rulenum [options]\n"
296" %s -[LFZ] [chain] [options]\n"
297" %s -[NX] chain\n"
298" %s -E old-chain-name new-chain-name\n"
299" %s -P chain target [options]\n"
300" %s -h (print this help information)\n\n",
301 program_name, program_version, program_name, program_name,
302 program_name, program_name, program_name, program_name,
303 program_name, program_name);
304
305 printf(
306"Commands:\n"
307"Either long or short options are allowed.\n"
308" --append -A chain Append to chain\n"
309" --delete -D chain Delete matching rule from chain\n"
310" --delete -D chain rulenum\n"
311" Delete rule rulenum (1 = first) from chain\n"
312" --insert -I chain [rulenum]\n"
313" Insert in chain as rulenum (default 1=first)\n"
314" --replace -R chain rulenum\n"
315" Replace rule rulenum (1 = first) in chain\n"
316" --list -L [chain] List the rules in a chain or all chains\n"
317" --flush -F [chain] Delete all rules in chain or all chains\n"
318" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000319" --new -N chain Create a new user-defined chain\n"
320" --delete-chain\n"
321" -X [chain] Delete a user-defined chain\n"
322" --policy -P chain target\n"
323" Change policy on chain to target\n"
324" --rename-chain\n"
325" -E old-chain new-chain\n"
326" Change chain name, (moving any references)\n"
327
328"Options:\n"
329" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
330" --source -s [!] address[/mask]\n"
331" source specification\n"
332" --destination -d [!] address[/mask]\n"
333" destination specification\n"
334" --in-interface -i [!] input name[+]\n"
335" network interface name ([+] for wildcard)\n"
336" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000337" target for rule (may load target extension)\n"
338" --match -m match\n"
339" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000340" --numeric -n numeric output of addresses and ports\n"
341" --out-interface -o [!] output name[+]\n"
342" network interface name ([+] for wildcard)\n"
343" --table -t table table to manipulate (default: `filter')\n"
344" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000345" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000346" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000347/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000348" --modprobe=<command> try to insert modules using this command\n"
349" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000350"[!] --version -V print package version.\n");
351
352 /* Print out any special helps. A user might like to be able to add a --help
353 to the commandline, and see expected results. So we call help for all
354 matches & targets */
355 for (t=ip6tables_targets;t;t=t->next) {
356 printf("\n");
357 t->help();
358 }
359 for (m=ip6tables_matches;m;m=m->next) {
360 printf("\n");
361 m->help();
362 }
363 exit(0);
364}
365
366static void
367generic_opt_check(int command, int options)
368{
369 int i, j, legal = 0;
370
371 /* Check that commands are valid with options. Complicated by the
372 * fact that if an option is legal with *any* command given, it is
373 * legal overall (ie. -z and -l).
374 */
375 for (i = 0; i < NUMBER_OF_OPT; i++) {
376 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
377
378 for (j = 0; j < NUMBER_OF_CMD; j++) {
379 if (!(command & (1<<j)))
380 continue;
381
382 if (!(options & (1<<i))) {
383 if (commands_v_options[j][i] == '+')
384 exit_error(PARAMETER_PROBLEM,
385 "You need to supply the `-%c' "
386 "option for this command\n",
387 optflags[i]);
388 } else {
389 if (commands_v_options[j][i] != 'x')
390 legal = 1;
391 else if (legal == 0)
392 legal = -1;
393 }
394 }
395 if (legal == -1)
396 exit_error(PARAMETER_PROBLEM,
397 "Illegal option `-%c' with this command\n",
398 optflags[i]);
399 }
400}
401
402static char
403opt2char(int option)
404{
405 const char *ptr;
406 for (ptr = optflags; option > 1; option >>= 1, ptr++);
407
408 return *ptr;
409}
410
411static char
412cmd2char(int option)
413{
414 const char *ptr;
415 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
416
417 return *ptr;
418}
419
420static void
421add_command(int *cmd, const int newcmd, const int othercmds, int invert)
422{
423 if (invert)
424 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
425 if (*cmd & (~othercmds))
426 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
427 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
428 *cmd |= newcmd;
429}
430
431int
Harald Welteb77f1da2002-03-14 11:35:58 +0000432check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000433{
434 if (option && strcmp(option, "!") == 0) {
435 if (*invert)
436 exit_error(PARAMETER_PROBLEM,
437 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000438 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000439 if (optind) {
440 *optind = *optind+1;
441 if (argc && *optind > argc)
442 exit_error(PARAMETER_PROBLEM,
443 "no argument following `!'");
444 }
445
Rusty Russell5eed48a2000-06-02 20:12:24 +0000446 return TRUE;
447 }
448 return FALSE;
449}
450
451static void *
452fw_calloc(size_t count, size_t size)
453{
454 void *p;
455
456 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000457 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000458 exit(1);
459 }
460 return p;
461}
462
463static void *
464fw_malloc(size_t size)
465{
466 void *p;
467
468 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000469 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470 exit(1);
471 }
472 return p;
473}
474
Rusty Russell5eed48a2000-06-02 20:12:24 +0000475static char *
476addr_to_numeric(const struct in6_addr *addrp)
477{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000478 /* 0000:0000:0000:0000:0000:000.000.000.000
479 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000480 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000481 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000482}
483
484static struct in6_addr *
485numeric_to_addr(const char *num)
486{
487 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000488 int err;
489 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000490 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000491#ifdef DEBUG
492 fprintf(stderr, "\nnumeric2addr: %d\n", err);
493#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000494 return (struct in6_addr *)NULL;
495}
496
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000497
498static struct in6_addr *
499host_to_addr(const char *name, unsigned int *naddr)
500{
501 struct addrinfo hints;
502 struct addrinfo *res;
503 static struct in6_addr *addr;
504 int err;
505
506 memset(&hints, 0, sizeof(hints));
507 hints.ai_flags=AI_CANONNAME;
508 hints.ai_family=AF_INET6;
509 hints.ai_socktype=SOCK_RAW;
510 hints.ai_protocol=41;
511 hints.ai_next=NULL;
512
513 *naddr = 0;
514 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
515#ifdef DEBUG
516 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
517#endif
518 return (struct in6_addr *) NULL;
519 } else {
520 if (res->ai_family != AF_INET6 ||
521 res->ai_addrlen != sizeof(struct sockaddr_in6))
522 return (struct in6_addr *) NULL;
523
524#ifdef DEBUG
525 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
526 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
527#endif
528 /* Get the first element of the address-chain */
529 addr = fw_calloc(1, sizeof(struct in6_addr));
530 in6addrcpy(addr, (struct in6_addr *)
531 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
532 freeaddrinfo(res);
533 *naddr = 1;
534 return addr;
535 }
536
537 return (struct in6_addr *) NULL;
538}
539
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000540static char *
541addr_to_host(const struct in6_addr *addr)
542{
543 struct sockaddr_in6 saddr;
544 int err;
545 static char hostname[NI_MAXHOST];
546
547 memset(&saddr, 0, sizeof(struct sockaddr_in6));
548 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000549 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000550
551 if ( (err=getnameinfo((struct sockaddr *)&saddr,
552 sizeof(struct sockaddr_in6),
553 hostname, sizeof(hostname)-1,
554 NULL, 0, 0)) != 0 ){
555#ifdef DEBUG
556 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
557#endif
558 return (char *) NULL;
559 } else {
560#ifdef DEBUG
561 fprintf (stderr, "\naddr2host: %s\n", hostname);
562#endif
563
564 return hostname;
565 }
566
567 return (char *) NULL;
568}
569
Rusty Russell5eed48a2000-06-02 20:12:24 +0000570static char *
571mask_to_numeric(const struct in6_addr *addrp)
572{
573 static char buf[20];
574 int l = ipv6_prefix_length(addrp);
575 if (l == -1)
576 return addr_to_numeric(addrp);
Harald Welte43ac1912002-03-03 09:43:07 +0000577 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578 return buf;
579}
580
581static struct in6_addr *
582network_to_addr(const char *name)
583{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000584 /* abort();*/
585 /* TODO: not implemented yet, but the exception breaks the
586 * name resolvation */
587 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000588}
589
590static char *
591addr_to_anyname(const struct in6_addr *addr)
592{
593 char *name;
594
595 if ((name = addr_to_host(addr)) != NULL)
596 return name;
597
598 return addr_to_numeric(addr);
599}
600
601/*
602 * All functions starting with "parse" should succeed, otherwise
603 * the program fails.
604 * Most routines return pointers to static data that may change
605 * between calls to the same or other routines with a few exceptions:
606 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
607 * return global static data.
608*/
609
610static struct in6_addr *
611parse_hostnetwork(const char *name, unsigned int *naddrs)
612{
613 struct in6_addr *addrp, *addrptmp;
614
615 if ((addrptmp = numeric_to_addr(name)) != NULL ||
616 (addrptmp = network_to_addr(name)) != NULL) {
617 addrp = fw_malloc(sizeof(struct in6_addr));
618 in6addrcpy(addrp, addrptmp);
619 *naddrs = 1;
620 return addrp;
621 }
622 if ((addrp = host_to_addr(name, naddrs)) != NULL)
623 return addrp;
624
625 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
626}
627
628static struct in6_addr *
629parse_mask(char *mask)
630{
631 static struct in6_addr maskaddr;
632 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000633 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000634
635 if (mask == NULL) {
636 /* no mask at all defaults to 128 bits */
637 memset(&maskaddr, 0xff, sizeof maskaddr);
638 return &maskaddr;
639 }
640 if ((addrp = numeric_to_addr(mask)) != NULL)
641 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000642 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000643 exit_error(PARAMETER_PROBLEM,
644 "invalid mask `%s' specified", mask);
645 if (bits != 0) {
646 char *p = (char *)&maskaddr;
647 memset(p, 0xff, bits / 8);
648 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
649 p[bits / 8] = 0xff << (8 - (bits & 7));
650 return &maskaddr;
651 }
652
653 memset(&maskaddr, 0, sizeof maskaddr);
654 return &maskaddr;
655}
656
Harald Welte43ac1912002-03-03 09:43:07 +0000657void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000658parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
659 struct in6_addr *maskp, unsigned int *naddrs)
660{
661 struct in6_addr *addrp;
662 char buf[256];
663 char *p;
664 int i, j, n;
665
666 strncpy(buf, name, sizeof(buf) - 1);
667 if ((p = strrchr(buf, '/')) != NULL) {
668 *p = '\0';
669 addrp = parse_mask(p + 1);
670 } else
671 addrp = parse_mask(NULL);
672 in6addrcpy(maskp, addrp);
673
674 /* if a null mask is given, the name is ignored, like in "any/0" */
675 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
676 strcpy(buf, "::");
677
678 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
679 n = *naddrs;
680 for (i = 0, j = 0; i < n; i++) {
681 int k;
682 for (k = 0; k < 4; k++)
683 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
684 j++;
685 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000686 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000687 (*naddrs)--;
688 j--;
689 break;
690 }
691 }
692 }
693}
694
695struct ip6tables_match *
696find_match(const char *name, enum ip6t_tryload tryload)
697{
698 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000699 int icmphack = 0;
700
701 /* This is ugly as hell. Nonetheless, there is no way of changing
702 * this without hurting backwards compatibility */
703 if ( (strcmp(name,"icmpv6") == 0) ||
704 (strcmp(name,"ipv6-icmp") == 0) ||
705 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
706
707 if (!icmphack) {
708 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
709 if (strcmp(name, ptr->name) == 0)
710 break;
711 }
712 } else {
713 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
714 if (strcmp("icmp6", ptr->name) == 0)
715 break;
716 }
717 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000718
Harald Welte3efb6ea2001-08-06 18:50:21 +0000719#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720 if (!ptr && tryload != DONT_LOAD) {
721 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
722 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000723 if (!icmphack)
724 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
725 else
726 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000727 if (dlopen(path, RTLD_NOW)) {
728 /* Found library. If it didn't register itself,
729 maybe they specified target as match. */
730 ptr = find_match(name, DONT_LOAD);
731
732 if (!ptr)
733 exit_error(PARAMETER_PROBLEM,
734 "Couldn't load match `%s'\n",
735 name);
736 } else if (tryload == LOAD_MUST_SUCCEED)
737 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000738 "Couldn't load match `%s':%s\n",
739 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000740 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000741#else
742 if (ptr && !ptr->loaded) {
743 if (tryload != DONT_LOAD)
744 ptr->loaded = 1;
745 else
746 ptr = NULL;
747 }
Marc Boucher067477b2002-03-24 15:09:31 +0000748 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
749 exit_error(PARAMETER_PROBLEM,
750 "Couldn't find match `%s'\n", name);
751 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000752#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000753
Harald Welte43ac1912002-03-03 09:43:07 +0000754 if (ptr)
755 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000756
Rusty Russell5eed48a2000-06-02 20:12:24 +0000757 return ptr;
758}
759
760/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
761static struct ip6tables_match *
762find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
763{
Harald Welteed498492001-07-23 01:24:22 +0000764 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765
Harald Welte43ac1912002-03-03 09:43:07 +0000766 if (string_to_number(pname, 0, 255, &proto) != -1) {
767 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000768
Harald Welte43ac1912002-03-03 09:43:07 +0000769 if (protoname)
770 return find_match(protoname, tryload);
771 } else
772 return find_match(pname, tryload);
773
774 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000775}
776
Harald Welte43ac1912002-03-03 09:43:07 +0000777u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778parse_protocol(const char *s)
779{
Harald Welteed498492001-07-23 01:24:22 +0000780 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781
Harald Welteed498492001-07-23 01:24:22 +0000782 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000783 struct protoent *pent;
784
785 if ((pent = getprotobyname(s)))
786 proto = pent->p_proto;
787 else {
788 unsigned int i;
789 for (i = 0;
790 i < sizeof(chain_protos)/sizeof(struct pprot);
791 i++) {
792 if (strcmp(s, chain_protos[i].name) == 0) {
793 proto = chain_protos[i].num;
794 break;
795 }
796 }
797 if (i == sizeof(chain_protos)/sizeof(struct pprot))
798 exit_error(PARAMETER_PROBLEM,
799 "unknown protocol `%s' specified",
800 s);
801 }
802 }
803
804 return (u_int16_t)proto;
805}
806
807static void
808parse_interface(const char *arg, char *vianame, unsigned char *mask)
809{
810 int vialen = strlen(arg);
811 unsigned int i;
812
813 memset(mask, 0, IFNAMSIZ);
814 memset(vianame, 0, IFNAMSIZ);
815
816 if (vialen + 1 > IFNAMSIZ)
817 exit_error(PARAMETER_PROBLEM,
818 "interface name `%s' must be shorter than IFNAMSIZ"
819 " (%i)", arg, IFNAMSIZ-1);
820
821 strcpy(vianame, arg);
822 if (vialen == 0)
823 memset(mask, 0, IFNAMSIZ);
824 else if (vianame[vialen - 1] == '+') {
825 memset(mask, 0xFF, vialen - 1);
826 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000827 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828 } else {
829 /* Include nul-terminator in match */
830 memset(mask, 0xFF, vialen + 1);
831 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000832 for (i = 0; vianame[i]; i++) {
833 if (!isalnum(vianame[i])
834 && vianame[i] != '_'
835 && vianame[i] != '.') {
836 printf("Warning: wierd character in interface"
837 " `%s' (No aliases, :, ! or *).\n",
838 vianame);
839 break;
840 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841 }
842 }
843}
844
845/* Can't be zero. */
846static int
847parse_rulenumber(const char *rule)
848{
Harald Welte43ac1912002-03-03 09:43:07 +0000849 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850
Harald Welte43ac1912002-03-03 09:43:07 +0000851 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000852 exit_error(PARAMETER_PROBLEM,
853 "Invalid rule number `%s'", rule);
854
855 return rulenum;
856}
857
858static const char *
859parse_target(const char *targetname)
860{
861 const char *ptr;
862
863 if (strlen(targetname) < 1)
864 exit_error(PARAMETER_PROBLEM,
865 "Invalid target name (too short)");
866
867 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
868 exit_error(PARAMETER_PROBLEM,
869 "Invalid target name `%s' (%i chars max)",
870 targetname, sizeof(ip6t_chainlabel)-1);
871
872 for (ptr = targetname; *ptr; ptr++)
873 if (isspace(*ptr))
874 exit_error(PARAMETER_PROBLEM,
875 "Invalid target name `%s'", targetname);
876 return targetname;
877}
878
879int
Harald Welteed498492001-07-23 01:24:22 +0000880string_to_number(const char *s, unsigned int min, unsigned int max,
881 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000882{
Harald Welteed498492001-07-23 01:24:22 +0000883 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000884 char *end;
885
886 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000887 errno = 0;
888 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889 if (*end == '\0' && end != s) {
890 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000891 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000892 *ret = number;
893 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000894 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000895 }
896 return -1;
897}
898
899static void
900set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
901 int invert)
902{
903 if (*options & option)
904 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
905 opt2char(option));
906 *options |= option;
907
908 if (invert) {
909 unsigned int i;
910 for (i = 0; 1 << i != option; i++);
911
912 if (!inverse_for_options[i])
913 exit_error(PARAMETER_PROBLEM,
914 "cannot have ! before -%c",
915 opt2char(option));
916 *invflg |= inverse_for_options[i];
917 }
918}
919
920struct ip6tables_target *
921find_target(const char *name, enum ip6t_tryload tryload)
922{
923 struct ip6tables_target *ptr;
924
925 /* Standard target? */
926 if (strcmp(name, "") == 0
927 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
928 || strcmp(name, IP6TC_LABEL_DROP) == 0
929 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
930 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
931 name = "standard";
932
933 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
934 if (strcmp(name, ptr->name) == 0)
935 break;
936 }
937
Harald Welte3efb6ea2001-08-06 18:50:21 +0000938#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000939 if (!ptr && tryload != DONT_LOAD) {
940 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
941 + strlen(name)];
942 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
943 if (dlopen(path, RTLD_NOW)) {
944 /* Found library. If it didn't register itself,
945 maybe they specified match as a target. */
946 ptr = find_target(name, DONT_LOAD);
947 if (!ptr)
948 exit_error(PARAMETER_PROBLEM,
949 "Couldn't load target `%s'\n",
950 name);
951 } else if (tryload == LOAD_MUST_SUCCEED)
952 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000953 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000954 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000955 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000956#else
957 if (ptr && !ptr->loaded) {
958 if (tryload != DONT_LOAD)
959 ptr->loaded = 1;
960 else
961 ptr = NULL;
962 }
Marc Boucher067477b2002-03-24 15:09:31 +0000963 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
964 exit_error(PARAMETER_PROBLEM,
965 "Couldn't find target `%s'\n", name);
966 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000967#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000968
Harald Welte43ac1912002-03-03 09:43:07 +0000969 if (ptr)
970 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000971
Rusty Russell5eed48a2000-06-02 20:12:24 +0000972 return ptr;
973}
974
975static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000976merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000977 unsigned int *option_offset)
978{
979 unsigned int num_old, num_new, i;
980 struct option *merge;
981
982 for (num_old = 0; oldopts[num_old].name; num_old++);
983 for (num_new = 0; newopts[num_new].name; num_new++);
984
985 global_option_offset += OPTION_OFFSET;
986 *option_offset = global_option_offset;
987
988 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
989 memcpy(merge, oldopts, num_old * sizeof(struct option));
990 for (i = 0; i < num_new; i++) {
991 merge[num_old + i] = newopts[i];
992 merge[num_old + i].val += *option_offset;
993 }
994 memset(merge + num_old + num_new, 0, sizeof(struct option));
995
996 return merge;
997}
998
999void
1000register_match6(struct ip6tables_match *me)
1001{
Harald Welte43ac1912002-03-03 09:43:07 +00001002 struct ip6tables_match **i;
1003
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004 if (strcmp(me->version, program_version) != 0) {
1005 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1006 program_name, me->name, me->version, program_version);
1007 exit(1);
1008 }
1009
1010 if (find_match(me->name, DONT_LOAD)) {
1011 fprintf(stderr, "%s: match `%s' already registered.\n",
1012 program_name, me->name);
1013 exit(1);
1014 }
1015
Harald Welte43ac1912002-03-03 09:43:07 +00001016 if (me->size != IP6T_ALIGN(me->size)) {
1017 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1018 program_name, me->name, me->size);
1019 exit(1);
1020 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001021
Harald Welte43ac1912002-03-03 09:43:07 +00001022 /* Append to list. */
1023 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1024 me->next = NULL;
1025 *i = me;
1026
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027 me->m = NULL;
1028 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001029}
1030
1031void
1032register_target6(struct ip6tables_target *me)
1033{
1034 if (strcmp(me->version, program_version) != 0) {
1035 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1036 program_name, me->name, me->version, program_version);
1037 exit(1);
1038 }
1039
1040 if (find_target(me->name, DONT_LOAD)) {
1041 fprintf(stderr, "%s: target `%s' already registered.\n",
1042 program_name, me->name);
1043 exit(1);
1044 }
1045
Harald Welte43ac1912002-03-03 09:43:07 +00001046 if (me->size != IP6T_ALIGN(me->size)) {
1047 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1048 program_name, me->name, me->size);
1049 exit(1);
1050 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001051
Rusty Russell5eed48a2000-06-02 20:12:24 +00001052 /* Prepend to list. */
1053 me->next = ip6tables_targets;
1054 ip6tables_targets = me;
1055 me->t = NULL;
1056 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001057}
1058
1059static void
1060print_num(u_int64_t number, unsigned int format)
1061{
Harald Welte43ac1912002-03-03 09:43:07 +00001062 if (format & FMT_KILOMEGAGIGA) {
1063 if (number > 99999) {
1064 number = (number + 500) / 1000;
1065 if (number > 9999) {
1066 number = (number + 500) / 1000;
1067 if (number > 9999) {
1068 number = (number + 500) / 1000;
1069 if (number > 9999) {
1070 number = (number + 500) / 1000;
1071 printf(FMT("%4lluT ","%lluT "), number);
1072 }
1073 else printf(FMT("%4lluG ","%lluG "), number);
1074 }
1075 else printf(FMT("%4lluM ","%lluM "), number);
1076 } else
1077 printf(FMT("%4lluK ","%lluK "), number);
1078 } else
1079 printf(FMT("%5llu ","%llu "), number);
1080 } else
1081 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001082}
1083
Harald Welte43ac1912002-03-03 09:43:07 +00001084
Rusty Russell5eed48a2000-06-02 20:12:24 +00001085static void
1086print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1087{
1088 struct ip6t_counters counters;
1089 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1090 printf("Chain %s", chain);
1091 if (pol) {
1092 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001093 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001094 fputc(' ', stdout);
1095 print_num(counters.pcnt, (format|FMT_NOTABLE));
1096 fputs("packets, ", stdout);
1097 print_num(counters.bcnt, (format|FMT_NOTABLE));
1098 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001099 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001100 printf(")\n");
1101 } else {
1102 unsigned int refs;
1103 if (!ip6tc_get_references(&refs, chain, handle))
1104 printf(" (ERROR obtaining refs)\n");
1105 else
1106 printf(" (%u references)\n", refs);
1107 }
1108
1109 if (format & FMT_LINENUMBERS)
1110 printf(FMT("%-4s ", "%s "), "num");
1111 if (!(format & FMT_NOCOUNTS)) {
1112 if (format & FMT_KILOMEGAGIGA) {
1113 printf(FMT("%5s ","%s "), "pkts");
1114 printf(FMT("%5s ","%s "), "bytes");
1115 } else {
1116 printf(FMT("%8s ","%s "), "pkts");
1117 printf(FMT("%10s ","%s "), "bytes");
1118 }
1119 }
1120 if (!(format & FMT_NOTARGET))
1121 printf(FMT("%-9s ","%s "), "target");
1122 fputs(" prot ", stdout);
1123 if (format & FMT_OPTIONS)
1124 fputs("opt", stdout);
1125 if (format & FMT_VIA) {
1126 printf(FMT(" %-6s ","%s "), "in");
1127 printf(FMT("%-6s ","%s "), "out");
1128 }
1129 printf(FMT(" %-19s ","%s "), "source");
1130 printf(FMT(" %-19s "," %s "), "destination");
1131 printf("\n");
1132}
1133
Harald Welte43ac1912002-03-03 09:43:07 +00001134
Rusty Russell5eed48a2000-06-02 20:12:24 +00001135static int
1136print_match(const struct ip6t_entry_match *m,
1137 const struct ip6t_ip6 *ip,
1138 int numeric)
1139{
1140 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1141
1142 if (match) {
1143 if (match->print)
1144 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001145 else
1146 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001147 } else {
1148 if (m->u.user.name[0])
1149 printf("UNKNOWN match `%s' ", m->u.user.name);
1150 }
1151 /* Don't stop iterating. */
1152 return 0;
1153}
1154
1155/* e is called `fw' here for hysterical raisins */
1156static void
1157print_firewall(const struct ip6t_entry *fw,
1158 const char *targname,
1159 unsigned int num,
1160 unsigned int format,
1161 const ip6tc_handle_t handle)
1162{
1163 struct ip6tables_target *target = NULL;
1164 const struct ip6t_entry_target *t;
1165 u_int8_t flags;
1166 char buf[BUFSIZ];
1167
Rusty Russell5eed48a2000-06-02 20:12:24 +00001168 if (!ip6tc_is_chain(targname, handle))
1169 target = find_target(targname, TRY_LOAD);
1170 else
1171 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1172
1173 t = ip6t_get_target((struct ip6t_entry *)fw);
1174 flags = fw->ipv6.flags;
1175
1176 if (format & FMT_LINENUMBERS)
1177 printf(FMT("%-4u ", "%u "), num+1);
1178
1179 if (!(format & FMT_NOCOUNTS)) {
1180 print_num(fw->counters.pcnt, format);
1181 print_num(fw->counters.bcnt, format);
1182 }
1183
1184 if (!(format & FMT_NOTARGET))
1185 printf(FMT("%-9s ", "%s "), targname);
1186
1187 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1188 {
Harald Welte43ac1912002-03-03 09:43:07 +00001189 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001190 if (pname)
1191 printf(FMT("%-5s", "%s "), pname);
1192 else
1193 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1194 }
1195
1196 if (format & FMT_OPTIONS) {
1197 if (format & FMT_NOTABLE)
1198 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001199 fputc(' ', stdout); /* Invert flag of FRAG */
1200 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001201 fputc(' ', stdout);
1202 }
1203
1204 if (format & FMT_VIA) {
1205 char iface[IFNAMSIZ+2];
1206
1207 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1208 iface[0] = '!';
1209 iface[1] = '\0';
1210 }
1211 else iface[0] = '\0';
1212
1213 if (fw->ipv6.iniface[0] != '\0') {
1214 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001215 }
1216 else if (format & FMT_NUMERIC) strcat(iface, "*");
1217 else strcat(iface, "any");
1218 printf(FMT(" %-6s ","in %s "), iface);
1219
1220 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1221 iface[0] = '!';
1222 iface[1] = '\0';
1223 }
1224 else iface[0] = '\0';
1225
1226 if (fw->ipv6.outiface[0] != '\0') {
1227 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001228 }
1229 else if (format & FMT_NUMERIC) strcat(iface, "*");
1230 else strcat(iface, "any");
1231 printf(FMT("%-6s ","out %s "), iface);
1232 }
1233
1234 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1235 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1236 && !(format & FMT_NUMERIC))
1237 printf(FMT("%-19s ","%s "), "anywhere");
1238 else {
1239 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001240 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241 else
Harald Welte43ac1912002-03-03 09:43:07 +00001242 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001243 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1244 printf(FMT("%-19s ","%s "), buf);
1245 }
1246
1247 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1248 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1249 && !(format & FMT_NUMERIC))
1250 printf(FMT("%-19s","-> %s"), "anywhere");
1251 else {
1252 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001253 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001254 else
Harald Welte43ac1912002-03-03 09:43:07 +00001255 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001256 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1257 printf(FMT("%-19s","-> %s"), buf);
1258 }
1259
1260 if (format & FMT_NOTABLE)
1261 fputs(" ", stdout);
1262
1263 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1264
1265 if (target) {
1266 if (target->print)
1267 /* Print the target information. */
1268 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1269 } else if (t->u.target_size != sizeof(*t))
1270 printf("[%u bytes of unknown target data] ",
1271 t->u.target_size - sizeof(*t));
1272
1273 if (!(format & FMT_NONEWLINE))
1274 fputc('\n', stdout);
1275}
1276
1277static void
1278print_firewall_line(const struct ip6t_entry *fw,
1279 const ip6tc_handle_t h)
1280{
1281 struct ip6t_entry_target *t;
1282
1283 t = ip6t_get_target((struct ip6t_entry *)fw);
1284 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1285}
1286
1287static int
1288append_entry(const ip6t_chainlabel chain,
1289 struct ip6t_entry *fw,
1290 unsigned int nsaddrs,
1291 const struct in6_addr saddrs[],
1292 unsigned int ndaddrs,
1293 const struct in6_addr daddrs[],
1294 int verbose,
1295 ip6tc_handle_t *handle)
1296{
1297 unsigned int i, j;
1298 int ret = 1;
1299
1300 for (i = 0; i < nsaddrs; i++) {
1301 fw->ipv6.src = saddrs[i];
1302 for (j = 0; j < ndaddrs; j++) {
1303 fw->ipv6.dst = daddrs[j];
1304 if (verbose)
1305 print_firewall_line(fw, *handle);
1306 ret &= ip6tc_append_entry(chain, fw, handle);
1307 }
1308 }
1309
1310 return ret;
1311}
1312
1313static int
1314replace_entry(const ip6t_chainlabel chain,
1315 struct ip6t_entry *fw,
1316 unsigned int rulenum,
1317 const struct in6_addr *saddr,
1318 const struct in6_addr *daddr,
1319 int verbose,
1320 ip6tc_handle_t *handle)
1321{
1322 fw->ipv6.src = *saddr;
1323 fw->ipv6.dst = *daddr;
1324
1325 if (verbose)
1326 print_firewall_line(fw, *handle);
1327 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1328}
1329
1330static int
1331insert_entry(const ip6t_chainlabel chain,
1332 struct ip6t_entry *fw,
1333 unsigned int rulenum,
1334 unsigned int nsaddrs,
1335 const struct in6_addr saddrs[],
1336 unsigned int ndaddrs,
1337 const struct in6_addr daddrs[],
1338 int verbose,
1339 ip6tc_handle_t *handle)
1340{
1341 unsigned int i, j;
1342 int ret = 1;
1343
1344 for (i = 0; i < nsaddrs; i++) {
1345 fw->ipv6.src = saddrs[i];
1346 for (j = 0; j < ndaddrs; j++) {
1347 fw->ipv6.dst = daddrs[j];
1348 if (verbose)
1349 print_firewall_line(fw, *handle);
1350 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1351 }
1352 }
1353
1354 return ret;
1355}
1356
1357static unsigned char *
1358make_delete_mask(struct ip6t_entry *fw)
1359{
1360 /* Establish mask for comparison */
1361 unsigned int size;
1362 struct ip6tables_match *m;
1363 unsigned char *mask, *mptr;
1364
1365 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001366 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001367 if (!m->used)
1368 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001369
1370 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1371 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001372
1373 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001374 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375 + ip6tables_targets->size);
1376
1377 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1378 mptr = mask + sizeof(struct ip6t_entry);
1379
1380 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001381 if (!m->used)
1382 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001383
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001385 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1386 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001387 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001388 }
1389
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001390 memset(mptr, 0xFF,
1391 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1392 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001393
1394 return mask;
1395}
1396
1397static int
1398delete_entry(const ip6t_chainlabel chain,
1399 struct ip6t_entry *fw,
1400 unsigned int nsaddrs,
1401 const struct in6_addr saddrs[],
1402 unsigned int ndaddrs,
1403 const struct in6_addr daddrs[],
1404 int verbose,
1405 ip6tc_handle_t *handle)
1406{
1407 unsigned int i, j;
1408 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001409 unsigned char *mask;
1410
1411 mask = make_delete_mask(fw);
1412 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001413 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001414 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001415 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001416 if (verbose)
1417 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001418 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 }
1420 }
1421 return ret;
1422}
1423
András Kis-Szabó764316a2001-02-26 17:31:20 +00001424int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001425for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1426 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1427{
1428 int ret = 1;
1429 const char *chain;
1430 char *chains;
1431 unsigned int i, chaincount = 0;
1432
1433 chain = ip6tc_first_chain(handle);
1434 while (chain) {
1435 chaincount++;
1436 chain = ip6tc_next_chain(handle);
1437 }
1438
1439 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1440 i = 0;
1441 chain = ip6tc_first_chain(handle);
1442 while (chain) {
1443 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1444 i++;
1445 chain = ip6tc_next_chain(handle);
1446 }
1447
1448 for (i = 0; i < chaincount; i++) {
1449 if (!builtinstoo
1450 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1451 *handle))
1452 continue;
1453 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1454 }
1455
1456 free(chains);
1457 return ret;
1458}
1459
András Kis-Szabó764316a2001-02-26 17:31:20 +00001460int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001461flush_entries(const ip6t_chainlabel chain, int verbose,
1462 ip6tc_handle_t *handle)
1463{
1464 if (!chain)
1465 return for_each_chain(flush_entries, verbose, 1, handle);
1466
1467 if (verbose)
1468 fprintf(stdout, "Flushing chain `%s'\n", chain);
1469 return ip6tc_flush_entries(chain, handle);
1470}
1471
1472static int
1473zero_entries(const ip6t_chainlabel chain, int verbose,
1474 ip6tc_handle_t *handle)
1475{
1476 if (!chain)
1477 return for_each_chain(zero_entries, verbose, 1, handle);
1478
1479 if (verbose)
1480 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1481 return ip6tc_zero_entries(chain, handle);
1482}
1483
András Kis-Szabó764316a2001-02-26 17:31:20 +00001484int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001485delete_chain(const ip6t_chainlabel chain, int verbose,
1486 ip6tc_handle_t *handle)
1487{
1488 if (!chain)
1489 return for_each_chain(delete_chain, verbose, 0, handle);
1490
1491 if (verbose)
1492 fprintf(stdout, "Deleting chain `%s'\n", chain);
1493 return ip6tc_delete_chain(chain, handle);
1494}
1495
1496static int
1497list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1498 int expanded, int linenumbers, ip6tc_handle_t *handle)
1499{
1500 int found = 0;
1501 unsigned int format;
1502 const char *this;
1503
1504 format = FMT_OPTIONS;
1505 if (!verbose)
1506 format |= FMT_NOCOUNTS;
1507 else
1508 format |= FMT_VIA;
1509
1510 if (numeric)
1511 format |= FMT_NUMERIC;
1512
1513 if (!expanded)
1514 format |= FMT_KILOMEGAGIGA;
1515
1516 if (linenumbers)
1517 format |= FMT_LINENUMBERS;
1518
1519 for (this = ip6tc_first_chain(handle);
1520 this;
1521 this = ip6tc_next_chain(handle)) {
1522 const struct ip6t_entry *i;
1523 unsigned int num;
1524
1525 if (chain && strcmp(chain, this) != 0)
1526 continue;
1527
1528 if (found) printf("\n");
1529
1530 print_header(format, this, handle);
1531 i = ip6tc_first_rule(this, handle);
1532
1533 num = 0;
1534 while (i) {
1535 print_firewall(i,
1536 ip6tc_get_target(i, handle),
1537 num++,
1538 format,
1539 *handle);
1540 i = ip6tc_next_rule(i, handle);
1541 }
1542 found = 1;
1543 }
1544
1545 errno = ENOENT;
1546 return found;
1547}
1548
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001549static char *get_modprobe(void)
1550{
Harald Welte43ac1912002-03-03 09:43:07 +00001551 int procfile;
1552 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001553
Harald Welte43ac1912002-03-03 09:43:07 +00001554 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1555 if (procfile < 0)
1556 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001557
Harald Welte43ac1912002-03-03 09:43:07 +00001558 ret = malloc(1024);
1559 if (ret) {
1560 switch (read(procfile, ret, 1024)) {
1561 case -1: goto fail;
1562 case 1024: goto fail; /* Partial read. Wierd */
1563 }
1564 if (ret[strlen(ret)-1]=='\n')
1565 ret[strlen(ret)-1]=0;
1566 close(procfile);
1567 return ret;
1568 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001569 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001570 free(ret);
1571 close(procfile);
1572 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001573}
1574
Harald Welte58918652001-06-16 18:25:25 +00001575int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001576{
Harald Welte43ac1912002-03-03 09:43:07 +00001577 char *buf = NULL;
1578 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001579
Harald Welte43ac1912002-03-03 09:43:07 +00001580 /* If they don't explicitly set it, read out of kernel */
1581 if (!modprobe) {
1582 buf = get_modprobe();
1583 if (!buf)
1584 return -1;
1585 modprobe = buf;
1586 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001587
Harald Welte43ac1912002-03-03 09:43:07 +00001588 switch (fork()) {
1589 case 0:
1590 argv[0] = (char *)modprobe;
1591 argv[1] = (char *)modname;
1592 argv[2] = NULL;
1593 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001594
Harald Welte43ac1912002-03-03 09:43:07 +00001595 /* not usually reached */
1596 exit(0);
1597 case -1:
1598 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001599
Harald Welte43ac1912002-03-03 09:43:07 +00001600 default: /* parent */
1601 wait(NULL);
1602 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001603
Harald Welte43ac1912002-03-03 09:43:07 +00001604 free(buf);
1605 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001606}
1607
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608static struct ip6t_entry *
1609generate_entry(const struct ip6t_entry *fw,
1610 struct ip6tables_match *matches,
1611 struct ip6t_entry_target *target)
1612{
1613 unsigned int size;
1614 struct ip6tables_match *m;
1615 struct ip6t_entry *e;
1616
1617 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001618 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001619 if (!m->used)
1620 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001621
Rusty Russell5eed48a2000-06-02 20:12:24 +00001622 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001624
1625 e = fw_malloc(size + target->u.target_size);
1626 *e = *fw;
1627 e->target_offset = size;
1628 e->next_offset = size + target->u.target_size;
1629
1630 size = 0;
1631 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001632 if (!m->used)
1633 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001634
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635 memcpy(e->elems + size, m->m, m->m->u.match_size);
1636 size += m->m->u.match_size;
1637 }
1638 memcpy(e->elems + size, target, target->u.target_size);
1639
1640 return e;
1641}
1642
1643int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1644{
1645 struct ip6t_entry fw, *e = NULL;
1646 int invert = 0;
1647 unsigned int nsaddrs = 0, ndaddrs = 0;
1648 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1649
1650 int c, verbose = 0;
1651 const char *chain = NULL;
1652 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1653 const char *policy = NULL, *newname = NULL;
1654 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001655 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001656 int ret = 1;
1657 struct ip6tables_match *m;
1658 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001659 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001660 const char *jumpto = "";
1661 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001662 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001663 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001664 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001665
1666 memset(&fw, 0, sizeof(fw));
1667
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001668 opts = original_opts;
1669 global_option_offset = 0;
1670
Harald Welte43ac1912002-03-03 09:43:07 +00001671 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001672 * a second time */
1673 optind = 0;
1674
Harald Welte43ac1912002-03-03 09:43:07 +00001675 /* clear mflags in case do_command gets called a second time
1676 * (we clear the global list of all matches for security)*/
1677 for (m = ip6tables_matches; m; m = m->next) {
1678 m->mflags = 0;
1679 m->used = 0;
1680 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001681
Harald Welte43ac1912002-03-03 09:43:07 +00001682 for (t = ip6tables_targets; t; t = t->next) {
1683 t->tflags = 0;
1684 t->used = 0;
1685 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001686
Rusty Russell5eed48a2000-06-02 20:12:24 +00001687 /* Suppress error messages: we may add new options if we
1688 demand-load a protocol. */
1689 opterr = 0;
1690
1691 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001692 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693 opts, NULL)) != -1) {
1694 switch (c) {
1695 /*
1696 * Command selection
1697 */
1698 case 'A':
1699 add_command(&command, CMD_APPEND, CMD_NONE,
1700 invert);
1701 chain = optarg;
1702 break;
1703
1704 case 'D':
1705 add_command(&command, CMD_DELETE, CMD_NONE,
1706 invert);
1707 chain = optarg;
1708 if (optind < argc && argv[optind][0] != '-'
1709 && argv[optind][0] != '!') {
1710 rulenum = parse_rulenumber(argv[optind++]);
1711 command = CMD_DELETE_NUM;
1712 }
1713 break;
1714
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715 case 'R':
1716 add_command(&command, CMD_REPLACE, CMD_NONE,
1717 invert);
1718 chain = optarg;
1719 if (optind < argc && argv[optind][0] != '-'
1720 && argv[optind][0] != '!')
1721 rulenum = parse_rulenumber(argv[optind++]);
1722 else
1723 exit_error(PARAMETER_PROBLEM,
1724 "-%c requires a rule number",
1725 cmd2char(CMD_REPLACE));
1726 break;
1727
1728 case 'I':
1729 add_command(&command, CMD_INSERT, CMD_NONE,
1730 invert);
1731 chain = optarg;
1732 if (optind < argc && argv[optind][0] != '-'
1733 && argv[optind][0] != '!')
1734 rulenum = parse_rulenumber(argv[optind++]);
1735 else rulenum = 1;
1736 break;
1737
1738 case 'L':
1739 add_command(&command, CMD_LIST, CMD_ZERO,
1740 invert);
1741 if (optarg) chain = optarg;
1742 else if (optind < argc && argv[optind][0] != '-'
1743 && argv[optind][0] != '!')
1744 chain = argv[optind++];
1745 break;
1746
1747 case 'F':
1748 add_command(&command, CMD_FLUSH, CMD_NONE,
1749 invert);
1750 if (optarg) chain = optarg;
1751 else if (optind < argc && argv[optind][0] != '-'
1752 && argv[optind][0] != '!')
1753 chain = argv[optind++];
1754 break;
1755
1756 case 'Z':
1757 add_command(&command, CMD_ZERO, CMD_LIST,
1758 invert);
1759 if (optarg) chain = optarg;
1760 else if (optind < argc && argv[optind][0] != '-'
1761 && argv[optind][0] != '!')
1762 chain = argv[optind++];
1763 break;
1764
1765 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001766 if (optarg && *optarg == '-')
1767 exit_error(PARAMETER_PROBLEM,
1768 "chain name not allowed to start "
1769 "with `-'\n");
1770 if (find_target(optarg, TRY_LOAD))
1771 exit_error(PARAMETER_PROBLEM,
1772 "chain name may not clash "
1773 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001774 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1775 invert);
1776 chain = optarg;
1777 break;
1778
1779 case 'X':
1780 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1781 invert);
1782 if (optarg) chain = optarg;
1783 else if (optind < argc && argv[optind][0] != '-'
1784 && argv[optind][0] != '!')
1785 chain = argv[optind++];
1786 break;
1787
1788 case 'E':
1789 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1790 invert);
1791 chain = optarg;
1792 if (optind < argc && argv[optind][0] != '-'
1793 && argv[optind][0] != '!')
1794 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001795 else
1796 exit_error(PARAMETER_PROBLEM,
1797 "-%c requires old-chain-name and "
1798 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001799 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001800 break;
1801
1802 case 'P':
1803 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1804 invert);
1805 chain = optarg;
1806 if (optind < argc && argv[optind][0] != '-'
1807 && argv[optind][0] != '!')
1808 policy = argv[optind++];
1809 else
1810 exit_error(PARAMETER_PROBLEM,
1811 "-%c requires a chain and a policy",
1812 cmd2char(CMD_SET_POLICY));
1813 break;
1814
1815 case 'h':
1816 if (!optarg)
1817 optarg = argv[optind];
1818
1819 /* iptables -p icmp -h */
1820 if (!ip6tables_matches && protocol)
1821 find_match(protocol, TRY_LOAD);
1822
1823 exit_printhelp();
1824
1825 /*
1826 * Option selection
1827 */
1828 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001829 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001830 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1831 invert);
1832
1833 /* Canonicalize into lower case */
1834 for (protocol = argv[optind-1]; *protocol; protocol++)
1835 *protocol = tolower(*protocol);
1836
1837 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001838 if ( strcmp(protocol,"ipv6-icmp") == 0)
1839 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001840 fw.ipv6.proto = parse_protocol(protocol);
1841 fw.ipv6.flags |= IP6T_F_PROTO;
1842
1843 if (fw.ipv6.proto == 0
1844 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1845 exit_error(PARAMETER_PROBLEM,
1846 "rule would never match protocol");
1847 fw.nfcache |= NFC_IP6_PROTO;
1848 break;
1849
1850 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001851 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001852 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1853 invert);
1854 shostnetworkmask = argv[optind-1];
1855 fw.nfcache |= NFC_IP6_SRC;
1856 break;
1857
1858 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001859 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001860 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1861 invert);
1862 dhostnetworkmask = argv[optind-1];
1863 fw.nfcache |= NFC_IP6_DST;
1864 break;
1865
1866 case 'j':
1867 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1868 invert);
1869 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001870 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001871 target = find_target(jumpto, TRY_LOAD);
1872
1873 if (target) {
1874 size_t size;
1875
Harald Welte43ac1912002-03-03 09:43:07 +00001876 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1877 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878
1879 target->t = fw_calloc(1, size);
1880 target->t->u.target_size = size;
1881 strcpy(target->t->u.user.name, jumpto);
1882 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001883 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001884 }
1885 break;
1886
1887
1888 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001889 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001890 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1891 invert);
1892 parse_interface(argv[optind-1],
1893 fw.ipv6.iniface,
1894 fw.ipv6.iniface_mask);
1895 fw.nfcache |= NFC_IP6_IF_IN;
1896 break;
1897
1898 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001899 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1901 invert);
1902 parse_interface(argv[optind-1],
1903 fw.ipv6.outiface,
1904 fw.ipv6.outiface_mask);
1905 fw.nfcache |= NFC_IP6_IF_OUT;
1906 break;
1907
1908 case 'v':
1909 if (!verbose)
1910 set_option(&options, OPT_VERBOSE,
1911 &fw.ipv6.invflags, invert);
1912 verbose++;
1913 break;
1914
1915 case 'm': {
1916 size_t size;
1917
1918 if (invert)
1919 exit_error(PARAMETER_PROBLEM,
1920 "unexpected ! flag before --match");
1921
1922 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001923 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1924 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001925 m->m = fw_calloc(1, size);
1926 m->m->u.match_size = size;
1927 strcpy(m->m->u.user.name, m->name);
1928 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001929 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001930 }
1931 break;
1932
1933 case 'n':
1934 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1935 invert);
1936 break;
1937
1938 case 't':
1939 if (invert)
1940 exit_error(PARAMETER_PROBLEM,
1941 "unexpected ! flag before --table");
1942 *table = argv[optind-1];
1943 break;
1944
1945 case 'x':
1946 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1947 invert);
1948 break;
1949
1950 case 'V':
1951 if (invert)
1952 printf("Not %s ;-)\n", program_version);
1953 else
1954 printf("%s v%s\n",
1955 program_name, program_version);
1956 exit(0);
1957
1958 case '0':
1959 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1960 invert);
1961 break;
1962
Harald Welte43ac1912002-03-03 09:43:07 +00001963 case 'M':
1964 modprobe = optarg;
1965 break;
1966
1967 case 'c':
1968
1969 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1970 invert);
1971 pcnt = optarg;
1972 if (optind < argc && argv[optind][0] != '-'
1973 && argv[optind][0] != '!')
1974 bcnt = argv[optind++];
1975 else
1976 exit_error(PARAMETER_PROBLEM,
1977 "-%c requires packet and byte counter",
1978 opt2char(OPT_COUNTERS));
1979
1980 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1981 exit_error(PARAMETER_PROBLEM,
1982 "-%c packet counter not numeric",
1983 opt2char(OPT_COUNTERS));
1984
1985 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
1986 exit_error(PARAMETER_PROBLEM,
1987 "-%c byte counter not numeric",
1988 opt2char(OPT_COUNTERS));
1989
1990 break;
1991
1992
Rusty Russell5eed48a2000-06-02 20:12:24 +00001993 case 1: /* non option */
1994 if (optarg[0] == '!' && optarg[1] == '\0') {
1995 if (invert)
1996 exit_error(PARAMETER_PROBLEM,
1997 "multiple consecutive ! not"
1998 " allowed");
1999 invert = TRUE;
2000 optarg[0] = '\0';
2001 continue;
2002 }
2003 printf("Bad argument `%s'\n", optarg);
2004 exit_tryhelp(2);
2005
2006 default:
2007 /* FIXME: This scheme doesn't allow two of the same
2008 matches --RR */
2009 if (!target
2010 || !(target->parse(c - target->option_offset,
2011 argv, invert,
2012 &target->tflags,
2013 &fw, &target->t))) {
2014 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002015 if (!m->used)
2016 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002017
Rusty Russell5eed48a2000-06-02 20:12:24 +00002018 if (m->parse(c - m->option_offset,
2019 argv, invert,
2020 &m->mflags,
2021 &fw,
2022 &fw.nfcache,
2023 &m->m))
2024 break;
2025 }
Harald Welte00f5a9c2002-08-26 14:37:35 +00002026
2027 /* If you listen carefully, you can
2028 actually hear this code suck. */
2029
2030 /* some explanations (after four different bugs
2031 * in 3 different releases): If we encountere a
2032 * parameter, that has not been parsed yet,
2033 * it's not an option of an explicitly loaded
2034 * match or a target. However, we support
2035 * implicit loading of the protocol match
2036 * extension. '-p tcp' means 'l4 proto 6' and
2037 * at the same time 'load tcp protocol match on
2038 * demand if we specify --dport'.
2039 *
2040 * To make this work, we need to make sure:
2041 * - the parameter has not been parsed by
2042 * a match (m above)
2043 * - a protocol has been specified
2044 * - the protocol extension has not been
2045 * loaded yet, or is loaded and unused
2046 * [think of iptables-restore!]
2047 * - the protocol extension can be successively
2048 * loaded
2049 */
2050 if (m == NULL
2051 && protocol
2052 && (!find_proto(protocol, DONT_LOAD,
2053 options&OPT_NUMERIC)
2054 || (find_proto(protocol, DONT_LOAD,
2055 options&OPT_NUMERIC)
2056 && (proto_used == 0))
2057 )
2058 && (m = find_proto(protocol, TRY_LOAD,
2059 options&OPT_NUMERIC))) {
2060 /* Try loading protocol */
2061 size_t size;
2062
2063 proto_used = 1;
2064
2065 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2066 + m->size;
2067
2068 m->m = fw_calloc(1, size);
2069 m->m->u.match_size = size;
2070 strcpy(m->m->u.user.name, m->name);
2071 m->init(m->m, &fw.nfcache);
2072
2073 opts = merge_options(opts,
2074 m->extra_opts, &m->option_offset);
2075
2076 optind--;
2077 continue;
2078 }
2079
Rusty Russell5eed48a2000-06-02 20:12:24 +00002080 if (!m)
2081 exit_error(PARAMETER_PROBLEM,
2082 "Unknown arg `%s'",
2083 argv[optind-1]);
2084 }
2085 }
2086 invert = FALSE;
2087 }
2088
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002089 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002090 if (!m->used)
2091 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002092
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002093 m->final_check(m->mflags);
2094 }
2095
Harald Welte43ac1912002-03-03 09:43:07 +00002096 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002097 target->final_check(target->tflags);
2098
2099 /* Fix me: must put inverse options checking here --MN */
2100
2101 if (optind < argc)
2102 exit_error(PARAMETER_PROBLEM,
2103 "unknown arguments found on commandline");
2104 if (!command)
2105 exit_error(PARAMETER_PROBLEM, "no command specified");
2106 if (invert)
2107 exit_error(PARAMETER_PROBLEM,
2108 "nothing appropriate following !");
2109
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002110 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002111 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002112 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002113 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002114 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002115 }
2116
2117 if (shostnetworkmask)
2118 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2119 &(fw.ipv6.smsk), &nsaddrs);
2120
2121 if (dhostnetworkmask)
2122 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2123 &(fw.ipv6.dmsk), &ndaddrs);
2124
2125 if ((nsaddrs > 1 || ndaddrs > 1) &&
2126 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2127 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2128 " source or destination IP addresses");
2129
Rusty Russell5eed48a2000-06-02 20:12:24 +00002130 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2131 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2132 "specify a unique address");
2133
2134 generic_opt_check(command, options);
2135
2136 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2137 exit_error(PARAMETER_PROBLEM,
2138 "chain name `%s' too long (must be under %i chars)",
2139 chain, IP6T_FUNCTION_MAXNAMELEN);
2140
Harald Welte43ac1912002-03-03 09:43:07 +00002141 /* only allocate handle if we weren't called with a handle */
2142 if (!*handle)
2143 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002144
Harald Welte43ac1912002-03-03 09:43:07 +00002145 if (!*handle) {
2146 /* try to insmod the module if iptc_init failed */
2147 ip6tables_insmod("ip6_tables", modprobe);
2148 *handle = ip6tc_init(*table);
2149 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002150
Harald Welte43ac1912002-03-03 09:43:07 +00002151 if (!*handle)
2152 exit_error(VERSION_PROBLEM,
2153 "can't initialize ip6tables table `%s': %s",
2154 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002155
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002156 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002157 || command == CMD_DELETE
2158 || command == CMD_INSERT
2159 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002160 if (strcmp(chain, "PREROUTING") == 0
2161 || strcmp(chain, "INPUT") == 0) {
2162 /* -o not valid with incoming packets. */
2163 if (options & OPT_VIANAMEOUT)
2164 exit_error(PARAMETER_PROBLEM,
2165 "Can't use -%c with %s\n",
2166 opt2char(OPT_VIANAMEOUT),
2167 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002168 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002169
Harald Welte43ac1912002-03-03 09:43:07 +00002170 if (strcmp(chain, "POSTROUTING") == 0
2171 || strcmp(chain, "OUTPUT") == 0) {
2172 /* -i not valid with outgoing packets */
2173 if (options & OPT_VIANAMEIN)
2174 exit_error(PARAMETER_PROBLEM,
2175 "Can't use -%c with %s\n",
2176 opt2char(OPT_VIANAMEIN),
2177 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002178 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002179
2180 if (target && ip6tc_is_chain(jumpto, *handle)) {
2181 printf("Warning: using chain %s, not extension\n",
2182 jumpto);
2183
2184 target = NULL;
2185 }
2186
2187 /* If they didn't specify a target, or it's a chain
2188 name, use standard. */
2189 if (!target
2190 && (strlen(jumpto) == 0
2191 || ip6tc_is_chain(jumpto, *handle))) {
2192 size_t size;
2193
2194 target = find_target(IP6T_STANDARD_TARGET,
2195 LOAD_MUST_SUCCEED);
2196
2197 size = sizeof(struct ip6t_entry_target)
2198 + target->size;
2199 target->t = fw_calloc(1, size);
2200 target->t->u.target_size = size;
2201 strcpy(target->t->u.user.name, jumpto);
2202 target->init(target->t, &fw.nfcache);
2203 }
2204
2205 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002206 /* it is no chain, and we can't load a plugin.
2207 * We cannot know if the plugin is corrupt, non
2208 * existant OR if the user just misspelled a
2209 * chain. */
2210 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002211 } else {
2212 e = generate_entry(&fw, ip6tables_matches, target->t);
2213 }
2214 }
2215
2216 switch (command) {
2217 case CMD_APPEND:
2218 ret = append_entry(chain, e,
2219 nsaddrs, saddrs, ndaddrs, daddrs,
2220 options&OPT_VERBOSE,
2221 handle);
2222 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002223 case CMD_DELETE:
2224 ret = delete_entry(chain, e,
2225 nsaddrs, saddrs, ndaddrs, daddrs,
2226 options&OPT_VERBOSE,
2227 handle);
2228 break;
2229 case CMD_DELETE_NUM:
2230 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2231 break;
2232 case CMD_REPLACE:
2233 ret = replace_entry(chain, e, rulenum - 1,
2234 saddrs, daddrs, options&OPT_VERBOSE,
2235 handle);
2236 break;
2237 case CMD_INSERT:
2238 ret = insert_entry(chain, e, rulenum - 1,
2239 nsaddrs, saddrs, ndaddrs, daddrs,
2240 options&OPT_VERBOSE,
2241 handle);
2242 break;
2243 case CMD_LIST:
2244 ret = list_entries(chain,
2245 options&OPT_VERBOSE,
2246 options&OPT_NUMERIC,
2247 options&OPT_EXPANDED,
2248 options&OPT_LINENUMBERS,
2249 handle);
2250 break;
2251 case CMD_FLUSH:
2252 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2253 break;
2254 case CMD_ZERO:
2255 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2256 break;
2257 case CMD_LIST|CMD_ZERO:
2258 ret = list_entries(chain,
2259 options&OPT_VERBOSE,
2260 options&OPT_NUMERIC,
2261 options&OPT_EXPANDED,
2262 options&OPT_LINENUMBERS,
2263 handle);
2264 if (ret)
2265 ret = zero_entries(chain,
2266 options&OPT_VERBOSE, handle);
2267 break;
2268 case CMD_NEW_CHAIN:
2269 ret = ip6tc_create_chain(chain, handle);
2270 break;
2271 case CMD_DELETE_CHAIN:
2272 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2273 break;
2274 case CMD_RENAME_CHAIN:
2275 ret = ip6tc_rename_chain(chain, newname, handle);
2276 break;
2277 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002278 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002279 break;
2280 default:
2281 /* We should never reach this... */
2282 exit_tryhelp(2);
2283 }
2284
2285 if (verbose > 1)
2286 dump_entries6(*handle);
2287
2288 return ret;
2289}