blob: 138eed9142903cfbffe55a9fe995204591a8058f [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' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000122 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000123 { "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
Harald Weltea8658ca2003-03-05 07:46:15 +0000145/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
146 * current line of the input file, in order to give a more precise error
147 * message. ip6tables itself doesn't need this, so it is initialized to the
148 * magic number of -1 */
149int line = -1;
150
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000151#ifndef __OPTIMIZE__
152struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000153ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000154{
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000155 return (void *)e + e->target_offset;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000156}
157#endif
158
Rusty Russell5eed48a2000-06-02 20:12:24 +0000159static struct option *opts = original_opts;
160static unsigned int global_option_offset = 0;
161
162/* Table of legal combinations of commands and options. If any of the
163 * given commands make an option legal, that option is legal (applies to
164 * CMD_LIST and CMD_ZERO only).
165 * Key:
166 * + compulsory
167 * x illegal
168 * optional
169 */
170
171static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
172/* Well, it's better than "Re: Linux vs FreeBSD" */
173{
174 /* -n -s -d -p -j -v -x -i -o --line */
175/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
176/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
177/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
178/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
179/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
180/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
181/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
182/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
183/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
184/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
185/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000186/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000187/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
188};
189
190static int inverse_for_options[NUMBER_OF_OPT] =
191{
192/* -n */ 0,
193/* -s */ IP6T_INV_SRCIP,
194/* -d */ IP6T_INV_DSTIP,
195/* -p */ IP6T_INV_PROTO,
196/* -j */ 0,
197/* -v */ 0,
198/* -x */ 0,
199/* -i */ IP6T_INV_VIA_IN,
200/* -o */ IP6T_INV_VIA_OUT,
201/*--line*/ 0
202};
203
204const char *program_version;
205const char *program_name;
206
207/* Keeping track of external matches and targets: linked lists. */
208struct ip6tables_match *ip6tables_matches = NULL;
209struct ip6tables_target *ip6tables_targets = NULL;
210
211/* Extra debugging from libiptc */
212extern void dump_entries6(const ip6tc_handle_t handle);
213
214/* A few hardcoded protocols for 'all' and in case the user has no
215 /etc/protocols */
216struct pprot {
217 char *name;
218 u_int8_t num;
219};
220
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000221/* Primitive headers... */
222/* defined in netinet/in.h */
223#if 0
224#ifndef IPPROTO_ESP
225#define IPPROTO_ESP 50
226#endif
227#ifndef IPPROTO_AH
228#define IPPROTO_AH 51
229#endif
230#endif
231
Rusty Russell5eed48a2000-06-02 20:12:24 +0000232static const struct pprot chain_protos[] = {
233 { "tcp", IPPROTO_TCP },
234 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000235 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000236 { "esp", IPPROTO_ESP },
237 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000238 { "all", 0 },
239};
240
241static char *
242proto_to_name(u_int8_t proto, int nolookup)
243{
244 unsigned int i;
245
246 if (proto && !nolookup) {
247 struct protoent *pent = getprotobynumber(proto);
248 if (pent)
249 return pent->p_name;
250 }
251
252 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
253 if (chain_protos[i].num == proto)
254 return chain_protos[i].name;
255
256 return NULL;
257}
258
259static void
260in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
261{
262 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000263 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000264}
265
266void
267exit_error(enum exittype status, char *msg, ...)
268{
269 va_list args;
270
271 va_start(args, msg);
272 fprintf(stderr, "%s v%s: ", program_name, program_version);
273 vfprintf(stderr, msg, args);
274 va_end(args);
275 fprintf(stderr, "\n");
276 if (status == PARAMETER_PROBLEM)
277 exit_tryhelp(status);
278 if (status == VERSION_PROBLEM)
279 fprintf(stderr,
280 "Perhaps iptables or your kernel needs to be upgraded.\n");
281 exit(status);
282}
283
284void
285exit_tryhelp(int status)
286{
Harald Weltea8658ca2003-03-05 07:46:15 +0000287 if (line != -1)
288 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
290 program_name, program_name );
291 exit(status);
292}
293
294void
295exit_printhelp(void)
296{
297 struct ip6tables_match *m = NULL;
298 struct ip6tables_target *t = NULL;
299
300 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000301"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000302" %s -[RI] chain rulenum rule-specification [options]\n"
303" %s -D chain rulenum [options]\n"
304" %s -[LFZ] [chain] [options]\n"
305" %s -[NX] chain\n"
306" %s -E old-chain-name new-chain-name\n"
307" %s -P chain target [options]\n"
308" %s -h (print this help information)\n\n",
309 program_name, program_version, program_name, program_name,
310 program_name, program_name, program_name, program_name,
311 program_name, program_name);
312
313 printf(
314"Commands:\n"
315"Either long or short options are allowed.\n"
316" --append -A chain Append to chain\n"
317" --delete -D chain Delete matching rule from chain\n"
318" --delete -D chain rulenum\n"
319" Delete rule rulenum (1 = first) from chain\n"
320" --insert -I chain [rulenum]\n"
321" Insert in chain as rulenum (default 1=first)\n"
322" --replace -R chain rulenum\n"
323" Replace rule rulenum (1 = first) in chain\n"
324" --list -L [chain] List the rules in a chain or all chains\n"
325" --flush -F [chain] Delete all rules in chain or all chains\n"
326" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000327" --new -N chain Create a new user-defined chain\n"
328" --delete-chain\n"
329" -X [chain] Delete a user-defined chain\n"
330" --policy -P chain target\n"
331" Change policy on chain to target\n"
332" --rename-chain\n"
333" -E old-chain new-chain\n"
334" Change chain name, (moving any references)\n"
335
336"Options:\n"
337" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
338" --source -s [!] address[/mask]\n"
339" source specification\n"
340" --destination -d [!] address[/mask]\n"
341" destination specification\n"
342" --in-interface -i [!] input name[+]\n"
343" network interface name ([+] for wildcard)\n"
344" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000345" target for rule (may load target extension)\n"
346" --match -m match\n"
347" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000348" --numeric -n numeric output of addresses and ports\n"
349" --out-interface -o [!] output name[+]\n"
350" network interface name ([+] for wildcard)\n"
351" --table -t table table to manipulate (default: `filter')\n"
352" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000353" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000354" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000355/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000356" --modprobe=<command> try to insert modules using this command\n"
357" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000358"[!] --version -V print package version.\n");
359
360 /* Print out any special helps. A user might like to be able to add a --help
361 to the commandline, and see expected results. So we call help for all
362 matches & targets */
363 for (t=ip6tables_targets;t;t=t->next) {
364 printf("\n");
365 t->help();
366 }
367 for (m=ip6tables_matches;m;m=m->next) {
368 printf("\n");
369 m->help();
370 }
371 exit(0);
372}
373
374static void
375generic_opt_check(int command, int options)
376{
377 int i, j, legal = 0;
378
379 /* Check that commands are valid with options. Complicated by the
380 * fact that if an option is legal with *any* command given, it is
381 * legal overall (ie. -z and -l).
382 */
383 for (i = 0; i < NUMBER_OF_OPT; i++) {
384 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
385
386 for (j = 0; j < NUMBER_OF_CMD; j++) {
387 if (!(command & (1<<j)))
388 continue;
389
390 if (!(options & (1<<i))) {
391 if (commands_v_options[j][i] == '+')
392 exit_error(PARAMETER_PROBLEM,
393 "You need to supply the `-%c' "
394 "option for this command\n",
395 optflags[i]);
396 } else {
397 if (commands_v_options[j][i] != 'x')
398 legal = 1;
399 else if (legal == 0)
400 legal = -1;
401 }
402 }
403 if (legal == -1)
404 exit_error(PARAMETER_PROBLEM,
405 "Illegal option `-%c' with this command\n",
406 optflags[i]);
407 }
408}
409
410static char
411opt2char(int option)
412{
413 const char *ptr;
414 for (ptr = optflags; option > 1; option >>= 1, ptr++);
415
416 return *ptr;
417}
418
419static char
420cmd2char(int option)
421{
422 const char *ptr;
423 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
424
425 return *ptr;
426}
427
428static void
429add_command(int *cmd, const int newcmd, const int othercmds, int invert)
430{
431 if (invert)
432 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
433 if (*cmd & (~othercmds))
434 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
435 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
436 *cmd |= newcmd;
437}
438
439int
Harald Welteb77f1da2002-03-14 11:35:58 +0000440check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000441{
442 if (option && strcmp(option, "!") == 0) {
443 if (*invert)
444 exit_error(PARAMETER_PROBLEM,
445 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000446 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000447 if (optind) {
448 *optind = *optind+1;
449 if (argc && *optind > argc)
450 exit_error(PARAMETER_PROBLEM,
451 "no argument following `!'");
452 }
453
Rusty Russell5eed48a2000-06-02 20:12:24 +0000454 return TRUE;
455 }
456 return FALSE;
457}
458
459static void *
460fw_calloc(size_t count, size_t size)
461{
462 void *p;
463
464 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000465 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000466 exit(1);
467 }
468 return p;
469}
470
471static void *
472fw_malloc(size_t size)
473{
474 void *p;
475
476 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000477 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000478 exit(1);
479 }
480 return p;
481}
482
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483static char *
484addr_to_numeric(const struct in6_addr *addrp)
485{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000486 /* 0000:0000:0000:0000:0000:000.000.000.000
487 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000488 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000489 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000490}
491
492static struct in6_addr *
493numeric_to_addr(const char *num)
494{
495 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000496 int err;
497 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000498 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000499#ifdef DEBUG
500 fprintf(stderr, "\nnumeric2addr: %d\n", err);
501#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000502 return (struct in6_addr *)NULL;
503}
504
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000505
506static struct in6_addr *
507host_to_addr(const char *name, unsigned int *naddr)
508{
509 struct addrinfo hints;
510 struct addrinfo *res;
511 static struct in6_addr *addr;
512 int err;
513
514 memset(&hints, 0, sizeof(hints));
515 hints.ai_flags=AI_CANONNAME;
516 hints.ai_family=AF_INET6;
517 hints.ai_socktype=SOCK_RAW;
518 hints.ai_protocol=41;
519 hints.ai_next=NULL;
520
521 *naddr = 0;
522 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
523#ifdef DEBUG
524 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
525#endif
526 return (struct in6_addr *) NULL;
527 } else {
528 if (res->ai_family != AF_INET6 ||
529 res->ai_addrlen != sizeof(struct sockaddr_in6))
530 return (struct in6_addr *) NULL;
531
532#ifdef DEBUG
533 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
534 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
535#endif
536 /* Get the first element of the address-chain */
537 addr = fw_calloc(1, sizeof(struct in6_addr));
538 in6addrcpy(addr, (struct in6_addr *)
539 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
540 freeaddrinfo(res);
541 *naddr = 1;
542 return addr;
543 }
544
545 return (struct in6_addr *) NULL;
546}
547
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000548static char *
549addr_to_host(const struct in6_addr *addr)
550{
551 struct sockaddr_in6 saddr;
552 int err;
553 static char hostname[NI_MAXHOST];
554
555 memset(&saddr, 0, sizeof(struct sockaddr_in6));
556 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000557 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000558
559 if ( (err=getnameinfo((struct sockaddr *)&saddr,
560 sizeof(struct sockaddr_in6),
561 hostname, sizeof(hostname)-1,
562 NULL, 0, 0)) != 0 ){
563#ifdef DEBUG
564 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
565#endif
566 return (char *) NULL;
567 } else {
568#ifdef DEBUG
569 fprintf (stderr, "\naddr2host: %s\n", hostname);
570#endif
571
572 return hostname;
573 }
574
575 return (char *) NULL;
576}
577
Rusty Russell5eed48a2000-06-02 20:12:24 +0000578static char *
579mask_to_numeric(const struct in6_addr *addrp)
580{
Harald Welte8a50ab82003-06-24 18:15:59 +0000581 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000582 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000583 if (l == -1) {
584 strcpy(buf, "/");
585 strcat(buf, addr_to_numeric(addrp));
586 return buf;
587 }
Harald Welte43ac1912002-03-03 09:43:07 +0000588 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000589 return buf;
590}
591
592static struct in6_addr *
593network_to_addr(const char *name)
594{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000595 /* abort();*/
596 /* TODO: not implemented yet, but the exception breaks the
597 * name resolvation */
598 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000599}
600
601static char *
602addr_to_anyname(const struct in6_addr *addr)
603{
604 char *name;
605
606 if ((name = addr_to_host(addr)) != NULL)
607 return name;
608
609 return addr_to_numeric(addr);
610}
611
612/*
613 * All functions starting with "parse" should succeed, otherwise
614 * the program fails.
615 * Most routines return pointers to static data that may change
616 * between calls to the same or other routines with a few exceptions:
617 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
618 * return global static data.
619*/
620
621static struct in6_addr *
622parse_hostnetwork(const char *name, unsigned int *naddrs)
623{
624 struct in6_addr *addrp, *addrptmp;
625
626 if ((addrptmp = numeric_to_addr(name)) != NULL ||
627 (addrptmp = network_to_addr(name)) != NULL) {
628 addrp = fw_malloc(sizeof(struct in6_addr));
629 in6addrcpy(addrp, addrptmp);
630 *naddrs = 1;
631 return addrp;
632 }
633 if ((addrp = host_to_addr(name, naddrs)) != NULL)
634 return addrp;
635
636 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
637}
638
639static struct in6_addr *
640parse_mask(char *mask)
641{
642 static struct in6_addr maskaddr;
643 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000644 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000645
646 if (mask == NULL) {
647 /* no mask at all defaults to 128 bits */
648 memset(&maskaddr, 0xff, sizeof maskaddr);
649 return &maskaddr;
650 }
651 if ((addrp = numeric_to_addr(mask)) != NULL)
652 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000653 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000654 exit_error(PARAMETER_PROBLEM,
655 "invalid mask `%s' specified", mask);
656 if (bits != 0) {
657 char *p = (char *)&maskaddr;
658 memset(p, 0xff, bits / 8);
659 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
660 p[bits / 8] = 0xff << (8 - (bits & 7));
661 return &maskaddr;
662 }
663
664 memset(&maskaddr, 0, sizeof maskaddr);
665 return &maskaddr;
666}
667
Harald Welte43ac1912002-03-03 09:43:07 +0000668void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000669parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
670 struct in6_addr *maskp, unsigned int *naddrs)
671{
672 struct in6_addr *addrp;
673 char buf[256];
674 char *p;
675 int i, j, n;
676
677 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000678 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000679 if ((p = strrchr(buf, '/')) != NULL) {
680 *p = '\0';
681 addrp = parse_mask(p + 1);
682 } else
683 addrp = parse_mask(NULL);
684 in6addrcpy(maskp, addrp);
685
686 /* if a null mask is given, the name is ignored, like in "any/0" */
687 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
688 strcpy(buf, "::");
689
690 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
691 n = *naddrs;
692 for (i = 0, j = 0; i < n; i++) {
693 int k;
694 for (k = 0; k < 4; k++)
695 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
696 j++;
697 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000698 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000699 (*naddrs)--;
700 j--;
701 break;
702 }
703 }
704 }
705}
706
707struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000708find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709{
710 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000711 int icmphack = 0;
712
713 /* This is ugly as hell. Nonetheless, there is no way of changing
714 * this without hurting backwards compatibility */
715 if ( (strcmp(name,"icmpv6") == 0) ||
716 (strcmp(name,"ipv6-icmp") == 0) ||
717 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
718
719 if (!icmphack) {
720 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
721 if (strcmp(name, ptr->name) == 0)
722 break;
723 }
724 } else {
725 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
726 if (strcmp("icmp6", ptr->name) == 0)
727 break;
728 }
729 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000730
Harald Welte3efb6ea2001-08-06 18:50:21 +0000731#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000732 if (!ptr && tryload != DONT_LOAD) {
733 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
734 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000735 if (!icmphack)
736 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
737 else
738 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739 if (dlopen(path, RTLD_NOW)) {
740 /* Found library. If it didn't register itself,
741 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000742 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743
744 if (!ptr)
745 exit_error(PARAMETER_PROBLEM,
746 "Couldn't load match `%s'\n",
747 name);
748 } else if (tryload == LOAD_MUST_SUCCEED)
749 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000750 "Couldn't load match `%s':%s\n",
751 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000752 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000753#else
754 if (ptr && !ptr->loaded) {
755 if (tryload != DONT_LOAD)
756 ptr->loaded = 1;
757 else
758 ptr = NULL;
759 }
Marc Boucher067477b2002-03-24 15:09:31 +0000760 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
761 exit_error(PARAMETER_PROBLEM,
762 "Couldn't find match `%s'\n", name);
763 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000764#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000765
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000766 if (ptr && matches) {
767 struct ip6tables_rule_match **i;
768 struct ip6tables_rule_match *newentry;
769
770 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
771
772 for (i = matches; *i; i = &(*i)->next);
773 newentry->match = ptr;
774 newentry->next = NULL;
775 *i = newentry;
776 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000777
Rusty Russell5eed48a2000-06-02 20:12:24 +0000778 return ptr;
779}
780
781/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
782static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000783find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784{
Harald Welteed498492001-07-23 01:24:22 +0000785 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000786
Harald Welte43ac1912002-03-03 09:43:07 +0000787 if (string_to_number(pname, 0, 255, &proto) != -1) {
788 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000789
Harald Welte43ac1912002-03-03 09:43:07 +0000790 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000791 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000792 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000793 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000794
795 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000796}
797
Harald Welte43ac1912002-03-03 09:43:07 +0000798u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000799parse_protocol(const char *s)
800{
Harald Welteed498492001-07-23 01:24:22 +0000801 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000802
Harald Welteed498492001-07-23 01:24:22 +0000803 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000804 struct protoent *pent;
805
806 if ((pent = getprotobyname(s)))
807 proto = pent->p_proto;
808 else {
809 unsigned int i;
810 for (i = 0;
811 i < sizeof(chain_protos)/sizeof(struct pprot);
812 i++) {
813 if (strcmp(s, chain_protos[i].name) == 0) {
814 proto = chain_protos[i].num;
815 break;
816 }
817 }
818 if (i == sizeof(chain_protos)/sizeof(struct pprot))
819 exit_error(PARAMETER_PROBLEM,
820 "unknown protocol `%s' specified",
821 s);
822 }
823 }
824
825 return (u_int16_t)proto;
826}
827
828static void
829parse_interface(const char *arg, char *vianame, unsigned char *mask)
830{
831 int vialen = strlen(arg);
832 unsigned int i;
833
834 memset(mask, 0, IFNAMSIZ);
835 memset(vianame, 0, IFNAMSIZ);
836
837 if (vialen + 1 > IFNAMSIZ)
838 exit_error(PARAMETER_PROBLEM,
839 "interface name `%s' must be shorter than IFNAMSIZ"
840 " (%i)", arg, IFNAMSIZ-1);
841
842 strcpy(vianame, arg);
843 if (vialen == 0)
844 memset(mask, 0, IFNAMSIZ);
845 else if (vianame[vialen - 1] == '+') {
846 memset(mask, 0xFF, vialen - 1);
847 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000848 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000849 } else {
850 /* Include nul-terminator in match */
851 memset(mask, 0xFF, vialen + 1);
852 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000853 for (i = 0; vianame[i]; i++) {
854 if (!isalnum(vianame[i])
855 && vianame[i] != '_'
856 && vianame[i] != '.') {
857 printf("Warning: wierd character in interface"
858 " `%s' (No aliases, :, ! or *).\n",
859 vianame);
860 break;
861 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000862 }
863 }
864}
865
866/* Can't be zero. */
867static int
868parse_rulenumber(const char *rule)
869{
Harald Welte43ac1912002-03-03 09:43:07 +0000870 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000871
Harald Welte43ac1912002-03-03 09:43:07 +0000872 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873 exit_error(PARAMETER_PROBLEM,
874 "Invalid rule number `%s'", rule);
875
876 return rulenum;
877}
878
879static const char *
880parse_target(const char *targetname)
881{
882 const char *ptr;
883
884 if (strlen(targetname) < 1)
885 exit_error(PARAMETER_PROBLEM,
886 "Invalid target name (too short)");
887
888 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
889 exit_error(PARAMETER_PROBLEM,
890 "Invalid target name `%s' (%i chars max)",
891 targetname, sizeof(ip6t_chainlabel)-1);
892
893 for (ptr = targetname; *ptr; ptr++)
894 if (isspace(*ptr))
895 exit_error(PARAMETER_PROBLEM,
896 "Invalid target name `%s'", targetname);
897 return targetname;
898}
899
900int
Harald Welteed498492001-07-23 01:24:22 +0000901string_to_number(const char *s, unsigned int min, unsigned int max,
902 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903{
Harald Welteed498492001-07-23 01:24:22 +0000904 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000905 char *end;
906
907 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000908 errno = 0;
909 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000910 if (*end == '\0' && end != s) {
911 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000912 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000913 *ret = number;
914 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000915 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916 }
917 return -1;
918}
919
920static void
921set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
922 int invert)
923{
924 if (*options & option)
925 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
926 opt2char(option));
927 *options |= option;
928
929 if (invert) {
930 unsigned int i;
931 for (i = 0; 1 << i != option; i++);
932
933 if (!inverse_for_options[i])
934 exit_error(PARAMETER_PROBLEM,
935 "cannot have ! before -%c",
936 opt2char(option));
937 *invflg |= inverse_for_options[i];
938 }
939}
940
941struct ip6tables_target *
942find_target(const char *name, enum ip6t_tryload tryload)
943{
944 struct ip6tables_target *ptr;
945
946 /* Standard target? */
947 if (strcmp(name, "") == 0
948 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
949 || strcmp(name, IP6TC_LABEL_DROP) == 0
950 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
951 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
952 name = "standard";
953
954 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
955 if (strcmp(name, ptr->name) == 0)
956 break;
957 }
958
Harald Welte3efb6ea2001-08-06 18:50:21 +0000959#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000960 if (!ptr && tryload != DONT_LOAD) {
961 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
962 + strlen(name)];
963 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
964 if (dlopen(path, RTLD_NOW)) {
965 /* Found library. If it didn't register itself,
966 maybe they specified match as a target. */
967 ptr = find_target(name, DONT_LOAD);
968 if (!ptr)
969 exit_error(PARAMETER_PROBLEM,
970 "Couldn't load target `%s'\n",
971 name);
972 } else if (tryload == LOAD_MUST_SUCCEED)
973 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000974 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000975 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000976 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000977#else
978 if (ptr && !ptr->loaded) {
979 if (tryload != DONT_LOAD)
980 ptr->loaded = 1;
981 else
982 ptr = NULL;
983 }
Marc Boucher067477b2002-03-24 15:09:31 +0000984 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
985 exit_error(PARAMETER_PROBLEM,
986 "Couldn't find target `%s'\n", name);
987 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000988#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000989
Harald Welte43ac1912002-03-03 09:43:07 +0000990 if (ptr)
991 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000992
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993 return ptr;
994}
995
996static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000997merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000998 unsigned int *option_offset)
999{
1000 unsigned int num_old, num_new, i;
1001 struct option *merge;
1002
1003 for (num_old = 0; oldopts[num_old].name; num_old++);
1004 for (num_new = 0; newopts[num_new].name; num_new++);
1005
1006 global_option_offset += OPTION_OFFSET;
1007 *option_offset = global_option_offset;
1008
1009 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1010 memcpy(merge, oldopts, num_old * sizeof(struct option));
1011 for (i = 0; i < num_new; i++) {
1012 merge[num_old + i] = newopts[i];
1013 merge[num_old + i].val += *option_offset;
1014 }
1015 memset(merge + num_old + num_new, 0, sizeof(struct option));
1016
1017 return merge;
1018}
1019
1020void
1021register_match6(struct ip6tables_match *me)
1022{
Harald Welte43ac1912002-03-03 09:43:07 +00001023 struct ip6tables_match **i;
1024
Rusty Russell5eed48a2000-06-02 20:12:24 +00001025 if (strcmp(me->version, program_version) != 0) {
1026 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1027 program_name, me->name, me->version, program_version);
1028 exit(1);
1029 }
1030
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001031 if (find_match(me->name, DONT_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001032 fprintf(stderr, "%s: match `%s' already registered.\n",
1033 program_name, me->name);
1034 exit(1);
1035 }
1036
Harald Welte43ac1912002-03-03 09:43:07 +00001037 if (me->size != IP6T_ALIGN(me->size)) {
1038 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1039 program_name, me->name, me->size);
1040 exit(1);
1041 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001042
Harald Welte43ac1912002-03-03 09:43:07 +00001043 /* Append to list. */
1044 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1045 me->next = NULL;
1046 *i = me;
1047
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 me->m = NULL;
1049 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001050}
1051
1052void
1053register_target6(struct ip6tables_target *me)
1054{
1055 if (strcmp(me->version, program_version) != 0) {
1056 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1057 program_name, me->name, me->version, program_version);
1058 exit(1);
1059 }
1060
1061 if (find_target(me->name, DONT_LOAD)) {
1062 fprintf(stderr, "%s: target `%s' already registered.\n",
1063 program_name, me->name);
1064 exit(1);
1065 }
1066
Harald Welte43ac1912002-03-03 09:43:07 +00001067 if (me->size != IP6T_ALIGN(me->size)) {
1068 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1069 program_name, me->name, me->size);
1070 exit(1);
1071 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001072
Rusty Russell5eed48a2000-06-02 20:12:24 +00001073 /* Prepend to list. */
1074 me->next = ip6tables_targets;
1075 ip6tables_targets = me;
1076 me->t = NULL;
1077 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001078}
1079
1080static void
1081print_num(u_int64_t number, unsigned int format)
1082{
Harald Welte43ac1912002-03-03 09:43:07 +00001083 if (format & FMT_KILOMEGAGIGA) {
1084 if (number > 99999) {
1085 number = (number + 500) / 1000;
1086 if (number > 9999) {
1087 number = (number + 500) / 1000;
1088 if (number > 9999) {
1089 number = (number + 500) / 1000;
1090 if (number > 9999) {
1091 number = (number + 500) / 1000;
1092 printf(FMT("%4lluT ","%lluT "), number);
1093 }
1094 else printf(FMT("%4lluG ","%lluG "), number);
1095 }
1096 else printf(FMT("%4lluM ","%lluM "), number);
1097 } else
1098 printf(FMT("%4lluK ","%lluK "), number);
1099 } else
1100 printf(FMT("%5llu ","%llu "), number);
1101 } else
1102 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001103}
1104
Harald Welte43ac1912002-03-03 09:43:07 +00001105
Rusty Russell5eed48a2000-06-02 20:12:24 +00001106static void
1107print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1108{
1109 struct ip6t_counters counters;
1110 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1111 printf("Chain %s", chain);
1112 if (pol) {
1113 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001114 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001115 fputc(' ', stdout);
1116 print_num(counters.pcnt, (format|FMT_NOTABLE));
1117 fputs("packets, ", stdout);
1118 print_num(counters.bcnt, (format|FMT_NOTABLE));
1119 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001120 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001121 printf(")\n");
1122 } else {
1123 unsigned int refs;
1124 if (!ip6tc_get_references(&refs, chain, handle))
1125 printf(" (ERROR obtaining refs)\n");
1126 else
1127 printf(" (%u references)\n", refs);
1128 }
1129
1130 if (format & FMT_LINENUMBERS)
1131 printf(FMT("%-4s ", "%s "), "num");
1132 if (!(format & FMT_NOCOUNTS)) {
1133 if (format & FMT_KILOMEGAGIGA) {
1134 printf(FMT("%5s ","%s "), "pkts");
1135 printf(FMT("%5s ","%s "), "bytes");
1136 } else {
1137 printf(FMT("%8s ","%s "), "pkts");
1138 printf(FMT("%10s ","%s "), "bytes");
1139 }
1140 }
1141 if (!(format & FMT_NOTARGET))
1142 printf(FMT("%-9s ","%s "), "target");
1143 fputs(" prot ", stdout);
1144 if (format & FMT_OPTIONS)
1145 fputs("opt", stdout);
1146 if (format & FMT_VIA) {
1147 printf(FMT(" %-6s ","%s "), "in");
1148 printf(FMT("%-6s ","%s "), "out");
1149 }
1150 printf(FMT(" %-19s ","%s "), "source");
1151 printf(FMT(" %-19s "," %s "), "destination");
1152 printf("\n");
1153}
1154
Harald Welte43ac1912002-03-03 09:43:07 +00001155
Rusty Russell5eed48a2000-06-02 20:12:24 +00001156static int
1157print_match(const struct ip6t_entry_match *m,
1158 const struct ip6t_ip6 *ip,
1159 int numeric)
1160{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001161 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001162
1163 if (match) {
1164 if (match->print)
1165 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001166 else
1167 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001168 } else {
1169 if (m->u.user.name[0])
1170 printf("UNKNOWN match `%s' ", m->u.user.name);
1171 }
1172 /* Don't stop iterating. */
1173 return 0;
1174}
1175
1176/* e is called `fw' here for hysterical raisins */
1177static void
1178print_firewall(const struct ip6t_entry *fw,
1179 const char *targname,
1180 unsigned int num,
1181 unsigned int format,
1182 const ip6tc_handle_t handle)
1183{
1184 struct ip6tables_target *target = NULL;
1185 const struct ip6t_entry_target *t;
1186 u_int8_t flags;
1187 char buf[BUFSIZ];
1188
Rusty Russell5eed48a2000-06-02 20:12:24 +00001189 if (!ip6tc_is_chain(targname, handle))
1190 target = find_target(targname, TRY_LOAD);
1191 else
1192 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1193
1194 t = ip6t_get_target((struct ip6t_entry *)fw);
1195 flags = fw->ipv6.flags;
1196
1197 if (format & FMT_LINENUMBERS)
1198 printf(FMT("%-4u ", "%u "), num+1);
1199
1200 if (!(format & FMT_NOCOUNTS)) {
1201 print_num(fw->counters.pcnt, format);
1202 print_num(fw->counters.bcnt, format);
1203 }
1204
1205 if (!(format & FMT_NOTARGET))
1206 printf(FMT("%-9s ", "%s "), targname);
1207
1208 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1209 {
Harald Welte43ac1912002-03-03 09:43:07 +00001210 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001211 if (pname)
1212 printf(FMT("%-5s", "%s "), pname);
1213 else
1214 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1215 }
1216
1217 if (format & FMT_OPTIONS) {
1218 if (format & FMT_NOTABLE)
1219 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001220 fputc(' ', stdout); /* Invert flag of FRAG */
1221 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001222 fputc(' ', stdout);
1223 }
1224
1225 if (format & FMT_VIA) {
1226 char iface[IFNAMSIZ+2];
1227
1228 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1229 iface[0] = '!';
1230 iface[1] = '\0';
1231 }
1232 else iface[0] = '\0';
1233
1234 if (fw->ipv6.iniface[0] != '\0') {
1235 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001236 }
1237 else if (format & FMT_NUMERIC) strcat(iface, "*");
1238 else strcat(iface, "any");
1239 printf(FMT(" %-6s ","in %s "), iface);
1240
1241 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1242 iface[0] = '!';
1243 iface[1] = '\0';
1244 }
1245 else iface[0] = '\0';
1246
1247 if (fw->ipv6.outiface[0] != '\0') {
1248 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001249 }
1250 else if (format & FMT_NUMERIC) strcat(iface, "*");
1251 else strcat(iface, "any");
1252 printf(FMT("%-6s ","out %s "), iface);
1253 }
1254
1255 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1256 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1257 && !(format & FMT_NUMERIC))
1258 printf(FMT("%-19s ","%s "), "anywhere");
1259 else {
1260 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001261 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001262 else
Harald Welte43ac1912002-03-03 09:43:07 +00001263 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001264 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1265 printf(FMT("%-19s ","%s "), buf);
1266 }
1267
1268 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1269 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1270 && !(format & FMT_NUMERIC))
1271 printf(FMT("%-19s","-> %s"), "anywhere");
1272 else {
1273 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001274 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001275 else
Harald Welte43ac1912002-03-03 09:43:07 +00001276 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001277 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1278 printf(FMT("%-19s","-> %s"), buf);
1279 }
1280
1281 if (format & FMT_NOTABLE)
1282 fputs(" ", stdout);
1283
1284 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1285
1286 if (target) {
1287 if (target->print)
1288 /* Print the target information. */
1289 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1290 } else if (t->u.target_size != sizeof(*t))
1291 printf("[%u bytes of unknown target data] ",
1292 t->u.target_size - sizeof(*t));
1293
1294 if (!(format & FMT_NONEWLINE))
1295 fputc('\n', stdout);
1296}
1297
1298static void
1299print_firewall_line(const struct ip6t_entry *fw,
1300 const ip6tc_handle_t h)
1301{
1302 struct ip6t_entry_target *t;
1303
1304 t = ip6t_get_target((struct ip6t_entry *)fw);
1305 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1306}
1307
1308static int
1309append_entry(const ip6t_chainlabel chain,
1310 struct ip6t_entry *fw,
1311 unsigned int nsaddrs,
1312 const struct in6_addr saddrs[],
1313 unsigned int ndaddrs,
1314 const struct in6_addr daddrs[],
1315 int verbose,
1316 ip6tc_handle_t *handle)
1317{
1318 unsigned int i, j;
1319 int ret = 1;
1320
1321 for (i = 0; i < nsaddrs; i++) {
1322 fw->ipv6.src = saddrs[i];
1323 for (j = 0; j < ndaddrs; j++) {
1324 fw->ipv6.dst = daddrs[j];
1325 if (verbose)
1326 print_firewall_line(fw, *handle);
1327 ret &= ip6tc_append_entry(chain, fw, handle);
1328 }
1329 }
1330
1331 return ret;
1332}
1333
1334static int
1335replace_entry(const ip6t_chainlabel chain,
1336 struct ip6t_entry *fw,
1337 unsigned int rulenum,
1338 const struct in6_addr *saddr,
1339 const struct in6_addr *daddr,
1340 int verbose,
1341 ip6tc_handle_t *handle)
1342{
1343 fw->ipv6.src = *saddr;
1344 fw->ipv6.dst = *daddr;
1345
1346 if (verbose)
1347 print_firewall_line(fw, *handle);
1348 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1349}
1350
1351static int
1352insert_entry(const ip6t_chainlabel chain,
1353 struct ip6t_entry *fw,
1354 unsigned int rulenum,
1355 unsigned int nsaddrs,
1356 const struct in6_addr saddrs[],
1357 unsigned int ndaddrs,
1358 const struct in6_addr daddrs[],
1359 int verbose,
1360 ip6tc_handle_t *handle)
1361{
1362 unsigned int i, j;
1363 int ret = 1;
1364
1365 for (i = 0; i < nsaddrs; i++) {
1366 fw->ipv6.src = saddrs[i];
1367 for (j = 0; j < ndaddrs; j++) {
1368 fw->ipv6.dst = daddrs[j];
1369 if (verbose)
1370 print_firewall_line(fw, *handle);
1371 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1372 }
1373 }
1374
1375 return ret;
1376}
1377
1378static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001379make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001380{
1381 /* Establish mask for comparison */
1382 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001383 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001384 unsigned char *mask, *mptr;
1385
1386 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001387 for (matchp = matches; matchp; matchp = matchp->next)
1388 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001389
1390 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001391 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392 + ip6tables_targets->size);
1393
1394 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1395 mptr = mask + sizeof(struct ip6t_entry);
1396
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001397 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001398 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001399 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001400 + matchp->match->userspacesize);
1401 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001402 }
1403
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001404 memset(mptr, 0xFF,
1405 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1406 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001407
1408 return mask;
1409}
1410
1411static int
1412delete_entry(const ip6t_chainlabel chain,
1413 struct ip6t_entry *fw,
1414 unsigned int nsaddrs,
1415 const struct in6_addr saddrs[],
1416 unsigned int ndaddrs,
1417 const struct in6_addr daddrs[],
1418 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001419 ip6tc_handle_t *handle,
1420 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001421{
1422 unsigned int i, j;
1423 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424 unsigned char *mask;
1425
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001426 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001428 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001430 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 if (verbose)
1432 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001433 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001434 }
1435 }
1436 return ret;
1437}
1438
András Kis-Szabó764316a2001-02-26 17:31:20 +00001439int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1441 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1442{
1443 int ret = 1;
1444 const char *chain;
1445 char *chains;
1446 unsigned int i, chaincount = 0;
1447
1448 chain = ip6tc_first_chain(handle);
1449 while (chain) {
1450 chaincount++;
1451 chain = ip6tc_next_chain(handle);
1452 }
1453
1454 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1455 i = 0;
1456 chain = ip6tc_first_chain(handle);
1457 while (chain) {
1458 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1459 i++;
1460 chain = ip6tc_next_chain(handle);
1461 }
1462
1463 for (i = 0; i < chaincount; i++) {
1464 if (!builtinstoo
1465 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1466 *handle))
1467 continue;
1468 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1469 }
1470
1471 free(chains);
1472 return ret;
1473}
1474
András Kis-Szabó764316a2001-02-26 17:31:20 +00001475int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001476flush_entries(const ip6t_chainlabel chain, int verbose,
1477 ip6tc_handle_t *handle)
1478{
1479 if (!chain)
1480 return for_each_chain(flush_entries, verbose, 1, handle);
1481
1482 if (verbose)
1483 fprintf(stdout, "Flushing chain `%s'\n", chain);
1484 return ip6tc_flush_entries(chain, handle);
1485}
1486
1487static int
1488zero_entries(const ip6t_chainlabel chain, int verbose,
1489 ip6tc_handle_t *handle)
1490{
1491 if (!chain)
1492 return for_each_chain(zero_entries, verbose, 1, handle);
1493
1494 if (verbose)
1495 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1496 return ip6tc_zero_entries(chain, handle);
1497}
1498
András Kis-Szabó764316a2001-02-26 17:31:20 +00001499int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500delete_chain(const ip6t_chainlabel chain, int verbose,
1501 ip6tc_handle_t *handle)
1502{
1503 if (!chain)
1504 return for_each_chain(delete_chain, verbose, 0, handle);
1505
1506 if (verbose)
1507 fprintf(stdout, "Deleting chain `%s'\n", chain);
1508 return ip6tc_delete_chain(chain, handle);
1509}
1510
1511static int
1512list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1513 int expanded, int linenumbers, ip6tc_handle_t *handle)
1514{
1515 int found = 0;
1516 unsigned int format;
1517 const char *this;
1518
1519 format = FMT_OPTIONS;
1520 if (!verbose)
1521 format |= FMT_NOCOUNTS;
1522 else
1523 format |= FMT_VIA;
1524
1525 if (numeric)
1526 format |= FMT_NUMERIC;
1527
1528 if (!expanded)
1529 format |= FMT_KILOMEGAGIGA;
1530
1531 if (linenumbers)
1532 format |= FMT_LINENUMBERS;
1533
1534 for (this = ip6tc_first_chain(handle);
1535 this;
1536 this = ip6tc_next_chain(handle)) {
1537 const struct ip6t_entry *i;
1538 unsigned int num;
1539
1540 if (chain && strcmp(chain, this) != 0)
1541 continue;
1542
1543 if (found) printf("\n");
1544
1545 print_header(format, this, handle);
1546 i = ip6tc_first_rule(this, handle);
1547
1548 num = 0;
1549 while (i) {
1550 print_firewall(i,
1551 ip6tc_get_target(i, handle),
1552 num++,
1553 format,
1554 *handle);
1555 i = ip6tc_next_rule(i, handle);
1556 }
1557 found = 1;
1558 }
1559
1560 errno = ENOENT;
1561 return found;
1562}
1563
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001564static char *get_modprobe(void)
1565{
Harald Welte43ac1912002-03-03 09:43:07 +00001566 int procfile;
1567 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001568
Harald Welte43ac1912002-03-03 09:43:07 +00001569 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1570 if (procfile < 0)
1571 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001572
Harald Welte43ac1912002-03-03 09:43:07 +00001573 ret = malloc(1024);
1574 if (ret) {
1575 switch (read(procfile, ret, 1024)) {
1576 case -1: goto fail;
1577 case 1024: goto fail; /* Partial read. Wierd */
1578 }
1579 if (ret[strlen(ret)-1]=='\n')
1580 ret[strlen(ret)-1]=0;
1581 close(procfile);
1582 return ret;
1583 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001584 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001585 free(ret);
1586 close(procfile);
1587 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001588}
1589
Harald Welte58918652001-06-16 18:25:25 +00001590int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001591{
Harald Welte43ac1912002-03-03 09:43:07 +00001592 char *buf = NULL;
1593 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001594
Harald Welte43ac1912002-03-03 09:43:07 +00001595 /* If they don't explicitly set it, read out of kernel */
1596 if (!modprobe) {
1597 buf = get_modprobe();
1598 if (!buf)
1599 return -1;
1600 modprobe = buf;
1601 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602
Harald Welte43ac1912002-03-03 09:43:07 +00001603 switch (fork()) {
1604 case 0:
1605 argv[0] = (char *)modprobe;
1606 argv[1] = (char *)modname;
1607 argv[2] = NULL;
1608 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001609
Harald Welte43ac1912002-03-03 09:43:07 +00001610 /* not usually reached */
1611 exit(0);
1612 case -1:
1613 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001614
Harald Welte43ac1912002-03-03 09:43:07 +00001615 default: /* parent */
1616 wait(NULL);
1617 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001618
Harald Welte43ac1912002-03-03 09:43:07 +00001619 free(buf);
1620 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001621}
1622
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623static struct ip6t_entry *
1624generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001625 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001626 struct ip6t_entry_target *target)
1627{
1628 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001629 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001630 struct ip6t_entry *e;
1631
1632 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001633 for (matchp = matches; matchp; matchp = matchp->next)
1634 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635
1636 e = fw_malloc(size + target->u.target_size);
1637 *e = *fw;
1638 e->target_offset = size;
1639 e->next_offset = size + target->u.target_size;
1640
1641 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001642 for (matchp = matches; matchp; matchp = matchp->next) {
1643 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1644 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001645 }
1646 memcpy(e->elems + size, target, target->u.target_size);
1647
1648 return e;
1649}
1650
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001651void clear_rule_matches(struct ip6tables_rule_match **matches)
1652{
1653 struct ip6tables_rule_match *matchp, *tmp;
1654
1655 for (matchp = *matches; matchp;) {
1656 tmp = matchp->next;
1657 free(matchp);
1658 matchp = tmp;
1659 }
1660
1661 *matches = NULL;
1662}
1663
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1665{
1666 struct ip6t_entry fw, *e = NULL;
1667 int invert = 0;
1668 unsigned int nsaddrs = 0, ndaddrs = 0;
1669 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1670
1671 int c, verbose = 0;
1672 const char *chain = NULL;
1673 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1674 const char *policy = NULL, *newname = NULL;
1675 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001676 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001677 int ret = 1;
1678 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001679 struct ip6tables_rule_match *matches = NULL;
1680 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001681 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001682 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683 const char *jumpto = "";
1684 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001685 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001686 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001687 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001688
1689 memset(&fw, 0, sizeof(fw));
1690
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001691 opts = original_opts;
1692 global_option_offset = 0;
1693
Harald Welte43ac1912002-03-03 09:43:07 +00001694 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001695 * a second time */
1696 optind = 0;
1697
Harald Welte43ac1912002-03-03 09:43:07 +00001698 /* clear mflags in case do_command gets called a second time
1699 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001700 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001701 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001702
Harald Welte43ac1912002-03-03 09:43:07 +00001703 for (t = ip6tables_targets; t; t = t->next) {
1704 t->tflags = 0;
1705 t->used = 0;
1706 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001707
Rusty Russell5eed48a2000-06-02 20:12:24 +00001708 /* Suppress error messages: we may add new options if we
1709 demand-load a protocol. */
1710 opterr = 0;
1711
1712 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001713 "-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 +00001714 opts, NULL)) != -1) {
1715 switch (c) {
1716 /*
1717 * Command selection
1718 */
1719 case 'A':
1720 add_command(&command, CMD_APPEND, CMD_NONE,
1721 invert);
1722 chain = optarg;
1723 break;
1724
1725 case 'D':
1726 add_command(&command, CMD_DELETE, CMD_NONE,
1727 invert);
1728 chain = optarg;
1729 if (optind < argc && argv[optind][0] != '-'
1730 && argv[optind][0] != '!') {
1731 rulenum = parse_rulenumber(argv[optind++]);
1732 command = CMD_DELETE_NUM;
1733 }
1734 break;
1735
Rusty Russell5eed48a2000-06-02 20:12:24 +00001736 case 'R':
1737 add_command(&command, CMD_REPLACE, 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 else
1744 exit_error(PARAMETER_PROBLEM,
1745 "-%c requires a rule number",
1746 cmd2char(CMD_REPLACE));
1747 break;
1748
1749 case 'I':
1750 add_command(&command, CMD_INSERT, CMD_NONE,
1751 invert);
1752 chain = optarg;
1753 if (optind < argc && argv[optind][0] != '-'
1754 && argv[optind][0] != '!')
1755 rulenum = parse_rulenumber(argv[optind++]);
1756 else rulenum = 1;
1757 break;
1758
1759 case 'L':
1760 add_command(&command, CMD_LIST, CMD_ZERO,
1761 invert);
1762 if (optarg) chain = optarg;
1763 else if (optind < argc && argv[optind][0] != '-'
1764 && argv[optind][0] != '!')
1765 chain = argv[optind++];
1766 break;
1767
1768 case 'F':
1769 add_command(&command, CMD_FLUSH, CMD_NONE,
1770 invert);
1771 if (optarg) chain = optarg;
1772 else if (optind < argc && argv[optind][0] != '-'
1773 && argv[optind][0] != '!')
1774 chain = argv[optind++];
1775 break;
1776
1777 case 'Z':
1778 add_command(&command, CMD_ZERO, CMD_LIST,
1779 invert);
1780 if (optarg) chain = optarg;
1781 else if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 chain = argv[optind++];
1784 break;
1785
1786 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001787 if (optarg && *optarg == '-')
1788 exit_error(PARAMETER_PROBLEM,
1789 "chain name not allowed to start "
1790 "with `-'\n");
1791 if (find_target(optarg, TRY_LOAD))
1792 exit_error(PARAMETER_PROBLEM,
1793 "chain name may not clash "
1794 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001795 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1796 invert);
1797 chain = optarg;
1798 break;
1799
1800 case 'X':
1801 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1802 invert);
1803 if (optarg) chain = optarg;
1804 else if (optind < argc && argv[optind][0] != '-'
1805 && argv[optind][0] != '!')
1806 chain = argv[optind++];
1807 break;
1808
1809 case 'E':
1810 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1811 invert);
1812 chain = optarg;
1813 if (optind < argc && argv[optind][0] != '-'
1814 && argv[optind][0] != '!')
1815 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001816 else
1817 exit_error(PARAMETER_PROBLEM,
1818 "-%c requires old-chain-name and "
1819 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001820 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001821 break;
1822
1823 case 'P':
1824 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1825 invert);
1826 chain = optarg;
1827 if (optind < argc && argv[optind][0] != '-'
1828 && argv[optind][0] != '!')
1829 policy = argv[optind++];
1830 else
1831 exit_error(PARAMETER_PROBLEM,
1832 "-%c requires a chain and a policy",
1833 cmd2char(CMD_SET_POLICY));
1834 break;
1835
1836 case 'h':
1837 if (!optarg)
1838 optarg = argv[optind];
1839
1840 /* iptables -p icmp -h */
1841 if (!ip6tables_matches && protocol)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001842 find_match(protocol, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001843
1844 exit_printhelp();
1845
1846 /*
1847 * Option selection
1848 */
1849 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001850 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001851 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1852 invert);
1853
1854 /* Canonicalize into lower case */
1855 for (protocol = argv[optind-1]; *protocol; protocol++)
1856 *protocol = tolower(*protocol);
1857
1858 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001859 if ( strcmp(protocol,"ipv6-icmp") == 0)
1860 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001861 fw.ipv6.proto = parse_protocol(protocol);
1862 fw.ipv6.flags |= IP6T_F_PROTO;
1863
1864 if (fw.ipv6.proto == 0
1865 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1866 exit_error(PARAMETER_PROBLEM,
1867 "rule would never match protocol");
1868 fw.nfcache |= NFC_IP6_PROTO;
1869 break;
1870
1871 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001872 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1874 invert);
1875 shostnetworkmask = argv[optind-1];
1876 fw.nfcache |= NFC_IP6_SRC;
1877 break;
1878
1879 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001880 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1882 invert);
1883 dhostnetworkmask = argv[optind-1];
1884 fw.nfcache |= NFC_IP6_DST;
1885 break;
1886
1887 case 'j':
1888 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1889 invert);
1890 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001891 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001892 target = find_target(jumpto, TRY_LOAD);
1893
1894 if (target) {
1895 size_t size;
1896
Harald Welte43ac1912002-03-03 09:43:07 +00001897 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1898 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899
1900 target->t = fw_calloc(1, size);
1901 target->t->u.target_size = size;
1902 strcpy(target->t->u.user.name, jumpto);
1903 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001904 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905 }
1906 break;
1907
1908
1909 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001910 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001911 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1912 invert);
1913 parse_interface(argv[optind-1],
1914 fw.ipv6.iniface,
1915 fw.ipv6.iniface_mask);
1916 fw.nfcache |= NFC_IP6_IF_IN;
1917 break;
1918
1919 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001920 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1922 invert);
1923 parse_interface(argv[optind-1],
1924 fw.ipv6.outiface,
1925 fw.ipv6.outiface_mask);
1926 fw.nfcache |= NFC_IP6_IF_OUT;
1927 break;
1928
1929 case 'v':
1930 if (!verbose)
1931 set_option(&options, OPT_VERBOSE,
1932 &fw.ipv6.invflags, invert);
1933 verbose++;
1934 break;
1935
1936 case 'm': {
1937 size_t size;
1938
1939 if (invert)
1940 exit_error(PARAMETER_PROBLEM,
1941 "unexpected ! flag before --match");
1942
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001943 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001944 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1945 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001946 m->m = fw_calloc(1, size);
1947 m->m->u.match_size = size;
1948 strcpy(m->m->u.user.name, m->name);
1949 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001950 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 }
1952 break;
1953
1954 case 'n':
1955 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1956 invert);
1957 break;
1958
1959 case 't':
1960 if (invert)
1961 exit_error(PARAMETER_PROBLEM,
1962 "unexpected ! flag before --table");
1963 *table = argv[optind-1];
1964 break;
1965
1966 case 'x':
1967 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1968 invert);
1969 break;
1970
1971 case 'V':
1972 if (invert)
1973 printf("Not %s ;-)\n", program_version);
1974 else
1975 printf("%s v%s\n",
1976 program_name, program_version);
1977 exit(0);
1978
1979 case '0':
1980 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1981 invert);
1982 break;
1983
Harald Welte43ac1912002-03-03 09:43:07 +00001984 case 'M':
1985 modprobe = optarg;
1986 break;
1987
1988 case 'c':
1989
1990 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1991 invert);
1992 pcnt = optarg;
1993 if (optind < argc && argv[optind][0] != '-'
1994 && argv[optind][0] != '!')
1995 bcnt = argv[optind++];
1996 else
1997 exit_error(PARAMETER_PROBLEM,
1998 "-%c requires packet and byte counter",
1999 opt2char(OPT_COUNTERS));
2000
2001 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2002 exit_error(PARAMETER_PROBLEM,
2003 "-%c packet counter not numeric",
2004 opt2char(OPT_COUNTERS));
2005
2006 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2007 exit_error(PARAMETER_PROBLEM,
2008 "-%c byte counter not numeric",
2009 opt2char(OPT_COUNTERS));
2010
2011 break;
2012
2013
Rusty Russell5eed48a2000-06-02 20:12:24 +00002014 case 1: /* non option */
2015 if (optarg[0] == '!' && optarg[1] == '\0') {
2016 if (invert)
2017 exit_error(PARAMETER_PROBLEM,
2018 "multiple consecutive ! not"
2019 " allowed");
2020 invert = TRUE;
2021 optarg[0] = '\0';
2022 continue;
2023 }
2024 printf("Bad argument `%s'\n", optarg);
2025 exit_tryhelp(2);
2026
2027 default:
2028 /* FIXME: This scheme doesn't allow two of the same
2029 matches --RR */
2030 if (!target
2031 || !(target->parse(c - target->option_offset,
2032 argv, invert,
2033 &target->tflags,
2034 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002035 for (matchp = matches; matchp; matchp = matchp->next) {
2036 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002037 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002038 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002039 &fw,
2040 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002041 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002042 break;
2043 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002044 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002045
2046 /* If you listen carefully, you can
2047 actually hear this code suck. */
2048
2049 /* some explanations (after four different bugs
2050 * in 3 different releases): If we encountere a
2051 * parameter, that has not been parsed yet,
2052 * it's not an option of an explicitly loaded
2053 * match or a target. However, we support
2054 * implicit loading of the protocol match
2055 * extension. '-p tcp' means 'l4 proto 6' and
2056 * at the same time 'load tcp protocol match on
2057 * demand if we specify --dport'.
2058 *
2059 * To make this work, we need to make sure:
2060 * - the parameter has not been parsed by
2061 * a match (m above)
2062 * - a protocol has been specified
2063 * - the protocol extension has not been
2064 * loaded yet, or is loaded and unused
2065 * [think of iptables-restore!]
2066 * - the protocol extension can be successively
2067 * loaded
2068 */
2069 if (m == NULL
2070 && protocol
2071 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002072 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002073 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002074 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002075 && (proto_used == 0))
2076 )
2077 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002078 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002079 /* Try loading protocol */
2080 size_t size;
2081
2082 proto_used = 1;
2083
2084 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2085 + m->size;
2086
2087 m->m = fw_calloc(1, size);
2088 m->m->u.match_size = size;
2089 strcpy(m->m->u.user.name, m->name);
2090 m->init(m->m, &fw.nfcache);
2091
2092 opts = merge_options(opts,
2093 m->extra_opts, &m->option_offset);
2094
2095 optind--;
2096 continue;
2097 }
2098
Rusty Russell5eed48a2000-06-02 20:12:24 +00002099 if (!m)
2100 exit_error(PARAMETER_PROBLEM,
2101 "Unknown arg `%s'",
2102 argv[optind-1]);
2103 }
2104 }
2105 invert = FALSE;
2106 }
2107
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002108 for (matchp = matches; matchp; matchp = matchp->next)
2109 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002110
Harald Welte43ac1912002-03-03 09:43:07 +00002111 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 target->final_check(target->tflags);
2113
2114 /* Fix me: must put inverse options checking here --MN */
2115
2116 if (optind < argc)
2117 exit_error(PARAMETER_PROBLEM,
2118 "unknown arguments found on commandline");
2119 if (!command)
2120 exit_error(PARAMETER_PROBLEM, "no command specified");
2121 if (invert)
2122 exit_error(PARAMETER_PROBLEM,
2123 "nothing appropriate following !");
2124
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002125 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002126 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002127 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002128 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002129 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002130 }
2131
2132 if (shostnetworkmask)
2133 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2134 &(fw.ipv6.smsk), &nsaddrs);
2135
2136 if (dhostnetworkmask)
2137 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2138 &(fw.ipv6.dmsk), &ndaddrs);
2139
2140 if ((nsaddrs > 1 || ndaddrs > 1) &&
2141 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2142 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2143 " source or destination IP addresses");
2144
Rusty Russell5eed48a2000-06-02 20:12:24 +00002145 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 */
Martin Josefsson8371e152003-05-05 19:33:40 +00002157 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002158 *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
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002171 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002172 || command == CMD_DELETE
2173 || command == CMD_INSERT
2174 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002175 if (strcmp(chain, "PREROUTING") == 0
2176 || strcmp(chain, "INPUT") == 0) {
2177 /* -o not valid with incoming packets. */
2178 if (options & OPT_VIANAMEOUT)
2179 exit_error(PARAMETER_PROBLEM,
2180 "Can't use -%c with %s\n",
2181 opt2char(OPT_VIANAMEOUT),
2182 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002183 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002184
Harald Welte43ac1912002-03-03 09:43:07 +00002185 if (strcmp(chain, "POSTROUTING") == 0
2186 || strcmp(chain, "OUTPUT") == 0) {
2187 /* -i not valid with outgoing packets */
2188 if (options & OPT_VIANAMEIN)
2189 exit_error(PARAMETER_PROBLEM,
2190 "Can't use -%c with %s\n",
2191 opt2char(OPT_VIANAMEIN),
2192 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002193 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002194
2195 if (target && ip6tc_is_chain(jumpto, *handle)) {
2196 printf("Warning: using chain %s, not extension\n",
2197 jumpto);
2198
2199 target = NULL;
2200 }
2201
2202 /* If they didn't specify a target, or it's a chain
2203 name, use standard. */
2204 if (!target
2205 && (strlen(jumpto) == 0
2206 || ip6tc_is_chain(jumpto, *handle))) {
2207 size_t size;
2208
2209 target = find_target(IP6T_STANDARD_TARGET,
2210 LOAD_MUST_SUCCEED);
2211
2212 size = sizeof(struct ip6t_entry_target)
2213 + target->size;
2214 target->t = fw_calloc(1, size);
2215 target->t->u.target_size = size;
2216 strcpy(target->t->u.user.name, jumpto);
2217 target->init(target->t, &fw.nfcache);
2218 }
2219
2220 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002221 /* it is no chain, and we can't load a plugin.
2222 * We cannot know if the plugin is corrupt, non
2223 * existant OR if the user just misspelled a
2224 * chain. */
2225 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002226 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002227 e = generate_entry(&fw, matches, target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002228 }
2229 }
2230
2231 switch (command) {
2232 case CMD_APPEND:
2233 ret = append_entry(chain, e,
2234 nsaddrs, saddrs, ndaddrs, daddrs,
2235 options&OPT_VERBOSE,
2236 handle);
2237 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002238 case CMD_DELETE:
2239 ret = delete_entry(chain, e,
2240 nsaddrs, saddrs, ndaddrs, daddrs,
2241 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002242 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002243 break;
2244 case CMD_DELETE_NUM:
2245 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2246 break;
2247 case CMD_REPLACE:
2248 ret = replace_entry(chain, e, rulenum - 1,
2249 saddrs, daddrs, options&OPT_VERBOSE,
2250 handle);
2251 break;
2252 case CMD_INSERT:
2253 ret = insert_entry(chain, e, rulenum - 1,
2254 nsaddrs, saddrs, ndaddrs, daddrs,
2255 options&OPT_VERBOSE,
2256 handle);
2257 break;
2258 case CMD_LIST:
2259 ret = list_entries(chain,
2260 options&OPT_VERBOSE,
2261 options&OPT_NUMERIC,
2262 options&OPT_EXPANDED,
2263 options&OPT_LINENUMBERS,
2264 handle);
2265 break;
2266 case CMD_FLUSH:
2267 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2268 break;
2269 case CMD_ZERO:
2270 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2271 break;
2272 case CMD_LIST|CMD_ZERO:
2273 ret = list_entries(chain,
2274 options&OPT_VERBOSE,
2275 options&OPT_NUMERIC,
2276 options&OPT_EXPANDED,
2277 options&OPT_LINENUMBERS,
2278 handle);
2279 if (ret)
2280 ret = zero_entries(chain,
2281 options&OPT_VERBOSE, handle);
2282 break;
2283 case CMD_NEW_CHAIN:
2284 ret = ip6tc_create_chain(chain, handle);
2285 break;
2286 case CMD_DELETE_CHAIN:
2287 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2288 break;
2289 case CMD_RENAME_CHAIN:
2290 ret = ip6tc_rename_chain(chain, newname, handle);
2291 break;
2292 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002293 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002294 break;
2295 default:
2296 /* We should never reach this... */
2297 exit_tryhelp(2);
2298 }
2299
2300 if (verbose > 1)
2301 dump_entries6(*handle);
2302
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002303 clear_rule_matches(&matches);
2304
Rusty Russell5eed48a2000-06-02 20:12:24 +00002305 return ret;
2306}