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