blob: 2d13f3af20d0430cfce300fe1e20a44b0ae01604 [file] [log] [blame]
Rusty Russell5eed48a2000-06-02 20:12:24 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
31#include <ip6tables.h>
32#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000033#include <unistd.h>
34#include <fcntl.h>
35#include <sys/wait.h>
36#include <sys/types.h>
37#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000038
39#ifndef TRUE
40#define TRUE 1
41#endif
42#ifndef FALSE
43#define FALSE 0
44#endif
45
46#ifndef IP6T_LIB_DIR
47#define IP6T_LIB_DIR "/usr/local/lib/iptables"
48#endif
49
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000050#ifndef PROC_SYS_MODPROBE
51#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
52#endif
53
Rusty Russell5eed48a2000-06-02 20:12:24 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
81#define CMD_CHECK 0x0800U
82#define CMD_RENAME_CHAIN 0x1000U
83#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
85 'N', 'X', 'P', 'C', 'E' };
86
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
100#define NUMBER_OF_OPT 10
101static const char optflags[NUMBER_OF_OPT]
102= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3'};
103
104static struct option original_opts[] = {
105 { "append", 1, 0, 'A' },
106 { "delete", 1, 0, 'D' },
107 { "insert", 1, 0, 'I' },
108 { "replace", 1, 0, 'R' },
109 { "list", 2, 0, 'L' },
110 { "flush", 2, 0, 'F' },
111 { "zero", 2, 0, 'Z' },
112 { "check", 1, 0, 'C' },
113 { "new-chain", 1, 0, 'N' },
114 { "delete-chain", 2, 0, 'X' },
115 { "rename-chain", 2, 0, 'E' },
116 { "policy", 1, 0, 'P' },
117 { "source", 1, 0, 's' },
118 { "destination", 1, 0, 'd' },
119 { "src", 1, 0, 's' }, /* synonym */
120 { "dst", 1, 0, 'd' }, /* synonym */
121 { "protocol", 1, 0, 'p' },
122 { "in-interface", 1, 0, 'i' },
123 { "jump", 1, 0, 'j' },
124 { "table", 1, 0, 't' },
125 { "match", 1, 0, 'm' },
126 { "numeric", 0, 0, 'n' },
127 { "out-interface", 1, 0, 'o' },
128 { "verbose", 0, 0, 'v' },
129 { "exact", 0, 0, 'x' },
130 { "version", 0, 0, 'V' },
131 { "help", 2, 0, 'h' },
132 { "line-numbers", 0, 0, '0' },
133 { 0 }
134};
135
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000136#ifndef __OPTIMIZE__
137struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000138ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000139{
140 return (void *)e + e->target_offset;
141}
142#endif
143
Rusty Russell5eed48a2000-06-02 20:12:24 +0000144static struct option *opts = original_opts;
145static unsigned int global_option_offset = 0;
146
147/* Table of legal combinations of commands and options. If any of the
148 * given commands make an option legal, that option is legal (applies to
149 * CMD_LIST and CMD_ZERO only).
150 * Key:
151 * + compulsory
152 * x illegal
153 * optional
154 */
155
156static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
157/* Well, it's better than "Re: Linux vs FreeBSD" */
158{
159 /* -n -s -d -p -j -v -x -i -o --line */
160/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
161/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
162/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
163/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
164/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
165/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
166/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
167/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
168/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
169/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
170/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
171/*CHECK*/ {'x','+','+','+','x',' ','x','+','+','x'},
172/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
173};
174
175static int inverse_for_options[NUMBER_OF_OPT] =
176{
177/* -n */ 0,
178/* -s */ IP6T_INV_SRCIP,
179/* -d */ IP6T_INV_DSTIP,
180/* -p */ IP6T_INV_PROTO,
181/* -j */ 0,
182/* -v */ 0,
183/* -x */ 0,
184/* -i */ IP6T_INV_VIA_IN,
185/* -o */ IP6T_INV_VIA_OUT,
186/*--line*/ 0
187};
188
189const char *program_version;
190const char *program_name;
191
192/* Keeping track of external matches and targets: linked lists. */
193struct ip6tables_match *ip6tables_matches = NULL;
194struct ip6tables_target *ip6tables_targets = NULL;
195
196/* Extra debugging from libiptc */
197extern void dump_entries6(const ip6tc_handle_t handle);
198
199/* A few hardcoded protocols for 'all' and in case the user has no
200 /etc/protocols */
201struct pprot {
202 char *name;
203 u_int8_t num;
204};
205
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000206/* Primitive headers... */
207/* defined in netinet/in.h */
208#if 0
209#ifndef IPPROTO_ESP
210#define IPPROTO_ESP 50
211#endif
212#ifndef IPPROTO_AH
213#define IPPROTO_AH 51
214#endif
215#endif
216
Rusty Russell5eed48a2000-06-02 20:12:24 +0000217static const struct pprot chain_protos[] = {
218 { "tcp", IPPROTO_TCP },
219 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000220 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000221 { "esp", IPPROTO_ESP },
222 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000223 { "all", 0 },
224};
225
226static char *
227proto_to_name(u_int8_t proto, int nolookup)
228{
229 unsigned int i;
230
231 if (proto && !nolookup) {
232 struct protoent *pent = getprotobynumber(proto);
233 if (pent)
234 return pent->p_name;
235 }
236
237 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
238 if (chain_protos[i].num == proto)
239 return chain_protos[i].name;
240
241 return NULL;
242}
243
244static void
245in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
246{
247 memcpy(dst, src, sizeof(struct in6_addr));
248}
249
250void
251exit_error(enum exittype status, char *msg, ...)
252{
253 va_list args;
254
255 va_start(args, msg);
256 fprintf(stderr, "%s v%s: ", program_name, program_version);
257 vfprintf(stderr, msg, args);
258 va_end(args);
259 fprintf(stderr, "\n");
260 if (status == PARAMETER_PROBLEM)
261 exit_tryhelp(status);
262 if (status == VERSION_PROBLEM)
263 fprintf(stderr,
264 "Perhaps iptables or your kernel needs to be upgraded.\n");
265 exit(status);
266}
267
268void
269exit_tryhelp(int status)
270{
271 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
272 program_name, program_name );
273 exit(status);
274}
275
276void
277exit_printhelp(void)
278{
279 struct ip6tables_match *m = NULL;
280 struct ip6tables_target *t = NULL;
281
282 printf("%s v%s\n\n"
283"Usage: %s -[ADC] chain rule-specification [options]\n"
284" %s -[RI] chain rulenum rule-specification [options]\n"
285" %s -D chain rulenum [options]\n"
286" %s -[LFZ] [chain] [options]\n"
287" %s -[NX] chain\n"
288" %s -E old-chain-name new-chain-name\n"
289" %s -P chain target [options]\n"
290" %s -h (print this help information)\n\n",
291 program_name, program_version, program_name, program_name,
292 program_name, program_name, program_name, program_name,
293 program_name, program_name);
294
295 printf(
296"Commands:\n"
297"Either long or short options are allowed.\n"
298" --append -A chain Append to chain\n"
299" --delete -D chain Delete matching rule from chain\n"
300" --delete -D chain rulenum\n"
301" Delete rule rulenum (1 = first) from chain\n"
302" --insert -I chain [rulenum]\n"
303" Insert in chain as rulenum (default 1=first)\n"
304" --replace -R chain rulenum\n"
305" Replace rule rulenum (1 = first) in chain\n"
306" --list -L [chain] List the rules in a chain or all chains\n"
307" --flush -F [chain] Delete all rules in chain or all chains\n"
308" --zero -Z [chain] Zero counters in chain or all chains\n"
309" --check -C chain Test this packet on chain\n"
310" --new -N chain Create a new user-defined chain\n"
311" --delete-chain\n"
312" -X [chain] Delete a user-defined chain\n"
313" --policy -P chain target\n"
314" Change policy on chain to target\n"
315" --rename-chain\n"
316" -E old-chain new-chain\n"
317" Change chain name, (moving any references)\n"
318
319"Options:\n"
320" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
321" --source -s [!] address[/mask]\n"
322" source specification\n"
323" --destination -d [!] address[/mask]\n"
324" destination specification\n"
325" --in-interface -i [!] input name[+]\n"
326" network interface name ([+] for wildcard)\n"
327" --jump -j target\n"
328" target for rule\n"
329" --numeric -n numeric output of addresses and ports\n"
330" --out-interface -o [!] output name[+]\n"
331" network interface name ([+] for wildcard)\n"
332" --table -t table table to manipulate (default: `filter')\n"
333" --verbose -v verbose mode\n"
334" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000335/*"[!] --fragment -f match second or further fragments only\n"*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000336"[!] --version -V print package version.\n");
337
338 /* Print out any special helps. A user might like to be able to add a --help
339 to the commandline, and see expected results. So we call help for all
340 matches & targets */
341 for (t=ip6tables_targets;t;t=t->next) {
342 printf("\n");
343 t->help();
344 }
345 for (m=ip6tables_matches;m;m=m->next) {
346 printf("\n");
347 m->help();
348 }
349 exit(0);
350}
351
352static void
353generic_opt_check(int command, int options)
354{
355 int i, j, legal = 0;
356
357 /* Check that commands are valid with options. Complicated by the
358 * fact that if an option is legal with *any* command given, it is
359 * legal overall (ie. -z and -l).
360 */
361 for (i = 0; i < NUMBER_OF_OPT; i++) {
362 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
363
364 for (j = 0; j < NUMBER_OF_CMD; j++) {
365 if (!(command & (1<<j)))
366 continue;
367
368 if (!(options & (1<<i))) {
369 if (commands_v_options[j][i] == '+')
370 exit_error(PARAMETER_PROBLEM,
371 "You need to supply the `-%c' "
372 "option for this command\n",
373 optflags[i]);
374 } else {
375 if (commands_v_options[j][i] != 'x')
376 legal = 1;
377 else if (legal == 0)
378 legal = -1;
379 }
380 }
381 if (legal == -1)
382 exit_error(PARAMETER_PROBLEM,
383 "Illegal option `-%c' with this command\n",
384 optflags[i]);
385 }
386}
387
388static char
389opt2char(int option)
390{
391 const char *ptr;
392 for (ptr = optflags; option > 1; option >>= 1, ptr++);
393
394 return *ptr;
395}
396
397static char
398cmd2char(int option)
399{
400 const char *ptr;
401 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
402
403 return *ptr;
404}
405
406static void
407add_command(int *cmd, const int newcmd, const int othercmds, int invert)
408{
409 if (invert)
410 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
411 if (*cmd & (~othercmds))
412 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
413 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
414 *cmd |= newcmd;
415}
416
417int
418check_inverse(const char option[], int *invert)
419{
420 if (option && strcmp(option, "!") == 0) {
421 if (*invert)
422 exit_error(PARAMETER_PROBLEM,
423 "Multiple `!' flags not allowed");
424
425 *invert = TRUE;
426 return TRUE;
427 }
428 return FALSE;
429}
430
431static void *
432fw_calloc(size_t count, size_t size)
433{
434 void *p;
435
436 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000437 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000438 exit(1);
439 }
440 return p;
441}
442
443static void *
444fw_malloc(size_t size)
445{
446 void *p;
447
448 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000449 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000450 exit(1);
451 }
452 return p;
453}
454
Rusty Russell5eed48a2000-06-02 20:12:24 +0000455static char *
456addr_to_numeric(const struct in6_addr *addrp)
457{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000458 /* 0000:0000:0000:0000:0000:000.000.000.000
459 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000460 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000461 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000462}
463
464static struct in6_addr *
465numeric_to_addr(const char *num)
466{
467 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000468 int err;
469 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000471#ifdef DEBUG
472 fprintf(stderr, "\nnumeric2addr: %d\n", err);
473#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000474 return (struct in6_addr *)NULL;
475}
476
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000477
478static struct in6_addr *
479host_to_addr(const char *name, unsigned int *naddr)
480{
481 struct addrinfo hints;
482 struct addrinfo *res;
483 static struct in6_addr *addr;
484 int err;
485
486 memset(&hints, 0, sizeof(hints));
487 hints.ai_flags=AI_CANONNAME;
488 hints.ai_family=AF_INET6;
489 hints.ai_socktype=SOCK_RAW;
490 hints.ai_protocol=41;
491 hints.ai_next=NULL;
492
493 *naddr = 0;
494 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
495#ifdef DEBUG
496 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
497#endif
498 return (struct in6_addr *) NULL;
499 } else {
500 if (res->ai_family != AF_INET6 ||
501 res->ai_addrlen != sizeof(struct sockaddr_in6))
502 return (struct in6_addr *) NULL;
503
504#ifdef DEBUG
505 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
506 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
507#endif
508 /* Get the first element of the address-chain */
509 addr = fw_calloc(1, sizeof(struct in6_addr));
510 in6addrcpy(addr, (struct in6_addr *)
511 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
512 freeaddrinfo(res);
513 *naddr = 1;
514 return addr;
515 }
516
517 return (struct in6_addr *) NULL;
518}
519
520/* XXX: the getnameinfo() returns "ai_family not supported".
521 * it can be a glibc error. */
522static char *
523addr_to_host(const struct in6_addr *addr)
524{
525 struct sockaddr_in6 saddr;
526 int err;
527 static char hostname[NI_MAXHOST];
528
529 memset(&saddr, 0, sizeof(struct sockaddr_in6));
530 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000531 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000532
533 if ( (err=getnameinfo((struct sockaddr *)&saddr,
534 sizeof(struct sockaddr_in6),
535 hostname, sizeof(hostname)-1,
536 NULL, 0, 0)) != 0 ){
537#ifdef DEBUG
538 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
539#endif
540 return (char *) NULL;
541 } else {
542#ifdef DEBUG
543 fprintf (stderr, "\naddr2host: %s\n", hostname);
544#endif
545
546 return hostname;
547 }
548
549 return (char *) NULL;
550}
551
Rusty Russell5eed48a2000-06-02 20:12:24 +0000552static char *
553mask_to_numeric(const struct in6_addr *addrp)
554{
555 static char buf[20];
556 int l = ipv6_prefix_length(addrp);
557 if (l == -1)
558 return addr_to_numeric(addrp);
559 sprintf(buf, "%d", l);
560 return buf;
561}
562
563static struct in6_addr *
564network_to_addr(const char *name)
565{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000566 /* abort();*/
567 /* TODO: not implemented yet, but the exception breaks the
568 * name resolvation */
569 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000570}
571
572static char *
573addr_to_anyname(const struct in6_addr *addr)
574{
575 char *name;
576
577 if ((name = addr_to_host(addr)) != NULL)
578 return name;
579
580 return addr_to_numeric(addr);
581}
582
583/*
584 * All functions starting with "parse" should succeed, otherwise
585 * the program fails.
586 * Most routines return pointers to static data that may change
587 * between calls to the same or other routines with a few exceptions:
588 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
589 * return global static data.
590*/
591
592static struct in6_addr *
593parse_hostnetwork(const char *name, unsigned int *naddrs)
594{
595 struct in6_addr *addrp, *addrptmp;
596
597 if ((addrptmp = numeric_to_addr(name)) != NULL ||
598 (addrptmp = network_to_addr(name)) != NULL) {
599 addrp = fw_malloc(sizeof(struct in6_addr));
600 in6addrcpy(addrp, addrptmp);
601 *naddrs = 1;
602 return addrp;
603 }
604 if ((addrp = host_to_addr(name, naddrs)) != NULL)
605 return addrp;
606
607 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
608}
609
610static struct in6_addr *
611parse_mask(char *mask)
612{
613 static struct in6_addr maskaddr;
614 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000615 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000616
617 if (mask == NULL) {
618 /* no mask at all defaults to 128 bits */
619 memset(&maskaddr, 0xff, sizeof maskaddr);
620 return &maskaddr;
621 }
622 if ((addrp = numeric_to_addr(mask)) != NULL)
623 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000624 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000625 exit_error(PARAMETER_PROBLEM,
626 "invalid mask `%s' specified", mask);
627 if (bits != 0) {
628 char *p = (char *)&maskaddr;
629 memset(p, 0xff, bits / 8);
630 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
631 p[bits / 8] = 0xff << (8 - (bits & 7));
632 return &maskaddr;
633 }
634
635 memset(&maskaddr, 0, sizeof maskaddr);
636 return &maskaddr;
637}
638
639static void
640parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
641 struct in6_addr *maskp, unsigned int *naddrs)
642{
643 struct in6_addr *addrp;
644 char buf[256];
645 char *p;
646 int i, j, n;
647
648 strncpy(buf, name, sizeof(buf) - 1);
649 if ((p = strrchr(buf, '/')) != NULL) {
650 *p = '\0';
651 addrp = parse_mask(p + 1);
652 } else
653 addrp = parse_mask(NULL);
654 in6addrcpy(maskp, addrp);
655
656 /* if a null mask is given, the name is ignored, like in "any/0" */
657 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
658 strcpy(buf, "::");
659
660 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
661 n = *naddrs;
662 for (i = 0, j = 0; i < n; i++) {
663 int k;
664 for (k = 0; k < 4; k++)
665 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
666 j++;
667 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000668 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000669 (*naddrs)--;
670 j--;
671 break;
672 }
673 }
674 }
675}
676
677struct ip6tables_match *
678find_match(const char *name, enum ip6t_tryload tryload)
679{
680 struct ip6tables_match *ptr;
681
682 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
683 if (strcmp(name, ptr->name) == 0)
684 break;
685 }
686
Harald Welte3efb6ea2001-08-06 18:50:21 +0000687#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000688 if (!ptr && tryload != DONT_LOAD) {
689 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
690 + strlen(name)];
691 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
692 if (dlopen(path, RTLD_NOW)) {
693 /* Found library. If it didn't register itself,
694 maybe they specified target as match. */
695 ptr = find_match(name, DONT_LOAD);
696
697 if (!ptr)
698 exit_error(PARAMETER_PROBLEM,
699 "Couldn't load match `%s'\n",
700 name);
701 } else if (tryload == LOAD_MUST_SUCCEED)
702 exit_error(PARAMETER_PROBLEM,
703 "Couldn't load match `%s'\n", name);
704 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000705#else
706 if (ptr && !ptr->loaded) {
707 if (tryload != DONT_LOAD)
708 ptr->loaded = 1;
709 else
710 ptr = NULL;
711 }
712#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000713
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000714 if (ptr)
715 ptr->used = 1;
716
717
Rusty Russell5eed48a2000-06-02 20:12:24 +0000718 return ptr;
719}
720
721/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
722static struct ip6tables_match *
723find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
724{
Harald Welteed498492001-07-23 01:24:22 +0000725 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000726
Harald Welteed498492001-07-23 01:24:22 +0000727 if (string_to_number(pname, 0, 255, &proto) != -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 return find_match(proto_to_name(proto, nolookup), tryload);
729
730 return find_match(pname, tryload);
731}
732
733static u_int16_t
734parse_protocol(const char *s)
735{
Harald Welteed498492001-07-23 01:24:22 +0000736 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000737
Harald Welteed498492001-07-23 01:24:22 +0000738 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000739 struct protoent *pent;
740
741 if ((pent = getprotobyname(s)))
742 proto = pent->p_proto;
743 else {
744 unsigned int i;
745 for (i = 0;
746 i < sizeof(chain_protos)/sizeof(struct pprot);
747 i++) {
748 if (strcmp(s, chain_protos[i].name) == 0) {
749 proto = chain_protos[i].num;
750 break;
751 }
752 }
753 if (i == sizeof(chain_protos)/sizeof(struct pprot))
754 exit_error(PARAMETER_PROBLEM,
755 "unknown protocol `%s' specified",
756 s);
757 }
758 }
759
760 return (u_int16_t)proto;
761}
762
763static void
764parse_interface(const char *arg, char *vianame, unsigned char *mask)
765{
766 int vialen = strlen(arg);
767 unsigned int i;
768
769 memset(mask, 0, IFNAMSIZ);
770 memset(vianame, 0, IFNAMSIZ);
771
772 if (vialen + 1 > IFNAMSIZ)
773 exit_error(PARAMETER_PROBLEM,
774 "interface name `%s' must be shorter than IFNAMSIZ"
775 " (%i)", arg, IFNAMSIZ-1);
776
777 strcpy(vianame, arg);
778 if (vialen == 0)
779 memset(mask, 0, IFNAMSIZ);
780 else if (vianame[vialen - 1] == '+') {
781 memset(mask, 0xFF, vialen - 1);
782 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
783 /* Remove `+' */
784 vianame[vialen - 1] = '\0';
785 } else {
786 /* Include nul-terminator in match */
787 memset(mask, 0xFF, vialen + 1);
788 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
789 }
790 for (i = 0; vianame[i]; i++) {
Harald Welte6a4542a2001-05-12 04:38:31 +0000791 if (!isalnum(vianame[i]) && vianame[i] != '_') {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000792 printf("Warning: wierd character in interface"
793 " `%s' (No aliases, :, ! or *).\n",
794 vianame);
795 break;
796 }
797 }
798}
799
800/* Can't be zero. */
801static int
802parse_rulenumber(const char *rule)
803{
Harald Welteed498492001-07-23 01:24:22 +0000804 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000805
Harald Welteed498492001-07-23 01:24:22 +0000806 if (string_to_number(rule, 1, INT_MAX, &rulenum))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000807 exit_error(PARAMETER_PROBLEM,
808 "Invalid rule number `%s'", rule);
809
810 return rulenum;
811}
812
813static const char *
814parse_target(const char *targetname)
815{
816 const char *ptr;
817
818 if (strlen(targetname) < 1)
819 exit_error(PARAMETER_PROBLEM,
820 "Invalid target name (too short)");
821
822 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
823 exit_error(PARAMETER_PROBLEM,
824 "Invalid target name `%s' (%i chars max)",
825 targetname, sizeof(ip6t_chainlabel)-1);
826
827 for (ptr = targetname; *ptr; ptr++)
828 if (isspace(*ptr))
829 exit_error(PARAMETER_PROBLEM,
830 "Invalid target name `%s'", targetname);
831 return targetname;
832}
833
834int
Harald Welteed498492001-07-23 01:24:22 +0000835string_to_number(const char *s, unsigned int min, unsigned int max,
836 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000837{
Harald Welteed498492001-07-23 01:24:22 +0000838 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000839 char *end;
840
841 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000842 errno = 0;
843 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844 if (*end == '\0' && end != s) {
845 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000846 if (errno != ERANGE && min <= number && number <= max) {
847 *ret = number;
848 return 0;
849 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000850 }
851 return -1;
852}
853
854static void
855set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
856 int invert)
857{
858 if (*options & option)
859 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
860 opt2char(option));
861 *options |= option;
862
863 if (invert) {
864 unsigned int i;
865 for (i = 0; 1 << i != option; i++);
866
867 if (!inverse_for_options[i])
868 exit_error(PARAMETER_PROBLEM,
869 "cannot have ! before -%c",
870 opt2char(option));
871 *invflg |= inverse_for_options[i];
872 }
873}
874
875struct ip6tables_target *
876find_target(const char *name, enum ip6t_tryload tryload)
877{
878 struct ip6tables_target *ptr;
879
880 /* Standard target? */
881 if (strcmp(name, "") == 0
882 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
883 || strcmp(name, IP6TC_LABEL_DROP) == 0
884 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
885 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
886 name = "standard";
887
888 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
889 if (strcmp(name, ptr->name) == 0)
890 break;
891 }
892
Harald Welte3efb6ea2001-08-06 18:50:21 +0000893#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000894 if (!ptr && tryload != DONT_LOAD) {
895 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
896 + strlen(name)];
897 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
898 if (dlopen(path, RTLD_NOW)) {
899 /* Found library. If it didn't register itself,
900 maybe they specified match as a target. */
901 ptr = find_target(name, DONT_LOAD);
902 if (!ptr)
903 exit_error(PARAMETER_PROBLEM,
904 "Couldn't load target `%s'\n",
905 name);
906 } else if (tryload == LOAD_MUST_SUCCEED)
907 exit_error(PARAMETER_PROBLEM,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000908 "Couldn't load target `%s'%s\n",
909 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000910 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000911#else
912 if (ptr && !ptr->loaded) {
913 if (tryload != DONT_LOAD)
914 ptr->loaded = 1;
915 else
916 ptr = NULL;
917 }
918#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000919
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000920 if (ptr)
921 ptr->used = 1;
922
Rusty Russell5eed48a2000-06-02 20:12:24 +0000923 return ptr;
924}
925
926static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000927merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000928 unsigned int *option_offset)
929{
930 unsigned int num_old, num_new, i;
931 struct option *merge;
932
933 for (num_old = 0; oldopts[num_old].name; num_old++);
934 for (num_new = 0; newopts[num_new].name; num_new++);
935
936 global_option_offset += OPTION_OFFSET;
937 *option_offset = global_option_offset;
938
939 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
940 memcpy(merge, oldopts, num_old * sizeof(struct option));
941 for (i = 0; i < num_new; i++) {
942 merge[num_old + i] = newopts[i];
943 merge[num_old + i].val += *option_offset;
944 }
945 memset(merge + num_old + num_new, 0, sizeof(struct option));
946
947 return merge;
948}
949
950void
951register_match6(struct ip6tables_match *me)
952{
953 if (strcmp(me->version, program_version) != 0) {
954 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
955 program_name, me->name, me->version, program_version);
956 exit(1);
957 }
958
959 if (find_match(me->name, DONT_LOAD)) {
960 fprintf(stderr, "%s: match `%s' already registered.\n",
961 program_name, me->name);
962 exit(1);
963 }
964
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000965 if (me->size != IP6T_ALIGN(me->size)) {
966 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
967 program_name, me->name, me->size);
968 exit(1);
969 }
970
Rusty Russell5eed48a2000-06-02 20:12:24 +0000971 /* Prepend to list. */
972 me->next = ip6tables_matches;
973 ip6tables_matches = me;
974 me->m = NULL;
975 me->mflags = 0;
976
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000977 /* opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000978}
979
980void
981register_target6(struct ip6tables_target *me)
982{
983 if (strcmp(me->version, program_version) != 0) {
984 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
985 program_name, me->name, me->version, program_version);
986 exit(1);
987 }
988
989 if (find_target(me->name, DONT_LOAD)) {
990 fprintf(stderr, "%s: target `%s' already registered.\n",
991 program_name, me->name);
992 exit(1);
993 }
994
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000995 if (me->size != IP6T_ALIGN(me->size)) {
996 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
997 program_name, me->name, me->size);
998 exit(1);
999 }
1000
Rusty Russell5eed48a2000-06-02 20:12:24 +00001001 /* Prepend to list. */
1002 me->next = ip6tables_targets;
1003 ip6tables_targets = me;
1004 me->t = NULL;
1005 me->tflags = 0;
1006
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001007 /*opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
1008}
1009
1010static void
1011print_num(u_int64_t number, unsigned int format)
1012{
1013 if (format & FMT_KILOMEGAGIGA) {
1014 if (number > 99999) {
1015 number = (number + 500) / 1000;
1016 if (number > 9999) {
1017 number = (number + 500) / 1000;
1018 if (number > 9999) {
1019 number = (number + 500) / 1000;
1020 printf(FMT("%4lluG ","%lluG "),number);
1021 }
1022 else printf(FMT("%4lluM ","%lluM "), number);
1023 } else
1024 printf(FMT("%4lluK ","%lluK "), number);
1025 } else
1026 printf(FMT("%5llu ","%llu "), number);
1027 } else
1028 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001029}
1030
1031static void
1032print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1033{
1034 struct ip6t_counters counters;
1035 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1036 printf("Chain %s", chain);
1037 if (pol) {
1038 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001039 if (!(format & FMT_NOCOUNTS)) {
1040 /*printf(" %llu packets, %llu bytes",
1041 counters.pcnt, counters.bcnt);*/
1042 fputc(' ', stdout);
1043 print_num(counters.pcnt, (format|FMT_NOTABLE));
1044 fputs("packets, ", stdout);
1045 print_num(counters.bcnt, (format|FMT_NOTABLE));
1046 fputs("bytes", stdout);
1047 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 printf(")\n");
1049 } else {
1050 unsigned int refs;
1051 if (!ip6tc_get_references(&refs, chain, handle))
1052 printf(" (ERROR obtaining refs)\n");
1053 else
1054 printf(" (%u references)\n", refs);
1055 }
1056
1057 if (format & FMT_LINENUMBERS)
1058 printf(FMT("%-4s ", "%s "), "num");
1059 if (!(format & FMT_NOCOUNTS)) {
1060 if (format & FMT_KILOMEGAGIGA) {
1061 printf(FMT("%5s ","%s "), "pkts");
1062 printf(FMT("%5s ","%s "), "bytes");
1063 } else {
1064 printf(FMT("%8s ","%s "), "pkts");
1065 printf(FMT("%10s ","%s "), "bytes");
1066 }
1067 }
1068 if (!(format & FMT_NOTARGET))
1069 printf(FMT("%-9s ","%s "), "target");
1070 fputs(" prot ", stdout);
1071 if (format & FMT_OPTIONS)
1072 fputs("opt", stdout);
1073 if (format & FMT_VIA) {
1074 printf(FMT(" %-6s ","%s "), "in");
1075 printf(FMT("%-6s ","%s "), "out");
1076 }
1077 printf(FMT(" %-19s ","%s "), "source");
1078 printf(FMT(" %-19s "," %s "), "destination");
1079 printf("\n");
1080}
1081
Rusty Russell5eed48a2000-06-02 20:12:24 +00001082static int
1083print_match(const struct ip6t_entry_match *m,
1084 const struct ip6t_ip6 *ip,
1085 int numeric)
1086{
1087 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1088
1089 if (match) {
1090 if (match->print)
1091 match->print(ip, m, numeric);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001092 else
1093 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001094 } else {
1095 if (m->u.user.name[0])
1096 printf("UNKNOWN match `%s' ", m->u.user.name);
1097 }
1098 /* Don't stop iterating. */
1099 return 0;
1100}
1101
1102/* e is called `fw' here for hysterical raisins */
1103static void
1104print_firewall(const struct ip6t_entry *fw,
1105 const char *targname,
1106 unsigned int num,
1107 unsigned int format,
1108 const ip6tc_handle_t handle)
1109{
1110 struct ip6tables_target *target = NULL;
1111 const struct ip6t_entry_target *t;
1112 u_int8_t flags;
1113 char buf[BUFSIZ];
1114
1115 /* User creates a chain called "REJECT": this overrides the
1116 `REJECT' target module. Keep feeding them rope until the
1117 revolution... Bwahahahahah */
1118 if (!ip6tc_is_chain(targname, handle))
1119 target = find_target(targname, TRY_LOAD);
1120 else
1121 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1122
1123 t = ip6t_get_target((struct ip6t_entry *)fw);
1124 flags = fw->ipv6.flags;
1125
1126 if (format & FMT_LINENUMBERS)
1127 printf(FMT("%-4u ", "%u "), num+1);
1128
1129 if (!(format & FMT_NOCOUNTS)) {
1130 print_num(fw->counters.pcnt, format);
1131 print_num(fw->counters.bcnt, format);
1132 }
1133
1134 if (!(format & FMT_NOTARGET))
1135 printf(FMT("%-9s ", "%s "), targname);
1136
1137 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1138 {
1139 char *pname = proto_to_name(fw->ipv6.proto,
1140 format&FMT_NUMERIC);
1141 if (pname)
1142 printf(FMT("%-5s", "%s "), pname);
1143 else
1144 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1145 }
1146
1147 if (format & FMT_OPTIONS) {
1148 if (format & FMT_NOTABLE)
1149 fputs("opt ", stdout);
1150 fputc(' ', stdout);
1151 fputc(' ', stdout);
1152 fputc(' ', stdout);
1153 }
1154
1155 if (format & FMT_VIA) {
1156 char iface[IFNAMSIZ+2];
1157
1158 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1159 iface[0] = '!';
1160 iface[1] = '\0';
1161 }
1162 else iface[0] = '\0';
1163
1164 if (fw->ipv6.iniface[0] != '\0') {
1165 strcat(iface, fw->ipv6.iniface);
1166 /* If it doesn't compare the nul-term, it's a
1167 wildcard. */
1168 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1169 strcat(iface, "+");
1170 }
1171 else if (format & FMT_NUMERIC) strcat(iface, "*");
1172 else strcat(iface, "any");
1173 printf(FMT(" %-6s ","in %s "), iface);
1174
1175 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1176 iface[0] = '!';
1177 iface[1] = '\0';
1178 }
1179 else iface[0] = '\0';
1180
1181 if (fw->ipv6.outiface[0] != '\0') {
1182 strcat(iface, fw->ipv6.outiface);
1183 /* If it doesn't compare the nul-term, it's a
1184 wildcard. */
1185 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1186 strcat(iface, "+");
1187 }
1188 else if (format & FMT_NUMERIC) strcat(iface, "*");
1189 else strcat(iface, "any");
1190 printf(FMT("%-6s ","out %s "), iface);
1191 }
1192
1193 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1194 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1195 && !(format & FMT_NUMERIC))
1196 printf(FMT("%-19s ","%s "), "anywhere");
1197 else {
1198 if (format & FMT_NUMERIC)
1199 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1200 else
1201 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1202 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1203 printf(FMT("%-19s ","%s "), buf);
1204 }
1205
1206 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1207 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1208 && !(format & FMT_NUMERIC))
1209 printf(FMT("%-19s","-> %s"), "anywhere");
1210 else {
1211 if (format & FMT_NUMERIC)
1212 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1213 else
1214 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1215 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1216 printf(FMT("%-19s","-> %s"), buf);
1217 }
1218
1219 if (format & FMT_NOTABLE)
1220 fputs(" ", stdout);
1221
1222 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1223
1224 if (target) {
1225 if (target->print)
1226 /* Print the target information. */
1227 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1228 } else if (t->u.target_size != sizeof(*t))
1229 printf("[%u bytes of unknown target data] ",
1230 t->u.target_size - sizeof(*t));
1231
1232 if (!(format & FMT_NONEWLINE))
1233 fputc('\n', stdout);
1234}
1235
1236static void
1237print_firewall_line(const struct ip6t_entry *fw,
1238 const ip6tc_handle_t h)
1239{
1240 struct ip6t_entry_target *t;
1241
1242 t = ip6t_get_target((struct ip6t_entry *)fw);
1243 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1244}
1245
1246static int
1247append_entry(const ip6t_chainlabel chain,
1248 struct ip6t_entry *fw,
1249 unsigned int nsaddrs,
1250 const struct in6_addr saddrs[],
1251 unsigned int ndaddrs,
1252 const struct in6_addr daddrs[],
1253 int verbose,
1254 ip6tc_handle_t *handle)
1255{
1256 unsigned int i, j;
1257 int ret = 1;
1258
1259 for (i = 0; i < nsaddrs; i++) {
1260 fw->ipv6.src = saddrs[i];
1261 for (j = 0; j < ndaddrs; j++) {
1262 fw->ipv6.dst = daddrs[j];
1263 if (verbose)
1264 print_firewall_line(fw, *handle);
1265 ret &= ip6tc_append_entry(chain, fw, handle);
1266 }
1267 }
1268
1269 return ret;
1270}
1271
1272static int
1273replace_entry(const ip6t_chainlabel chain,
1274 struct ip6t_entry *fw,
1275 unsigned int rulenum,
1276 const struct in6_addr *saddr,
1277 const struct in6_addr *daddr,
1278 int verbose,
1279 ip6tc_handle_t *handle)
1280{
1281 fw->ipv6.src = *saddr;
1282 fw->ipv6.dst = *daddr;
1283
1284 if (verbose)
1285 print_firewall_line(fw, *handle);
1286 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1287}
1288
1289static int
1290insert_entry(const ip6t_chainlabel chain,
1291 struct ip6t_entry *fw,
1292 unsigned int rulenum,
1293 unsigned int nsaddrs,
1294 const struct in6_addr saddrs[],
1295 unsigned int ndaddrs,
1296 const struct in6_addr daddrs[],
1297 int verbose,
1298 ip6tc_handle_t *handle)
1299{
1300 unsigned int i, j;
1301 int ret = 1;
1302
1303 for (i = 0; i < nsaddrs; i++) {
1304 fw->ipv6.src = saddrs[i];
1305 for (j = 0; j < ndaddrs; j++) {
1306 fw->ipv6.dst = daddrs[j];
1307 if (verbose)
1308 print_firewall_line(fw, *handle);
1309 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1310 }
1311 }
1312
1313 return ret;
1314}
1315
1316static unsigned char *
1317make_delete_mask(struct ip6t_entry *fw)
1318{
1319 /* Establish mask for comparison */
1320 unsigned int size;
1321 struct ip6tables_match *m;
1322 unsigned char *mask, *mptr;
1323
1324 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001325 for (m = ip6tables_matches; m; m = m->next) {
1326 if (!m->used)
1327 continue;
1328
1329 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1330 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001331
1332 mask = fw_calloc(1, size
1333 + sizeof(struct ip6t_entry_target)
1334 + ip6tables_targets->size);
1335
1336 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1337 mptr = mask + sizeof(struct ip6t_entry);
1338
1339 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001340 if (!m->used)
1341 continue;
1342
Rusty Russell5eed48a2000-06-02 20:12:24 +00001343 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001344 IP6T_ALIGN(sizeof(struct ip6t_entry_match)) +
1345 m->userspacesize);
1346 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001347 }
1348
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001349 memset(mptr, 0xFF,
1350 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1351 + ip6tables_targets->userspacesize);
1352 /*mptr += sizeof(struct ip6t_entry_target);
1353 memset(mptr, 0xFF, ip6tables_targets->userspacesize);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +00001354
1355 return mask;
1356}
1357
1358static int
1359delete_entry(const ip6t_chainlabel chain,
1360 struct ip6t_entry *fw,
1361 unsigned int nsaddrs,
1362 const struct in6_addr saddrs[],
1363 unsigned int ndaddrs,
1364 const struct in6_addr daddrs[],
1365 int verbose,
1366 ip6tc_handle_t *handle)
1367{
1368 unsigned int i, j;
1369 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001370 unsigned char *mask;
1371
1372 mask = make_delete_mask(fw);
1373 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001374 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001375 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001376 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001377 if (verbose)
1378 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001379 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001380 }
1381 }
1382 return ret;
1383}
1384
1385static int
1386check_packet(const ip6t_chainlabel chain,
1387 struct ip6t_entry *fw,
1388 unsigned int nsaddrs,
1389 const struct in6_addr saddrs[],
1390 unsigned int ndaddrs,
1391 const struct in6_addr daddrs[],
1392 int verbose,
1393 ip6tc_handle_t *handle)
1394{
1395 int ret = 1;
1396 unsigned int i, j;
1397 struct ip6t_entry ipfw = *fw;
1398 const char *msg;
1399
1400 for (i = 0; i < nsaddrs; i++) {
1401 ipfw.ipv6.src = saddrs[i];
1402 for (j = 0; j < ndaddrs; j++) {
1403 ipfw.ipv6.dst = daddrs[j];
1404 if (verbose)
1405 print_firewall_line(fw, *handle);
1406 msg = ip6tc_check_packet(chain, &ipfw, handle);
1407 if (!msg) ret = 0;
1408 else printf("%s\n", msg);
1409 }
1410 }
1411
1412 return ret;
1413}
1414
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001415/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001416int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001417for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1418 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1419{
1420 int ret = 1;
1421 const char *chain;
1422 char *chains;
1423 unsigned int i, chaincount = 0;
1424
1425 chain = ip6tc_first_chain(handle);
1426 while (chain) {
1427 chaincount++;
1428 chain = ip6tc_next_chain(handle);
1429 }
1430
1431 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1432 i = 0;
1433 chain = ip6tc_first_chain(handle);
1434 while (chain) {
1435 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1436 i++;
1437 chain = ip6tc_next_chain(handle);
1438 }
1439
1440 for (i = 0; i < chaincount; i++) {
1441 if (!builtinstoo
1442 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1443 *handle))
1444 continue;
1445 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1446 }
1447
1448 free(chains);
1449 return ret;
1450}
1451
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001452/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001453int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001454flush_entries(const ip6t_chainlabel chain, int verbose,
1455 ip6tc_handle_t *handle)
1456{
1457 if (!chain)
1458 return for_each_chain(flush_entries, verbose, 1, handle);
1459
1460 if (verbose)
1461 fprintf(stdout, "Flushing chain `%s'\n", chain);
1462 return ip6tc_flush_entries(chain, handle);
1463}
1464
1465static int
1466zero_entries(const ip6t_chainlabel chain, int verbose,
1467 ip6tc_handle_t *handle)
1468{
1469 if (!chain)
1470 return for_each_chain(zero_entries, verbose, 1, handle);
1471
1472 if (verbose)
1473 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1474 return ip6tc_zero_entries(chain, handle);
1475}
1476
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001477/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001478int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001479delete_chain(const ip6t_chainlabel chain, int verbose,
1480 ip6tc_handle_t *handle)
1481{
1482 if (!chain)
1483 return for_each_chain(delete_chain, verbose, 0, handle);
1484
1485 if (verbose)
1486 fprintf(stdout, "Deleting chain `%s'\n", chain);
1487 return ip6tc_delete_chain(chain, handle);
1488}
1489
1490static int
1491list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1492 int expanded, int linenumbers, ip6tc_handle_t *handle)
1493{
1494 int found = 0;
1495 unsigned int format;
1496 const char *this;
1497
1498 format = FMT_OPTIONS;
1499 if (!verbose)
1500 format |= FMT_NOCOUNTS;
1501 else
1502 format |= FMT_VIA;
1503
1504 if (numeric)
1505 format |= FMT_NUMERIC;
1506
1507 if (!expanded)
1508 format |= FMT_KILOMEGAGIGA;
1509
1510 if (linenumbers)
1511 format |= FMT_LINENUMBERS;
1512
1513 for (this = ip6tc_first_chain(handle);
1514 this;
1515 this = ip6tc_next_chain(handle)) {
1516 const struct ip6t_entry *i;
1517 unsigned int num;
1518
1519 if (chain && strcmp(chain, this) != 0)
1520 continue;
1521
1522 if (found) printf("\n");
1523
1524 print_header(format, this, handle);
1525 i = ip6tc_first_rule(this, handle);
1526
1527 num = 0;
1528 while (i) {
1529 print_firewall(i,
1530 ip6tc_get_target(i, handle),
1531 num++,
1532 format,
1533 *handle);
1534 i = ip6tc_next_rule(i, handle);
1535 }
1536 found = 1;
1537 }
1538
1539 errno = ENOENT;
1540 return found;
1541}
1542
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001543static char *get_modprobe(void)
1544{
1545 int procfile;
1546 char *ret;
1547
1548 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1549 if (procfile < 0)
1550 return NULL;
1551
1552 ret = malloc(1024);
1553 if (ret) {
1554 switch (read(procfile, ret, 1024)) {
1555 case -1: goto fail;
1556 case 1024: goto fail; /* Partial read. Wierd */
1557 }
1558 if (ret[strlen(ret)-1]=='\n')
1559 ret[strlen(ret)-1]=0;
1560 close(procfile);
1561 return ret;
1562 }
1563 fail:
1564 free(ret);
1565 close(procfile);
1566 return NULL;
1567}
1568
Harald Welte58918652001-06-16 18:25:25 +00001569int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001570{
1571 char *buf = NULL;
1572 char *argv[3];
1573
1574 /* If they don't explicitly set it, read out of kernel */
1575 if (!modprobe) {
1576 buf = get_modprobe();
1577 if (!buf)
1578 return -1;
1579 modprobe = buf;
1580 }
1581
1582 switch (fork()) {
1583 case 0:
1584 argv[0] = (char *)modprobe;
1585 argv[1] = (char *)modname;
1586 argv[2] = NULL;
1587 execv(argv[0], argv);
1588
1589 /* not usually reached */
1590 exit(0);
1591 case -1:
1592 return -1;
1593
1594 default: /* parent */
1595 wait(NULL);
1596 }
1597
1598 free(buf);
1599 return 0;
1600}
1601
Rusty Russell5eed48a2000-06-02 20:12:24 +00001602static struct ip6t_entry *
1603generate_entry(const struct ip6t_entry *fw,
1604 struct ip6tables_match *matches,
1605 struct ip6t_entry_target *target)
1606{
1607 unsigned int size;
1608 struct ip6tables_match *m;
1609 struct ip6t_entry *e;
1610
1611 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001612 for (m = matches; m; m = m->next) {
1613 if (!m->used)
1614 continue;
1615
Rusty Russell5eed48a2000-06-02 20:12:24 +00001616 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001617 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001618
1619 e = fw_malloc(size + target->u.target_size);
1620 *e = *fw;
1621 e->target_offset = size;
1622 e->next_offset = size + target->u.target_size;
1623
1624 size = 0;
1625 for (m = matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001626 if (!m->used)
1627 continue;
1628
Rusty Russell5eed48a2000-06-02 20:12:24 +00001629 memcpy(e->elems + size, m->m, m->m->u.match_size);
1630 size += m->m->u.match_size;
1631 }
1632 memcpy(e->elems + size, target, target->u.target_size);
1633
1634 return e;
1635}
1636
1637int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1638{
1639 struct ip6t_entry fw, *e = NULL;
1640 int invert = 0;
1641 unsigned int nsaddrs = 0, ndaddrs = 0;
1642 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1643
1644 int c, verbose = 0;
1645 const char *chain = NULL;
1646 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1647 const char *policy = NULL, *newname = NULL;
1648 unsigned int rulenum = 0, options = 0, command = 0;
1649 int ret = 1;
1650 struct ip6tables_match *m;
1651 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001652 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001653 const char *jumpto = "";
1654 char *protocol = NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001655 const char *modprobe = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001656
1657 memset(&fw, 0, sizeof(fw));
1658
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001659 opts = original_opts;
1660 global_option_offset = 0;
1661
1662 /* re-set optind to 0 in case do_command gets called
1663 * a second time */
1664 optind = 0;
1665
1666 /* clear mflags in case do_command gets called a second time
1667 * (we clear the global list of all matches for security)*/
1668 for (m = ip6tables_matches; m; m = m->next) {
1669 m->mflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001670 m->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001671 }
1672
1673 for (t = ip6tables_targets; t; t = t->next) {
1674 t->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001675 t->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001676 }
1677
Rusty Russell5eed48a2000-06-02 20:12:24 +00001678 /* Suppress error messages: we may add new options if we
1679 demand-load a protocol. */
1680 opterr = 0;
1681
1682 while ((c = getopt_long(argc, argv,
1683 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1684 opts, NULL)) != -1) {
1685 switch (c) {
1686 /*
1687 * Command selection
1688 */
1689 case 'A':
1690 add_command(&command, CMD_APPEND, CMD_NONE,
1691 invert);
1692 chain = optarg;
1693 break;
1694
1695 case 'D':
1696 add_command(&command, CMD_DELETE, CMD_NONE,
1697 invert);
1698 chain = optarg;
1699 if (optind < argc && argv[optind][0] != '-'
1700 && argv[optind][0] != '!') {
1701 rulenum = parse_rulenumber(argv[optind++]);
1702 command = CMD_DELETE_NUM;
1703 }
1704 break;
1705
1706 case 'C':
1707 add_command(&command, CMD_CHECK, CMD_NONE,
1708 invert);
1709 chain = optarg;
1710 break;
1711
1712 case 'R':
1713 add_command(&command, CMD_REPLACE, CMD_NONE,
1714 invert);
1715 chain = optarg;
1716 if (optind < argc && argv[optind][0] != '-'
1717 && argv[optind][0] != '!')
1718 rulenum = parse_rulenumber(argv[optind++]);
1719 else
1720 exit_error(PARAMETER_PROBLEM,
1721 "-%c requires a rule number",
1722 cmd2char(CMD_REPLACE));
1723 break;
1724
1725 case 'I':
1726 add_command(&command, CMD_INSERT, 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 else rulenum = 1;
1733 break;
1734
1735 case 'L':
1736 add_command(&command, CMD_LIST, CMD_ZERO,
1737 invert);
1738 if (optarg) chain = optarg;
1739 else if (optind < argc && argv[optind][0] != '-'
1740 && argv[optind][0] != '!')
1741 chain = argv[optind++];
1742 break;
1743
1744 case 'F':
1745 add_command(&command, CMD_FLUSH, CMD_NONE,
1746 invert);
1747 if (optarg) chain = optarg;
1748 else if (optind < argc && argv[optind][0] != '-'
1749 && argv[optind][0] != '!')
1750 chain = argv[optind++];
1751 break;
1752
1753 case 'Z':
1754 add_command(&command, CMD_ZERO, CMD_LIST,
1755 invert);
1756 if (optarg) chain = optarg;
1757 else if (optind < argc && argv[optind][0] != '-'
1758 && argv[optind][0] != '!')
1759 chain = argv[optind++];
1760 break;
1761
1762 case 'N':
1763 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1764 invert);
1765 chain = optarg;
1766 break;
1767
1768 case 'X':
1769 add_command(&command, CMD_DELETE_CHAIN, 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 'E':
1778 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1779 invert);
1780 chain = optarg;
1781 if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001784 else
1785 exit_error(PARAMETER_PROBLEM,
1786 "-%c requires old-chain-name and "
1787 "new-chain-name",
1788 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001789 break;
1790
1791 case 'P':
1792 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1793 invert);
1794 chain = optarg;
1795 if (optind < argc && argv[optind][0] != '-'
1796 && argv[optind][0] != '!')
1797 policy = argv[optind++];
1798 else
1799 exit_error(PARAMETER_PROBLEM,
1800 "-%c requires a chain and a policy",
1801 cmd2char(CMD_SET_POLICY));
1802 break;
1803
1804 case 'h':
1805 if (!optarg)
1806 optarg = argv[optind];
1807
1808 /* iptables -p icmp -h */
1809 if (!ip6tables_matches && protocol)
1810 find_match(protocol, TRY_LOAD);
1811
1812 exit_printhelp();
1813
1814 /*
1815 * Option selection
1816 */
1817 case 'p':
1818 if (check_inverse(optarg, &invert))
1819 optind++;
1820 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1821 invert);
1822
1823 /* Canonicalize into lower case */
1824 for (protocol = argv[optind-1]; *protocol; protocol++)
1825 *protocol = tolower(*protocol);
1826
1827 protocol = argv[optind-1];
1828 fw.ipv6.proto = parse_protocol(protocol);
1829 fw.ipv6.flags |= IP6T_F_PROTO;
1830
1831 if (fw.ipv6.proto == 0
1832 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1833 exit_error(PARAMETER_PROBLEM,
1834 "rule would never match protocol");
1835 fw.nfcache |= NFC_IP6_PROTO;
1836 break;
1837
1838 case 's':
1839 if (check_inverse(optarg, &invert))
1840 optind++;
1841 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1842 invert);
1843 shostnetworkmask = argv[optind-1];
1844 fw.nfcache |= NFC_IP6_SRC;
1845 break;
1846
1847 case 'd':
1848 if (check_inverse(optarg, &invert))
1849 optind++;
1850 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1851 invert);
1852 dhostnetworkmask = argv[optind-1];
1853 fw.nfcache |= NFC_IP6_DST;
1854 break;
1855
1856 case 'j':
1857 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1858 invert);
1859 jumpto = parse_target(optarg);
1860 target = find_target(jumpto, TRY_LOAD);
1861
1862 if (target) {
1863 size_t size;
1864
1865 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1866 + target->size);
1867
1868 target->t = fw_calloc(1, size);
1869 target->t->u.target_size = size;
1870 strcpy(target->t->u.user.name, jumpto);
1871 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001872 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001873 }
1874 break;
1875
1876
1877 case 'i':
1878 if (check_inverse(optarg, &invert))
1879 optind++;
1880 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1881 invert);
1882 parse_interface(argv[optind-1],
1883 fw.ipv6.iniface,
1884 fw.ipv6.iniface_mask);
1885 fw.nfcache |= NFC_IP6_IF_IN;
1886 break;
1887
1888 case 'o':
1889 if (check_inverse(optarg, &invert))
1890 optind++;
1891 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1892 invert);
1893 parse_interface(argv[optind-1],
1894 fw.ipv6.outiface,
1895 fw.ipv6.outiface_mask);
1896 fw.nfcache |= NFC_IP6_IF_OUT;
1897 break;
1898
1899 case 'v':
1900 if (!verbose)
1901 set_option(&options, OPT_VERBOSE,
1902 &fw.ipv6.invflags, invert);
1903 verbose++;
1904 break;
1905
1906 case 'm': {
1907 size_t size;
1908
1909 if (invert)
1910 exit_error(PARAMETER_PROBLEM,
1911 "unexpected ! flag before --match");
1912
1913 m = find_match(optarg, LOAD_MUST_SUCCEED);
1914 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1915 + m->size);
1916 m->m = fw_calloc(1, size);
1917 m->m->u.match_size = size;
1918 strcpy(m->m->u.user.name, m->name);
1919 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001920 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001921 }
1922 break;
1923
1924 case 'n':
1925 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1926 invert);
1927 break;
1928
1929 case 't':
1930 if (invert)
1931 exit_error(PARAMETER_PROBLEM,
1932 "unexpected ! flag before --table");
1933 *table = argv[optind-1];
1934 break;
1935
1936 case 'x':
1937 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1938 invert);
1939 break;
1940
1941 case 'V':
1942 if (invert)
1943 printf("Not %s ;-)\n", program_version);
1944 else
1945 printf("%s v%s\n",
1946 program_name, program_version);
1947 exit(0);
1948
1949 case '0':
1950 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1951 invert);
1952 break;
1953
1954 case 1: /* non option */
1955 if (optarg[0] == '!' && optarg[1] == '\0') {
1956 if (invert)
1957 exit_error(PARAMETER_PROBLEM,
1958 "multiple consecutive ! not"
1959 " allowed");
1960 invert = TRUE;
1961 optarg[0] = '\0';
1962 continue;
1963 }
1964 printf("Bad argument `%s'\n", optarg);
1965 exit_tryhelp(2);
1966
1967 default:
1968 /* FIXME: This scheme doesn't allow two of the same
1969 matches --RR */
1970 if (!target
1971 || !(target->parse(c - target->option_offset,
1972 argv, invert,
1973 &target->tflags,
1974 &fw, &target->t))) {
1975 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001976 if (!m->used)
1977 continue;
1978
Rusty Russell5eed48a2000-06-02 20:12:24 +00001979 if (m->parse(c - m->option_offset,
1980 argv, invert,
1981 &m->mflags,
1982 &fw,
1983 &fw.nfcache,
1984 &m->m))
1985 break;
1986 }
1987
1988 /* If you listen carefully, you can
1989 actually hear this code suck. */
1990 if (m == NULL
1991 && protocol
1992 && !find_proto(protocol, DONT_LOAD,
1993 options&OPT_NUMERIC)
1994 && (m = find_proto(protocol, TRY_LOAD,
1995 options&OPT_NUMERIC))) {
1996 /* Try loading protocol */
1997 size_t size;
1998
1999 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
2000 + m->size);
2001
2002 m->m = fw_calloc(1, size);
2003 m->m->u.match_size = size;
2004 strcpy(m->m->u.user.name, m->name);
2005 m->init(m->m, &fw.nfcache);
2006
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002007 opts = merge_options(opts,
2008 m->extra_opts, &m->option_offset);
2009
Rusty Russell5eed48a2000-06-02 20:12:24 +00002010 optind--;
2011 continue;
2012 }
2013 if (!m)
2014 exit_error(PARAMETER_PROBLEM,
2015 "Unknown arg `%s'",
2016 argv[optind-1]);
2017 }
2018 }
2019 invert = FALSE;
2020 }
2021
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002022 for (m = ip6tables_matches; m; m = m->next) {
2023 if (!m->used)
2024 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002025
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002026 m->final_check(m->mflags);
2027 }
2028
2029 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002030 target->final_check(target->tflags);
2031
2032 /* Fix me: must put inverse options checking here --MN */
2033
2034 if (optind < argc)
2035 exit_error(PARAMETER_PROBLEM,
2036 "unknown arguments found on commandline");
2037 if (!command)
2038 exit_error(PARAMETER_PROBLEM, "no command specified");
2039 if (invert)
2040 exit_error(PARAMETER_PROBLEM,
2041 "nothing appropriate following !");
2042
2043 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2044 CMD_CHECK)) {
2045 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002046 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002047 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002048 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 }
2050
2051 if (shostnetworkmask)
2052 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2053 &(fw.ipv6.smsk), &nsaddrs);
2054
2055 if (dhostnetworkmask)
2056 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2057 &(fw.ipv6.dmsk), &ndaddrs);
2058
2059 if ((nsaddrs > 1 || ndaddrs > 1) &&
2060 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2061 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2062 " source or destination IP addresses");
2063
2064 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2065 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2066 cmd2char(CMD_CHECK));
2067
2068 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2069 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2070 "specify a unique address");
2071
2072 generic_opt_check(command, options);
2073
2074 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2075 exit_error(PARAMETER_PROBLEM,
2076 "chain name `%s' too long (must be under %i chars)",
2077 chain, IP6T_FUNCTION_MAXNAMELEN);
2078
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002079 /* only allocate handle if we weren't called with a handle */
2080 if (!*handle)
2081 *handle = ip6tc_init(*table);
2082
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002083 if (!*handle) {
2084 /* try to insmod the module if iptc_init failed */
2085 ip6tables_insmod("ip6_tables", modprobe);
2086 *handle = ip6tc_init(*table);
2087 }
2088
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002089 if (!*handle)
2090 exit_error(VERSION_PROBLEM,
2091 "can't initialize ip6tables table `%s': %s",
2092 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002093
2094 if (command == CMD_CHECK
2095 || command == CMD_APPEND
2096 || command == CMD_DELETE
2097 || command == CMD_INSERT
2098 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002099 if (strcmp(chain, "PREROUTING") == 0
2100 || strcmp(chain, "INPUT") == 0) {
2101 /* -o not valid with incoming packets. */
2102 if (options & OPT_VIANAMEOUT)
2103 exit_error(PARAMETER_PROBLEM,
2104 "Can't use -%c with %s\n",
2105 opt2char(OPT_VIANAMEOUT),
2106 chain);
2107 /* -i required with -C */
2108 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2109 exit_error(PARAMETER_PROBLEM,
2110 "Need -%c with %s\n",
2111 opt2char(OPT_VIANAMEIN),
2112 chain);
2113 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002114
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002115 if (strcmp(chain, "POSTROUTING") == 0
2116 || strcmp(chain, "OUTPUT") == 0) {
2117 /* -i not valid with outgoing packets */
2118 if (options & OPT_VIANAMEIN)
2119 exit_error(PARAMETER_PROBLEM,
2120 "Can't use -%c with %s\n",
2121 opt2char(OPT_VIANAMEIN),
2122 chain);
2123 /* -o required with -C */
2124 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2125 exit_error(PARAMETER_PROBLEM,
2126 "Need -%c with %s\n",
2127 opt2char(OPT_VIANAMEOUT),
2128 chain);
2129 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002130
2131 if (target && ip6tc_is_chain(jumpto, *handle)) {
2132 printf("Warning: using chain %s, not extension\n",
2133 jumpto);
2134
2135 target = NULL;
2136 }
2137
2138 /* If they didn't specify a target, or it's a chain
2139 name, use standard. */
2140 if (!target
2141 && (strlen(jumpto) == 0
2142 || ip6tc_is_chain(jumpto, *handle))) {
2143 size_t size;
2144
2145 target = find_target(IP6T_STANDARD_TARGET,
2146 LOAD_MUST_SUCCEED);
2147
2148 size = sizeof(struct ip6t_entry_target)
2149 + target->size;
2150 target->t = fw_calloc(1, size);
2151 target->t->u.target_size = size;
2152 strcpy(target->t->u.user.name, jumpto);
2153 target->init(target->t, &fw.nfcache);
2154 }
2155
2156 if (!target) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002157#if 0
Rusty Russell5eed48a2000-06-02 20:12:24 +00002158 struct ip6t_entry_target unknown_target;
2159
2160 /* Don't know it. Must be extension with no
2161 options? */
2162 unknown_target.u.target_size = sizeof(unknown_target);
2163 strcpy(unknown_target.u.user.name, jumpto);
2164
2165 e = generate_entry(&fw, ip6tables_matches,
2166 &unknown_target);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002167#endif
2168 /* it is no chain, and we can't load a plugin.
2169 * We cannot know if the plugin is corrupt, non
2170 * existant OR if the user just misspelled a
2171 * chain. */
2172 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002173 } else {
2174 e = generate_entry(&fw, ip6tables_matches, target->t);
2175 }
2176 }
2177
2178 switch (command) {
2179 case CMD_APPEND:
2180 ret = append_entry(chain, e,
2181 nsaddrs, saddrs, ndaddrs, daddrs,
2182 options&OPT_VERBOSE,
2183 handle);
2184 break;
2185 case CMD_CHECK:
2186 ret = check_packet(chain, e,
2187 nsaddrs, saddrs, ndaddrs, daddrs,
2188 options&OPT_VERBOSE, handle);
2189 break;
2190 case CMD_DELETE:
2191 ret = delete_entry(chain, e,
2192 nsaddrs, saddrs, ndaddrs, daddrs,
2193 options&OPT_VERBOSE,
2194 handle);
2195 break;
2196 case CMD_DELETE_NUM:
2197 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2198 break;
2199 case CMD_REPLACE:
2200 ret = replace_entry(chain, e, rulenum - 1,
2201 saddrs, daddrs, options&OPT_VERBOSE,
2202 handle);
2203 break;
2204 case CMD_INSERT:
2205 ret = insert_entry(chain, e, rulenum - 1,
2206 nsaddrs, saddrs, ndaddrs, daddrs,
2207 options&OPT_VERBOSE,
2208 handle);
2209 break;
2210 case CMD_LIST:
2211 ret = list_entries(chain,
2212 options&OPT_VERBOSE,
2213 options&OPT_NUMERIC,
2214 options&OPT_EXPANDED,
2215 options&OPT_LINENUMBERS,
2216 handle);
2217 break;
2218 case CMD_FLUSH:
2219 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2220 break;
2221 case CMD_ZERO:
2222 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2223 break;
2224 case CMD_LIST|CMD_ZERO:
2225 ret = list_entries(chain,
2226 options&OPT_VERBOSE,
2227 options&OPT_NUMERIC,
2228 options&OPT_EXPANDED,
2229 options&OPT_LINENUMBERS,
2230 handle);
2231 if (ret)
2232 ret = zero_entries(chain,
2233 options&OPT_VERBOSE, handle);
2234 break;
2235 case CMD_NEW_CHAIN:
2236 ret = ip6tc_create_chain(chain, handle);
2237 break;
2238 case CMD_DELETE_CHAIN:
2239 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2240 break;
2241 case CMD_RENAME_CHAIN:
2242 ret = ip6tc_rename_chain(chain, newname, handle);
2243 break;
2244 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002245 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002246 break;
2247 default:
2248 /* We should never reach this... */
2249 exit_tryhelp(2);
2250 }
2251
2252 if (verbose > 1)
2253 dump_entries6(*handle);
2254
2255 return ret;
2256}