blob: b2816bad7202c39957cb138631030d2a4121f3d4 [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);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000843 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 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 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001436 free(mask);
1437
Rusty Russell5eed48a2000-06-02 20:12:24 +00001438 return ret;
1439}
1440
András Kis-Szabó764316a2001-02-26 17:31:20 +00001441int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001442for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1443 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1444{
1445 int ret = 1;
1446 const char *chain;
1447 char *chains;
1448 unsigned int i, chaincount = 0;
1449
1450 chain = ip6tc_first_chain(handle);
1451 while (chain) {
1452 chaincount++;
1453 chain = ip6tc_next_chain(handle);
1454 }
1455
1456 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1457 i = 0;
1458 chain = ip6tc_first_chain(handle);
1459 while (chain) {
1460 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1461 i++;
1462 chain = ip6tc_next_chain(handle);
1463 }
1464
1465 for (i = 0; i < chaincount; i++) {
1466 if (!builtinstoo
1467 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1468 *handle))
1469 continue;
1470 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1471 }
1472
1473 free(chains);
1474 return ret;
1475}
1476
András Kis-Szabó764316a2001-02-26 17:31:20 +00001477int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001478flush_entries(const ip6t_chainlabel chain, int verbose,
1479 ip6tc_handle_t *handle)
1480{
1481 if (!chain)
1482 return for_each_chain(flush_entries, verbose, 1, handle);
1483
1484 if (verbose)
1485 fprintf(stdout, "Flushing chain `%s'\n", chain);
1486 return ip6tc_flush_entries(chain, handle);
1487}
1488
1489static int
1490zero_entries(const ip6t_chainlabel chain, int verbose,
1491 ip6tc_handle_t *handle)
1492{
1493 if (!chain)
1494 return for_each_chain(zero_entries, verbose, 1, handle);
1495
1496 if (verbose)
1497 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1498 return ip6tc_zero_entries(chain, handle);
1499}
1500
András Kis-Szabó764316a2001-02-26 17:31:20 +00001501int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001502delete_chain(const ip6t_chainlabel chain, int verbose,
1503 ip6tc_handle_t *handle)
1504{
1505 if (!chain)
1506 return for_each_chain(delete_chain, verbose, 0, handle);
1507
1508 if (verbose)
1509 fprintf(stdout, "Deleting chain `%s'\n", chain);
1510 return ip6tc_delete_chain(chain, handle);
1511}
1512
1513static int
1514list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1515 int expanded, int linenumbers, ip6tc_handle_t *handle)
1516{
1517 int found = 0;
1518 unsigned int format;
1519 const char *this;
1520
1521 format = FMT_OPTIONS;
1522 if (!verbose)
1523 format |= FMT_NOCOUNTS;
1524 else
1525 format |= FMT_VIA;
1526
1527 if (numeric)
1528 format |= FMT_NUMERIC;
1529
1530 if (!expanded)
1531 format |= FMT_KILOMEGAGIGA;
1532
1533 if (linenumbers)
1534 format |= FMT_LINENUMBERS;
1535
1536 for (this = ip6tc_first_chain(handle);
1537 this;
1538 this = ip6tc_next_chain(handle)) {
1539 const struct ip6t_entry *i;
1540 unsigned int num;
1541
1542 if (chain && strcmp(chain, this) != 0)
1543 continue;
1544
1545 if (found) printf("\n");
1546
1547 print_header(format, this, handle);
1548 i = ip6tc_first_rule(this, handle);
1549
1550 num = 0;
1551 while (i) {
1552 print_firewall(i,
1553 ip6tc_get_target(i, handle),
1554 num++,
1555 format,
1556 *handle);
1557 i = ip6tc_next_rule(i, handle);
1558 }
1559 found = 1;
1560 }
1561
1562 errno = ENOENT;
1563 return found;
1564}
1565
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001566static char *get_modprobe(void)
1567{
Harald Welte43ac1912002-03-03 09:43:07 +00001568 int procfile;
1569 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001570
Harald Welte43ac1912002-03-03 09:43:07 +00001571 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1572 if (procfile < 0)
1573 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001574
Harald Welte43ac1912002-03-03 09:43:07 +00001575 ret = malloc(1024);
1576 if (ret) {
1577 switch (read(procfile, ret, 1024)) {
1578 case -1: goto fail;
1579 case 1024: goto fail; /* Partial read. Wierd */
1580 }
1581 if (ret[strlen(ret)-1]=='\n')
1582 ret[strlen(ret)-1]=0;
1583 close(procfile);
1584 return ret;
1585 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001586 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001587 free(ret);
1588 close(procfile);
1589 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001590}
1591
Harald Welte58918652001-06-16 18:25:25 +00001592int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001593{
Harald Welte43ac1912002-03-03 09:43:07 +00001594 char *buf = NULL;
1595 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001596
Harald Welte43ac1912002-03-03 09:43:07 +00001597 /* If they don't explicitly set it, read out of kernel */
1598 if (!modprobe) {
1599 buf = get_modprobe();
1600 if (!buf)
1601 return -1;
1602 modprobe = buf;
1603 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001604
Harald Welte43ac1912002-03-03 09:43:07 +00001605 switch (fork()) {
1606 case 0:
1607 argv[0] = (char *)modprobe;
1608 argv[1] = (char *)modname;
1609 argv[2] = NULL;
1610 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001611
Harald Welte43ac1912002-03-03 09:43:07 +00001612 /* not usually reached */
1613 exit(0);
1614 case -1:
1615 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001616
Harald Welte43ac1912002-03-03 09:43:07 +00001617 default: /* parent */
1618 wait(NULL);
1619 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001620
Harald Welte43ac1912002-03-03 09:43:07 +00001621 free(buf);
1622 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623}
1624
Rusty Russell5eed48a2000-06-02 20:12:24 +00001625static struct ip6t_entry *
1626generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001627 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001628 struct ip6t_entry_target *target)
1629{
1630 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001631 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632 struct ip6t_entry *e;
1633
1634 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001635 for (matchp = matches; matchp; matchp = matchp->next)
1636 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001637
1638 e = fw_malloc(size + target->u.target_size);
1639 *e = *fw;
1640 e->target_offset = size;
1641 e->next_offset = size + target->u.target_size;
1642
1643 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001644 for (matchp = matches; matchp; matchp = matchp->next) {
1645 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1646 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001647 }
1648 memcpy(e->elems + size, target, target->u.target_size);
1649
1650 return e;
1651}
1652
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001653void clear_rule_matches(struct ip6tables_rule_match **matches)
1654{
1655 struct ip6tables_rule_match *matchp, *tmp;
1656
1657 for (matchp = *matches; matchp;) {
1658 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001659 if (matchp->match->m)
1660 free(matchp->match->m);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001661 free(matchp);
1662 matchp = tmp;
1663 }
1664
1665 *matches = NULL;
1666}
1667
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1669{
1670 struct ip6t_entry fw, *e = NULL;
1671 int invert = 0;
1672 unsigned int nsaddrs = 0, ndaddrs = 0;
1673 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1674
1675 int c, verbose = 0;
1676 const char *chain = NULL;
1677 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1678 const char *policy = NULL, *newname = NULL;
1679 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001680 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001681 int ret = 1;
1682 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001683 struct ip6tables_rule_match *matches = NULL;
1684 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001685 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001686 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001687 const char *jumpto = "";
1688 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001689 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001690 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001691 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001692
1693 memset(&fw, 0, sizeof(fw));
1694
Harald Welte43ac1912002-03-03 09:43:07 +00001695 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001696 * a second time */
1697 optind = 0;
1698
Harald Welte43ac1912002-03-03 09:43:07 +00001699 /* clear mflags in case do_command gets called a second time
1700 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001701 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001702 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001703
Harald Welte43ac1912002-03-03 09:43:07 +00001704 for (t = ip6tables_targets; t; t = t->next) {
1705 t->tflags = 0;
1706 t->used = 0;
1707 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001708
Rusty Russell5eed48a2000-06-02 20:12:24 +00001709 /* Suppress error messages: we may add new options if we
1710 demand-load a protocol. */
1711 opterr = 0;
1712
1713 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001714 "-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 +00001715 opts, NULL)) != -1) {
1716 switch (c) {
1717 /*
1718 * Command selection
1719 */
1720 case 'A':
1721 add_command(&command, CMD_APPEND, CMD_NONE,
1722 invert);
1723 chain = optarg;
1724 break;
1725
1726 case 'D':
1727 add_command(&command, CMD_DELETE, CMD_NONE,
1728 invert);
1729 chain = optarg;
1730 if (optind < argc && argv[optind][0] != '-'
1731 && argv[optind][0] != '!') {
1732 rulenum = parse_rulenumber(argv[optind++]);
1733 command = CMD_DELETE_NUM;
1734 }
1735 break;
1736
Rusty Russell5eed48a2000-06-02 20:12:24 +00001737 case 'R':
1738 add_command(&command, CMD_REPLACE, CMD_NONE,
1739 invert);
1740 chain = optarg;
1741 if (optind < argc && argv[optind][0] != '-'
1742 && argv[optind][0] != '!')
1743 rulenum = parse_rulenumber(argv[optind++]);
1744 else
1745 exit_error(PARAMETER_PROBLEM,
1746 "-%c requires a rule number",
1747 cmd2char(CMD_REPLACE));
1748 break;
1749
1750 case 'I':
1751 add_command(&command, CMD_INSERT, CMD_NONE,
1752 invert);
1753 chain = optarg;
1754 if (optind < argc && argv[optind][0] != '-'
1755 && argv[optind][0] != '!')
1756 rulenum = parse_rulenumber(argv[optind++]);
1757 else rulenum = 1;
1758 break;
1759
1760 case 'L':
1761 add_command(&command, CMD_LIST, CMD_ZERO,
1762 invert);
1763 if (optarg) chain = optarg;
1764 else if (optind < argc && argv[optind][0] != '-'
1765 && argv[optind][0] != '!')
1766 chain = argv[optind++];
1767 break;
1768
1769 case 'F':
1770 add_command(&command, CMD_FLUSH, CMD_NONE,
1771 invert);
1772 if (optarg) chain = optarg;
1773 else if (optind < argc && argv[optind][0] != '-'
1774 && argv[optind][0] != '!')
1775 chain = argv[optind++];
1776 break;
1777
1778 case 'Z':
1779 add_command(&command, CMD_ZERO, CMD_LIST,
1780 invert);
1781 if (optarg) chain = optarg;
1782 else if (optind < argc && argv[optind][0] != '-'
1783 && argv[optind][0] != '!')
1784 chain = argv[optind++];
1785 break;
1786
1787 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001788 if (optarg && *optarg == '-')
1789 exit_error(PARAMETER_PROBLEM,
1790 "chain name not allowed to start "
1791 "with `-'\n");
1792 if (find_target(optarg, TRY_LOAD))
1793 exit_error(PARAMETER_PROBLEM,
1794 "chain name may not clash "
1795 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001796 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1797 invert);
1798 chain = optarg;
1799 break;
1800
1801 case 'X':
1802 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1803 invert);
1804 if (optarg) chain = optarg;
1805 else if (optind < argc && argv[optind][0] != '-'
1806 && argv[optind][0] != '!')
1807 chain = argv[optind++];
1808 break;
1809
1810 case 'E':
1811 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1812 invert);
1813 chain = optarg;
1814 if (optind < argc && argv[optind][0] != '-'
1815 && argv[optind][0] != '!')
1816 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001817 else
1818 exit_error(PARAMETER_PROBLEM,
1819 "-%c requires old-chain-name and "
1820 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001821 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001822 break;
1823
1824 case 'P':
1825 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1826 invert);
1827 chain = optarg;
1828 if (optind < argc && argv[optind][0] != '-'
1829 && argv[optind][0] != '!')
1830 policy = argv[optind++];
1831 else
1832 exit_error(PARAMETER_PROBLEM,
1833 "-%c requires a chain and a policy",
1834 cmd2char(CMD_SET_POLICY));
1835 break;
1836
1837 case 'h':
1838 if (!optarg)
1839 optarg = argv[optind];
1840
1841 /* iptables -p icmp -h */
1842 if (!ip6tables_matches && protocol)
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001843 find_match(protocol, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001844
1845 exit_printhelp();
1846
1847 /*
1848 * Option selection
1849 */
1850 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001851 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001852 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1853 invert);
1854
1855 /* Canonicalize into lower case */
1856 for (protocol = argv[optind-1]; *protocol; protocol++)
1857 *protocol = tolower(*protocol);
1858
1859 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001860 if ( strcmp(protocol,"ipv6-icmp") == 0)
1861 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001862 fw.ipv6.proto = parse_protocol(protocol);
1863 fw.ipv6.flags |= IP6T_F_PROTO;
1864
1865 if (fw.ipv6.proto == 0
1866 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1867 exit_error(PARAMETER_PROBLEM,
1868 "rule would never match protocol");
1869 fw.nfcache |= NFC_IP6_PROTO;
1870 break;
1871
1872 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001873 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001874 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1875 invert);
1876 shostnetworkmask = argv[optind-1];
1877 fw.nfcache |= NFC_IP6_SRC;
1878 break;
1879
1880 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001881 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1883 invert);
1884 dhostnetworkmask = argv[optind-1];
1885 fw.nfcache |= NFC_IP6_DST;
1886 break;
1887
1888 case 'j':
1889 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1890 invert);
1891 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001892 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893 target = find_target(jumpto, TRY_LOAD);
1894
1895 if (target) {
1896 size_t size;
1897
Harald Welte43ac1912002-03-03 09:43:07 +00001898 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1899 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900
1901 target->t = fw_calloc(1, size);
1902 target->t->u.target_size = size;
1903 strcpy(target->t->u.user.name, jumpto);
1904 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001905 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001906 }
1907 break;
1908
1909
1910 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001911 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1913 invert);
1914 parse_interface(argv[optind-1],
1915 fw.ipv6.iniface,
1916 fw.ipv6.iniface_mask);
1917 fw.nfcache |= NFC_IP6_IF_IN;
1918 break;
1919
1920 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001921 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001922 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1923 invert);
1924 parse_interface(argv[optind-1],
1925 fw.ipv6.outiface,
1926 fw.ipv6.outiface_mask);
1927 fw.nfcache |= NFC_IP6_IF_OUT;
1928 break;
1929
1930 case 'v':
1931 if (!verbose)
1932 set_option(&options, OPT_VERBOSE,
1933 &fw.ipv6.invflags, invert);
1934 verbose++;
1935 break;
1936
1937 case 'm': {
1938 size_t size;
1939
1940 if (invert)
1941 exit_error(PARAMETER_PROBLEM,
1942 "unexpected ! flag before --match");
1943
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001944 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001945 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1946 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 m->m = fw_calloc(1, size);
1948 m->m->u.match_size = size;
1949 strcpy(m->m->u.user.name, m->name);
1950 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001951 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001952 }
1953 break;
1954
1955 case 'n':
1956 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1957 invert);
1958 break;
1959
1960 case 't':
1961 if (invert)
1962 exit_error(PARAMETER_PROBLEM,
1963 "unexpected ! flag before --table");
1964 *table = argv[optind-1];
1965 break;
1966
1967 case 'x':
1968 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1969 invert);
1970 break;
1971
1972 case 'V':
1973 if (invert)
1974 printf("Not %s ;-)\n", program_version);
1975 else
1976 printf("%s v%s\n",
1977 program_name, program_version);
1978 exit(0);
1979
1980 case '0':
1981 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1982 invert);
1983 break;
1984
Harald Welte43ac1912002-03-03 09:43:07 +00001985 case 'M':
1986 modprobe = optarg;
1987 break;
1988
1989 case 'c':
1990
1991 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1992 invert);
1993 pcnt = optarg;
1994 if (optind < argc && argv[optind][0] != '-'
1995 && argv[optind][0] != '!')
1996 bcnt = argv[optind++];
1997 else
1998 exit_error(PARAMETER_PROBLEM,
1999 "-%c requires packet and byte counter",
2000 opt2char(OPT_COUNTERS));
2001
2002 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
2003 exit_error(PARAMETER_PROBLEM,
2004 "-%c packet counter not numeric",
2005 opt2char(OPT_COUNTERS));
2006
2007 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2008 exit_error(PARAMETER_PROBLEM,
2009 "-%c byte counter not numeric",
2010 opt2char(OPT_COUNTERS));
2011
2012 break;
2013
2014
Rusty Russell5eed48a2000-06-02 20:12:24 +00002015 case 1: /* non option */
2016 if (optarg[0] == '!' && optarg[1] == '\0') {
2017 if (invert)
2018 exit_error(PARAMETER_PROBLEM,
2019 "multiple consecutive ! not"
2020 " allowed");
2021 invert = TRUE;
2022 optarg[0] = '\0';
2023 continue;
2024 }
2025 printf("Bad argument `%s'\n", optarg);
2026 exit_tryhelp(2);
2027
2028 default:
2029 /* FIXME: This scheme doesn't allow two of the same
2030 matches --RR */
2031 if (!target
2032 || !(target->parse(c - target->option_offset,
2033 argv, invert,
2034 &target->tflags,
2035 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002036 for (matchp = matches; matchp; matchp = matchp->next) {
2037 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002038 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002039 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002040 &fw,
2041 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002042 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002043 break;
2044 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002045 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002046
2047 /* If you listen carefully, you can
2048 actually hear this code suck. */
2049
2050 /* some explanations (after four different bugs
2051 * in 3 different releases): If we encountere a
2052 * parameter, that has not been parsed yet,
2053 * it's not an option of an explicitly loaded
2054 * match or a target. However, we support
2055 * implicit loading of the protocol match
2056 * extension. '-p tcp' means 'l4 proto 6' and
2057 * at the same time 'load tcp protocol match on
2058 * demand if we specify --dport'.
2059 *
2060 * To make this work, we need to make sure:
2061 * - the parameter has not been parsed by
2062 * a match (m above)
2063 * - a protocol has been specified
2064 * - the protocol extension has not been
2065 * loaded yet, or is loaded and unused
2066 * [think of iptables-restore!]
2067 * - the protocol extension can be successively
2068 * loaded
2069 */
2070 if (m == NULL
2071 && protocol
2072 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002073 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002074 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002075 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002076 && (proto_used == 0))
2077 )
2078 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002079 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002080 /* Try loading protocol */
2081 size_t size;
2082
2083 proto_used = 1;
2084
2085 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2086 + m->size;
2087
2088 m->m = fw_calloc(1, size);
2089 m->m->u.match_size = size;
2090 strcpy(m->m->u.user.name, m->name);
2091 m->init(m->m, &fw.nfcache);
2092
2093 opts = merge_options(opts,
2094 m->extra_opts, &m->option_offset);
2095
2096 optind--;
2097 continue;
2098 }
2099
Rusty Russell5eed48a2000-06-02 20:12:24 +00002100 if (!m)
2101 exit_error(PARAMETER_PROBLEM,
2102 "Unknown arg `%s'",
2103 argv[optind-1]);
2104 }
2105 }
2106 invert = FALSE;
2107 }
2108
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002109 for (matchp = matches; matchp; matchp = matchp->next)
2110 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002111
Harald Welte43ac1912002-03-03 09:43:07 +00002112 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002113 target->final_check(target->tflags);
2114
2115 /* Fix me: must put inverse options checking here --MN */
2116
2117 if (optind < argc)
2118 exit_error(PARAMETER_PROBLEM,
2119 "unknown arguments found on commandline");
2120 if (!command)
2121 exit_error(PARAMETER_PROBLEM, "no command specified");
2122 if (invert)
2123 exit_error(PARAMETER_PROBLEM,
2124 "nothing appropriate following !");
2125
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002126 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002127 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002128 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002129 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002130 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002131 }
2132
2133 if (shostnetworkmask)
2134 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2135 &(fw.ipv6.smsk), &nsaddrs);
2136
2137 if (dhostnetworkmask)
2138 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2139 &(fw.ipv6.dmsk), &ndaddrs);
2140
2141 if ((nsaddrs > 1 || ndaddrs > 1) &&
2142 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2143 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2144 " source or destination IP addresses");
2145
Rusty Russell5eed48a2000-06-02 20:12:24 +00002146 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2147 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2148 "specify a unique address");
2149
2150 generic_opt_check(command, options);
2151
2152 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2153 exit_error(PARAMETER_PROBLEM,
2154 "chain name `%s' too long (must be under %i chars)",
2155 chain, IP6T_FUNCTION_MAXNAMELEN);
2156
Harald Welte43ac1912002-03-03 09:43:07 +00002157 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002158 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002159 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002160
Harald Welte43ac1912002-03-03 09:43:07 +00002161 if (!*handle) {
2162 /* try to insmod the module if iptc_init failed */
2163 ip6tables_insmod("ip6_tables", modprobe);
2164 *handle = ip6tc_init(*table);
2165 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002166
Harald Welte43ac1912002-03-03 09:43:07 +00002167 if (!*handle)
2168 exit_error(VERSION_PROBLEM,
2169 "can't initialize ip6tables table `%s': %s",
2170 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002171
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002172 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002173 || 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);
Harald Welte43ac1912002-03-03 09:43:07 +00002184 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002185
Harald Welte43ac1912002-03-03 09:43:07 +00002186 if (strcmp(chain, "POSTROUTING") == 0
2187 || strcmp(chain, "OUTPUT") == 0) {
2188 /* -i not valid with outgoing packets */
2189 if (options & OPT_VIANAMEIN)
2190 exit_error(PARAMETER_PROBLEM,
2191 "Can't use -%c with %s\n",
2192 opt2char(OPT_VIANAMEIN),
2193 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002194 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002195
2196 if (target && ip6tc_is_chain(jumpto, *handle)) {
2197 printf("Warning: using chain %s, not extension\n",
2198 jumpto);
2199
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002200 if (target->t)
2201 free(target->t);
2202
Rusty Russell5eed48a2000-06-02 20:12:24 +00002203 target = NULL;
2204 }
2205
2206 /* If they didn't specify a target, or it's a chain
2207 name, use standard. */
2208 if (!target
2209 && (strlen(jumpto) == 0
2210 || ip6tc_is_chain(jumpto, *handle))) {
2211 size_t size;
2212
2213 target = find_target(IP6T_STANDARD_TARGET,
2214 LOAD_MUST_SUCCEED);
2215
2216 size = sizeof(struct ip6t_entry_target)
2217 + target->size;
2218 target->t = fw_calloc(1, size);
2219 target->t->u.target_size = size;
2220 strcpy(target->t->u.user.name, jumpto);
2221 target->init(target->t, &fw.nfcache);
2222 }
2223
2224 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002225 /* it is no chain, and we can't load a plugin.
2226 * We cannot know if the plugin is corrupt, non
2227 * existant OR if the user just misspelled a
2228 * chain. */
2229 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002230 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002231 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002232 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002233 }
2234 }
2235
2236 switch (command) {
2237 case CMD_APPEND:
2238 ret = append_entry(chain, e,
2239 nsaddrs, saddrs, ndaddrs, daddrs,
2240 options&OPT_VERBOSE,
2241 handle);
2242 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002243 case CMD_DELETE:
2244 ret = delete_entry(chain, e,
2245 nsaddrs, saddrs, ndaddrs, daddrs,
2246 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002247 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002248 break;
2249 case CMD_DELETE_NUM:
2250 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2251 break;
2252 case CMD_REPLACE:
2253 ret = replace_entry(chain, e, rulenum - 1,
2254 saddrs, daddrs, options&OPT_VERBOSE,
2255 handle);
2256 break;
2257 case CMD_INSERT:
2258 ret = insert_entry(chain, e, rulenum - 1,
2259 nsaddrs, saddrs, ndaddrs, daddrs,
2260 options&OPT_VERBOSE,
2261 handle);
2262 break;
2263 case CMD_LIST:
2264 ret = list_entries(chain,
2265 options&OPT_VERBOSE,
2266 options&OPT_NUMERIC,
2267 options&OPT_EXPANDED,
2268 options&OPT_LINENUMBERS,
2269 handle);
2270 break;
2271 case CMD_FLUSH:
2272 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2273 break;
2274 case CMD_ZERO:
2275 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2276 break;
2277 case CMD_LIST|CMD_ZERO:
2278 ret = list_entries(chain,
2279 options&OPT_VERBOSE,
2280 options&OPT_NUMERIC,
2281 options&OPT_EXPANDED,
2282 options&OPT_LINENUMBERS,
2283 handle);
2284 if (ret)
2285 ret = zero_entries(chain,
2286 options&OPT_VERBOSE, handle);
2287 break;
2288 case CMD_NEW_CHAIN:
2289 ret = ip6tc_create_chain(chain, handle);
2290 break;
2291 case CMD_DELETE_CHAIN:
2292 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2293 break;
2294 case CMD_RENAME_CHAIN:
2295 ret = ip6tc_rename_chain(chain, newname, handle);
2296 break;
2297 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002298 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002299 break;
2300 default:
2301 /* We should never reach this... */
2302 exit_tryhelp(2);
2303 }
2304
2305 if (verbose > 1)
2306 dump_entries6(*handle);
2307
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002308 clear_rule_matches(&matches);
2309
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002310 if (e != NULL) {
2311 free(e);
2312 e = NULL;
2313 }
2314
2315 for (c = 0; c < nsaddrs; c++)
2316 free(&saddrs[c]);
2317
2318 for (c = 0; c < ndaddrs; c++)
2319 free(&daddrs[c]);
2320
2321 if (opts != original_opts) {
2322 free(opts);
2323 opts = original_opts;
2324 global_option_offset = 0;
2325 }
2326
Rusty Russell5eed48a2000-06-02 20:12:24 +00002327 return ret;
2328}