blob: ed607d9ba6966af740553f9fbaadba9ebc11d567 [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',
92 'N', 'X', 'P', 'C', 'E' };
93
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' },
120 { "check", 1, 0, 'C' },
121 { "new-chain", 1, 0, 'N' },
122 { "delete-chain", 2, 0, 'X' },
123 { "rename-chain", 2, 0, 'E' },
124 { "policy", 1, 0, 'P' },
125 { "source", 1, 0, 's' },
126 { "destination", 1, 0, 'd' },
127 { "src", 1, 0, 's' }, /* synonym */
128 { "dst", 1, 0, 'd' }, /* synonym */
129 { "protocol", 1, 0, 'p' },
130 { "in-interface", 1, 0, 'i' },
131 { "jump", 1, 0, 'j' },
132 { "table", 1, 0, 't' },
133 { "match", 1, 0, 'm' },
134 { "numeric", 0, 0, 'n' },
135 { "out-interface", 1, 0, 'o' },
136 { "verbose", 0, 0, 'v' },
137 { "exact", 0, 0, 'x' },
138 { "version", 0, 0, 'V' },
139 { "help", 2, 0, 'h' },
140 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000141 { "modprobe", 1, 0, 'M' },
142 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000143 { 0 }
144};
145
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000146#ifndef __OPTIMIZE__
147struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000148ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000149{
150 return (void *)e + e->target_offset;
151}
152#endif
153
Rusty Russell5eed48a2000-06-02 20:12:24 +0000154static struct option *opts = original_opts;
155static unsigned int global_option_offset = 0;
156
157/* Table of legal combinations of commands and options. If any of the
158 * given commands make an option legal, that option is legal (applies to
159 * CMD_LIST and CMD_ZERO only).
160 * Key:
161 * + compulsory
162 * x illegal
163 * optional
164 */
165
166static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
167/* Well, it's better than "Re: Linux vs FreeBSD" */
168{
169 /* -n -s -d -p -j -v -x -i -o --line */
170/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
171/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
172/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
173/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
174/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
175/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
176/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
177/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
178/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
179/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
180/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000181/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000182/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
183};
184
185static int inverse_for_options[NUMBER_OF_OPT] =
186{
187/* -n */ 0,
188/* -s */ IP6T_INV_SRCIP,
189/* -d */ IP6T_INV_DSTIP,
190/* -p */ IP6T_INV_PROTO,
191/* -j */ 0,
192/* -v */ 0,
193/* -x */ 0,
194/* -i */ IP6T_INV_VIA_IN,
195/* -o */ IP6T_INV_VIA_OUT,
196/*--line*/ 0
197};
198
199const char *program_version;
200const char *program_name;
201
202/* Keeping track of external matches and targets: linked lists. */
203struct ip6tables_match *ip6tables_matches = NULL;
204struct ip6tables_target *ip6tables_targets = NULL;
205
206/* Extra debugging from libiptc */
207extern void dump_entries6(const ip6tc_handle_t handle);
208
209/* A few hardcoded protocols for 'all' and in case the user has no
210 /etc/protocols */
211struct pprot {
212 char *name;
213 u_int8_t num;
214};
215
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000216/* Primitive headers... */
217/* defined in netinet/in.h */
218#if 0
219#ifndef IPPROTO_ESP
220#define IPPROTO_ESP 50
221#endif
222#ifndef IPPROTO_AH
223#define IPPROTO_AH 51
224#endif
225#endif
226
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227static const struct pprot chain_protos[] = {
228 { "tcp", IPPROTO_TCP },
229 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000230 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000231 { "esp", IPPROTO_ESP },
232 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000233 { "all", 0 },
234};
235
236static char *
237proto_to_name(u_int8_t proto, int nolookup)
238{
239 unsigned int i;
240
241 if (proto && !nolookup) {
242 struct protoent *pent = getprotobynumber(proto);
243 if (pent)
244 return pent->p_name;
245 }
246
247 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
248 if (chain_protos[i].num == proto)
249 return chain_protos[i].name;
250
251 return NULL;
252}
253
254static void
255in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
256{
257 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000258 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000259}
260
261void
262exit_error(enum exittype status, char *msg, ...)
263{
264 va_list args;
265
266 va_start(args, msg);
267 fprintf(stderr, "%s v%s: ", program_name, program_version);
268 vfprintf(stderr, msg, args);
269 va_end(args);
270 fprintf(stderr, "\n");
271 if (status == PARAMETER_PROBLEM)
272 exit_tryhelp(status);
273 if (status == VERSION_PROBLEM)
274 fprintf(stderr,
275 "Perhaps iptables or your kernel needs to be upgraded.\n");
276 exit(status);
277}
278
279void
280exit_tryhelp(int status)
281{
282 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
283 program_name, program_name );
284 exit(status);
285}
286
287void
288exit_printhelp(void)
289{
290 struct ip6tables_match *m = NULL;
291 struct ip6tables_target *t = NULL;
292
293 printf("%s v%s\n\n"
294"Usage: %s -[ADC] chain rule-specification [options]\n"
295" %s -[RI] chain rulenum rule-specification [options]\n"
296" %s -D chain rulenum [options]\n"
297" %s -[LFZ] [chain] [options]\n"
298" %s -[NX] chain\n"
299" %s -E old-chain-name new-chain-name\n"
300" %s -P chain target [options]\n"
301" %s -h (print this help information)\n\n",
302 program_name, program_version, program_name, program_name,
303 program_name, program_name, program_name, program_name,
304 program_name, program_name);
305
306 printf(
307"Commands:\n"
308"Either long or short options are allowed.\n"
309" --append -A chain Append to chain\n"
310" --delete -D chain Delete matching rule from chain\n"
311" --delete -D chain rulenum\n"
312" Delete rule rulenum (1 = first) from chain\n"
313" --insert -I chain [rulenum]\n"
314" Insert in chain as rulenum (default 1=first)\n"
315" --replace -R chain rulenum\n"
316" Replace rule rulenum (1 = first) in chain\n"
317" --list -L [chain] List the rules in a chain or all chains\n"
318" --flush -F [chain] Delete all rules in chain or all chains\n"
319" --zero -Z [chain] Zero counters in chain or all chains\n"
320" --check -C chain Test this packet on chain\n"
321" --new -N chain Create a new user-defined chain\n"
322" --delete-chain\n"
323" -X [chain] Delete a user-defined chain\n"
324" --policy -P chain target\n"
325" Change policy on chain to target\n"
326" --rename-chain\n"
327" -E old-chain new-chain\n"
328" Change chain name, (moving any references)\n"
329
330"Options:\n"
331" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
332" --source -s [!] address[/mask]\n"
333" source specification\n"
334" --destination -d [!] address[/mask]\n"
335" destination specification\n"
336" --in-interface -i [!] input name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000339" target for rule (may load target extension)\n"
340" --match -m match\n"
341" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000342" --numeric -n numeric output of addresses and ports\n"
343" --out-interface -o [!] output name[+]\n"
344" network interface name ([+] for wildcard)\n"
345" --table -t table table to manipulate (default: `filter')\n"
346" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000347" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000349/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000350" --modprobe=<command> try to insert modules using this command\n"
351" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000352"[!] --version -V print package version.\n");
353
354 /* Print out any special helps. A user might like to be able to add a --help
355 to the commandline, and see expected results. So we call help for all
356 matches & targets */
357 for (t=ip6tables_targets;t;t=t->next) {
358 printf("\n");
359 t->help();
360 }
361 for (m=ip6tables_matches;m;m=m->next) {
362 printf("\n");
363 m->help();
364 }
365 exit(0);
366}
367
368static void
369generic_opt_check(int command, int options)
370{
371 int i, j, legal = 0;
372
373 /* Check that commands are valid with options. Complicated by the
374 * fact that if an option is legal with *any* command given, it is
375 * legal overall (ie. -z and -l).
376 */
377 for (i = 0; i < NUMBER_OF_OPT; i++) {
378 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
379
380 for (j = 0; j < NUMBER_OF_CMD; j++) {
381 if (!(command & (1<<j)))
382 continue;
383
384 if (!(options & (1<<i))) {
385 if (commands_v_options[j][i] == '+')
386 exit_error(PARAMETER_PROBLEM,
387 "You need to supply the `-%c' "
388 "option for this command\n",
389 optflags[i]);
390 } else {
391 if (commands_v_options[j][i] != 'x')
392 legal = 1;
393 else if (legal == 0)
394 legal = -1;
395 }
396 }
397 if (legal == -1)
398 exit_error(PARAMETER_PROBLEM,
399 "Illegal option `-%c' with this command\n",
400 optflags[i]);
401 }
402}
403
404static char
405opt2char(int option)
406{
407 const char *ptr;
408 for (ptr = optflags; option > 1; option >>= 1, ptr++);
409
410 return *ptr;
411}
412
413static char
414cmd2char(int option)
415{
416 const char *ptr;
417 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
418
419 return *ptr;
420}
421
422static void
423add_command(int *cmd, const int newcmd, const int othercmds, int invert)
424{
425 if (invert)
426 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
427 if (*cmd & (~othercmds))
428 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
429 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
430 *cmd |= newcmd;
431}
432
433int
Harald Welteb77f1da2002-03-14 11:35:58 +0000434check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000435{
436 if (option && strcmp(option, "!") == 0) {
437 if (*invert)
438 exit_error(PARAMETER_PROBLEM,
439 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000440 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000441 if (optind) {
442 *optind = *optind+1;
443 if (argc && *optind > argc)
444 exit_error(PARAMETER_PROBLEM,
445 "no argument following `!'");
446 }
447
Rusty Russell5eed48a2000-06-02 20:12:24 +0000448 return TRUE;
449 }
450 return FALSE;
451}
452
453static void *
454fw_calloc(size_t count, size_t size)
455{
456 void *p;
457
458 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000459 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460 exit(1);
461 }
462 return p;
463}
464
465static void *
466fw_malloc(size_t size)
467{
468 void *p;
469
470 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000471 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000472 exit(1);
473 }
474 return p;
475}
476
Rusty Russell5eed48a2000-06-02 20:12:24 +0000477static char *
478addr_to_numeric(const struct in6_addr *addrp)
479{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000480 /* 0000:0000:0000:0000:0000:000.000.000.000
481 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000482 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000483 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000484}
485
486static struct in6_addr *
487numeric_to_addr(const char *num)
488{
489 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000490 int err;
491 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000492 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000493#ifdef DEBUG
494 fprintf(stderr, "\nnumeric2addr: %d\n", err);
495#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000496 return (struct in6_addr *)NULL;
497}
498
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000499
500static struct in6_addr *
501host_to_addr(const char *name, unsigned int *naddr)
502{
503 struct addrinfo hints;
504 struct addrinfo *res;
505 static struct in6_addr *addr;
506 int err;
507
508 memset(&hints, 0, sizeof(hints));
509 hints.ai_flags=AI_CANONNAME;
510 hints.ai_family=AF_INET6;
511 hints.ai_socktype=SOCK_RAW;
512 hints.ai_protocol=41;
513 hints.ai_next=NULL;
514
515 *naddr = 0;
516 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
517#ifdef DEBUG
518 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
519#endif
520 return (struct in6_addr *) NULL;
521 } else {
522 if (res->ai_family != AF_INET6 ||
523 res->ai_addrlen != sizeof(struct sockaddr_in6))
524 return (struct in6_addr *) NULL;
525
526#ifdef DEBUG
527 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
528 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
529#endif
530 /* Get the first element of the address-chain */
531 addr = fw_calloc(1, sizeof(struct in6_addr));
532 in6addrcpy(addr, (struct in6_addr *)
533 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
534 freeaddrinfo(res);
535 *naddr = 1;
536 return addr;
537 }
538
539 return (struct in6_addr *) NULL;
540}
541
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000542static char *
543addr_to_host(const struct in6_addr *addr)
544{
545 struct sockaddr_in6 saddr;
546 int err;
547 static char hostname[NI_MAXHOST];
548
549 memset(&saddr, 0, sizeof(struct sockaddr_in6));
550 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000551 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000552
553 if ( (err=getnameinfo((struct sockaddr *)&saddr,
554 sizeof(struct sockaddr_in6),
555 hostname, sizeof(hostname)-1,
556 NULL, 0, 0)) != 0 ){
557#ifdef DEBUG
558 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
559#endif
560 return (char *) NULL;
561 } else {
562#ifdef DEBUG
563 fprintf (stderr, "\naddr2host: %s\n", hostname);
564#endif
565
566 return hostname;
567 }
568
569 return (char *) NULL;
570}
571
Rusty Russell5eed48a2000-06-02 20:12:24 +0000572static char *
573mask_to_numeric(const struct in6_addr *addrp)
574{
575 static char buf[20];
576 int l = ipv6_prefix_length(addrp);
577 if (l == -1)
578 return addr_to_numeric(addrp);
Harald Welte43ac1912002-03-03 09:43:07 +0000579 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000580 return buf;
581}
582
583static struct in6_addr *
584network_to_addr(const char *name)
585{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000586 /* abort();*/
587 /* TODO: not implemented yet, but the exception breaks the
588 * name resolvation */
589 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000590}
591
592static char *
593addr_to_anyname(const struct in6_addr *addr)
594{
595 char *name;
596
597 if ((name = addr_to_host(addr)) != NULL)
598 return name;
599
600 return addr_to_numeric(addr);
601}
602
603/*
604 * All functions starting with "parse" should succeed, otherwise
605 * the program fails.
606 * Most routines return pointers to static data that may change
607 * between calls to the same or other routines with a few exceptions:
608 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
609 * return global static data.
610*/
611
612static struct in6_addr *
613parse_hostnetwork(const char *name, unsigned int *naddrs)
614{
615 struct in6_addr *addrp, *addrptmp;
616
617 if ((addrptmp = numeric_to_addr(name)) != NULL ||
618 (addrptmp = network_to_addr(name)) != NULL) {
619 addrp = fw_malloc(sizeof(struct in6_addr));
620 in6addrcpy(addrp, addrptmp);
621 *naddrs = 1;
622 return addrp;
623 }
624 if ((addrp = host_to_addr(name, naddrs)) != NULL)
625 return addrp;
626
627 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
628}
629
630static struct in6_addr *
631parse_mask(char *mask)
632{
633 static struct in6_addr maskaddr;
634 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000635 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000636
637 if (mask == NULL) {
638 /* no mask at all defaults to 128 bits */
639 memset(&maskaddr, 0xff, sizeof maskaddr);
640 return &maskaddr;
641 }
642 if ((addrp = numeric_to_addr(mask)) != NULL)
643 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000644 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000645 exit_error(PARAMETER_PROBLEM,
646 "invalid mask `%s' specified", mask);
647 if (bits != 0) {
648 char *p = (char *)&maskaddr;
649 memset(p, 0xff, bits / 8);
650 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
651 p[bits / 8] = 0xff << (8 - (bits & 7));
652 return &maskaddr;
653 }
654
655 memset(&maskaddr, 0, sizeof maskaddr);
656 return &maskaddr;
657}
658
Harald Welte43ac1912002-03-03 09:43:07 +0000659void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000660parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
661 struct in6_addr *maskp, unsigned int *naddrs)
662{
663 struct in6_addr *addrp;
664 char buf[256];
665 char *p;
666 int i, j, n;
667
668 strncpy(buf, name, sizeof(buf) - 1);
669 if ((p = strrchr(buf, '/')) != NULL) {
670 *p = '\0';
671 addrp = parse_mask(p + 1);
672 } else
673 addrp = parse_mask(NULL);
674 in6addrcpy(maskp, addrp);
675
676 /* if a null mask is given, the name is ignored, like in "any/0" */
677 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
678 strcpy(buf, "::");
679
680 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
681 n = *naddrs;
682 for (i = 0, j = 0; i < n; i++) {
683 int k;
684 for (k = 0; k < 4; k++)
685 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
686 j++;
687 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000688 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000689 (*naddrs)--;
690 j--;
691 break;
692 }
693 }
694 }
695}
696
697struct ip6tables_match *
698find_match(const char *name, enum ip6t_tryload tryload)
699{
700 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000701 int icmphack = 0;
702
703 /* This is ugly as hell. Nonetheless, there is no way of changing
704 * this without hurting backwards compatibility */
705 if ( (strcmp(name,"icmpv6") == 0) ||
706 (strcmp(name,"ipv6-icmp") == 0) ||
707 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
708
709 if (!icmphack) {
710 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
711 if (strcmp(name, ptr->name) == 0)
712 break;
713 }
714 } else {
715 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
716 if (strcmp("icmp6", ptr->name) == 0)
717 break;
718 }
719 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000720
Harald Welte3efb6ea2001-08-06 18:50:21 +0000721#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000722 if (!ptr && tryload != DONT_LOAD) {
723 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
724 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000725 if (!icmphack)
726 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
727 else
728 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000729 if (dlopen(path, RTLD_NOW)) {
730 /* Found library. If it didn't register itself,
731 maybe they specified target as match. */
732 ptr = find_match(name, DONT_LOAD);
733
734 if (!ptr)
735 exit_error(PARAMETER_PROBLEM,
736 "Couldn't load match `%s'\n",
737 name);
738 } else if (tryload == LOAD_MUST_SUCCEED)
739 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000740 "Couldn't load match `%s':%s\n",
741 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000742 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000743#else
744 if (ptr && !ptr->loaded) {
745 if (tryload != DONT_LOAD)
746 ptr->loaded = 1;
747 else
748 ptr = NULL;
749 }
Marc Boucher067477b2002-03-24 15:09:31 +0000750 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
751 exit_error(PARAMETER_PROBLEM,
752 "Couldn't find match `%s'\n", name);
753 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000754#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000755
Harald Welte43ac1912002-03-03 09:43:07 +0000756 if (ptr)
757 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000758
759
Rusty Russell5eed48a2000-06-02 20:12:24 +0000760 return ptr;
761}
762
763/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
764static struct ip6tables_match *
765find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
766{
Harald Welteed498492001-07-23 01:24:22 +0000767 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000768
Harald Welte43ac1912002-03-03 09:43:07 +0000769 if (string_to_number(pname, 0, 255, &proto) != -1) {
770 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771
Harald Welte43ac1912002-03-03 09:43:07 +0000772 if (protoname)
773 return find_match(protoname, tryload);
774 } else
775 return find_match(pname, tryload);
776
777 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778}
779
Harald Welte43ac1912002-03-03 09:43:07 +0000780u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781parse_protocol(const char *s)
782{
Harald Welteed498492001-07-23 01:24:22 +0000783 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784
Harald Welteed498492001-07-23 01:24:22 +0000785 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000786 struct protoent *pent;
787
788 if ((pent = getprotobyname(s)))
789 proto = pent->p_proto;
790 else {
791 unsigned int i;
792 for (i = 0;
793 i < sizeof(chain_protos)/sizeof(struct pprot);
794 i++) {
795 if (strcmp(s, chain_protos[i].name) == 0) {
796 proto = chain_protos[i].num;
797 break;
798 }
799 }
800 if (i == sizeof(chain_protos)/sizeof(struct pprot))
801 exit_error(PARAMETER_PROBLEM,
802 "unknown protocol `%s' specified",
803 s);
804 }
805 }
806
807 return (u_int16_t)proto;
808}
809
810static void
811parse_interface(const char *arg, char *vianame, unsigned char *mask)
812{
813 int vialen = strlen(arg);
814 unsigned int i;
815
816 memset(mask, 0, IFNAMSIZ);
817 memset(vianame, 0, IFNAMSIZ);
818
819 if (vialen + 1 > IFNAMSIZ)
820 exit_error(PARAMETER_PROBLEM,
821 "interface name `%s' must be shorter than IFNAMSIZ"
822 " (%i)", arg, IFNAMSIZ-1);
823
824 strcpy(vianame, arg);
825 if (vialen == 0)
826 memset(mask, 0, IFNAMSIZ);
827 else if (vianame[vialen - 1] == '+') {
828 memset(mask, 0xFF, vialen - 1);
829 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000830 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831 } else {
832 /* Include nul-terminator in match */
833 memset(mask, 0xFF, vialen + 1);
834 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000835 for (i = 0; vianame[i]; i++) {
836 if (!isalnum(vianame[i])
837 && vianame[i] != '_'
838 && vianame[i] != '.') {
839 printf("Warning: wierd character in interface"
840 " `%s' (No aliases, :, ! or *).\n",
841 vianame);
842 break;
843 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 }
845 }
846}
847
848/* Can't be zero. */
849static int
850parse_rulenumber(const char *rule)
851{
Harald Welte43ac1912002-03-03 09:43:07 +0000852 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853
Harald Welte43ac1912002-03-03 09:43:07 +0000854 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 exit_error(PARAMETER_PROBLEM,
856 "Invalid rule number `%s'", rule);
857
858 return rulenum;
859}
860
861static const char *
862parse_target(const char *targetname)
863{
864 const char *ptr;
865
866 if (strlen(targetname) < 1)
867 exit_error(PARAMETER_PROBLEM,
868 "Invalid target name (too short)");
869
870 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
871 exit_error(PARAMETER_PROBLEM,
872 "Invalid target name `%s' (%i chars max)",
873 targetname, sizeof(ip6t_chainlabel)-1);
874
875 for (ptr = targetname; *ptr; ptr++)
876 if (isspace(*ptr))
877 exit_error(PARAMETER_PROBLEM,
878 "Invalid target name `%s'", targetname);
879 return targetname;
880}
881
882int
Harald Welteed498492001-07-23 01:24:22 +0000883string_to_number(const char *s, unsigned int min, unsigned int max,
884 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000885{
Harald Welteed498492001-07-23 01:24:22 +0000886 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000887 char *end;
888
889 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000890 errno = 0;
891 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000892 if (*end == '\0' && end != s) {
893 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000894 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000895 *ret = number;
896 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000897 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898 }
899 return -1;
900}
901
902static void
903set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
904 int invert)
905{
906 if (*options & option)
907 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
908 opt2char(option));
909 *options |= option;
910
911 if (invert) {
912 unsigned int i;
913 for (i = 0; 1 << i != option; i++);
914
915 if (!inverse_for_options[i])
916 exit_error(PARAMETER_PROBLEM,
917 "cannot have ! before -%c",
918 opt2char(option));
919 *invflg |= inverse_for_options[i];
920 }
921}
922
923struct ip6tables_target *
924find_target(const char *name, enum ip6t_tryload tryload)
925{
926 struct ip6tables_target *ptr;
927
928 /* Standard target? */
929 if (strcmp(name, "") == 0
930 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
931 || strcmp(name, IP6TC_LABEL_DROP) == 0
932 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
933 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
934 name = "standard";
935
936 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
937 if (strcmp(name, ptr->name) == 0)
938 break;
939 }
940
Harald Welte3efb6ea2001-08-06 18:50:21 +0000941#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000942 if (!ptr && tryload != DONT_LOAD) {
943 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
944 + strlen(name)];
945 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
946 if (dlopen(path, RTLD_NOW)) {
947 /* Found library. If it didn't register itself,
948 maybe they specified match as a target. */
949 ptr = find_target(name, DONT_LOAD);
950 if (!ptr)
951 exit_error(PARAMETER_PROBLEM,
952 "Couldn't load target `%s'\n",
953 name);
954 } else if (tryload == LOAD_MUST_SUCCEED)
955 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000956 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000957 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000958 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000959#else
960 if (ptr && !ptr->loaded) {
961 if (tryload != DONT_LOAD)
962 ptr->loaded = 1;
963 else
964 ptr = NULL;
965 }
Marc Boucher067477b2002-03-24 15:09:31 +0000966 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
967 exit_error(PARAMETER_PROBLEM,
968 "Couldn't find target `%s'\n", name);
969 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000970#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000971
Harald Welte43ac1912002-03-03 09:43:07 +0000972 if (ptr)
973 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000974
Rusty Russell5eed48a2000-06-02 20:12:24 +0000975 return ptr;
976}
977
978static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000979merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 unsigned int *option_offset)
981{
982 unsigned int num_old, num_new, i;
983 struct option *merge;
984
985 for (num_old = 0; oldopts[num_old].name; num_old++);
986 for (num_new = 0; newopts[num_new].name; num_new++);
987
988 global_option_offset += OPTION_OFFSET;
989 *option_offset = global_option_offset;
990
991 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
992 memcpy(merge, oldopts, num_old * sizeof(struct option));
993 for (i = 0; i < num_new; i++) {
994 merge[num_old + i] = newopts[i];
995 merge[num_old + i].val += *option_offset;
996 }
997 memset(merge + num_old + num_new, 0, sizeof(struct option));
998
999 return merge;
1000}
1001
1002void
1003register_match6(struct ip6tables_match *me)
1004{
Harald Welte43ac1912002-03-03 09:43:07 +00001005 struct ip6tables_match **i;
1006
Rusty Russell5eed48a2000-06-02 20:12:24 +00001007 if (strcmp(me->version, program_version) != 0) {
1008 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1009 program_name, me->name, me->version, program_version);
1010 exit(1);
1011 }
1012
1013 if (find_match(me->name, DONT_LOAD)) {
1014 fprintf(stderr, "%s: match `%s' already registered.\n",
1015 program_name, me->name);
1016 exit(1);
1017 }
1018
Harald Welte43ac1912002-03-03 09:43:07 +00001019 if (me->size != IP6T_ALIGN(me->size)) {
1020 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1021 program_name, me->name, me->size);
1022 exit(1);
1023 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001024
Harald Welte43ac1912002-03-03 09:43:07 +00001025 /* Append to list. */
1026 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1027 me->next = NULL;
1028 *i = me;
1029
Rusty Russell5eed48a2000-06-02 20:12:24 +00001030 me->m = NULL;
1031 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001032}
1033
1034void
1035register_target6(struct ip6tables_target *me)
1036{
1037 if (strcmp(me->version, program_version) != 0) {
1038 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1039 program_name, me->name, me->version, program_version);
1040 exit(1);
1041 }
1042
1043 if (find_target(me->name, DONT_LOAD)) {
1044 fprintf(stderr, "%s: target `%s' already registered.\n",
1045 program_name, me->name);
1046 exit(1);
1047 }
1048
Harald Welte43ac1912002-03-03 09:43:07 +00001049 if (me->size != IP6T_ALIGN(me->size)) {
1050 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1051 program_name, me->name, me->size);
1052 exit(1);
1053 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001054
Rusty Russell5eed48a2000-06-02 20:12:24 +00001055 /* Prepend to list. */
1056 me->next = ip6tables_targets;
1057 ip6tables_targets = me;
1058 me->t = NULL;
1059 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001060}
1061
1062static void
1063print_num(u_int64_t number, unsigned int format)
1064{
Harald Welte43ac1912002-03-03 09:43:07 +00001065 if (format & FMT_KILOMEGAGIGA) {
1066 if (number > 99999) {
1067 number = (number + 500) / 1000;
1068 if (number > 9999) {
1069 number = (number + 500) / 1000;
1070 if (number > 9999) {
1071 number = (number + 500) / 1000;
1072 if (number > 9999) {
1073 number = (number + 500) / 1000;
1074 printf(FMT("%4lluT ","%lluT "), number);
1075 }
1076 else printf(FMT("%4lluG ","%lluG "), number);
1077 }
1078 else printf(FMT("%4lluM ","%lluM "), number);
1079 } else
1080 printf(FMT("%4lluK ","%lluK "), number);
1081 } else
1082 printf(FMT("%5llu ","%llu "), number);
1083 } else
1084 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001085}
1086
Harald Welte43ac1912002-03-03 09:43:07 +00001087
Rusty Russell5eed48a2000-06-02 20:12:24 +00001088static void
1089print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1090{
1091 struct ip6t_counters counters;
1092 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1093 printf("Chain %s", chain);
1094 if (pol) {
1095 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001096 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001097 fputc(' ', stdout);
1098 print_num(counters.pcnt, (format|FMT_NOTABLE));
1099 fputs("packets, ", stdout);
1100 print_num(counters.bcnt, (format|FMT_NOTABLE));
1101 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001102 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001103 printf(")\n");
1104 } else {
1105 unsigned int refs;
1106 if (!ip6tc_get_references(&refs, chain, handle))
1107 printf(" (ERROR obtaining refs)\n");
1108 else
1109 printf(" (%u references)\n", refs);
1110 }
1111
1112 if (format & FMT_LINENUMBERS)
1113 printf(FMT("%-4s ", "%s "), "num");
1114 if (!(format & FMT_NOCOUNTS)) {
1115 if (format & FMT_KILOMEGAGIGA) {
1116 printf(FMT("%5s ","%s "), "pkts");
1117 printf(FMT("%5s ","%s "), "bytes");
1118 } else {
1119 printf(FMT("%8s ","%s "), "pkts");
1120 printf(FMT("%10s ","%s "), "bytes");
1121 }
1122 }
1123 if (!(format & FMT_NOTARGET))
1124 printf(FMT("%-9s ","%s "), "target");
1125 fputs(" prot ", stdout);
1126 if (format & FMT_OPTIONS)
1127 fputs("opt", stdout);
1128 if (format & FMT_VIA) {
1129 printf(FMT(" %-6s ","%s "), "in");
1130 printf(FMT("%-6s ","%s "), "out");
1131 }
1132 printf(FMT(" %-19s ","%s "), "source");
1133 printf(FMT(" %-19s "," %s "), "destination");
1134 printf("\n");
1135}
1136
Harald Welte43ac1912002-03-03 09:43:07 +00001137
Rusty Russell5eed48a2000-06-02 20:12:24 +00001138static int
1139print_match(const struct ip6t_entry_match *m,
1140 const struct ip6t_ip6 *ip,
1141 int numeric)
1142{
1143 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1144
1145 if (match) {
1146 if (match->print)
1147 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001148 else
1149 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001150 } else {
1151 if (m->u.user.name[0])
1152 printf("UNKNOWN match `%s' ", m->u.user.name);
1153 }
1154 /* Don't stop iterating. */
1155 return 0;
1156}
1157
1158/* e is called `fw' here for hysterical raisins */
1159static void
1160print_firewall(const struct ip6t_entry *fw,
1161 const char *targname,
1162 unsigned int num,
1163 unsigned int format,
1164 const ip6tc_handle_t handle)
1165{
1166 struct ip6tables_target *target = NULL;
1167 const struct ip6t_entry_target *t;
1168 u_int8_t flags;
1169 char buf[BUFSIZ];
1170
Rusty Russell5eed48a2000-06-02 20:12:24 +00001171 if (!ip6tc_is_chain(targname, handle))
1172 target = find_target(targname, TRY_LOAD);
1173 else
1174 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1175
1176 t = ip6t_get_target((struct ip6t_entry *)fw);
1177 flags = fw->ipv6.flags;
1178
1179 if (format & FMT_LINENUMBERS)
1180 printf(FMT("%-4u ", "%u "), num+1);
1181
1182 if (!(format & FMT_NOCOUNTS)) {
1183 print_num(fw->counters.pcnt, format);
1184 print_num(fw->counters.bcnt, format);
1185 }
1186
1187 if (!(format & FMT_NOTARGET))
1188 printf(FMT("%-9s ", "%s "), targname);
1189
1190 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1191 {
Harald Welte43ac1912002-03-03 09:43:07 +00001192 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001193 if (pname)
1194 printf(FMT("%-5s", "%s "), pname);
1195 else
1196 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1197 }
1198
1199 if (format & FMT_OPTIONS) {
1200 if (format & FMT_NOTABLE)
1201 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001202 fputc(' ', stdout); /* Invert flag of FRAG */
1203 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001204 fputc(' ', stdout);
1205 }
1206
1207 if (format & FMT_VIA) {
1208 char iface[IFNAMSIZ+2];
1209
1210 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1211 iface[0] = '!';
1212 iface[1] = '\0';
1213 }
1214 else iface[0] = '\0';
1215
1216 if (fw->ipv6.iniface[0] != '\0') {
1217 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001218 }
1219 else if (format & FMT_NUMERIC) strcat(iface, "*");
1220 else strcat(iface, "any");
1221 printf(FMT(" %-6s ","in %s "), iface);
1222
1223 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1224 iface[0] = '!';
1225 iface[1] = '\0';
1226 }
1227 else iface[0] = '\0';
1228
1229 if (fw->ipv6.outiface[0] != '\0') {
1230 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001231 }
1232 else if (format & FMT_NUMERIC) strcat(iface, "*");
1233 else strcat(iface, "any");
1234 printf(FMT("%-6s ","out %s "), iface);
1235 }
1236
1237 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1238 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1239 && !(format & FMT_NUMERIC))
1240 printf(FMT("%-19s ","%s "), "anywhere");
1241 else {
1242 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001243 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001244 else
Harald Welte43ac1912002-03-03 09:43:07 +00001245 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001246 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1247 printf(FMT("%-19s ","%s "), buf);
1248 }
1249
1250 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1251 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1252 && !(format & FMT_NUMERIC))
1253 printf(FMT("%-19s","-> %s"), "anywhere");
1254 else {
1255 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001256 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001257 else
Harald Welte43ac1912002-03-03 09:43:07 +00001258 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001259 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1260 printf(FMT("%-19s","-> %s"), buf);
1261 }
1262
1263 if (format & FMT_NOTABLE)
1264 fputs(" ", stdout);
1265
1266 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1267
1268 if (target) {
1269 if (target->print)
1270 /* Print the target information. */
1271 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1272 } else if (t->u.target_size != sizeof(*t))
1273 printf("[%u bytes of unknown target data] ",
1274 t->u.target_size - sizeof(*t));
1275
1276 if (!(format & FMT_NONEWLINE))
1277 fputc('\n', stdout);
1278}
1279
1280static void
1281print_firewall_line(const struct ip6t_entry *fw,
1282 const ip6tc_handle_t h)
1283{
1284 struct ip6t_entry_target *t;
1285
1286 t = ip6t_get_target((struct ip6t_entry *)fw);
1287 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1288}
1289
1290static int
1291append_entry(const ip6t_chainlabel chain,
1292 struct ip6t_entry *fw,
1293 unsigned int nsaddrs,
1294 const struct in6_addr saddrs[],
1295 unsigned int ndaddrs,
1296 const struct in6_addr daddrs[],
1297 int verbose,
1298 ip6tc_handle_t *handle)
1299{
1300 unsigned int i, j;
1301 int ret = 1;
1302
1303 for (i = 0; i < nsaddrs; i++) {
1304 fw->ipv6.src = saddrs[i];
1305 for (j = 0; j < ndaddrs; j++) {
1306 fw->ipv6.dst = daddrs[j];
1307 if (verbose)
1308 print_firewall_line(fw, *handle);
1309 ret &= ip6tc_append_entry(chain, fw, handle);
1310 }
1311 }
1312
1313 return ret;
1314}
1315
1316static int
1317replace_entry(const ip6t_chainlabel chain,
1318 struct ip6t_entry *fw,
1319 unsigned int rulenum,
1320 const struct in6_addr *saddr,
1321 const struct in6_addr *daddr,
1322 int verbose,
1323 ip6tc_handle_t *handle)
1324{
1325 fw->ipv6.src = *saddr;
1326 fw->ipv6.dst = *daddr;
1327
1328 if (verbose)
1329 print_firewall_line(fw, *handle);
1330 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1331}
1332
1333static int
1334insert_entry(const ip6t_chainlabel chain,
1335 struct ip6t_entry *fw,
1336 unsigned int rulenum,
1337 unsigned int nsaddrs,
1338 const struct in6_addr saddrs[],
1339 unsigned int ndaddrs,
1340 const struct in6_addr daddrs[],
1341 int verbose,
1342 ip6tc_handle_t *handle)
1343{
1344 unsigned int i, j;
1345 int ret = 1;
1346
1347 for (i = 0; i < nsaddrs; i++) {
1348 fw->ipv6.src = saddrs[i];
1349 for (j = 0; j < ndaddrs; j++) {
1350 fw->ipv6.dst = daddrs[j];
1351 if (verbose)
1352 print_firewall_line(fw, *handle);
1353 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1354 }
1355 }
1356
1357 return ret;
1358}
1359
1360static unsigned char *
1361make_delete_mask(struct ip6t_entry *fw)
1362{
1363 /* Establish mask for comparison */
1364 unsigned int size;
1365 struct ip6tables_match *m;
1366 unsigned char *mask, *mptr;
1367
1368 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001369 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001370 if (!m->used)
1371 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001372
1373 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1374 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375
1376 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001377 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378 + ip6tables_targets->size);
1379
1380 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1381 mptr = mask + sizeof(struct ip6t_entry);
1382
1383 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001384 if (!m->used)
1385 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001386
Rusty Russell5eed48a2000-06-02 20:12:24 +00001387 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001388 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1389 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001390 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001391 }
1392
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001393 memset(mptr, 0xFF,
1394 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1395 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396
1397 return mask;
1398}
1399
1400static int
1401delete_entry(const ip6t_chainlabel chain,
1402 struct ip6t_entry *fw,
1403 unsigned int nsaddrs,
1404 const struct in6_addr saddrs[],
1405 unsigned int ndaddrs,
1406 const struct in6_addr daddrs[],
1407 int verbose,
1408 ip6tc_handle_t *handle)
1409{
1410 unsigned int i, j;
1411 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001412 unsigned char *mask;
1413
1414 mask = make_delete_mask(fw);
1415 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001416 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001418 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001419 if (verbose)
1420 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001421 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001422 }
1423 }
1424 return ret;
1425}
1426
1427static int
1428check_packet(const ip6t_chainlabel chain,
1429 struct ip6t_entry *fw,
1430 unsigned int nsaddrs,
1431 const struct in6_addr saddrs[],
1432 unsigned int ndaddrs,
1433 const struct in6_addr daddrs[],
1434 int verbose,
1435 ip6tc_handle_t *handle)
1436{
1437 int ret = 1;
1438 unsigned int i, j;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001439 const char *msg;
1440
1441 for (i = 0; i < nsaddrs; i++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001442 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001443 for (j = 0; j < ndaddrs; j++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001444 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001445 if (verbose)
1446 print_firewall_line(fw, *handle);
Harald Welte43ac1912002-03-03 09:43:07 +00001447 msg = ip6tc_check_packet(chain, fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001448 if (!msg) ret = 0;
1449 else printf("%s\n", msg);
1450 }
1451 }
1452
1453 return ret;
1454}
1455
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001456/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001457int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1459 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1460{
1461 int ret = 1;
1462 const char *chain;
1463 char *chains;
1464 unsigned int i, chaincount = 0;
1465
1466 chain = ip6tc_first_chain(handle);
1467 while (chain) {
1468 chaincount++;
1469 chain = ip6tc_next_chain(handle);
1470 }
1471
1472 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1473 i = 0;
1474 chain = ip6tc_first_chain(handle);
1475 while (chain) {
1476 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1477 i++;
1478 chain = ip6tc_next_chain(handle);
1479 }
1480
1481 for (i = 0; i < chaincount; i++) {
1482 if (!builtinstoo
1483 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1484 *handle))
1485 continue;
1486 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1487 }
1488
1489 free(chains);
1490 return ret;
1491}
1492
András Kis-Szabó764316a2001-02-26 17:31:20 +00001493int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494flush_entries(const ip6t_chainlabel chain, int verbose,
1495 ip6tc_handle_t *handle)
1496{
1497 if (!chain)
1498 return for_each_chain(flush_entries, verbose, 1, handle);
1499
1500 if (verbose)
1501 fprintf(stdout, "Flushing chain `%s'\n", chain);
1502 return ip6tc_flush_entries(chain, handle);
1503}
1504
1505static int
1506zero_entries(const ip6t_chainlabel chain, int verbose,
1507 ip6tc_handle_t *handle)
1508{
1509 if (!chain)
1510 return for_each_chain(zero_entries, verbose, 1, handle);
1511
1512 if (verbose)
1513 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1514 return ip6tc_zero_entries(chain, handle);
1515}
1516
András Kis-Szabó764316a2001-02-26 17:31:20 +00001517int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001518delete_chain(const ip6t_chainlabel chain, int verbose,
1519 ip6tc_handle_t *handle)
1520{
1521 if (!chain)
1522 return for_each_chain(delete_chain, verbose, 0, handle);
1523
1524 if (verbose)
1525 fprintf(stdout, "Deleting chain `%s'\n", chain);
1526 return ip6tc_delete_chain(chain, handle);
1527}
1528
1529static int
1530list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1531 int expanded, int linenumbers, ip6tc_handle_t *handle)
1532{
1533 int found = 0;
1534 unsigned int format;
1535 const char *this;
1536
1537 format = FMT_OPTIONS;
1538 if (!verbose)
1539 format |= FMT_NOCOUNTS;
1540 else
1541 format |= FMT_VIA;
1542
1543 if (numeric)
1544 format |= FMT_NUMERIC;
1545
1546 if (!expanded)
1547 format |= FMT_KILOMEGAGIGA;
1548
1549 if (linenumbers)
1550 format |= FMT_LINENUMBERS;
1551
1552 for (this = ip6tc_first_chain(handle);
1553 this;
1554 this = ip6tc_next_chain(handle)) {
1555 const struct ip6t_entry *i;
1556 unsigned int num;
1557
1558 if (chain && strcmp(chain, this) != 0)
1559 continue;
1560
1561 if (found) printf("\n");
1562
1563 print_header(format, this, handle);
1564 i = ip6tc_first_rule(this, handle);
1565
1566 num = 0;
1567 while (i) {
1568 print_firewall(i,
1569 ip6tc_get_target(i, handle),
1570 num++,
1571 format,
1572 *handle);
1573 i = ip6tc_next_rule(i, handle);
1574 }
1575 found = 1;
1576 }
1577
1578 errno = ENOENT;
1579 return found;
1580}
1581
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001582static char *get_modprobe(void)
1583{
Harald Welte43ac1912002-03-03 09:43:07 +00001584 int procfile;
1585 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001586
Harald Welte43ac1912002-03-03 09:43:07 +00001587 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1588 if (procfile < 0)
1589 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001590
Harald Welte43ac1912002-03-03 09:43:07 +00001591 ret = malloc(1024);
1592 if (ret) {
1593 switch (read(procfile, ret, 1024)) {
1594 case -1: goto fail;
1595 case 1024: goto fail; /* Partial read. Wierd */
1596 }
1597 if (ret[strlen(ret)-1]=='\n')
1598 ret[strlen(ret)-1]=0;
1599 close(procfile);
1600 return ret;
1601 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001603 free(ret);
1604 close(procfile);
1605 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001606}
1607
Harald Welte58918652001-06-16 18:25:25 +00001608int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001609{
Harald Welte43ac1912002-03-03 09:43:07 +00001610 char *buf = NULL;
1611 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001612
Harald Welte43ac1912002-03-03 09:43:07 +00001613 /* If they don't explicitly set it, read out of kernel */
1614 if (!modprobe) {
1615 buf = get_modprobe();
1616 if (!buf)
1617 return -1;
1618 modprobe = buf;
1619 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001620
Harald Welte43ac1912002-03-03 09:43:07 +00001621 switch (fork()) {
1622 case 0:
1623 argv[0] = (char *)modprobe;
1624 argv[1] = (char *)modname;
1625 argv[2] = NULL;
1626 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001627
Harald Welte43ac1912002-03-03 09:43:07 +00001628 /* not usually reached */
1629 exit(0);
1630 case -1:
1631 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001632
Harald Welte43ac1912002-03-03 09:43:07 +00001633 default: /* parent */
1634 wait(NULL);
1635 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001636
Harald Welte43ac1912002-03-03 09:43:07 +00001637 free(buf);
1638 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001639}
1640
Rusty Russell5eed48a2000-06-02 20:12:24 +00001641static struct ip6t_entry *
1642generate_entry(const struct ip6t_entry *fw,
1643 struct ip6tables_match *matches,
1644 struct ip6t_entry_target *target)
1645{
1646 unsigned int size;
1647 struct ip6tables_match *m;
1648 struct ip6t_entry *e;
1649
1650 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001651 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001652 if (!m->used)
1653 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001654
Rusty Russell5eed48a2000-06-02 20:12:24 +00001655 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001656 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001657
1658 e = fw_malloc(size + target->u.target_size);
1659 *e = *fw;
1660 e->target_offset = size;
1661 e->next_offset = size + target->u.target_size;
1662
1663 size = 0;
1664 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001665 if (!m->used)
1666 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001667
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 memcpy(e->elems + size, m->m, m->m->u.match_size);
1669 size += m->m->u.match_size;
1670 }
1671 memcpy(e->elems + size, target, target->u.target_size);
1672
1673 return e;
1674}
1675
1676int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1677{
1678 struct ip6t_entry fw, *e = NULL;
1679 int invert = 0;
1680 unsigned int nsaddrs = 0, ndaddrs = 0;
1681 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1682
1683 int c, verbose = 0;
1684 const char *chain = NULL;
1685 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1686 const char *policy = NULL, *newname = NULL;
1687 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001688 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001689 int ret = 1;
1690 struct ip6tables_match *m;
1691 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001692 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001693 const char *jumpto = "";
1694 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001695 const char *modprobe = NULL;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001696 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001697
1698 memset(&fw, 0, sizeof(fw));
1699
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001700 opts = original_opts;
1701 global_option_offset = 0;
1702
Harald Welte43ac1912002-03-03 09:43:07 +00001703 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001704 * a second time */
1705 optind = 0;
1706
Harald Welte43ac1912002-03-03 09:43:07 +00001707 /* clear mflags in case do_command gets called a second time
1708 * (we clear the global list of all matches for security)*/
1709 for (m = ip6tables_matches; m; m = m->next) {
1710 m->mflags = 0;
1711 m->used = 0;
1712 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001713
Harald Welte43ac1912002-03-03 09:43:07 +00001714 for (t = ip6tables_targets; t; t = t->next) {
1715 t->tflags = 0;
1716 t->used = 0;
1717 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001718
Rusty Russell5eed48a2000-06-02 20:12:24 +00001719 /* Suppress error messages: we may add new options if we
1720 demand-load a protocol. */
1721 opterr = 0;
1722
1723 while ((c = getopt_long(argc, argv,
Harald Welte43ac1912002-03-03 09:43:07 +00001724 "-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 +00001725 opts, NULL)) != -1) {
1726 switch (c) {
1727 /*
1728 * Command selection
1729 */
1730 case 'A':
1731 add_command(&command, CMD_APPEND, CMD_NONE,
1732 invert);
1733 chain = optarg;
1734 break;
1735
1736 case 'D':
1737 add_command(&command, CMD_DELETE, CMD_NONE,
1738 invert);
1739 chain = optarg;
1740 if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!') {
1742 rulenum = parse_rulenumber(argv[optind++]);
1743 command = CMD_DELETE_NUM;
1744 }
1745 break;
1746
1747 case 'C':
1748 add_command(&command, CMD_CHECK, CMD_NONE,
1749 invert);
1750 chain = optarg;
1751 break;
1752
1753 case 'R':
1754 add_command(&command, CMD_REPLACE, CMD_NONE,
1755 invert);
1756 chain = optarg;
1757 if (optind < argc && argv[optind][0] != '-'
1758 && argv[optind][0] != '!')
1759 rulenum = parse_rulenumber(argv[optind++]);
1760 else
1761 exit_error(PARAMETER_PROBLEM,
1762 "-%c requires a rule number",
1763 cmd2char(CMD_REPLACE));
1764 break;
1765
1766 case 'I':
1767 add_command(&command, CMD_INSERT, CMD_NONE,
1768 invert);
1769 chain = optarg;
1770 if (optind < argc && argv[optind][0] != '-'
1771 && argv[optind][0] != '!')
1772 rulenum = parse_rulenumber(argv[optind++]);
1773 else rulenum = 1;
1774 break;
1775
1776 case 'L':
1777 add_command(&command, CMD_LIST, CMD_ZERO,
1778 invert);
1779 if (optarg) chain = optarg;
1780 else if (optind < argc && argv[optind][0] != '-'
1781 && argv[optind][0] != '!')
1782 chain = argv[optind++];
1783 break;
1784
1785 case 'F':
1786 add_command(&command, CMD_FLUSH, CMD_NONE,
1787 invert);
1788 if (optarg) chain = optarg;
1789 else if (optind < argc && argv[optind][0] != '-'
1790 && argv[optind][0] != '!')
1791 chain = argv[optind++];
1792 break;
1793
1794 case 'Z':
1795 add_command(&command, CMD_ZERO, CMD_LIST,
1796 invert);
1797 if (optarg) chain = optarg;
1798 else if (optind < argc && argv[optind][0] != '-'
1799 && argv[optind][0] != '!')
1800 chain = argv[optind++];
1801 break;
1802
1803 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001804 if (optarg && *optarg == '-')
1805 exit_error(PARAMETER_PROBLEM,
1806 "chain name not allowed to start "
1807 "with `-'\n");
1808 if (find_target(optarg, TRY_LOAD))
1809 exit_error(PARAMETER_PROBLEM,
1810 "chain name may not clash "
1811 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001812 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1813 invert);
1814 chain = optarg;
1815 break;
1816
1817 case 'X':
1818 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1819 invert);
1820 if (optarg) chain = optarg;
1821 else if (optind < argc && argv[optind][0] != '-'
1822 && argv[optind][0] != '!')
1823 chain = argv[optind++];
1824 break;
1825
1826 case 'E':
1827 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1828 invert);
1829 chain = optarg;
1830 if (optind < argc && argv[optind][0] != '-'
1831 && argv[optind][0] != '!')
1832 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001833 else
1834 exit_error(PARAMETER_PROBLEM,
1835 "-%c requires old-chain-name and "
1836 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001837 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001838 break;
1839
1840 case 'P':
1841 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1842 invert);
1843 chain = optarg;
1844 if (optind < argc && argv[optind][0] != '-'
1845 && argv[optind][0] != '!')
1846 policy = argv[optind++];
1847 else
1848 exit_error(PARAMETER_PROBLEM,
1849 "-%c requires a chain and a policy",
1850 cmd2char(CMD_SET_POLICY));
1851 break;
1852
1853 case 'h':
1854 if (!optarg)
1855 optarg = argv[optind];
1856
1857 /* iptables -p icmp -h */
1858 if (!ip6tables_matches && protocol)
1859 find_match(protocol, TRY_LOAD);
1860
1861 exit_printhelp();
1862
1863 /*
1864 * Option selection
1865 */
1866 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001867 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001868 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1869 invert);
1870
1871 /* Canonicalize into lower case */
1872 for (protocol = argv[optind-1]; *protocol; protocol++)
1873 *protocol = tolower(*protocol);
1874
1875 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001876 if ( strcmp(protocol,"ipv6-icmp") == 0)
1877 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001878 fw.ipv6.proto = parse_protocol(protocol);
1879 fw.ipv6.flags |= IP6T_F_PROTO;
1880
1881 if (fw.ipv6.proto == 0
1882 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1883 exit_error(PARAMETER_PROBLEM,
1884 "rule would never match protocol");
1885 fw.nfcache |= NFC_IP6_PROTO;
1886 break;
1887
1888 case 's':
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_SOURCE, &fw.ipv6.invflags,
1891 invert);
1892 shostnetworkmask = argv[optind-1];
1893 fw.nfcache |= NFC_IP6_SRC;
1894 break;
1895
1896 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001897 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1899 invert);
1900 dhostnetworkmask = argv[optind-1];
1901 fw.nfcache |= NFC_IP6_DST;
1902 break;
1903
1904 case 'j':
1905 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1906 invert);
1907 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001908 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 target = find_target(jumpto, TRY_LOAD);
1910
1911 if (target) {
1912 size_t size;
1913
Harald Welte43ac1912002-03-03 09:43:07 +00001914 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1915 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001916
1917 target->t = fw_calloc(1, size);
1918 target->t->u.target_size = size;
1919 strcpy(target->t->u.user.name, jumpto);
1920 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001921 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922 }
1923 break;
1924
1925
1926 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001927 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1929 invert);
1930 parse_interface(argv[optind-1],
1931 fw.ipv6.iniface,
1932 fw.ipv6.iniface_mask);
1933 fw.nfcache |= NFC_IP6_IF_IN;
1934 break;
1935
1936 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001937 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001938 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1939 invert);
1940 parse_interface(argv[optind-1],
1941 fw.ipv6.outiface,
1942 fw.ipv6.outiface_mask);
1943 fw.nfcache |= NFC_IP6_IF_OUT;
1944 break;
1945
1946 case 'v':
1947 if (!verbose)
1948 set_option(&options, OPT_VERBOSE,
1949 &fw.ipv6.invflags, invert);
1950 verbose++;
1951 break;
1952
1953 case 'm': {
1954 size_t size;
1955
1956 if (invert)
1957 exit_error(PARAMETER_PROBLEM,
1958 "unexpected ! flag before --match");
1959
1960 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001961 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1962 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001963 m->m = fw_calloc(1, size);
1964 m->m->u.match_size = size;
1965 strcpy(m->m->u.user.name, m->name);
1966 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001967 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001968 }
1969 break;
1970
1971 case 'n':
1972 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1973 invert);
1974 break;
1975
1976 case 't':
1977 if (invert)
1978 exit_error(PARAMETER_PROBLEM,
1979 "unexpected ! flag before --table");
1980 *table = argv[optind-1];
1981 break;
1982
1983 case 'x':
1984 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1985 invert);
1986 break;
1987
1988 case 'V':
1989 if (invert)
1990 printf("Not %s ;-)\n", program_version);
1991 else
1992 printf("%s v%s\n",
1993 program_name, program_version);
1994 exit(0);
1995
1996 case '0':
1997 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1998 invert);
1999 break;
2000
Harald Welte43ac1912002-03-03 09:43:07 +00002001 case 'M':
2002 modprobe = optarg;
2003 break;
2004
2005 case 'c':
2006
2007 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2008 invert);
2009 pcnt = optarg;
2010 if (optind < argc && argv[optind][0] != '-'
2011 && argv[optind][0] != '!')
2012 bcnt = argv[optind++];
2013 else
2014 exit_error(PARAMETER_PROBLEM,
2015 "-%c requires packet and byte counter",
2016 opt2char(OPT_COUNTERS));
2017
2018 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2019 exit_error(PARAMETER_PROBLEM,
2020 "-%c packet counter not numeric",
2021 opt2char(OPT_COUNTERS));
2022
2023 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2024 exit_error(PARAMETER_PROBLEM,
2025 "-%c byte counter not numeric",
2026 opt2char(OPT_COUNTERS));
2027
2028 break;
2029
2030
Rusty Russell5eed48a2000-06-02 20:12:24 +00002031 case 1: /* non option */
2032 if (optarg[0] == '!' && optarg[1] == '\0') {
2033 if (invert)
2034 exit_error(PARAMETER_PROBLEM,
2035 "multiple consecutive ! not"
2036 " allowed");
2037 invert = TRUE;
2038 optarg[0] = '\0';
2039 continue;
2040 }
2041 printf("Bad argument `%s'\n", optarg);
2042 exit_tryhelp(2);
2043
2044 default:
2045 /* FIXME: This scheme doesn't allow two of the same
2046 matches --RR */
2047 if (!target
2048 || !(target->parse(c - target->option_offset,
2049 argv, invert,
2050 &target->tflags,
2051 &fw, &target->t))) {
2052 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002053 if (!m->used)
2054 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002055
Rusty Russell5eed48a2000-06-02 20:12:24 +00002056 if (m->parse(c - m->option_offset,
2057 argv, invert,
2058 &m->mflags,
2059 &fw,
2060 &fw.nfcache,
2061 &m->m))
2062 break;
2063 }
2064
2065 /* If you listen carefully, you can
2066 actually hear this code suck. */
2067 if (m == NULL
2068 && protocol
2069 && !find_proto(protocol, DONT_LOAD,
2070 options&OPT_NUMERIC)
2071 && (m = find_proto(protocol, TRY_LOAD,
2072 options&OPT_NUMERIC))) {
2073 /* Try loading protocol */
2074 size_t size;
2075
Harald Welte43ac1912002-03-03 09:43:07 +00002076 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2077 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002078
2079 m->m = fw_calloc(1, size);
2080 m->m->u.match_size = size;
2081 strcpy(m->m->u.user.name, m->name);
2082 m->init(m->m, &fw.nfcache);
2083
Harald Welte43ac1912002-03-03 09:43:07 +00002084 opts = merge_options(opts,
2085 m->extra_opts, &m->option_offset);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002086
Rusty Russell5eed48a2000-06-02 20:12:24 +00002087 optind--;
2088 continue;
2089 }
2090 if (!m)
2091 exit_error(PARAMETER_PROBLEM,
2092 "Unknown arg `%s'",
2093 argv[optind-1]);
2094 }
2095 }
2096 invert = FALSE;
2097 }
2098
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002099 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002100 if (!m->used)
2101 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002102
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002103 m->final_check(m->mflags);
2104 }
2105
Harald Welte43ac1912002-03-03 09:43:07 +00002106 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002107 target->final_check(target->tflags);
2108
2109 /* Fix me: must put inverse options checking here --MN */
2110
2111 if (optind < argc)
2112 exit_error(PARAMETER_PROBLEM,
2113 "unknown arguments found on commandline");
2114 if (!command)
2115 exit_error(PARAMETER_PROBLEM, "no command specified");
2116 if (invert)
2117 exit_error(PARAMETER_PROBLEM,
2118 "nothing appropriate following !");
2119
2120 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2121 CMD_CHECK)) {
2122 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002123 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002124 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002125 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002126 }
2127
2128 if (shostnetworkmask)
2129 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2130 &(fw.ipv6.smsk), &nsaddrs);
2131
2132 if (dhostnetworkmask)
2133 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2134 &(fw.ipv6.dmsk), &ndaddrs);
2135
2136 if ((nsaddrs > 1 || ndaddrs > 1) &&
2137 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2138 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2139 " source or destination IP addresses");
2140
2141 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2142 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2143 cmd2char(CMD_CHECK));
2144
2145 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2146 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2147 "specify a unique address");
2148
2149 generic_opt_check(command, options);
2150
2151 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2152 exit_error(PARAMETER_PROBLEM,
2153 "chain name `%s' too long (must be under %i chars)",
2154 chain, IP6T_FUNCTION_MAXNAMELEN);
2155
Harald Welte43ac1912002-03-03 09:43:07 +00002156 /* only allocate handle if we weren't called with a handle */
2157 if (!*handle)
2158 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002159
Harald Welte43ac1912002-03-03 09:43:07 +00002160 if (!*handle) {
2161 /* try to insmod the module if iptc_init failed */
2162 ip6tables_insmod("ip6_tables", modprobe);
2163 *handle = ip6tc_init(*table);
2164 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002165
Harald Welte43ac1912002-03-03 09:43:07 +00002166 if (!*handle)
2167 exit_error(VERSION_PROBLEM,
2168 "can't initialize ip6tables table `%s': %s",
2169 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002170
2171 if (command == CMD_CHECK
2172 || command == CMD_APPEND
2173 || command == CMD_DELETE
2174 || command == CMD_INSERT
2175 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002176 if (strcmp(chain, "PREROUTING") == 0
2177 || strcmp(chain, "INPUT") == 0) {
2178 /* -o not valid with incoming packets. */
2179 if (options & OPT_VIANAMEOUT)
2180 exit_error(PARAMETER_PROBLEM,
2181 "Can't use -%c with %s\n",
2182 opt2char(OPT_VIANAMEOUT),
2183 chain);
2184 /* -i required with -C */
2185 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2186 exit_error(PARAMETER_PROBLEM,
2187 "Need -%c with %s\n",
2188 opt2char(OPT_VIANAMEIN),
2189 chain);
2190 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002191
Harald Welte43ac1912002-03-03 09:43:07 +00002192 if (strcmp(chain, "POSTROUTING") == 0
2193 || strcmp(chain, "OUTPUT") == 0) {
2194 /* -i not valid with outgoing packets */
2195 if (options & OPT_VIANAMEIN)
2196 exit_error(PARAMETER_PROBLEM,
2197 "Can't use -%c with %s\n",
2198 opt2char(OPT_VIANAMEIN),
2199 chain);
2200 /* -o required with -C */
2201 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2202 exit_error(PARAMETER_PROBLEM,
2203 "Need -%c with %s\n",
2204 opt2char(OPT_VIANAMEOUT),
2205 chain);
2206 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002207
2208 if (target && ip6tc_is_chain(jumpto, *handle)) {
2209 printf("Warning: using chain %s, not extension\n",
2210 jumpto);
2211
2212 target = NULL;
2213 }
2214
2215 /* If they didn't specify a target, or it's a chain
2216 name, use standard. */
2217 if (!target
2218 && (strlen(jumpto) == 0
2219 || ip6tc_is_chain(jumpto, *handle))) {
2220 size_t size;
2221
2222 target = find_target(IP6T_STANDARD_TARGET,
2223 LOAD_MUST_SUCCEED);
2224
2225 size = sizeof(struct ip6t_entry_target)
2226 + target->size;
2227 target->t = fw_calloc(1, size);
2228 target->t->u.target_size = size;
2229 strcpy(target->t->u.user.name, jumpto);
2230 target->init(target->t, &fw.nfcache);
2231 }
2232
2233 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002234 /* it is no chain, and we can't load a plugin.
2235 * We cannot know if the plugin is corrupt, non
2236 * existant OR if the user just misspelled a
2237 * chain. */
2238 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002239 } else {
2240 e = generate_entry(&fw, ip6tables_matches, target->t);
2241 }
2242 }
2243
2244 switch (command) {
2245 case CMD_APPEND:
2246 ret = append_entry(chain, e,
2247 nsaddrs, saddrs, ndaddrs, daddrs,
2248 options&OPT_VERBOSE,
2249 handle);
2250 break;
2251 case CMD_CHECK:
2252 ret = check_packet(chain, e,
2253 nsaddrs, saddrs, ndaddrs, daddrs,
2254 options&OPT_VERBOSE, handle);
2255 break;
2256 case CMD_DELETE:
2257 ret = delete_entry(chain, e,
2258 nsaddrs, saddrs, ndaddrs, daddrs,
2259 options&OPT_VERBOSE,
2260 handle);
2261 break;
2262 case CMD_DELETE_NUM:
2263 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2264 break;
2265 case CMD_REPLACE:
2266 ret = replace_entry(chain, e, rulenum - 1,
2267 saddrs, daddrs, options&OPT_VERBOSE,
2268 handle);
2269 break;
2270 case CMD_INSERT:
2271 ret = insert_entry(chain, e, rulenum - 1,
2272 nsaddrs, saddrs, ndaddrs, daddrs,
2273 options&OPT_VERBOSE,
2274 handle);
2275 break;
2276 case CMD_LIST:
2277 ret = list_entries(chain,
2278 options&OPT_VERBOSE,
2279 options&OPT_NUMERIC,
2280 options&OPT_EXPANDED,
2281 options&OPT_LINENUMBERS,
2282 handle);
2283 break;
2284 case CMD_FLUSH:
2285 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2286 break;
2287 case CMD_ZERO:
2288 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2289 break;
2290 case CMD_LIST|CMD_ZERO:
2291 ret = list_entries(chain,
2292 options&OPT_VERBOSE,
2293 options&OPT_NUMERIC,
2294 options&OPT_EXPANDED,
2295 options&OPT_LINENUMBERS,
2296 handle);
2297 if (ret)
2298 ret = zero_entries(chain,
2299 options&OPT_VERBOSE, handle);
2300 break;
2301 case CMD_NEW_CHAIN:
2302 ret = ip6tc_create_chain(chain, handle);
2303 break;
2304 case CMD_DELETE_CHAIN:
2305 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2306 break;
2307 case CMD_RENAME_CHAIN:
2308 ret = ip6tc_rename_chain(chain, newname, handle);
2309 break;
2310 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002311 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002312 break;
2313 default:
2314 /* We should never reach this... */
2315 exit_tryhelp(2);
2316 }
2317
2318 if (verbose > 1)
2319 dump_entries6(*handle);
2320
2321 return ret;
2322}