blob: d60b2da15e43ac0a68da9d1e16e12a122039cc28 [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
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000520static char *
521addr_to_host(const struct in6_addr *addr)
522{
523 struct sockaddr_in6 saddr;
524 int err;
525 static char hostname[NI_MAXHOST];
526
527 memset(&saddr, 0, sizeof(struct sockaddr_in6));
528 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000529 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000530
531 if ( (err=getnameinfo((struct sockaddr *)&saddr,
532 sizeof(struct sockaddr_in6),
533 hostname, sizeof(hostname)-1,
534 NULL, 0, 0)) != 0 ){
535#ifdef DEBUG
536 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
537#endif
538 return (char *) NULL;
539 } else {
540#ifdef DEBUG
541 fprintf (stderr, "\naddr2host: %s\n", hostname);
542#endif
543
544 return hostname;
545 }
546
547 return (char *) NULL;
548}
549
Rusty Russell5eed48a2000-06-02 20:12:24 +0000550static char *
551mask_to_numeric(const struct in6_addr *addrp)
552{
553 static char buf[20];
554 int l = ipv6_prefix_length(addrp);
555 if (l == -1)
556 return addr_to_numeric(addrp);
557 sprintf(buf, "%d", l);
558 return buf;
559}
560
561static struct in6_addr *
562network_to_addr(const char *name)
563{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000564 /* abort();*/
565 /* TODO: not implemented yet, but the exception breaks the
566 * name resolvation */
567 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000568}
569
570static char *
571addr_to_anyname(const struct in6_addr *addr)
572{
573 char *name;
574
575 if ((name = addr_to_host(addr)) != NULL)
576 return name;
577
578 return addr_to_numeric(addr);
579}
580
581/*
582 * All functions starting with "parse" should succeed, otherwise
583 * the program fails.
584 * Most routines return pointers to static data that may change
585 * between calls to the same or other routines with a few exceptions:
586 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
587 * return global static data.
588*/
589
590static struct in6_addr *
591parse_hostnetwork(const char *name, unsigned int *naddrs)
592{
593 struct in6_addr *addrp, *addrptmp;
594
595 if ((addrptmp = numeric_to_addr(name)) != NULL ||
596 (addrptmp = network_to_addr(name)) != NULL) {
597 addrp = fw_malloc(sizeof(struct in6_addr));
598 in6addrcpy(addrp, addrptmp);
599 *naddrs = 1;
600 return addrp;
601 }
602 if ((addrp = host_to_addr(name, naddrs)) != NULL)
603 return addrp;
604
605 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
606}
607
608static struct in6_addr *
609parse_mask(char *mask)
610{
611 static struct in6_addr maskaddr;
612 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000613 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000614
615 if (mask == NULL) {
616 /* no mask at all defaults to 128 bits */
617 memset(&maskaddr, 0xff, sizeof maskaddr);
618 return &maskaddr;
619 }
620 if ((addrp = numeric_to_addr(mask)) != NULL)
621 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000622 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000623 exit_error(PARAMETER_PROBLEM,
624 "invalid mask `%s' specified", mask);
625 if (bits != 0) {
626 char *p = (char *)&maskaddr;
627 memset(p, 0xff, bits / 8);
628 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
629 p[bits / 8] = 0xff << (8 - (bits & 7));
630 return &maskaddr;
631 }
632
633 memset(&maskaddr, 0, sizeof maskaddr);
634 return &maskaddr;
635}
636
637static void
638parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
639 struct in6_addr *maskp, unsigned int *naddrs)
640{
641 struct in6_addr *addrp;
642 char buf[256];
643 char *p;
644 int i, j, n;
645
646 strncpy(buf, name, sizeof(buf) - 1);
647 if ((p = strrchr(buf, '/')) != NULL) {
648 *p = '\0';
649 addrp = parse_mask(p + 1);
650 } else
651 addrp = parse_mask(NULL);
652 in6addrcpy(maskp, addrp);
653
654 /* if a null mask is given, the name is ignored, like in "any/0" */
655 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
656 strcpy(buf, "::");
657
658 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
659 n = *naddrs;
660 for (i = 0, j = 0; i < n; i++) {
661 int k;
662 for (k = 0; k < 4; k++)
663 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
664 j++;
665 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000666 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000667 (*naddrs)--;
668 j--;
669 break;
670 }
671 }
672 }
673}
674
675struct ip6tables_match *
676find_match(const char *name, enum ip6t_tryload tryload)
677{
678 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000679 int icmphack = 0;
680
681 /* This is ugly as hell. Nonetheless, there is no way of changing
682 * this without hurting backwards compatibility */
683 if ( (strcmp(name,"icmpv6") == 0) ||
684 (strcmp(name,"ipv6-icmp") == 0) ||
685 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
686
687 if (!icmphack) {
688 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
689 if (strcmp(name, ptr->name) == 0)
690 break;
691 }
692 } else {
693 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
694 if (strcmp("icmp6", ptr->name) == 0)
695 break;
696 }
697 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000698
Harald Welte3efb6ea2001-08-06 18:50:21 +0000699#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000700 if (!ptr && tryload != DONT_LOAD) {
701 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
702 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000703 if (!icmphack)
704 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
705 else
706 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000707 if (dlopen(path, RTLD_NOW)) {
708 /* Found library. If it didn't register itself,
709 maybe they specified target as match. */
710 ptr = find_match(name, DONT_LOAD);
711
712 if (!ptr)
713 exit_error(PARAMETER_PROBLEM,
714 "Couldn't load match `%s'\n",
715 name);
716 } else if (tryload == LOAD_MUST_SUCCEED)
717 exit_error(PARAMETER_PROBLEM,
718 "Couldn't load match `%s'\n", name);
719 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000720#else
721 if (ptr && !ptr->loaded) {
722 if (tryload != DONT_LOAD)
723 ptr->loaded = 1;
724 else
725 ptr = NULL;
726 }
727#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000729 if (ptr)
730 ptr->used = 1;
731
732
Rusty Russell5eed48a2000-06-02 20:12:24 +0000733 return ptr;
734}
735
736/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
737static struct ip6tables_match *
738find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
739{
Harald Welteed498492001-07-23 01:24:22 +0000740 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000741
Harald Welteed498492001-07-23 01:24:22 +0000742 if (string_to_number(pname, 0, 255, &proto) != -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743 return find_match(proto_to_name(proto, nolookup), tryload);
744
745 return find_match(pname, tryload);
746}
747
748static u_int16_t
749parse_protocol(const char *s)
750{
Harald Welteed498492001-07-23 01:24:22 +0000751 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000752
Harald Welteed498492001-07-23 01:24:22 +0000753 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000754 struct protoent *pent;
755
756 if ((pent = getprotobyname(s)))
757 proto = pent->p_proto;
758 else {
759 unsigned int i;
760 for (i = 0;
761 i < sizeof(chain_protos)/sizeof(struct pprot);
762 i++) {
763 if (strcmp(s, chain_protos[i].name) == 0) {
764 proto = chain_protos[i].num;
765 break;
766 }
767 }
768 if (i == sizeof(chain_protos)/sizeof(struct pprot))
769 exit_error(PARAMETER_PROBLEM,
770 "unknown protocol `%s' specified",
771 s);
772 }
773 }
774
775 return (u_int16_t)proto;
776}
777
778static void
779parse_interface(const char *arg, char *vianame, unsigned char *mask)
780{
781 int vialen = strlen(arg);
782 unsigned int i;
783
784 memset(mask, 0, IFNAMSIZ);
785 memset(vianame, 0, IFNAMSIZ);
786
787 if (vialen + 1 > IFNAMSIZ)
788 exit_error(PARAMETER_PROBLEM,
789 "interface name `%s' must be shorter than IFNAMSIZ"
790 " (%i)", arg, IFNAMSIZ-1);
791
792 strcpy(vianame, arg);
793 if (vialen == 0)
794 memset(mask, 0, IFNAMSIZ);
795 else if (vianame[vialen - 1] == '+') {
796 memset(mask, 0xFF, vialen - 1);
797 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
798 /* Remove `+' */
799 vianame[vialen - 1] = '\0';
800 } else {
801 /* Include nul-terminator in match */
802 memset(mask, 0xFF, vialen + 1);
803 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
804 }
805 for (i = 0; vianame[i]; i++) {
Harald Welte6a4542a2001-05-12 04:38:31 +0000806 if (!isalnum(vianame[i]) && vianame[i] != '_') {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000807 printf("Warning: wierd character in interface"
808 " `%s' (No aliases, :, ! or *).\n",
809 vianame);
810 break;
811 }
812 }
813}
814
815/* Can't be zero. */
816static int
817parse_rulenumber(const char *rule)
818{
Harald Welteed498492001-07-23 01:24:22 +0000819 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820
Harald Welteed498492001-07-23 01:24:22 +0000821 if (string_to_number(rule, 1, INT_MAX, &rulenum))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000822 exit_error(PARAMETER_PROBLEM,
823 "Invalid rule number `%s'", rule);
824
825 return rulenum;
826}
827
828static const char *
829parse_target(const char *targetname)
830{
831 const char *ptr;
832
833 if (strlen(targetname) < 1)
834 exit_error(PARAMETER_PROBLEM,
835 "Invalid target name (too short)");
836
837 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
838 exit_error(PARAMETER_PROBLEM,
839 "Invalid target name `%s' (%i chars max)",
840 targetname, sizeof(ip6t_chainlabel)-1);
841
842 for (ptr = targetname; *ptr; ptr++)
843 if (isspace(*ptr))
844 exit_error(PARAMETER_PROBLEM,
845 "Invalid target name `%s'", targetname);
846 return targetname;
847}
848
849int
Harald Welteed498492001-07-23 01:24:22 +0000850string_to_number(const char *s, unsigned int min, unsigned int max,
851 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000852{
Harald Welteed498492001-07-23 01:24:22 +0000853 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000854 char *end;
855
856 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000857 errno = 0;
858 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000859 if (*end == '\0' && end != s) {
860 /* we parsed a number, let's see if we want this */
Harald Welteed498492001-07-23 01:24:22 +0000861 if (errno != ERANGE && min <= number && number <= max) {
862 *ret = number;
863 return 0;
864 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000865 }
866 return -1;
867}
868
869static void
870set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
871 int invert)
872{
873 if (*options & option)
874 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
875 opt2char(option));
876 *options |= option;
877
878 if (invert) {
879 unsigned int i;
880 for (i = 0; 1 << i != option; i++);
881
882 if (!inverse_for_options[i])
883 exit_error(PARAMETER_PROBLEM,
884 "cannot have ! before -%c",
885 opt2char(option));
886 *invflg |= inverse_for_options[i];
887 }
888}
889
890struct ip6tables_target *
891find_target(const char *name, enum ip6t_tryload tryload)
892{
893 struct ip6tables_target *ptr;
894
895 /* Standard target? */
896 if (strcmp(name, "") == 0
897 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
898 || strcmp(name, IP6TC_LABEL_DROP) == 0
899 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
900 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
901 name = "standard";
902
903 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
904 if (strcmp(name, ptr->name) == 0)
905 break;
906 }
907
Harald Welte3efb6ea2001-08-06 18:50:21 +0000908#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000909 if (!ptr && tryload != DONT_LOAD) {
910 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
911 + strlen(name)];
912 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
913 if (dlopen(path, RTLD_NOW)) {
914 /* Found library. If it didn't register itself,
915 maybe they specified match as a target. */
916 ptr = find_target(name, DONT_LOAD);
917 if (!ptr)
918 exit_error(PARAMETER_PROBLEM,
919 "Couldn't load target `%s'\n",
920 name);
921 } else if (tryload == LOAD_MUST_SUCCEED)
922 exit_error(PARAMETER_PROBLEM,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000923 "Couldn't load target `%s'%s\n",
924 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000925 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000926#else
927 if (ptr && !ptr->loaded) {
928 if (tryload != DONT_LOAD)
929 ptr->loaded = 1;
930 else
931 ptr = NULL;
932 }
933#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000934
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000935 if (ptr)
936 ptr->used = 1;
937
Rusty Russell5eed48a2000-06-02 20:12:24 +0000938 return ptr;
939}
940
941static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000942merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000943 unsigned int *option_offset)
944{
945 unsigned int num_old, num_new, i;
946 struct option *merge;
947
948 for (num_old = 0; oldopts[num_old].name; num_old++);
949 for (num_new = 0; newopts[num_new].name; num_new++);
950
951 global_option_offset += OPTION_OFFSET;
952 *option_offset = global_option_offset;
953
954 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
955 memcpy(merge, oldopts, num_old * sizeof(struct option));
956 for (i = 0; i < num_new; i++) {
957 merge[num_old + i] = newopts[i];
958 merge[num_old + i].val += *option_offset;
959 }
960 memset(merge + num_old + num_new, 0, sizeof(struct option));
961
962 return merge;
963}
964
965void
966register_match6(struct ip6tables_match *me)
967{
968 if (strcmp(me->version, program_version) != 0) {
969 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
970 program_name, me->name, me->version, program_version);
971 exit(1);
972 }
973
974 if (find_match(me->name, DONT_LOAD)) {
975 fprintf(stderr, "%s: match `%s' already registered.\n",
976 program_name, me->name);
977 exit(1);
978 }
979
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000980 if (me->size != IP6T_ALIGN(me->size)) {
981 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
982 program_name, me->name, me->size);
983 exit(1);
984 }
985
Rusty Russell5eed48a2000-06-02 20:12:24 +0000986 /* Prepend to list. */
987 me->next = ip6tables_matches;
988 ip6tables_matches = me;
989 me->m = NULL;
990 me->mflags = 0;
991
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000992 /* opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993}
994
995void
996register_target6(struct ip6tables_target *me)
997{
998 if (strcmp(me->version, program_version) != 0) {
999 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1000 program_name, me->name, me->version, program_version);
1001 exit(1);
1002 }
1003
1004 if (find_target(me->name, DONT_LOAD)) {
1005 fprintf(stderr, "%s: target `%s' already registered.\n",
1006 program_name, me->name);
1007 exit(1);
1008 }
1009
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001010 if (me->size != IP6T_ALIGN(me->size)) {
1011 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1012 program_name, me->name, me->size);
1013 exit(1);
1014 }
1015
Rusty Russell5eed48a2000-06-02 20:12:24 +00001016 /* Prepend to list. */
1017 me->next = ip6tables_targets;
1018 ip6tables_targets = me;
1019 me->t = NULL;
1020 me->tflags = 0;
1021
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001022 /*opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
1023}
1024
1025static void
1026print_num(u_int64_t number, unsigned int format)
1027{
1028 if (format & FMT_KILOMEGAGIGA) {
1029 if (number > 99999) {
1030 number = (number + 500) / 1000;
1031 if (number > 9999) {
1032 number = (number + 500) / 1000;
1033 if (number > 9999) {
1034 number = (number + 500) / 1000;
1035 printf(FMT("%4lluG ","%lluG "),number);
1036 }
1037 else printf(FMT("%4lluM ","%lluM "), number);
1038 } else
1039 printf(FMT("%4lluK ","%lluK "), number);
1040 } else
1041 printf(FMT("%5llu ","%llu "), number);
1042 } else
1043 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001044}
1045
1046static void
1047print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1048{
1049 struct ip6t_counters counters;
1050 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1051 printf("Chain %s", chain);
1052 if (pol) {
1053 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001054 if (!(format & FMT_NOCOUNTS)) {
1055 /*printf(" %llu packets, %llu bytes",
1056 counters.pcnt, counters.bcnt);*/
1057 fputc(' ', stdout);
1058 print_num(counters.pcnt, (format|FMT_NOTABLE));
1059 fputs("packets, ", stdout);
1060 print_num(counters.bcnt, (format|FMT_NOTABLE));
1061 fputs("bytes", stdout);
1062 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001063 printf(")\n");
1064 } else {
1065 unsigned int refs;
1066 if (!ip6tc_get_references(&refs, chain, handle))
1067 printf(" (ERROR obtaining refs)\n");
1068 else
1069 printf(" (%u references)\n", refs);
1070 }
1071
1072 if (format & FMT_LINENUMBERS)
1073 printf(FMT("%-4s ", "%s "), "num");
1074 if (!(format & FMT_NOCOUNTS)) {
1075 if (format & FMT_KILOMEGAGIGA) {
1076 printf(FMT("%5s ","%s "), "pkts");
1077 printf(FMT("%5s ","%s "), "bytes");
1078 } else {
1079 printf(FMT("%8s ","%s "), "pkts");
1080 printf(FMT("%10s ","%s "), "bytes");
1081 }
1082 }
1083 if (!(format & FMT_NOTARGET))
1084 printf(FMT("%-9s ","%s "), "target");
1085 fputs(" prot ", stdout);
1086 if (format & FMT_OPTIONS)
1087 fputs("opt", stdout);
1088 if (format & FMT_VIA) {
1089 printf(FMT(" %-6s ","%s "), "in");
1090 printf(FMT("%-6s ","%s "), "out");
1091 }
1092 printf(FMT(" %-19s ","%s "), "source");
1093 printf(FMT(" %-19s "," %s "), "destination");
1094 printf("\n");
1095}
1096
Rusty Russell5eed48a2000-06-02 20:12:24 +00001097static int
1098print_match(const struct ip6t_entry_match *m,
1099 const struct ip6t_ip6 *ip,
1100 int numeric)
1101{
1102 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1103
1104 if (match) {
1105 if (match->print)
1106 match->print(ip, m, numeric);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001107 else
1108 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001109 } else {
1110 if (m->u.user.name[0])
1111 printf("UNKNOWN match `%s' ", m->u.user.name);
1112 }
1113 /* Don't stop iterating. */
1114 return 0;
1115}
1116
1117/* e is called `fw' here for hysterical raisins */
1118static void
1119print_firewall(const struct ip6t_entry *fw,
1120 const char *targname,
1121 unsigned int num,
1122 unsigned int format,
1123 const ip6tc_handle_t handle)
1124{
1125 struct ip6tables_target *target = NULL;
1126 const struct ip6t_entry_target *t;
1127 u_int8_t flags;
1128 char buf[BUFSIZ];
1129
1130 /* User creates a chain called "REJECT": this overrides the
1131 `REJECT' target module. Keep feeding them rope until the
1132 revolution... Bwahahahahah */
1133 if (!ip6tc_is_chain(targname, handle))
1134 target = find_target(targname, TRY_LOAD);
1135 else
1136 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1137
1138 t = ip6t_get_target((struct ip6t_entry *)fw);
1139 flags = fw->ipv6.flags;
1140
1141 if (format & FMT_LINENUMBERS)
1142 printf(FMT("%-4u ", "%u "), num+1);
1143
1144 if (!(format & FMT_NOCOUNTS)) {
1145 print_num(fw->counters.pcnt, format);
1146 print_num(fw->counters.bcnt, format);
1147 }
1148
1149 if (!(format & FMT_NOTARGET))
1150 printf(FMT("%-9s ", "%s "), targname);
1151
1152 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1153 {
1154 char *pname = proto_to_name(fw->ipv6.proto,
1155 format&FMT_NUMERIC);
1156 if (pname)
1157 printf(FMT("%-5s", "%s "), pname);
1158 else
1159 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1160 }
1161
1162 if (format & FMT_OPTIONS) {
1163 if (format & FMT_NOTABLE)
1164 fputs("opt ", stdout);
1165 fputc(' ', stdout);
1166 fputc(' ', stdout);
1167 fputc(' ', stdout);
1168 }
1169
1170 if (format & FMT_VIA) {
1171 char iface[IFNAMSIZ+2];
1172
1173 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1174 iface[0] = '!';
1175 iface[1] = '\0';
1176 }
1177 else iface[0] = '\0';
1178
1179 if (fw->ipv6.iniface[0] != '\0') {
1180 strcat(iface, fw->ipv6.iniface);
1181 /* If it doesn't compare the nul-term, it's a
1182 wildcard. */
1183 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1184 strcat(iface, "+");
1185 }
1186 else if (format & FMT_NUMERIC) strcat(iface, "*");
1187 else strcat(iface, "any");
1188 printf(FMT(" %-6s ","in %s "), iface);
1189
1190 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1191 iface[0] = '!';
1192 iface[1] = '\0';
1193 }
1194 else iface[0] = '\0';
1195
1196 if (fw->ipv6.outiface[0] != '\0') {
1197 strcat(iface, fw->ipv6.outiface);
1198 /* If it doesn't compare the nul-term, it's a
1199 wildcard. */
1200 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1201 strcat(iface, "+");
1202 }
1203 else if (format & FMT_NUMERIC) strcat(iface, "*");
1204 else strcat(iface, "any");
1205 printf(FMT("%-6s ","out %s "), iface);
1206 }
1207
1208 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1209 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1210 && !(format & FMT_NUMERIC))
1211 printf(FMT("%-19s ","%s "), "anywhere");
1212 else {
1213 if (format & FMT_NUMERIC)
1214 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1215 else
1216 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1217 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1218 printf(FMT("%-19s ","%s "), buf);
1219 }
1220
1221 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1222 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1223 && !(format & FMT_NUMERIC))
1224 printf(FMT("%-19s","-> %s"), "anywhere");
1225 else {
1226 if (format & FMT_NUMERIC)
1227 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1228 else
1229 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1230 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1231 printf(FMT("%-19s","-> %s"), buf);
1232 }
1233
1234 if (format & FMT_NOTABLE)
1235 fputs(" ", stdout);
1236
1237 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1238
1239 if (target) {
1240 if (target->print)
1241 /* Print the target information. */
1242 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1243 } else if (t->u.target_size != sizeof(*t))
1244 printf("[%u bytes of unknown target data] ",
1245 t->u.target_size - sizeof(*t));
1246
1247 if (!(format & FMT_NONEWLINE))
1248 fputc('\n', stdout);
1249}
1250
1251static void
1252print_firewall_line(const struct ip6t_entry *fw,
1253 const ip6tc_handle_t h)
1254{
1255 struct ip6t_entry_target *t;
1256
1257 t = ip6t_get_target((struct ip6t_entry *)fw);
1258 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1259}
1260
1261static int
1262append_entry(const ip6t_chainlabel chain,
1263 struct ip6t_entry *fw,
1264 unsigned int nsaddrs,
1265 const struct in6_addr saddrs[],
1266 unsigned int ndaddrs,
1267 const struct in6_addr daddrs[],
1268 int verbose,
1269 ip6tc_handle_t *handle)
1270{
1271 unsigned int i, j;
1272 int ret = 1;
1273
1274 for (i = 0; i < nsaddrs; i++) {
1275 fw->ipv6.src = saddrs[i];
1276 for (j = 0; j < ndaddrs; j++) {
1277 fw->ipv6.dst = daddrs[j];
1278 if (verbose)
1279 print_firewall_line(fw, *handle);
1280 ret &= ip6tc_append_entry(chain, fw, handle);
1281 }
1282 }
1283
1284 return ret;
1285}
1286
1287static int
1288replace_entry(const ip6t_chainlabel chain,
1289 struct ip6t_entry *fw,
1290 unsigned int rulenum,
1291 const struct in6_addr *saddr,
1292 const struct in6_addr *daddr,
1293 int verbose,
1294 ip6tc_handle_t *handle)
1295{
1296 fw->ipv6.src = *saddr;
1297 fw->ipv6.dst = *daddr;
1298
1299 if (verbose)
1300 print_firewall_line(fw, *handle);
1301 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1302}
1303
1304static int
1305insert_entry(const ip6t_chainlabel chain,
1306 struct ip6t_entry *fw,
1307 unsigned int rulenum,
1308 unsigned int nsaddrs,
1309 const struct in6_addr saddrs[],
1310 unsigned int ndaddrs,
1311 const struct in6_addr daddrs[],
1312 int verbose,
1313 ip6tc_handle_t *handle)
1314{
1315 unsigned int i, j;
1316 int ret = 1;
1317
1318 for (i = 0; i < nsaddrs; i++) {
1319 fw->ipv6.src = saddrs[i];
1320 for (j = 0; j < ndaddrs; j++) {
1321 fw->ipv6.dst = daddrs[j];
1322 if (verbose)
1323 print_firewall_line(fw, *handle);
1324 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1325 }
1326 }
1327
1328 return ret;
1329}
1330
1331static unsigned char *
1332make_delete_mask(struct ip6t_entry *fw)
1333{
1334 /* Establish mask for comparison */
1335 unsigned int size;
1336 struct ip6tables_match *m;
1337 unsigned char *mask, *mptr;
1338
1339 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001340 for (m = ip6tables_matches; m; m = m->next) {
1341 if (!m->used)
1342 continue;
1343
1344 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1345 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001346
1347 mask = fw_calloc(1, size
1348 + sizeof(struct ip6t_entry_target)
1349 + ip6tables_targets->size);
1350
1351 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1352 mptr = mask + sizeof(struct ip6t_entry);
1353
1354 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001355 if (!m->used)
1356 continue;
1357
Rusty Russell5eed48a2000-06-02 20:12:24 +00001358 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001359 IP6T_ALIGN(sizeof(struct ip6t_entry_match)) +
1360 m->userspacesize);
1361 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362 }
1363
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001364 memset(mptr, 0xFF,
1365 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1366 + ip6tables_targets->userspacesize);
1367 /*mptr += sizeof(struct ip6t_entry_target);
1368 memset(mptr, 0xFF, ip6tables_targets->userspacesize);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +00001369
1370 return mask;
1371}
1372
1373static int
1374delete_entry(const ip6t_chainlabel chain,
1375 struct ip6t_entry *fw,
1376 unsigned int nsaddrs,
1377 const struct in6_addr saddrs[],
1378 unsigned int ndaddrs,
1379 const struct in6_addr daddrs[],
1380 int verbose,
1381 ip6tc_handle_t *handle)
1382{
1383 unsigned int i, j;
1384 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001385 unsigned char *mask;
1386
1387 mask = make_delete_mask(fw);
1388 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001389 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001390 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001391 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001392 if (verbose)
1393 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001394 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001395 }
1396 }
1397 return ret;
1398}
1399
1400static int
1401check_packet(const ip6t_chainlabel chain,
1402 struct ip6t_entry *fw,
1403 unsigned int nsaddrs,
1404 const struct in6_addr saddrs[],
1405 unsigned int ndaddrs,
1406 const struct in6_addr daddrs[],
1407 int verbose,
1408 ip6tc_handle_t *handle)
1409{
1410 int ret = 1;
1411 unsigned int i, j;
1412 struct ip6t_entry ipfw = *fw;
1413 const char *msg;
1414
1415 for (i = 0; i < nsaddrs; i++) {
1416 ipfw.ipv6.src = saddrs[i];
1417 for (j = 0; j < ndaddrs; j++) {
1418 ipfw.ipv6.dst = daddrs[j];
1419 if (verbose)
1420 print_firewall_line(fw, *handle);
1421 msg = ip6tc_check_packet(chain, &ipfw, handle);
1422 if (!msg) ret = 0;
1423 else printf("%s\n", msg);
1424 }
1425 }
1426
1427 return ret;
1428}
1429
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001430/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001431int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001432for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1433 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1434{
1435 int ret = 1;
1436 const char *chain;
1437 char *chains;
1438 unsigned int i, chaincount = 0;
1439
1440 chain = ip6tc_first_chain(handle);
1441 while (chain) {
1442 chaincount++;
1443 chain = ip6tc_next_chain(handle);
1444 }
1445
1446 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1447 i = 0;
1448 chain = ip6tc_first_chain(handle);
1449 while (chain) {
1450 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1451 i++;
1452 chain = ip6tc_next_chain(handle);
1453 }
1454
1455 for (i = 0; i < chaincount; i++) {
1456 if (!builtinstoo
1457 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1458 *handle))
1459 continue;
1460 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1461 }
1462
1463 free(chains);
1464 return ret;
1465}
1466
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001467/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001468int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469flush_entries(const ip6t_chainlabel chain, int verbose,
1470 ip6tc_handle_t *handle)
1471{
1472 if (!chain)
1473 return for_each_chain(flush_entries, verbose, 1, handle);
1474
1475 if (verbose)
1476 fprintf(stdout, "Flushing chain `%s'\n", chain);
1477 return ip6tc_flush_entries(chain, handle);
1478}
1479
1480static int
1481zero_entries(const ip6t_chainlabel chain, int verbose,
1482 ip6tc_handle_t *handle)
1483{
1484 if (!chain)
1485 return for_each_chain(zero_entries, verbose, 1, handle);
1486
1487 if (verbose)
1488 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1489 return ip6tc_zero_entries(chain, handle);
1490}
1491
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001492/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001493int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001494delete_chain(const ip6t_chainlabel chain, int verbose,
1495 ip6tc_handle_t *handle)
1496{
1497 if (!chain)
1498 return for_each_chain(delete_chain, verbose, 0, handle);
1499
1500 if (verbose)
1501 fprintf(stdout, "Deleting chain `%s'\n", chain);
1502 return ip6tc_delete_chain(chain, handle);
1503}
1504
1505static int
1506list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1507 int expanded, int linenumbers, ip6tc_handle_t *handle)
1508{
1509 int found = 0;
1510 unsigned int format;
1511 const char *this;
1512
1513 format = FMT_OPTIONS;
1514 if (!verbose)
1515 format |= FMT_NOCOUNTS;
1516 else
1517 format |= FMT_VIA;
1518
1519 if (numeric)
1520 format |= FMT_NUMERIC;
1521
1522 if (!expanded)
1523 format |= FMT_KILOMEGAGIGA;
1524
1525 if (linenumbers)
1526 format |= FMT_LINENUMBERS;
1527
1528 for (this = ip6tc_first_chain(handle);
1529 this;
1530 this = ip6tc_next_chain(handle)) {
1531 const struct ip6t_entry *i;
1532 unsigned int num;
1533
1534 if (chain && strcmp(chain, this) != 0)
1535 continue;
1536
1537 if (found) printf("\n");
1538
1539 print_header(format, this, handle);
1540 i = ip6tc_first_rule(this, handle);
1541
1542 num = 0;
1543 while (i) {
1544 print_firewall(i,
1545 ip6tc_get_target(i, handle),
1546 num++,
1547 format,
1548 *handle);
1549 i = ip6tc_next_rule(i, handle);
1550 }
1551 found = 1;
1552 }
1553
1554 errno = ENOENT;
1555 return found;
1556}
1557
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001558static char *get_modprobe(void)
1559{
1560 int procfile;
1561 char *ret;
1562
1563 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1564 if (procfile < 0)
1565 return NULL;
1566
1567 ret = malloc(1024);
1568 if (ret) {
1569 switch (read(procfile, ret, 1024)) {
1570 case -1: goto fail;
1571 case 1024: goto fail; /* Partial read. Wierd */
1572 }
1573 if (ret[strlen(ret)-1]=='\n')
1574 ret[strlen(ret)-1]=0;
1575 close(procfile);
1576 return ret;
1577 }
1578 fail:
1579 free(ret);
1580 close(procfile);
1581 return NULL;
1582}
1583
Harald Welte58918652001-06-16 18:25:25 +00001584int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001585{
1586 char *buf = NULL;
1587 char *argv[3];
1588
1589 /* If they don't explicitly set it, read out of kernel */
1590 if (!modprobe) {
1591 buf = get_modprobe();
1592 if (!buf)
1593 return -1;
1594 modprobe = buf;
1595 }
1596
1597 switch (fork()) {
1598 case 0:
1599 argv[0] = (char *)modprobe;
1600 argv[1] = (char *)modname;
1601 argv[2] = NULL;
1602 execv(argv[0], argv);
1603
1604 /* not usually reached */
1605 exit(0);
1606 case -1:
1607 return -1;
1608
1609 default: /* parent */
1610 wait(NULL);
1611 }
1612
1613 free(buf);
1614 return 0;
1615}
1616
Rusty Russell5eed48a2000-06-02 20:12:24 +00001617static struct ip6t_entry *
1618generate_entry(const struct ip6t_entry *fw,
1619 struct ip6tables_match *matches,
1620 struct ip6t_entry_target *target)
1621{
1622 unsigned int size;
1623 struct ip6tables_match *m;
1624 struct ip6t_entry *e;
1625
1626 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001627 for (m = matches; m; m = m->next) {
1628 if (!m->used)
1629 continue;
1630
Rusty Russell5eed48a2000-06-02 20:12:24 +00001631 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001632 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001633
1634 e = fw_malloc(size + target->u.target_size);
1635 *e = *fw;
1636 e->target_offset = size;
1637 e->next_offset = size + target->u.target_size;
1638
1639 size = 0;
1640 for (m = matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001641 if (!m->used)
1642 continue;
1643
Rusty Russell5eed48a2000-06-02 20:12:24 +00001644 memcpy(e->elems + size, m->m, m->m->u.match_size);
1645 size += m->m->u.match_size;
1646 }
1647 memcpy(e->elems + size, target, target->u.target_size);
1648
1649 return e;
1650}
1651
1652int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1653{
1654 struct ip6t_entry fw, *e = NULL;
1655 int invert = 0;
1656 unsigned int nsaddrs = 0, ndaddrs = 0;
1657 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1658
1659 int c, verbose = 0;
1660 const char *chain = NULL;
1661 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1662 const char *policy = NULL, *newname = NULL;
1663 unsigned int rulenum = 0, options = 0, command = 0;
1664 int ret = 1;
1665 struct ip6tables_match *m;
1666 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001667 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 const char *jumpto = "";
1669 char *protocol = NULL;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001670 char icmp6p[] = "icmpv6";
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001671 const char *modprobe = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001672
1673 memset(&fw, 0, sizeof(fw));
1674
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001675 opts = original_opts;
1676 global_option_offset = 0;
1677
1678 /* re-set optind to 0 in case do_command gets called
1679 * a second time */
1680 optind = 0;
1681
1682 /* clear mflags in case do_command gets called a second time
1683 * (we clear the global list of all matches for security)*/
1684 for (m = ip6tables_matches; m; m = m->next) {
1685 m->mflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001686 m->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001687 }
1688
1689 for (t = ip6tables_targets; t; t = t->next) {
1690 t->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001691 t->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001692 }
1693
Rusty Russell5eed48a2000-06-02 20:12:24 +00001694 /* Suppress error messages: we may add new options if we
1695 demand-load a protocol. */
1696 opterr = 0;
1697
1698 while ((c = getopt_long(argc, argv,
1699 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1700 opts, NULL)) != -1) {
1701 switch (c) {
1702 /*
1703 * Command selection
1704 */
1705 case 'A':
1706 add_command(&command, CMD_APPEND, CMD_NONE,
1707 invert);
1708 chain = optarg;
1709 break;
1710
1711 case 'D':
1712 add_command(&command, CMD_DELETE, CMD_NONE,
1713 invert);
1714 chain = optarg;
1715 if (optind < argc && argv[optind][0] != '-'
1716 && argv[optind][0] != '!') {
1717 rulenum = parse_rulenumber(argv[optind++]);
1718 command = CMD_DELETE_NUM;
1719 }
1720 break;
1721
1722 case 'C':
1723 add_command(&command, CMD_CHECK, CMD_NONE,
1724 invert);
1725 chain = optarg;
1726 break;
1727
1728 case 'R':
1729 add_command(&command, CMD_REPLACE, CMD_NONE,
1730 invert);
1731 chain = optarg;
1732 if (optind < argc && argv[optind][0] != '-'
1733 && argv[optind][0] != '!')
1734 rulenum = parse_rulenumber(argv[optind++]);
1735 else
1736 exit_error(PARAMETER_PROBLEM,
1737 "-%c requires a rule number",
1738 cmd2char(CMD_REPLACE));
1739 break;
1740
1741 case 'I':
1742 add_command(&command, CMD_INSERT, CMD_NONE,
1743 invert);
1744 chain = optarg;
1745 if (optind < argc && argv[optind][0] != '-'
1746 && argv[optind][0] != '!')
1747 rulenum = parse_rulenumber(argv[optind++]);
1748 else rulenum = 1;
1749 break;
1750
1751 case 'L':
1752 add_command(&command, CMD_LIST, CMD_ZERO,
1753 invert);
1754 if (optarg) chain = optarg;
1755 else if (optind < argc && argv[optind][0] != '-'
1756 && argv[optind][0] != '!')
1757 chain = argv[optind++];
1758 break;
1759
1760 case 'F':
1761 add_command(&command, CMD_FLUSH, CMD_NONE,
1762 invert);
1763 if (optarg) chain = optarg;
1764 else if (optind < argc && argv[optind][0] != '-'
1765 && argv[optind][0] != '!')
1766 chain = argv[optind++];
1767 break;
1768
1769 case 'Z':
1770 add_command(&command, CMD_ZERO, CMD_LIST,
1771 invert);
1772 if (optarg) chain = optarg;
1773 else if (optind < argc && argv[optind][0] != '-'
1774 && argv[optind][0] != '!')
1775 chain = argv[optind++];
1776 break;
1777
1778 case 'N':
1779 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1780 invert);
1781 chain = optarg;
1782 break;
1783
1784 case 'X':
1785 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1786 invert);
1787 if (optarg) chain = optarg;
1788 else if (optind < argc && argv[optind][0] != '-'
1789 && argv[optind][0] != '!')
1790 chain = argv[optind++];
1791 break;
1792
1793 case 'E':
1794 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1795 invert);
1796 chain = optarg;
1797 if (optind < argc && argv[optind][0] != '-'
1798 && argv[optind][0] != '!')
1799 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001800 else
1801 exit_error(PARAMETER_PROBLEM,
1802 "-%c requires old-chain-name and "
1803 "new-chain-name",
1804 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001805 break;
1806
1807 case 'P':
1808 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1809 invert);
1810 chain = optarg;
1811 if (optind < argc && argv[optind][0] != '-'
1812 && argv[optind][0] != '!')
1813 policy = argv[optind++];
1814 else
1815 exit_error(PARAMETER_PROBLEM,
1816 "-%c requires a chain and a policy",
1817 cmd2char(CMD_SET_POLICY));
1818 break;
1819
1820 case 'h':
1821 if (!optarg)
1822 optarg = argv[optind];
1823
1824 /* iptables -p icmp -h */
1825 if (!ip6tables_matches && protocol)
1826 find_match(protocol, TRY_LOAD);
1827
1828 exit_printhelp();
1829
1830 /*
1831 * Option selection
1832 */
1833 case 'p':
1834 if (check_inverse(optarg, &invert))
1835 optind++;
1836 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1837 invert);
1838
1839 /* Canonicalize into lower case */
1840 for (protocol = argv[optind-1]; *protocol; protocol++)
1841 *protocol = tolower(*protocol);
1842
1843 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001844 if ( strcmp(protocol,"ipv6-icmp") == 0)
1845 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001846 fw.ipv6.proto = parse_protocol(protocol);
1847 fw.ipv6.flags |= IP6T_F_PROTO;
1848
1849 if (fw.ipv6.proto == 0
1850 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1851 exit_error(PARAMETER_PROBLEM,
1852 "rule would never match protocol");
1853 fw.nfcache |= NFC_IP6_PROTO;
1854 break;
1855
1856 case 's':
1857 if (check_inverse(optarg, &invert))
1858 optind++;
1859 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1860 invert);
1861 shostnetworkmask = argv[optind-1];
1862 fw.nfcache |= NFC_IP6_SRC;
1863 break;
1864
1865 case 'd':
1866 if (check_inverse(optarg, &invert))
1867 optind++;
1868 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1869 invert);
1870 dhostnetworkmask = argv[optind-1];
1871 fw.nfcache |= NFC_IP6_DST;
1872 break;
1873
1874 case 'j':
1875 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1876 invert);
1877 jumpto = parse_target(optarg);
1878 target = find_target(jumpto, TRY_LOAD);
1879
1880 if (target) {
1881 size_t size;
1882
1883 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1884 + target->size);
1885
1886 target->t = fw_calloc(1, size);
1887 target->t->u.target_size = size;
1888 strcpy(target->t->u.user.name, jumpto);
1889 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001890 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891 }
1892 break;
1893
1894
1895 case 'i':
1896 if (check_inverse(optarg, &invert))
1897 optind++;
1898 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1899 invert);
1900 parse_interface(argv[optind-1],
1901 fw.ipv6.iniface,
1902 fw.ipv6.iniface_mask);
1903 fw.nfcache |= NFC_IP6_IF_IN;
1904 break;
1905
1906 case 'o':
1907 if (check_inverse(optarg, &invert))
1908 optind++;
1909 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1910 invert);
1911 parse_interface(argv[optind-1],
1912 fw.ipv6.outiface,
1913 fw.ipv6.outiface_mask);
1914 fw.nfcache |= NFC_IP6_IF_OUT;
1915 break;
1916
1917 case 'v':
1918 if (!verbose)
1919 set_option(&options, OPT_VERBOSE,
1920 &fw.ipv6.invflags, invert);
1921 verbose++;
1922 break;
1923
1924 case 'm': {
1925 size_t size;
1926
1927 if (invert)
1928 exit_error(PARAMETER_PROBLEM,
1929 "unexpected ! flag before --match");
1930
1931 m = find_match(optarg, LOAD_MUST_SUCCEED);
1932 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1933 + m->size);
1934 m->m = fw_calloc(1, size);
1935 m->m->u.match_size = size;
1936 strcpy(m->m->u.user.name, m->name);
1937 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001938 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001939 }
1940 break;
1941
1942 case 'n':
1943 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1944 invert);
1945 break;
1946
1947 case 't':
1948 if (invert)
1949 exit_error(PARAMETER_PROBLEM,
1950 "unexpected ! flag before --table");
1951 *table = argv[optind-1];
1952 break;
1953
1954 case 'x':
1955 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1956 invert);
1957 break;
1958
1959 case 'V':
1960 if (invert)
1961 printf("Not %s ;-)\n", program_version);
1962 else
1963 printf("%s v%s\n",
1964 program_name, program_version);
1965 exit(0);
1966
1967 case '0':
1968 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1969 invert);
1970 break;
1971
1972 case 1: /* non option */
1973 if (optarg[0] == '!' && optarg[1] == '\0') {
1974 if (invert)
1975 exit_error(PARAMETER_PROBLEM,
1976 "multiple consecutive ! not"
1977 " allowed");
1978 invert = TRUE;
1979 optarg[0] = '\0';
1980 continue;
1981 }
1982 printf("Bad argument `%s'\n", optarg);
1983 exit_tryhelp(2);
1984
1985 default:
1986 /* FIXME: This scheme doesn't allow two of the same
1987 matches --RR */
1988 if (!target
1989 || !(target->parse(c - target->option_offset,
1990 argv, invert,
1991 &target->tflags,
1992 &fw, &target->t))) {
1993 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001994 if (!m->used)
1995 continue;
1996
Rusty Russell5eed48a2000-06-02 20:12:24 +00001997 if (m->parse(c - m->option_offset,
1998 argv, invert,
1999 &m->mflags,
2000 &fw,
2001 &fw.nfcache,
2002 &m->m))
2003 break;
2004 }
2005
2006 /* If you listen carefully, you can
2007 actually hear this code suck. */
2008 if (m == NULL
2009 && protocol
2010 && !find_proto(protocol, DONT_LOAD,
2011 options&OPT_NUMERIC)
2012 && (m = find_proto(protocol, TRY_LOAD,
2013 options&OPT_NUMERIC))) {
2014 /* Try loading protocol */
2015 size_t size;
2016
2017 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
2018 + m->size);
2019
2020 m->m = fw_calloc(1, size);
2021 m->m->u.match_size = size;
2022 strcpy(m->m->u.user.name, m->name);
2023 m->init(m->m, &fw.nfcache);
2024
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002025 opts = merge_options(opts,
2026 m->extra_opts, &m->option_offset);
2027
Rusty Russell5eed48a2000-06-02 20:12:24 +00002028 optind--;
2029 continue;
2030 }
2031 if (!m)
2032 exit_error(PARAMETER_PROBLEM,
2033 "Unknown arg `%s'",
2034 argv[optind-1]);
2035 }
2036 }
2037 invert = FALSE;
2038 }
2039
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002040 for (m = ip6tables_matches; m; m = m->next) {
2041 if (!m->used)
2042 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002043
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002044 m->final_check(m->mflags);
2045 }
2046
2047 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002048 target->final_check(target->tflags);
2049
2050 /* Fix me: must put inverse options checking here --MN */
2051
2052 if (optind < argc)
2053 exit_error(PARAMETER_PROBLEM,
2054 "unknown arguments found on commandline");
2055 if (!command)
2056 exit_error(PARAMETER_PROBLEM, "no command specified");
2057 if (invert)
2058 exit_error(PARAMETER_PROBLEM,
2059 "nothing appropriate following !");
2060
2061 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2062 CMD_CHECK)) {
2063 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002064 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002065 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002066 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002067 }
2068
2069 if (shostnetworkmask)
2070 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2071 &(fw.ipv6.smsk), &nsaddrs);
2072
2073 if (dhostnetworkmask)
2074 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2075 &(fw.ipv6.dmsk), &ndaddrs);
2076
2077 if ((nsaddrs > 1 || ndaddrs > 1) &&
2078 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2079 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2080 " source or destination IP addresses");
2081
2082 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2083 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2084 cmd2char(CMD_CHECK));
2085
2086 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2087 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2088 "specify a unique address");
2089
2090 generic_opt_check(command, options);
2091
2092 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2093 exit_error(PARAMETER_PROBLEM,
2094 "chain name `%s' too long (must be under %i chars)",
2095 chain, IP6T_FUNCTION_MAXNAMELEN);
2096
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002097 /* only allocate handle if we weren't called with a handle */
2098 if (!*handle)
2099 *handle = ip6tc_init(*table);
2100
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002101 if (!*handle) {
2102 /* try to insmod the module if iptc_init failed */
2103 ip6tables_insmod("ip6_tables", modprobe);
2104 *handle = ip6tc_init(*table);
2105 }
2106
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002107 if (!*handle)
2108 exit_error(VERSION_PROBLEM,
2109 "can't initialize ip6tables table `%s': %s",
2110 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002111
2112 if (command == CMD_CHECK
2113 || command == CMD_APPEND
2114 || command == CMD_DELETE
2115 || command == CMD_INSERT
2116 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002117 if (strcmp(chain, "PREROUTING") == 0
2118 || strcmp(chain, "INPUT") == 0) {
2119 /* -o not valid with incoming packets. */
2120 if (options & OPT_VIANAMEOUT)
2121 exit_error(PARAMETER_PROBLEM,
2122 "Can't use -%c with %s\n",
2123 opt2char(OPT_VIANAMEOUT),
2124 chain);
2125 /* -i required with -C */
2126 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2127 exit_error(PARAMETER_PROBLEM,
2128 "Need -%c with %s\n",
2129 opt2char(OPT_VIANAMEIN),
2130 chain);
2131 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002132
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002133 if (strcmp(chain, "POSTROUTING") == 0
2134 || strcmp(chain, "OUTPUT") == 0) {
2135 /* -i not valid with outgoing packets */
2136 if (options & OPT_VIANAMEIN)
2137 exit_error(PARAMETER_PROBLEM,
2138 "Can't use -%c with %s\n",
2139 opt2char(OPT_VIANAMEIN),
2140 chain);
2141 /* -o required with -C */
2142 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2143 exit_error(PARAMETER_PROBLEM,
2144 "Need -%c with %s\n",
2145 opt2char(OPT_VIANAMEOUT),
2146 chain);
2147 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002148
2149 if (target && ip6tc_is_chain(jumpto, *handle)) {
2150 printf("Warning: using chain %s, not extension\n",
2151 jumpto);
2152
2153 target = NULL;
2154 }
2155
2156 /* If they didn't specify a target, or it's a chain
2157 name, use standard. */
2158 if (!target
2159 && (strlen(jumpto) == 0
2160 || ip6tc_is_chain(jumpto, *handle))) {
2161 size_t size;
2162
2163 target = find_target(IP6T_STANDARD_TARGET,
2164 LOAD_MUST_SUCCEED);
2165
2166 size = sizeof(struct ip6t_entry_target)
2167 + target->size;
2168 target->t = fw_calloc(1, size);
2169 target->t->u.target_size = size;
2170 strcpy(target->t->u.user.name, jumpto);
2171 target->init(target->t, &fw.nfcache);
2172 }
2173
2174 if (!target) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002175#if 0
Rusty Russell5eed48a2000-06-02 20:12:24 +00002176 struct ip6t_entry_target unknown_target;
2177
2178 /* Don't know it. Must be extension with no
2179 options? */
2180 unknown_target.u.target_size = sizeof(unknown_target);
2181 strcpy(unknown_target.u.user.name, jumpto);
2182
2183 e = generate_entry(&fw, ip6tables_matches,
2184 &unknown_target);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002185#endif
2186 /* it is no chain, and we can't load a plugin.
2187 * We cannot know if the plugin is corrupt, non
2188 * existant OR if the user just misspelled a
2189 * chain. */
2190 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002191 } else {
2192 e = generate_entry(&fw, ip6tables_matches, target->t);
2193 }
2194 }
2195
2196 switch (command) {
2197 case CMD_APPEND:
2198 ret = append_entry(chain, e,
2199 nsaddrs, saddrs, ndaddrs, daddrs,
2200 options&OPT_VERBOSE,
2201 handle);
2202 break;
2203 case CMD_CHECK:
2204 ret = check_packet(chain, e,
2205 nsaddrs, saddrs, ndaddrs, daddrs,
2206 options&OPT_VERBOSE, handle);
2207 break;
2208 case CMD_DELETE:
2209 ret = delete_entry(chain, e,
2210 nsaddrs, saddrs, ndaddrs, daddrs,
2211 options&OPT_VERBOSE,
2212 handle);
2213 break;
2214 case CMD_DELETE_NUM:
2215 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2216 break;
2217 case CMD_REPLACE:
2218 ret = replace_entry(chain, e, rulenum - 1,
2219 saddrs, daddrs, options&OPT_VERBOSE,
2220 handle);
2221 break;
2222 case CMD_INSERT:
2223 ret = insert_entry(chain, e, rulenum - 1,
2224 nsaddrs, saddrs, ndaddrs, daddrs,
2225 options&OPT_VERBOSE,
2226 handle);
2227 break;
2228 case CMD_LIST:
2229 ret = list_entries(chain,
2230 options&OPT_VERBOSE,
2231 options&OPT_NUMERIC,
2232 options&OPT_EXPANDED,
2233 options&OPT_LINENUMBERS,
2234 handle);
2235 break;
2236 case CMD_FLUSH:
2237 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2238 break;
2239 case CMD_ZERO:
2240 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2241 break;
2242 case CMD_LIST|CMD_ZERO:
2243 ret = list_entries(chain,
2244 options&OPT_VERBOSE,
2245 options&OPT_NUMERIC,
2246 options&OPT_EXPANDED,
2247 options&OPT_LINENUMBERS,
2248 handle);
2249 if (ret)
2250 ret = zero_entries(chain,
2251 options&OPT_VERBOSE, handle);
2252 break;
2253 case CMD_NEW_CHAIN:
2254 ret = ip6tc_create_chain(chain, handle);
2255 break;
2256 case CMD_DELETE_CHAIN:
2257 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2258 break;
2259 case CMD_RENAME_CHAIN:
2260 ret = ip6tc_rename_chain(chain, newname, handle);
2261 break;
2262 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002263 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002264 break;
2265 default:
2266 /* We should never reach this... */
2267 exit_tryhelp(2);
2268 }
2269
2270 if (verbose > 1)
2271 dump_entries6(*handle);
2272
2273 return ret;
2274}