blob: c69dd73e70ac6cb2db61784a41f3d1dc658647f8 [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;
615 int bits;
616
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;
624 if ((bits = string_to_number(mask, 0, 128)) == -1)
625 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
687 if (!ptr && tryload != DONT_LOAD) {
688 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
689 + strlen(name)];
690 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
691 if (dlopen(path, RTLD_NOW)) {
692 /* Found library. If it didn't register itself,
693 maybe they specified target as match. */
694 ptr = find_match(name, DONT_LOAD);
695
696 if (!ptr)
697 exit_error(PARAMETER_PROBLEM,
698 "Couldn't load match `%s'\n",
699 name);
700 } else if (tryload == LOAD_MUST_SUCCEED)
701 exit_error(PARAMETER_PROBLEM,
702 "Couldn't load match `%s'\n", name);
703 }
704
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000705 if (ptr)
706 ptr->used = 1;
707
708
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709 return ptr;
710}
711
712/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
713static struct ip6tables_match *
714find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
715{
716 int proto;
717
718 proto = string_to_number(pname, 0, 255);
719 if (proto != -1)
720 return find_match(proto_to_name(proto, nolookup), tryload);
721
722 return find_match(pname, tryload);
723}
724
725static u_int16_t
726parse_protocol(const char *s)
727{
728 int proto = string_to_number(s, 0, 255);
729
730 if (proto == -1) {
731 struct protoent *pent;
732
733 if ((pent = getprotobyname(s)))
734 proto = pent->p_proto;
735 else {
736 unsigned int i;
737 for (i = 0;
738 i < sizeof(chain_protos)/sizeof(struct pprot);
739 i++) {
740 if (strcmp(s, chain_protos[i].name) == 0) {
741 proto = chain_protos[i].num;
742 break;
743 }
744 }
745 if (i == sizeof(chain_protos)/sizeof(struct pprot))
746 exit_error(PARAMETER_PROBLEM,
747 "unknown protocol `%s' specified",
748 s);
749 }
750 }
751
752 return (u_int16_t)proto;
753}
754
755static void
756parse_interface(const char *arg, char *vianame, unsigned char *mask)
757{
758 int vialen = strlen(arg);
759 unsigned int i;
760
761 memset(mask, 0, IFNAMSIZ);
762 memset(vianame, 0, IFNAMSIZ);
763
764 if (vialen + 1 > IFNAMSIZ)
765 exit_error(PARAMETER_PROBLEM,
766 "interface name `%s' must be shorter than IFNAMSIZ"
767 " (%i)", arg, IFNAMSIZ-1);
768
769 strcpy(vianame, arg);
770 if (vialen == 0)
771 memset(mask, 0, IFNAMSIZ);
772 else if (vianame[vialen - 1] == '+') {
773 memset(mask, 0xFF, vialen - 1);
774 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
775 /* Remove `+' */
776 vianame[vialen - 1] = '\0';
777 } else {
778 /* Include nul-terminator in match */
779 memset(mask, 0xFF, vialen + 1);
780 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
781 }
782 for (i = 0; vianame[i]; i++) {
Harald Welte6a4542a2001-05-12 04:38:31 +0000783 if (!isalnum(vianame[i]) && vianame[i] != '_') {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000784 printf("Warning: wierd character in interface"
785 " `%s' (No aliases, :, ! or *).\n",
786 vianame);
787 break;
788 }
789 }
790}
791
792/* Can't be zero. */
793static int
794parse_rulenumber(const char *rule)
795{
796 int rulenum = string_to_number(rule, 1, INT_MAX);
797
798 if (rulenum == -1)
799 exit_error(PARAMETER_PROBLEM,
800 "Invalid rule number `%s'", rule);
801
802 return rulenum;
803}
804
805static const char *
806parse_target(const char *targetname)
807{
808 const char *ptr;
809
810 if (strlen(targetname) < 1)
811 exit_error(PARAMETER_PROBLEM,
812 "Invalid target name (too short)");
813
814 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
815 exit_error(PARAMETER_PROBLEM,
816 "Invalid target name `%s' (%i chars max)",
817 targetname, sizeof(ip6t_chainlabel)-1);
818
819 for (ptr = targetname; *ptr; ptr++)
820 if (isspace(*ptr))
821 exit_error(PARAMETER_PROBLEM,
822 "Invalid target name `%s'", targetname);
823 return targetname;
824}
825
826int
827string_to_number(const char *s, int min, int max)
828{
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000829 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000830 char *end;
831
832 /* Handle hex, octal, etc. */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000833 errno = 0;
834 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000835 if (*end == '\0' && end != s) {
836 /* we parsed a number, let's see if we want this */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000837 if (errno != ERANGE && min <= number && number <= max)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000838 return number;
839 }
840 return -1;
841}
842
843static void
844set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
845 int invert)
846{
847 if (*options & option)
848 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
849 opt2char(option));
850 *options |= option;
851
852 if (invert) {
853 unsigned int i;
854 for (i = 0; 1 << i != option; i++);
855
856 if (!inverse_for_options[i])
857 exit_error(PARAMETER_PROBLEM,
858 "cannot have ! before -%c",
859 opt2char(option));
860 *invflg |= inverse_for_options[i];
861 }
862}
863
864struct ip6tables_target *
865find_target(const char *name, enum ip6t_tryload tryload)
866{
867 struct ip6tables_target *ptr;
868
869 /* Standard target? */
870 if (strcmp(name, "") == 0
871 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
872 || strcmp(name, IP6TC_LABEL_DROP) == 0
873 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
874 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
875 name = "standard";
876
877 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
878 if (strcmp(name, ptr->name) == 0)
879 break;
880 }
881
882 if (!ptr && tryload != DONT_LOAD) {
883 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
884 + strlen(name)];
885 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
886 if (dlopen(path, RTLD_NOW)) {
887 /* Found library. If it didn't register itself,
888 maybe they specified match as a target. */
889 ptr = find_target(name, DONT_LOAD);
890 if (!ptr)
891 exit_error(PARAMETER_PROBLEM,
892 "Couldn't load target `%s'\n",
893 name);
894 } else if (tryload == LOAD_MUST_SUCCEED)
895 exit_error(PARAMETER_PROBLEM,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000896 "Couldn't load target `%s'%s\n",
897 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000898 }
899
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000900 if (ptr)
901 ptr->used = 1;
902
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 return ptr;
904}
905
906static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000907merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 unsigned int *option_offset)
909{
910 unsigned int num_old, num_new, i;
911 struct option *merge;
912
913 for (num_old = 0; oldopts[num_old].name; num_old++);
914 for (num_new = 0; newopts[num_new].name; num_new++);
915
916 global_option_offset += OPTION_OFFSET;
917 *option_offset = global_option_offset;
918
919 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
920 memcpy(merge, oldopts, num_old * sizeof(struct option));
921 for (i = 0; i < num_new; i++) {
922 merge[num_old + i] = newopts[i];
923 merge[num_old + i].val += *option_offset;
924 }
925 memset(merge + num_old + num_new, 0, sizeof(struct option));
926
927 return merge;
928}
929
930void
931register_match6(struct ip6tables_match *me)
932{
933 if (strcmp(me->version, program_version) != 0) {
934 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
935 program_name, me->name, me->version, program_version);
936 exit(1);
937 }
938
939 if (find_match(me->name, DONT_LOAD)) {
940 fprintf(stderr, "%s: match `%s' already registered.\n",
941 program_name, me->name);
942 exit(1);
943 }
944
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000945 if (me->size != IP6T_ALIGN(me->size)) {
946 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
947 program_name, me->name, me->size);
948 exit(1);
949 }
950
Rusty Russell5eed48a2000-06-02 20:12:24 +0000951 /* Prepend to list. */
952 me->next = ip6tables_matches;
953 ip6tables_matches = me;
954 me->m = NULL;
955 me->mflags = 0;
956
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000957 /* opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000958}
959
960void
961register_target6(struct ip6tables_target *me)
962{
963 if (strcmp(me->version, program_version) != 0) {
964 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
965 program_name, me->name, me->version, program_version);
966 exit(1);
967 }
968
969 if (find_target(me->name, DONT_LOAD)) {
970 fprintf(stderr, "%s: target `%s' already registered.\n",
971 program_name, me->name);
972 exit(1);
973 }
974
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000975 if (me->size != IP6T_ALIGN(me->size)) {
976 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
977 program_name, me->name, me->size);
978 exit(1);
979 }
980
Rusty Russell5eed48a2000-06-02 20:12:24 +0000981 /* Prepend to list. */
982 me->next = ip6tables_targets;
983 ip6tables_targets = me;
984 me->t = NULL;
985 me->tflags = 0;
986
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000987 /*opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
988}
989
990static void
991print_num(u_int64_t number, unsigned int format)
992{
993 if (format & FMT_KILOMEGAGIGA) {
994 if (number > 99999) {
995 number = (number + 500) / 1000;
996 if (number > 9999) {
997 number = (number + 500) / 1000;
998 if (number > 9999) {
999 number = (number + 500) / 1000;
1000 printf(FMT("%4lluG ","%lluG "),number);
1001 }
1002 else printf(FMT("%4lluM ","%lluM "), number);
1003 } else
1004 printf(FMT("%4lluK ","%lluK "), number);
1005 } else
1006 printf(FMT("%5llu ","%llu "), number);
1007 } else
1008 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001009}
1010
1011static void
1012print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1013{
1014 struct ip6t_counters counters;
1015 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1016 printf("Chain %s", chain);
1017 if (pol) {
1018 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001019 if (!(format & FMT_NOCOUNTS)) {
1020 /*printf(" %llu packets, %llu bytes",
1021 counters.pcnt, counters.bcnt);*/
1022 fputc(' ', stdout);
1023 print_num(counters.pcnt, (format|FMT_NOTABLE));
1024 fputs("packets, ", stdout);
1025 print_num(counters.bcnt, (format|FMT_NOTABLE));
1026 fputs("bytes", stdout);
1027 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001028 printf(")\n");
1029 } else {
1030 unsigned int refs;
1031 if (!ip6tc_get_references(&refs, chain, handle))
1032 printf(" (ERROR obtaining refs)\n");
1033 else
1034 printf(" (%u references)\n", refs);
1035 }
1036
1037 if (format & FMT_LINENUMBERS)
1038 printf(FMT("%-4s ", "%s "), "num");
1039 if (!(format & FMT_NOCOUNTS)) {
1040 if (format & FMT_KILOMEGAGIGA) {
1041 printf(FMT("%5s ","%s "), "pkts");
1042 printf(FMT("%5s ","%s "), "bytes");
1043 } else {
1044 printf(FMT("%8s ","%s "), "pkts");
1045 printf(FMT("%10s ","%s "), "bytes");
1046 }
1047 }
1048 if (!(format & FMT_NOTARGET))
1049 printf(FMT("%-9s ","%s "), "target");
1050 fputs(" prot ", stdout);
1051 if (format & FMT_OPTIONS)
1052 fputs("opt", stdout);
1053 if (format & FMT_VIA) {
1054 printf(FMT(" %-6s ","%s "), "in");
1055 printf(FMT("%-6s ","%s "), "out");
1056 }
1057 printf(FMT(" %-19s ","%s "), "source");
1058 printf(FMT(" %-19s "," %s "), "destination");
1059 printf("\n");
1060}
1061
Rusty Russell5eed48a2000-06-02 20:12:24 +00001062static int
1063print_match(const struct ip6t_entry_match *m,
1064 const struct ip6t_ip6 *ip,
1065 int numeric)
1066{
1067 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1068
1069 if (match) {
1070 if (match->print)
1071 match->print(ip, m, numeric);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001072 else
1073 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001074 } else {
1075 if (m->u.user.name[0])
1076 printf("UNKNOWN match `%s' ", m->u.user.name);
1077 }
1078 /* Don't stop iterating. */
1079 return 0;
1080}
1081
1082/* e is called `fw' here for hysterical raisins */
1083static void
1084print_firewall(const struct ip6t_entry *fw,
1085 const char *targname,
1086 unsigned int num,
1087 unsigned int format,
1088 const ip6tc_handle_t handle)
1089{
1090 struct ip6tables_target *target = NULL;
1091 const struct ip6t_entry_target *t;
1092 u_int8_t flags;
1093 char buf[BUFSIZ];
1094
1095 /* User creates a chain called "REJECT": this overrides the
1096 `REJECT' target module. Keep feeding them rope until the
1097 revolution... Bwahahahahah */
1098 if (!ip6tc_is_chain(targname, handle))
1099 target = find_target(targname, TRY_LOAD);
1100 else
1101 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1102
1103 t = ip6t_get_target((struct ip6t_entry *)fw);
1104 flags = fw->ipv6.flags;
1105
1106 if (format & FMT_LINENUMBERS)
1107 printf(FMT("%-4u ", "%u "), num+1);
1108
1109 if (!(format & FMT_NOCOUNTS)) {
1110 print_num(fw->counters.pcnt, format);
1111 print_num(fw->counters.bcnt, format);
1112 }
1113
1114 if (!(format & FMT_NOTARGET))
1115 printf(FMT("%-9s ", "%s "), targname);
1116
1117 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1118 {
1119 char *pname = proto_to_name(fw->ipv6.proto,
1120 format&FMT_NUMERIC);
1121 if (pname)
1122 printf(FMT("%-5s", "%s "), pname);
1123 else
1124 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1125 }
1126
1127 if (format & FMT_OPTIONS) {
1128 if (format & FMT_NOTABLE)
1129 fputs("opt ", stdout);
1130 fputc(' ', stdout);
1131 fputc(' ', stdout);
1132 fputc(' ', stdout);
1133 }
1134
1135 if (format & FMT_VIA) {
1136 char iface[IFNAMSIZ+2];
1137
1138 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1139 iface[0] = '!';
1140 iface[1] = '\0';
1141 }
1142 else iface[0] = '\0';
1143
1144 if (fw->ipv6.iniface[0] != '\0') {
1145 strcat(iface, fw->ipv6.iniface);
1146 /* If it doesn't compare the nul-term, it's a
1147 wildcard. */
1148 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1149 strcat(iface, "+");
1150 }
1151 else if (format & FMT_NUMERIC) strcat(iface, "*");
1152 else strcat(iface, "any");
1153 printf(FMT(" %-6s ","in %s "), iface);
1154
1155 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1156 iface[0] = '!';
1157 iface[1] = '\0';
1158 }
1159 else iface[0] = '\0';
1160
1161 if (fw->ipv6.outiface[0] != '\0') {
1162 strcat(iface, fw->ipv6.outiface);
1163 /* If it doesn't compare the nul-term, it's a
1164 wildcard. */
1165 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1166 strcat(iface, "+");
1167 }
1168 else if (format & FMT_NUMERIC) strcat(iface, "*");
1169 else strcat(iface, "any");
1170 printf(FMT("%-6s ","out %s "), iface);
1171 }
1172
1173 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1174 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1175 && !(format & FMT_NUMERIC))
1176 printf(FMT("%-19s ","%s "), "anywhere");
1177 else {
1178 if (format & FMT_NUMERIC)
1179 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1180 else
1181 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1182 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1183 printf(FMT("%-19s ","%s "), buf);
1184 }
1185
1186 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1187 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1188 && !(format & FMT_NUMERIC))
1189 printf(FMT("%-19s","-> %s"), "anywhere");
1190 else {
1191 if (format & FMT_NUMERIC)
1192 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1193 else
1194 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1195 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1196 printf(FMT("%-19s","-> %s"), buf);
1197 }
1198
1199 if (format & FMT_NOTABLE)
1200 fputs(" ", stdout);
1201
1202 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1203
1204 if (target) {
1205 if (target->print)
1206 /* Print the target information. */
1207 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1208 } else if (t->u.target_size != sizeof(*t))
1209 printf("[%u bytes of unknown target data] ",
1210 t->u.target_size - sizeof(*t));
1211
1212 if (!(format & FMT_NONEWLINE))
1213 fputc('\n', stdout);
1214}
1215
1216static void
1217print_firewall_line(const struct ip6t_entry *fw,
1218 const ip6tc_handle_t h)
1219{
1220 struct ip6t_entry_target *t;
1221
1222 t = ip6t_get_target((struct ip6t_entry *)fw);
1223 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1224}
1225
1226static int
1227append_entry(const ip6t_chainlabel chain,
1228 struct ip6t_entry *fw,
1229 unsigned int nsaddrs,
1230 const struct in6_addr saddrs[],
1231 unsigned int ndaddrs,
1232 const struct in6_addr daddrs[],
1233 int verbose,
1234 ip6tc_handle_t *handle)
1235{
1236 unsigned int i, j;
1237 int ret = 1;
1238
1239 for (i = 0; i < nsaddrs; i++) {
1240 fw->ipv6.src = saddrs[i];
1241 for (j = 0; j < ndaddrs; j++) {
1242 fw->ipv6.dst = daddrs[j];
1243 if (verbose)
1244 print_firewall_line(fw, *handle);
1245 ret &= ip6tc_append_entry(chain, fw, handle);
1246 }
1247 }
1248
1249 return ret;
1250}
1251
1252static int
1253replace_entry(const ip6t_chainlabel chain,
1254 struct ip6t_entry *fw,
1255 unsigned int rulenum,
1256 const struct in6_addr *saddr,
1257 const struct in6_addr *daddr,
1258 int verbose,
1259 ip6tc_handle_t *handle)
1260{
1261 fw->ipv6.src = *saddr;
1262 fw->ipv6.dst = *daddr;
1263
1264 if (verbose)
1265 print_firewall_line(fw, *handle);
1266 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1267}
1268
1269static int
1270insert_entry(const ip6t_chainlabel chain,
1271 struct ip6t_entry *fw,
1272 unsigned int rulenum,
1273 unsigned int nsaddrs,
1274 const struct in6_addr saddrs[],
1275 unsigned int ndaddrs,
1276 const struct in6_addr daddrs[],
1277 int verbose,
1278 ip6tc_handle_t *handle)
1279{
1280 unsigned int i, j;
1281 int ret = 1;
1282
1283 for (i = 0; i < nsaddrs; i++) {
1284 fw->ipv6.src = saddrs[i];
1285 for (j = 0; j < ndaddrs; j++) {
1286 fw->ipv6.dst = daddrs[j];
1287 if (verbose)
1288 print_firewall_line(fw, *handle);
1289 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1290 }
1291 }
1292
1293 return ret;
1294}
1295
1296static unsigned char *
1297make_delete_mask(struct ip6t_entry *fw)
1298{
1299 /* Establish mask for comparison */
1300 unsigned int size;
1301 struct ip6tables_match *m;
1302 unsigned char *mask, *mptr;
1303
1304 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001305 for (m = ip6tables_matches; m; m = m->next) {
1306 if (!m->used)
1307 continue;
1308
1309 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1310 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001311
1312 mask = fw_calloc(1, size
1313 + sizeof(struct ip6t_entry_target)
1314 + ip6tables_targets->size);
1315
1316 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1317 mptr = mask + sizeof(struct ip6t_entry);
1318
1319 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001320 if (!m->used)
1321 continue;
1322
Rusty Russell5eed48a2000-06-02 20:12:24 +00001323 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001324 IP6T_ALIGN(sizeof(struct ip6t_entry_match)) +
1325 m->userspacesize);
1326 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001327 }
1328
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001329 memset(mptr, 0xFF,
1330 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1331 + ip6tables_targets->userspacesize);
1332 /*mptr += sizeof(struct ip6t_entry_target);
1333 memset(mptr, 0xFF, ip6tables_targets->userspacesize);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +00001334
1335 return mask;
1336}
1337
1338static int
1339delete_entry(const ip6t_chainlabel chain,
1340 struct ip6t_entry *fw,
1341 unsigned int nsaddrs,
1342 const struct in6_addr saddrs[],
1343 unsigned int ndaddrs,
1344 const struct in6_addr daddrs[],
1345 int verbose,
1346 ip6tc_handle_t *handle)
1347{
1348 unsigned int i, j;
1349 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001350 unsigned char *mask;
1351
1352 mask = make_delete_mask(fw);
1353 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001354 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001355 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001356 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001357 if (verbose)
1358 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001359 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001360 }
1361 }
1362 return ret;
1363}
1364
1365static int
1366check_packet(const ip6t_chainlabel chain,
1367 struct ip6t_entry *fw,
1368 unsigned int nsaddrs,
1369 const struct in6_addr saddrs[],
1370 unsigned int ndaddrs,
1371 const struct in6_addr daddrs[],
1372 int verbose,
1373 ip6tc_handle_t *handle)
1374{
1375 int ret = 1;
1376 unsigned int i, j;
1377 struct ip6t_entry ipfw = *fw;
1378 const char *msg;
1379
1380 for (i = 0; i < nsaddrs; i++) {
1381 ipfw.ipv6.src = saddrs[i];
1382 for (j = 0; j < ndaddrs; j++) {
1383 ipfw.ipv6.dst = daddrs[j];
1384 if (verbose)
1385 print_firewall_line(fw, *handle);
1386 msg = ip6tc_check_packet(chain, &ipfw, handle);
1387 if (!msg) ret = 0;
1388 else printf("%s\n", msg);
1389 }
1390 }
1391
1392 return ret;
1393}
1394
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001395/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001396int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001397for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1398 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1399{
1400 int ret = 1;
1401 const char *chain;
1402 char *chains;
1403 unsigned int i, chaincount = 0;
1404
1405 chain = ip6tc_first_chain(handle);
1406 while (chain) {
1407 chaincount++;
1408 chain = ip6tc_next_chain(handle);
1409 }
1410
1411 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1412 i = 0;
1413 chain = ip6tc_first_chain(handle);
1414 while (chain) {
1415 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1416 i++;
1417 chain = ip6tc_next_chain(handle);
1418 }
1419
1420 for (i = 0; i < chaincount; i++) {
1421 if (!builtinstoo
1422 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1423 *handle))
1424 continue;
1425 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1426 }
1427
1428 free(chains);
1429 return ret;
1430}
1431
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001432/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001433int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001434flush_entries(const ip6t_chainlabel chain, int verbose,
1435 ip6tc_handle_t *handle)
1436{
1437 if (!chain)
1438 return for_each_chain(flush_entries, verbose, 1, handle);
1439
1440 if (verbose)
1441 fprintf(stdout, "Flushing chain `%s'\n", chain);
1442 return ip6tc_flush_entries(chain, handle);
1443}
1444
1445static int
1446zero_entries(const ip6t_chainlabel chain, int verbose,
1447 ip6tc_handle_t *handle)
1448{
1449 if (!chain)
1450 return for_each_chain(zero_entries, verbose, 1, handle);
1451
1452 if (verbose)
1453 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1454 return ip6tc_zero_entries(chain, handle);
1455}
1456
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001457/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001458int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001459delete_chain(const ip6t_chainlabel chain, int verbose,
1460 ip6tc_handle_t *handle)
1461{
1462 if (!chain)
1463 return for_each_chain(delete_chain, verbose, 0, handle);
1464
1465 if (verbose)
1466 fprintf(stdout, "Deleting chain `%s'\n", chain);
1467 return ip6tc_delete_chain(chain, handle);
1468}
1469
1470static int
1471list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1472 int expanded, int linenumbers, ip6tc_handle_t *handle)
1473{
1474 int found = 0;
1475 unsigned int format;
1476 const char *this;
1477
1478 format = FMT_OPTIONS;
1479 if (!verbose)
1480 format |= FMT_NOCOUNTS;
1481 else
1482 format |= FMT_VIA;
1483
1484 if (numeric)
1485 format |= FMT_NUMERIC;
1486
1487 if (!expanded)
1488 format |= FMT_KILOMEGAGIGA;
1489
1490 if (linenumbers)
1491 format |= FMT_LINENUMBERS;
1492
1493 for (this = ip6tc_first_chain(handle);
1494 this;
1495 this = ip6tc_next_chain(handle)) {
1496 const struct ip6t_entry *i;
1497 unsigned int num;
1498
1499 if (chain && strcmp(chain, this) != 0)
1500 continue;
1501
1502 if (found) printf("\n");
1503
1504 print_header(format, this, handle);
1505 i = ip6tc_first_rule(this, handle);
1506
1507 num = 0;
1508 while (i) {
1509 print_firewall(i,
1510 ip6tc_get_target(i, handle),
1511 num++,
1512 format,
1513 *handle);
1514 i = ip6tc_next_rule(i, handle);
1515 }
1516 found = 1;
1517 }
1518
1519 errno = ENOENT;
1520 return found;
1521}
1522
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001523static char *get_modprobe(void)
1524{
1525 int procfile;
1526 char *ret;
1527
1528 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1529 if (procfile < 0)
1530 return NULL;
1531
1532 ret = malloc(1024);
1533 if (ret) {
1534 switch (read(procfile, ret, 1024)) {
1535 case -1: goto fail;
1536 case 1024: goto fail; /* Partial read. Wierd */
1537 }
1538 if (ret[strlen(ret)-1]=='\n')
1539 ret[strlen(ret)-1]=0;
1540 close(procfile);
1541 return ret;
1542 }
1543 fail:
1544 free(ret);
1545 close(procfile);
1546 return NULL;
1547}
1548
Harald Welte58918652001-06-16 18:25:25 +00001549int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001550{
1551 char *buf = NULL;
1552 char *argv[3];
1553
1554 /* If they don't explicitly set it, read out of kernel */
1555 if (!modprobe) {
1556 buf = get_modprobe();
1557 if (!buf)
1558 return -1;
1559 modprobe = buf;
1560 }
1561
1562 switch (fork()) {
1563 case 0:
1564 argv[0] = (char *)modprobe;
1565 argv[1] = (char *)modname;
1566 argv[2] = NULL;
1567 execv(argv[0], argv);
1568
1569 /* not usually reached */
1570 exit(0);
1571 case -1:
1572 return -1;
1573
1574 default: /* parent */
1575 wait(NULL);
1576 }
1577
1578 free(buf);
1579 return 0;
1580}
1581
Rusty Russell5eed48a2000-06-02 20:12:24 +00001582static struct ip6t_entry *
1583generate_entry(const struct ip6t_entry *fw,
1584 struct ip6tables_match *matches,
1585 struct ip6t_entry_target *target)
1586{
1587 unsigned int size;
1588 struct ip6tables_match *m;
1589 struct ip6t_entry *e;
1590
1591 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001592 for (m = matches; m; m = m->next) {
1593 if (!m->used)
1594 continue;
1595
Rusty Russell5eed48a2000-06-02 20:12:24 +00001596 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001597 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001598
1599 e = fw_malloc(size + target->u.target_size);
1600 *e = *fw;
1601 e->target_offset = size;
1602 e->next_offset = size + target->u.target_size;
1603
1604 size = 0;
1605 for (m = matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001606 if (!m->used)
1607 continue;
1608
Rusty Russell5eed48a2000-06-02 20:12:24 +00001609 memcpy(e->elems + size, m->m, m->m->u.match_size);
1610 size += m->m->u.match_size;
1611 }
1612 memcpy(e->elems + size, target, target->u.target_size);
1613
1614 return e;
1615}
1616
1617int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1618{
1619 struct ip6t_entry fw, *e = NULL;
1620 int invert = 0;
1621 unsigned int nsaddrs = 0, ndaddrs = 0;
1622 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1623
1624 int c, verbose = 0;
1625 const char *chain = NULL;
1626 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1627 const char *policy = NULL, *newname = NULL;
1628 unsigned int rulenum = 0, options = 0, command = 0;
1629 int ret = 1;
1630 struct ip6tables_match *m;
1631 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001632 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001633 const char *jumpto = "";
1634 char *protocol = NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001635 const char *modprobe = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001636
1637 memset(&fw, 0, sizeof(fw));
1638
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001639 opts = original_opts;
1640 global_option_offset = 0;
1641
1642 /* re-set optind to 0 in case do_command gets called
1643 * a second time */
1644 optind = 0;
1645
1646 /* clear mflags in case do_command gets called a second time
1647 * (we clear the global list of all matches for security)*/
1648 for (m = ip6tables_matches; m; m = m->next) {
1649 m->mflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001650 m->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001651 }
1652
1653 for (t = ip6tables_targets; t; t = t->next) {
1654 t->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001655 t->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001656 }
1657
Rusty Russell5eed48a2000-06-02 20:12:24 +00001658 /* Suppress error messages: we may add new options if we
1659 demand-load a protocol. */
1660 opterr = 0;
1661
1662 while ((c = getopt_long(argc, argv,
1663 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1664 opts, NULL)) != -1) {
1665 switch (c) {
1666 /*
1667 * Command selection
1668 */
1669 case 'A':
1670 add_command(&command, CMD_APPEND, CMD_NONE,
1671 invert);
1672 chain = optarg;
1673 break;
1674
1675 case 'D':
1676 add_command(&command, CMD_DELETE, CMD_NONE,
1677 invert);
1678 chain = optarg;
1679 if (optind < argc && argv[optind][0] != '-'
1680 && argv[optind][0] != '!') {
1681 rulenum = parse_rulenumber(argv[optind++]);
1682 command = CMD_DELETE_NUM;
1683 }
1684 break;
1685
1686 case 'C':
1687 add_command(&command, CMD_CHECK, CMD_NONE,
1688 invert);
1689 chain = optarg;
1690 break;
1691
1692 case 'R':
1693 add_command(&command, CMD_REPLACE, CMD_NONE,
1694 invert);
1695 chain = optarg;
1696 if (optind < argc && argv[optind][0] != '-'
1697 && argv[optind][0] != '!')
1698 rulenum = parse_rulenumber(argv[optind++]);
1699 else
1700 exit_error(PARAMETER_PROBLEM,
1701 "-%c requires a rule number",
1702 cmd2char(CMD_REPLACE));
1703 break;
1704
1705 case 'I':
1706 add_command(&command, CMD_INSERT, CMD_NONE,
1707 invert);
1708 chain = optarg;
1709 if (optind < argc && argv[optind][0] != '-'
1710 && argv[optind][0] != '!')
1711 rulenum = parse_rulenumber(argv[optind++]);
1712 else rulenum = 1;
1713 break;
1714
1715 case 'L':
1716 add_command(&command, CMD_LIST, CMD_ZERO,
1717 invert);
1718 if (optarg) chain = optarg;
1719 else if (optind < argc && argv[optind][0] != '-'
1720 && argv[optind][0] != '!')
1721 chain = argv[optind++];
1722 break;
1723
1724 case 'F':
1725 add_command(&command, CMD_FLUSH, CMD_NONE,
1726 invert);
1727 if (optarg) chain = optarg;
1728 else if (optind < argc && argv[optind][0] != '-'
1729 && argv[optind][0] != '!')
1730 chain = argv[optind++];
1731 break;
1732
1733 case 'Z':
1734 add_command(&command, CMD_ZERO, CMD_LIST,
1735 invert);
1736 if (optarg) chain = optarg;
1737 else if (optind < argc && argv[optind][0] != '-'
1738 && argv[optind][0] != '!')
1739 chain = argv[optind++];
1740 break;
1741
1742 case 'N':
1743 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1744 invert);
1745 chain = optarg;
1746 break;
1747
1748 case 'X':
1749 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1750 invert);
1751 if (optarg) chain = optarg;
1752 else if (optind < argc && argv[optind][0] != '-'
1753 && argv[optind][0] != '!')
1754 chain = argv[optind++];
1755 break;
1756
1757 case 'E':
1758 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1759 invert);
1760 chain = optarg;
1761 if (optind < argc && argv[optind][0] != '-'
1762 && argv[optind][0] != '!')
1763 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001764 else
1765 exit_error(PARAMETER_PROBLEM,
1766 "-%c requires old-chain-name and "
1767 "new-chain-name",
1768 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001769 break;
1770
1771 case 'P':
1772 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1773 invert);
1774 chain = optarg;
1775 if (optind < argc && argv[optind][0] != '-'
1776 && argv[optind][0] != '!')
1777 policy = argv[optind++];
1778 else
1779 exit_error(PARAMETER_PROBLEM,
1780 "-%c requires a chain and a policy",
1781 cmd2char(CMD_SET_POLICY));
1782 break;
1783
1784 case 'h':
1785 if (!optarg)
1786 optarg = argv[optind];
1787
1788 /* iptables -p icmp -h */
1789 if (!ip6tables_matches && protocol)
1790 find_match(protocol, TRY_LOAD);
1791
1792 exit_printhelp();
1793
1794 /*
1795 * Option selection
1796 */
1797 case 'p':
1798 if (check_inverse(optarg, &invert))
1799 optind++;
1800 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1801 invert);
1802
1803 /* Canonicalize into lower case */
1804 for (protocol = argv[optind-1]; *protocol; protocol++)
1805 *protocol = tolower(*protocol);
1806
1807 protocol = argv[optind-1];
1808 fw.ipv6.proto = parse_protocol(protocol);
1809 fw.ipv6.flags |= IP6T_F_PROTO;
1810
1811 if (fw.ipv6.proto == 0
1812 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1813 exit_error(PARAMETER_PROBLEM,
1814 "rule would never match protocol");
1815 fw.nfcache |= NFC_IP6_PROTO;
1816 break;
1817
1818 case 's':
1819 if (check_inverse(optarg, &invert))
1820 optind++;
1821 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1822 invert);
1823 shostnetworkmask = argv[optind-1];
1824 fw.nfcache |= NFC_IP6_SRC;
1825 break;
1826
1827 case 'd':
1828 if (check_inverse(optarg, &invert))
1829 optind++;
1830 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1831 invert);
1832 dhostnetworkmask = argv[optind-1];
1833 fw.nfcache |= NFC_IP6_DST;
1834 break;
1835
1836 case 'j':
1837 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1838 invert);
1839 jumpto = parse_target(optarg);
1840 target = find_target(jumpto, TRY_LOAD);
1841
1842 if (target) {
1843 size_t size;
1844
1845 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1846 + target->size);
1847
1848 target->t = fw_calloc(1, size);
1849 target->t->u.target_size = size;
1850 strcpy(target->t->u.user.name, jumpto);
1851 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001852 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001853 }
1854 break;
1855
1856
1857 case 'i':
1858 if (check_inverse(optarg, &invert))
1859 optind++;
1860 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1861 invert);
1862 parse_interface(argv[optind-1],
1863 fw.ipv6.iniface,
1864 fw.ipv6.iniface_mask);
1865 fw.nfcache |= NFC_IP6_IF_IN;
1866 break;
1867
1868 case 'o':
1869 if (check_inverse(optarg, &invert))
1870 optind++;
1871 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1872 invert);
1873 parse_interface(argv[optind-1],
1874 fw.ipv6.outiface,
1875 fw.ipv6.outiface_mask);
1876 fw.nfcache |= NFC_IP6_IF_OUT;
1877 break;
1878
1879 case 'v':
1880 if (!verbose)
1881 set_option(&options, OPT_VERBOSE,
1882 &fw.ipv6.invflags, invert);
1883 verbose++;
1884 break;
1885
1886 case 'm': {
1887 size_t size;
1888
1889 if (invert)
1890 exit_error(PARAMETER_PROBLEM,
1891 "unexpected ! flag before --match");
1892
1893 m = find_match(optarg, LOAD_MUST_SUCCEED);
1894 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1895 + m->size);
1896 m->m = fw_calloc(1, size);
1897 m->m->u.match_size = size;
1898 strcpy(m->m->u.user.name, m->name);
1899 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001900 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001901 }
1902 break;
1903
1904 case 'n':
1905 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1906 invert);
1907 break;
1908
1909 case 't':
1910 if (invert)
1911 exit_error(PARAMETER_PROBLEM,
1912 "unexpected ! flag before --table");
1913 *table = argv[optind-1];
1914 break;
1915
1916 case 'x':
1917 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1918 invert);
1919 break;
1920
1921 case 'V':
1922 if (invert)
1923 printf("Not %s ;-)\n", program_version);
1924 else
1925 printf("%s v%s\n",
1926 program_name, program_version);
1927 exit(0);
1928
1929 case '0':
1930 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1931 invert);
1932 break;
1933
1934 case 1: /* non option */
1935 if (optarg[0] == '!' && optarg[1] == '\0') {
1936 if (invert)
1937 exit_error(PARAMETER_PROBLEM,
1938 "multiple consecutive ! not"
1939 " allowed");
1940 invert = TRUE;
1941 optarg[0] = '\0';
1942 continue;
1943 }
1944 printf("Bad argument `%s'\n", optarg);
1945 exit_tryhelp(2);
1946
1947 default:
1948 /* FIXME: This scheme doesn't allow two of the same
1949 matches --RR */
1950 if (!target
1951 || !(target->parse(c - target->option_offset,
1952 argv, invert,
1953 &target->tflags,
1954 &fw, &target->t))) {
1955 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001956 if (!m->used)
1957 continue;
1958
Rusty Russell5eed48a2000-06-02 20:12:24 +00001959 if (m->parse(c - m->option_offset,
1960 argv, invert,
1961 &m->mflags,
1962 &fw,
1963 &fw.nfcache,
1964 &m->m))
1965 break;
1966 }
1967
1968 /* If you listen carefully, you can
1969 actually hear this code suck. */
1970 if (m == NULL
1971 && protocol
1972 && !find_proto(protocol, DONT_LOAD,
1973 options&OPT_NUMERIC)
1974 && (m = find_proto(protocol, TRY_LOAD,
1975 options&OPT_NUMERIC))) {
1976 /* Try loading protocol */
1977 size_t size;
1978
1979 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1980 + m->size);
1981
1982 m->m = fw_calloc(1, size);
1983 m->m->u.match_size = size;
1984 strcpy(m->m->u.user.name, m->name);
1985 m->init(m->m, &fw.nfcache);
1986
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001987 opts = merge_options(opts,
1988 m->extra_opts, &m->option_offset);
1989
Rusty Russell5eed48a2000-06-02 20:12:24 +00001990 optind--;
1991 continue;
1992 }
1993 if (!m)
1994 exit_error(PARAMETER_PROBLEM,
1995 "Unknown arg `%s'",
1996 argv[optind-1]);
1997 }
1998 }
1999 invert = FALSE;
2000 }
2001
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002002 for (m = ip6tables_matches; m; m = m->next) {
2003 if (!m->used)
2004 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002005
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002006 m->final_check(m->mflags);
2007 }
2008
2009 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002010 target->final_check(target->tflags);
2011
2012 /* Fix me: must put inverse options checking here --MN */
2013
2014 if (optind < argc)
2015 exit_error(PARAMETER_PROBLEM,
2016 "unknown arguments found on commandline");
2017 if (!command)
2018 exit_error(PARAMETER_PROBLEM, "no command specified");
2019 if (invert)
2020 exit_error(PARAMETER_PROBLEM,
2021 "nothing appropriate following !");
2022
2023 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2024 CMD_CHECK)) {
2025 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002026 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002027 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002028 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002029 }
2030
2031 if (shostnetworkmask)
2032 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2033 &(fw.ipv6.smsk), &nsaddrs);
2034
2035 if (dhostnetworkmask)
2036 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2037 &(fw.ipv6.dmsk), &ndaddrs);
2038
2039 if ((nsaddrs > 1 || ndaddrs > 1) &&
2040 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2041 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2042 " source or destination IP addresses");
2043
2044 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2045 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2046 cmd2char(CMD_CHECK));
2047
2048 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2049 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2050 "specify a unique address");
2051
2052 generic_opt_check(command, options);
2053
2054 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2055 exit_error(PARAMETER_PROBLEM,
2056 "chain name `%s' too long (must be under %i chars)",
2057 chain, IP6T_FUNCTION_MAXNAMELEN);
2058
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002059 /* only allocate handle if we weren't called with a handle */
2060 if (!*handle)
2061 *handle = ip6tc_init(*table);
2062
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002063 if (!*handle) {
2064 /* try to insmod the module if iptc_init failed */
2065 ip6tables_insmod("ip6_tables", modprobe);
2066 *handle = ip6tc_init(*table);
2067 }
2068
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002069 if (!*handle)
2070 exit_error(VERSION_PROBLEM,
2071 "can't initialize ip6tables table `%s': %s",
2072 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073
2074 if (command == CMD_CHECK
2075 || command == CMD_APPEND
2076 || command == CMD_DELETE
2077 || command == CMD_INSERT
2078 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002079 if (strcmp(chain, "PREROUTING") == 0
2080 || strcmp(chain, "INPUT") == 0) {
2081 /* -o not valid with incoming packets. */
2082 if (options & OPT_VIANAMEOUT)
2083 exit_error(PARAMETER_PROBLEM,
2084 "Can't use -%c with %s\n",
2085 opt2char(OPT_VIANAMEOUT),
2086 chain);
2087 /* -i required with -C */
2088 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2089 exit_error(PARAMETER_PROBLEM,
2090 "Need -%c with %s\n",
2091 opt2char(OPT_VIANAMEIN),
2092 chain);
2093 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002094
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002095 if (strcmp(chain, "POSTROUTING") == 0
2096 || strcmp(chain, "OUTPUT") == 0) {
2097 /* -i not valid with outgoing packets */
2098 if (options & OPT_VIANAMEIN)
2099 exit_error(PARAMETER_PROBLEM,
2100 "Can't use -%c with %s\n",
2101 opt2char(OPT_VIANAMEIN),
2102 chain);
2103 /* -o required with -C */
2104 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2105 exit_error(PARAMETER_PROBLEM,
2106 "Need -%c with %s\n",
2107 opt2char(OPT_VIANAMEOUT),
2108 chain);
2109 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002110
2111 if (target && ip6tc_is_chain(jumpto, *handle)) {
2112 printf("Warning: using chain %s, not extension\n",
2113 jumpto);
2114
2115 target = NULL;
2116 }
2117
2118 /* If they didn't specify a target, or it's a chain
2119 name, use standard. */
2120 if (!target
2121 && (strlen(jumpto) == 0
2122 || ip6tc_is_chain(jumpto, *handle))) {
2123 size_t size;
2124
2125 target = find_target(IP6T_STANDARD_TARGET,
2126 LOAD_MUST_SUCCEED);
2127
2128 size = sizeof(struct ip6t_entry_target)
2129 + target->size;
2130 target->t = fw_calloc(1, size);
2131 target->t->u.target_size = size;
2132 strcpy(target->t->u.user.name, jumpto);
2133 target->init(target->t, &fw.nfcache);
2134 }
2135
2136 if (!target) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002137#if 0
Rusty Russell5eed48a2000-06-02 20:12:24 +00002138 struct ip6t_entry_target unknown_target;
2139
2140 /* Don't know it. Must be extension with no
2141 options? */
2142 unknown_target.u.target_size = sizeof(unknown_target);
2143 strcpy(unknown_target.u.user.name, jumpto);
2144
2145 e = generate_entry(&fw, ip6tables_matches,
2146 &unknown_target);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002147#endif
2148 /* it is no chain, and we can't load a plugin.
2149 * We cannot know if the plugin is corrupt, non
2150 * existant OR if the user just misspelled a
2151 * chain. */
2152 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002153 } else {
2154 e = generate_entry(&fw, ip6tables_matches, target->t);
2155 }
2156 }
2157
2158 switch (command) {
2159 case CMD_APPEND:
2160 ret = append_entry(chain, e,
2161 nsaddrs, saddrs, ndaddrs, daddrs,
2162 options&OPT_VERBOSE,
2163 handle);
2164 break;
2165 case CMD_CHECK:
2166 ret = check_packet(chain, e,
2167 nsaddrs, saddrs, ndaddrs, daddrs,
2168 options&OPT_VERBOSE, handle);
2169 break;
2170 case CMD_DELETE:
2171 ret = delete_entry(chain, e,
2172 nsaddrs, saddrs, ndaddrs, daddrs,
2173 options&OPT_VERBOSE,
2174 handle);
2175 break;
2176 case CMD_DELETE_NUM:
2177 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2178 break;
2179 case CMD_REPLACE:
2180 ret = replace_entry(chain, e, rulenum - 1,
2181 saddrs, daddrs, options&OPT_VERBOSE,
2182 handle);
2183 break;
2184 case CMD_INSERT:
2185 ret = insert_entry(chain, e, rulenum - 1,
2186 nsaddrs, saddrs, ndaddrs, daddrs,
2187 options&OPT_VERBOSE,
2188 handle);
2189 break;
2190 case CMD_LIST:
2191 ret = list_entries(chain,
2192 options&OPT_VERBOSE,
2193 options&OPT_NUMERIC,
2194 options&OPT_EXPANDED,
2195 options&OPT_LINENUMBERS,
2196 handle);
2197 break;
2198 case CMD_FLUSH:
2199 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2200 break;
2201 case CMD_ZERO:
2202 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2203 break;
2204 case CMD_LIST|CMD_ZERO:
2205 ret = list_entries(chain,
2206 options&OPT_VERBOSE,
2207 options&OPT_NUMERIC,
2208 options&OPT_EXPANDED,
2209 options&OPT_LINENUMBERS,
2210 handle);
2211 if (ret)
2212 ret = zero_entries(chain,
2213 options&OPT_VERBOSE, handle);
2214 break;
2215 case CMD_NEW_CHAIN:
2216 ret = ip6tc_create_chain(chain, handle);
2217 break;
2218 case CMD_DELETE_CHAIN:
2219 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2220 break;
2221 case CMD_RENAME_CHAIN:
2222 ret = ip6tc_rename_chain(chain, newname, handle);
2223 break;
2224 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002225 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002226 break;
2227 default:
2228 /* We should never reach this... */
2229 exit_tryhelp(2);
2230 }
2231
2232 if (verbose > 1)
2233 dump_entries6(*handle);
2234
2235 return ret;
2236}