blob: bce4b7bc6bf3f6e0e1281e8b0f6b72ae0c4067ff [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);
531
532 if ( (err=getnameinfo((struct sockaddr *)&saddr,
533 sizeof(struct sockaddr_in6),
534 hostname, sizeof(hostname)-1,
535 NULL, 0, 0)) != 0 ){
536#ifdef DEBUG
537 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
538#endif
539 return (char *) NULL;
540 } else {
541#ifdef DEBUG
542 fprintf (stderr, "\naddr2host: %s\n", hostname);
543#endif
544
545 return hostname;
546 }
547
548 return (char *) NULL;
549}
550
Rusty Russell5eed48a2000-06-02 20:12:24 +0000551static char *
552mask_to_numeric(const struct in6_addr *addrp)
553{
554 static char buf[20];
555 int l = ipv6_prefix_length(addrp);
556 if (l == -1)
557 return addr_to_numeric(addrp);
558 sprintf(buf, "%d", l);
559 return buf;
560}
561
562static struct in6_addr *
563network_to_addr(const char *name)
564{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000565 /* abort();*/
566 /* TODO: not implemented yet, but the exception breaks the
567 * name resolvation */
568 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000569}
570
571static char *
572addr_to_anyname(const struct in6_addr *addr)
573{
574 char *name;
575
576 if ((name = addr_to_host(addr)) != NULL)
577 return name;
578
579 return addr_to_numeric(addr);
580}
581
582/*
583 * All functions starting with "parse" should succeed, otherwise
584 * the program fails.
585 * Most routines return pointers to static data that may change
586 * between calls to the same or other routines with a few exceptions:
587 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
588 * return global static data.
589*/
590
591static struct in6_addr *
592parse_hostnetwork(const char *name, unsigned int *naddrs)
593{
594 struct in6_addr *addrp, *addrptmp;
595
596 if ((addrptmp = numeric_to_addr(name)) != NULL ||
597 (addrptmp = network_to_addr(name)) != NULL) {
598 addrp = fw_malloc(sizeof(struct in6_addr));
599 in6addrcpy(addrp, addrptmp);
600 *naddrs = 1;
601 return addrp;
602 }
603 if ((addrp = host_to_addr(name, naddrs)) != NULL)
604 return addrp;
605
606 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
607}
608
609static struct in6_addr *
610parse_mask(char *mask)
611{
612 static struct in6_addr maskaddr;
613 struct in6_addr *addrp;
614 int bits;
615
616 if (mask == NULL) {
617 /* no mask at all defaults to 128 bits */
618 memset(&maskaddr, 0xff, sizeof maskaddr);
619 return &maskaddr;
620 }
621 if ((addrp = numeric_to_addr(mask)) != NULL)
622 return addrp;
623 if ((bits = string_to_number(mask, 0, 128)) == -1)
624 exit_error(PARAMETER_PROBLEM,
625 "invalid mask `%s' specified", mask);
626 if (bits != 0) {
627 char *p = (char *)&maskaddr;
628 memset(p, 0xff, bits / 8);
629 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
630 p[bits / 8] = 0xff << (8 - (bits & 7));
631 return &maskaddr;
632 }
633
634 memset(&maskaddr, 0, sizeof maskaddr);
635 return &maskaddr;
636}
637
638static void
639parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
640 struct in6_addr *maskp, unsigned int *naddrs)
641{
642 struct in6_addr *addrp;
643 char buf[256];
644 char *p;
645 int i, j, n;
646
647 strncpy(buf, name, sizeof(buf) - 1);
648 if ((p = strrchr(buf, '/')) != NULL) {
649 *p = '\0';
650 addrp = parse_mask(p + 1);
651 } else
652 addrp = parse_mask(NULL);
653 in6addrcpy(maskp, addrp);
654
655 /* if a null mask is given, the name is ignored, like in "any/0" */
656 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
657 strcpy(buf, "::");
658
659 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
660 n = *naddrs;
661 for (i = 0, j = 0; i < n; i++) {
662 int k;
663 for (k = 0; k < 4; k++)
664 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
665 j++;
666 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000667 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000668 (*naddrs)--;
669 j--;
670 break;
671 }
672 }
673 }
674}
675
676struct ip6tables_match *
677find_match(const char *name, enum ip6t_tryload tryload)
678{
679 struct ip6tables_match *ptr;
680
681 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
682 if (strcmp(name, ptr->name) == 0)
683 break;
684 }
685
686 if (!ptr && tryload != DONT_LOAD) {
687 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
688 + strlen(name)];
689 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
690 if (dlopen(path, RTLD_NOW)) {
691 /* Found library. If it didn't register itself,
692 maybe they specified target as match. */
693 ptr = find_match(name, DONT_LOAD);
694
695 if (!ptr)
696 exit_error(PARAMETER_PROBLEM,
697 "Couldn't load match `%s'\n",
698 name);
699 } else if (tryload == LOAD_MUST_SUCCEED)
700 exit_error(PARAMETER_PROBLEM,
701 "Couldn't load match `%s'\n", name);
702 }
703
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000704 if (ptr)
705 ptr->used = 1;
706
707
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708 return ptr;
709}
710
711/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
712static struct ip6tables_match *
713find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
714{
715 int proto;
716
717 proto = string_to_number(pname, 0, 255);
718 if (proto != -1)
719 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{
727 int proto = string_to_number(s, 0, 255);
728
729 if (proto == -1) {
730 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{
795 int rulenum = string_to_number(rule, 1, INT_MAX);
796
797 if (rulenum == -1)
798 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
826string_to_number(const char *s, int min, int max)
827{
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000828 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000829 char *end;
830
831 /* Handle hex, octal, etc. */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000832 errno = 0;
833 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000834 if (*end == '\0' && end != s) {
835 /* we parsed a number, let's see if we want this */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000836 if (errno != ERANGE && min <= number && number <= max)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000837 return number;
838 }
839 return -1;
840}
841
842static void
843set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
844 int invert)
845{
846 if (*options & option)
847 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
848 opt2char(option));
849 *options |= option;
850
851 if (invert) {
852 unsigned int i;
853 for (i = 0; 1 << i != option; i++);
854
855 if (!inverse_for_options[i])
856 exit_error(PARAMETER_PROBLEM,
857 "cannot have ! before -%c",
858 opt2char(option));
859 *invflg |= inverse_for_options[i];
860 }
861}
862
863struct ip6tables_target *
864find_target(const char *name, enum ip6t_tryload tryload)
865{
866 struct ip6tables_target *ptr;
867
868 /* Standard target? */
869 if (strcmp(name, "") == 0
870 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
871 || strcmp(name, IP6TC_LABEL_DROP) == 0
872 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
873 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
874 name = "standard";
875
876 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
877 if (strcmp(name, ptr->name) == 0)
878 break;
879 }
880
881 if (!ptr && tryload != DONT_LOAD) {
882 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
883 + strlen(name)];
884 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
885 if (dlopen(path, RTLD_NOW)) {
886 /* Found library. If it didn't register itself,
887 maybe they specified match as a target. */
888 ptr = find_target(name, DONT_LOAD);
889 if (!ptr)
890 exit_error(PARAMETER_PROBLEM,
891 "Couldn't load target `%s'\n",
892 name);
893 } else if (tryload == LOAD_MUST_SUCCEED)
894 exit_error(PARAMETER_PROBLEM,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000895 "Couldn't load target `%s'%s\n",
896 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000897 }
898
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000899 if (ptr)
900 ptr->used = 1;
901
Rusty Russell5eed48a2000-06-02 20:12:24 +0000902 return ptr;
903}
904
905static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000906merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000907 unsigned int *option_offset)
908{
909 unsigned int num_old, num_new, i;
910 struct option *merge;
911
912 for (num_old = 0; oldopts[num_old].name; num_old++);
913 for (num_new = 0; newopts[num_new].name; num_new++);
914
915 global_option_offset += OPTION_OFFSET;
916 *option_offset = global_option_offset;
917
918 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
919 memcpy(merge, oldopts, num_old * sizeof(struct option));
920 for (i = 0; i < num_new; i++) {
921 merge[num_old + i] = newopts[i];
922 merge[num_old + i].val += *option_offset;
923 }
924 memset(merge + num_old + num_new, 0, sizeof(struct option));
925
926 return merge;
927}
928
929void
930register_match6(struct ip6tables_match *me)
931{
932 if (strcmp(me->version, program_version) != 0) {
933 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
934 program_name, me->name, me->version, program_version);
935 exit(1);
936 }
937
938 if (find_match(me->name, DONT_LOAD)) {
939 fprintf(stderr, "%s: match `%s' already registered.\n",
940 program_name, me->name);
941 exit(1);
942 }
943
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000944 if (me->size != IP6T_ALIGN(me->size)) {
945 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
946 program_name, me->name, me->size);
947 exit(1);
948 }
949
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950 /* Prepend to list. */
951 me->next = ip6tables_matches;
952 ip6tables_matches = me;
953 me->m = NULL;
954 me->mflags = 0;
955
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000956 /* opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000957}
958
959void
960register_target6(struct ip6tables_target *me)
961{
962 if (strcmp(me->version, program_version) != 0) {
963 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
964 program_name, me->name, me->version, program_version);
965 exit(1);
966 }
967
968 if (find_target(me->name, DONT_LOAD)) {
969 fprintf(stderr, "%s: target `%s' already registered.\n",
970 program_name, me->name);
971 exit(1);
972 }
973
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000974 if (me->size != IP6T_ALIGN(me->size)) {
975 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
976 program_name, me->name, me->size);
977 exit(1);
978 }
979
Rusty Russell5eed48a2000-06-02 20:12:24 +0000980 /* Prepend to list. */
981 me->next = ip6tables_targets;
982 ip6tables_targets = me;
983 me->t = NULL;
984 me->tflags = 0;
985
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000986 /*opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
987}
988
989static void
990print_num(u_int64_t number, unsigned int format)
991{
992 if (format & FMT_KILOMEGAGIGA) {
993 if (number > 99999) {
994 number = (number + 500) / 1000;
995 if (number > 9999) {
996 number = (number + 500) / 1000;
997 if (number > 9999) {
998 number = (number + 500) / 1000;
999 printf(FMT("%4lluG ","%lluG "),number);
1000 }
1001 else printf(FMT("%4lluM ","%lluM "), number);
1002 } else
1003 printf(FMT("%4lluK ","%lluK "), number);
1004 } else
1005 printf(FMT("%5llu ","%llu "), number);
1006 } else
1007 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001008}
1009
1010static void
1011print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1012{
1013 struct ip6t_counters counters;
1014 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1015 printf("Chain %s", chain);
1016 if (pol) {
1017 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001018 if (!(format & FMT_NOCOUNTS)) {
1019 /*printf(" %llu packets, %llu bytes",
1020 counters.pcnt, counters.bcnt);*/
1021 fputc(' ', stdout);
1022 print_num(counters.pcnt, (format|FMT_NOTABLE));
1023 fputs("packets, ", stdout);
1024 print_num(counters.bcnt, (format|FMT_NOTABLE));
1025 fputs("bytes", stdout);
1026 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027 printf(")\n");
1028 } else {
1029 unsigned int refs;
1030 if (!ip6tc_get_references(&refs, chain, handle))
1031 printf(" (ERROR obtaining refs)\n");
1032 else
1033 printf(" (%u references)\n", refs);
1034 }
1035
1036 if (format & FMT_LINENUMBERS)
1037 printf(FMT("%-4s ", "%s "), "num");
1038 if (!(format & FMT_NOCOUNTS)) {
1039 if (format & FMT_KILOMEGAGIGA) {
1040 printf(FMT("%5s ","%s "), "pkts");
1041 printf(FMT("%5s ","%s "), "bytes");
1042 } else {
1043 printf(FMT("%8s ","%s "), "pkts");
1044 printf(FMT("%10s ","%s "), "bytes");
1045 }
1046 }
1047 if (!(format & FMT_NOTARGET))
1048 printf(FMT("%-9s ","%s "), "target");
1049 fputs(" prot ", stdout);
1050 if (format & FMT_OPTIONS)
1051 fputs("opt", stdout);
1052 if (format & FMT_VIA) {
1053 printf(FMT(" %-6s ","%s "), "in");
1054 printf(FMT("%-6s ","%s "), "out");
1055 }
1056 printf(FMT(" %-19s ","%s "), "source");
1057 printf(FMT(" %-19s "," %s "), "destination");
1058 printf("\n");
1059}
1060
Rusty Russell5eed48a2000-06-02 20:12:24 +00001061static int
1062print_match(const struct ip6t_entry_match *m,
1063 const struct ip6t_ip6 *ip,
1064 int numeric)
1065{
1066 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1067
1068 if (match) {
1069 if (match->print)
1070 match->print(ip, m, numeric);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001071 else
1072 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001073 } else {
1074 if (m->u.user.name[0])
1075 printf("UNKNOWN match `%s' ", m->u.user.name);
1076 }
1077 /* Don't stop iterating. */
1078 return 0;
1079}
1080
1081/* e is called `fw' here for hysterical raisins */
1082static void
1083print_firewall(const struct ip6t_entry *fw,
1084 const char *targname,
1085 unsigned int num,
1086 unsigned int format,
1087 const ip6tc_handle_t handle)
1088{
1089 struct ip6tables_target *target = NULL;
1090 const struct ip6t_entry_target *t;
1091 u_int8_t flags;
1092 char buf[BUFSIZ];
1093
1094 /* User creates a chain called "REJECT": this overrides the
1095 `REJECT' target module. Keep feeding them rope until the
1096 revolution... Bwahahahahah */
1097 if (!ip6tc_is_chain(targname, handle))
1098 target = find_target(targname, TRY_LOAD);
1099 else
1100 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1101
1102 t = ip6t_get_target((struct ip6t_entry *)fw);
1103 flags = fw->ipv6.flags;
1104
1105 if (format & FMT_LINENUMBERS)
1106 printf(FMT("%-4u ", "%u "), num+1);
1107
1108 if (!(format & FMT_NOCOUNTS)) {
1109 print_num(fw->counters.pcnt, format);
1110 print_num(fw->counters.bcnt, format);
1111 }
1112
1113 if (!(format & FMT_NOTARGET))
1114 printf(FMT("%-9s ", "%s "), targname);
1115
1116 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1117 {
1118 char *pname = proto_to_name(fw->ipv6.proto,
1119 format&FMT_NUMERIC);
1120 if (pname)
1121 printf(FMT("%-5s", "%s "), pname);
1122 else
1123 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1124 }
1125
1126 if (format & FMT_OPTIONS) {
1127 if (format & FMT_NOTABLE)
1128 fputs("opt ", stdout);
1129 fputc(' ', stdout);
1130 fputc(' ', stdout);
1131 fputc(' ', stdout);
1132 }
1133
1134 if (format & FMT_VIA) {
1135 char iface[IFNAMSIZ+2];
1136
1137 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1138 iface[0] = '!';
1139 iface[1] = '\0';
1140 }
1141 else iface[0] = '\0';
1142
1143 if (fw->ipv6.iniface[0] != '\0') {
1144 strcat(iface, fw->ipv6.iniface);
1145 /* If it doesn't compare the nul-term, it's a
1146 wildcard. */
1147 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1148 strcat(iface, "+");
1149 }
1150 else if (format & FMT_NUMERIC) strcat(iface, "*");
1151 else strcat(iface, "any");
1152 printf(FMT(" %-6s ","in %s "), iface);
1153
1154 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1155 iface[0] = '!';
1156 iface[1] = '\0';
1157 }
1158 else iface[0] = '\0';
1159
1160 if (fw->ipv6.outiface[0] != '\0') {
1161 strcat(iface, fw->ipv6.outiface);
1162 /* If it doesn't compare the nul-term, it's a
1163 wildcard. */
1164 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1165 strcat(iface, "+");
1166 }
1167 else if (format & FMT_NUMERIC) strcat(iface, "*");
1168 else strcat(iface, "any");
1169 printf(FMT("%-6s ","out %s "), iface);
1170 }
1171
1172 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1173 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1174 && !(format & FMT_NUMERIC))
1175 printf(FMT("%-19s ","%s "), "anywhere");
1176 else {
1177 if (format & FMT_NUMERIC)
1178 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1179 else
1180 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1181 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1182 printf(FMT("%-19s ","%s "), buf);
1183 }
1184
1185 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1186 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1187 && !(format & FMT_NUMERIC))
1188 printf(FMT("%-19s","-> %s"), "anywhere");
1189 else {
1190 if (format & FMT_NUMERIC)
1191 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1192 else
1193 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1194 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1195 printf(FMT("%-19s","-> %s"), buf);
1196 }
1197
1198 if (format & FMT_NOTABLE)
1199 fputs(" ", stdout);
1200
1201 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1202
1203 if (target) {
1204 if (target->print)
1205 /* Print the target information. */
1206 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1207 } else if (t->u.target_size != sizeof(*t))
1208 printf("[%u bytes of unknown target data] ",
1209 t->u.target_size - sizeof(*t));
1210
1211 if (!(format & FMT_NONEWLINE))
1212 fputc('\n', stdout);
1213}
1214
1215static void
1216print_firewall_line(const struct ip6t_entry *fw,
1217 const ip6tc_handle_t h)
1218{
1219 struct ip6t_entry_target *t;
1220
1221 t = ip6t_get_target((struct ip6t_entry *)fw);
1222 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1223}
1224
1225static int
1226append_entry(const ip6t_chainlabel chain,
1227 struct ip6t_entry *fw,
1228 unsigned int nsaddrs,
1229 const struct in6_addr saddrs[],
1230 unsigned int ndaddrs,
1231 const struct in6_addr daddrs[],
1232 int verbose,
1233 ip6tc_handle_t *handle)
1234{
1235 unsigned int i, j;
1236 int ret = 1;
1237
1238 for (i = 0; i < nsaddrs; i++) {
1239 fw->ipv6.src = saddrs[i];
1240 for (j = 0; j < ndaddrs; j++) {
1241 fw->ipv6.dst = daddrs[j];
1242 if (verbose)
1243 print_firewall_line(fw, *handle);
1244 ret &= ip6tc_append_entry(chain, fw, handle);
1245 }
1246 }
1247
1248 return ret;
1249}
1250
1251static int
1252replace_entry(const ip6t_chainlabel chain,
1253 struct ip6t_entry *fw,
1254 unsigned int rulenum,
1255 const struct in6_addr *saddr,
1256 const struct in6_addr *daddr,
1257 int verbose,
1258 ip6tc_handle_t *handle)
1259{
1260 fw->ipv6.src = *saddr;
1261 fw->ipv6.dst = *daddr;
1262
1263 if (verbose)
1264 print_firewall_line(fw, *handle);
1265 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1266}
1267
1268static int
1269insert_entry(const ip6t_chainlabel chain,
1270 struct ip6t_entry *fw,
1271 unsigned int rulenum,
1272 unsigned int nsaddrs,
1273 const struct in6_addr saddrs[],
1274 unsigned int ndaddrs,
1275 const struct in6_addr daddrs[],
1276 int verbose,
1277 ip6tc_handle_t *handle)
1278{
1279 unsigned int i, j;
1280 int ret = 1;
1281
1282 for (i = 0; i < nsaddrs; i++) {
1283 fw->ipv6.src = saddrs[i];
1284 for (j = 0; j < ndaddrs; j++) {
1285 fw->ipv6.dst = daddrs[j];
1286 if (verbose)
1287 print_firewall_line(fw, *handle);
1288 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1289 }
1290 }
1291
1292 return ret;
1293}
1294
1295static unsigned char *
1296make_delete_mask(struct ip6t_entry *fw)
1297{
1298 /* Establish mask for comparison */
1299 unsigned int size;
1300 struct ip6tables_match *m;
1301 unsigned char *mask, *mptr;
1302
1303 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001304 for (m = ip6tables_matches; m; m = m->next) {
1305 if (!m->used)
1306 continue;
1307
1308 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1309 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001310
1311 mask = fw_calloc(1, size
1312 + sizeof(struct ip6t_entry_target)
1313 + ip6tables_targets->size);
1314
1315 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1316 mptr = mask + sizeof(struct ip6t_entry);
1317
1318 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001319 if (!m->used)
1320 continue;
1321
Rusty Russell5eed48a2000-06-02 20:12:24 +00001322 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001323 IP6T_ALIGN(sizeof(struct ip6t_entry_match)) +
1324 m->userspacesize);
1325 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001326 }
1327
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001328 memset(mptr, 0xFF,
1329 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1330 + ip6tables_targets->userspacesize);
1331 /*mptr += sizeof(struct ip6t_entry_target);
1332 memset(mptr, 0xFF, ip6tables_targets->userspacesize);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +00001333
1334 return mask;
1335}
1336
1337static int
1338delete_entry(const ip6t_chainlabel chain,
1339 struct ip6t_entry *fw,
1340 unsigned int nsaddrs,
1341 const struct in6_addr saddrs[],
1342 unsigned int ndaddrs,
1343 const struct in6_addr daddrs[],
1344 int verbose,
1345 ip6tc_handle_t *handle)
1346{
1347 unsigned int i, j;
1348 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349 unsigned char *mask;
1350
1351 mask = make_delete_mask(fw);
1352 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001353 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001354 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001355 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001356 if (verbose)
1357 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001358 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001359 }
1360 }
1361 return ret;
1362}
1363
1364static int
1365check_packet(const ip6t_chainlabel chain,
1366 struct ip6t_entry *fw,
1367 unsigned int nsaddrs,
1368 const struct in6_addr saddrs[],
1369 unsigned int ndaddrs,
1370 const struct in6_addr daddrs[],
1371 int verbose,
1372 ip6tc_handle_t *handle)
1373{
1374 int ret = 1;
1375 unsigned int i, j;
1376 struct ip6t_entry ipfw = *fw;
1377 const char *msg;
1378
1379 for (i = 0; i < nsaddrs; i++) {
1380 ipfw.ipv6.src = saddrs[i];
1381 for (j = 0; j < ndaddrs; j++) {
1382 ipfw.ipv6.dst = daddrs[j];
1383 if (verbose)
1384 print_firewall_line(fw, *handle);
1385 msg = ip6tc_check_packet(chain, &ipfw, handle);
1386 if (!msg) ret = 0;
1387 else printf("%s\n", msg);
1388 }
1389 }
1390
1391 return ret;
1392}
1393
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001394/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001395int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001396for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1397 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1398{
1399 int ret = 1;
1400 const char *chain;
1401 char *chains;
1402 unsigned int i, chaincount = 0;
1403
1404 chain = ip6tc_first_chain(handle);
1405 while (chain) {
1406 chaincount++;
1407 chain = ip6tc_next_chain(handle);
1408 }
1409
1410 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1411 i = 0;
1412 chain = ip6tc_first_chain(handle);
1413 while (chain) {
1414 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1415 i++;
1416 chain = ip6tc_next_chain(handle);
1417 }
1418
1419 for (i = 0; i < chaincount; i++) {
1420 if (!builtinstoo
1421 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1422 *handle))
1423 continue;
1424 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1425 }
1426
1427 free(chains);
1428 return ret;
1429}
1430
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001431/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001432int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433flush_entries(const ip6t_chainlabel chain, int verbose,
1434 ip6tc_handle_t *handle)
1435{
1436 if (!chain)
1437 return for_each_chain(flush_entries, verbose, 1, handle);
1438
1439 if (verbose)
1440 fprintf(stdout, "Flushing chain `%s'\n", chain);
1441 return ip6tc_flush_entries(chain, handle);
1442}
1443
1444static int
1445zero_entries(const ip6t_chainlabel chain, int verbose,
1446 ip6tc_handle_t *handle)
1447{
1448 if (!chain)
1449 return for_each_chain(zero_entries, verbose, 1, handle);
1450
1451 if (verbose)
1452 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1453 return ip6tc_zero_entries(chain, handle);
1454}
1455
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001456/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001457int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458delete_chain(const ip6t_chainlabel chain, int verbose,
1459 ip6tc_handle_t *handle)
1460{
1461 if (!chain)
1462 return for_each_chain(delete_chain, verbose, 0, handle);
1463
1464 if (verbose)
1465 fprintf(stdout, "Deleting chain `%s'\n", chain);
1466 return ip6tc_delete_chain(chain, handle);
1467}
1468
1469static int
1470list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1471 int expanded, int linenumbers, ip6tc_handle_t *handle)
1472{
1473 int found = 0;
1474 unsigned int format;
1475 const char *this;
1476
1477 format = FMT_OPTIONS;
1478 if (!verbose)
1479 format |= FMT_NOCOUNTS;
1480 else
1481 format |= FMT_VIA;
1482
1483 if (numeric)
1484 format |= FMT_NUMERIC;
1485
1486 if (!expanded)
1487 format |= FMT_KILOMEGAGIGA;
1488
1489 if (linenumbers)
1490 format |= FMT_LINENUMBERS;
1491
1492 for (this = ip6tc_first_chain(handle);
1493 this;
1494 this = ip6tc_next_chain(handle)) {
1495 const struct ip6t_entry *i;
1496 unsigned int num;
1497
1498 if (chain && strcmp(chain, this) != 0)
1499 continue;
1500
1501 if (found) printf("\n");
1502
1503 print_header(format, this, handle);
1504 i = ip6tc_first_rule(this, handle);
1505
1506 num = 0;
1507 while (i) {
1508 print_firewall(i,
1509 ip6tc_get_target(i, handle),
1510 num++,
1511 format,
1512 *handle);
1513 i = ip6tc_next_rule(i, handle);
1514 }
1515 found = 1;
1516 }
1517
1518 errno = ENOENT;
1519 return found;
1520}
1521
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001522static char *get_modprobe(void)
1523{
1524 int procfile;
1525 char *ret;
1526
1527 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1528 if (procfile < 0)
1529 return NULL;
1530
1531 ret = malloc(1024);
1532 if (ret) {
1533 switch (read(procfile, ret, 1024)) {
1534 case -1: goto fail;
1535 case 1024: goto fail; /* Partial read. Wierd */
1536 }
1537 if (ret[strlen(ret)-1]=='\n')
1538 ret[strlen(ret)-1]=0;
1539 close(procfile);
1540 return ret;
1541 }
1542 fail:
1543 free(ret);
1544 close(procfile);
1545 return NULL;
1546}
1547
Harald Welte58918652001-06-16 18:25:25 +00001548int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001549{
1550 char *buf = NULL;
1551 char *argv[3];
1552
1553 /* If they don't explicitly set it, read out of kernel */
1554 if (!modprobe) {
1555 buf = get_modprobe();
1556 if (!buf)
1557 return -1;
1558 modprobe = buf;
1559 }
1560
1561 switch (fork()) {
1562 case 0:
1563 argv[0] = (char *)modprobe;
1564 argv[1] = (char *)modname;
1565 argv[2] = NULL;
1566 execv(argv[0], argv);
1567
1568 /* not usually reached */
1569 exit(0);
1570 case -1:
1571 return -1;
1572
1573 default: /* parent */
1574 wait(NULL);
1575 }
1576
1577 free(buf);
1578 return 0;
1579}
1580
Rusty Russell5eed48a2000-06-02 20:12:24 +00001581static struct ip6t_entry *
1582generate_entry(const struct ip6t_entry *fw,
1583 struct ip6tables_match *matches,
1584 struct ip6t_entry_target *target)
1585{
1586 unsigned int size;
1587 struct ip6tables_match *m;
1588 struct ip6t_entry *e;
1589
1590 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001591 for (m = matches; m; m = m->next) {
1592 if (!m->used)
1593 continue;
1594
Rusty Russell5eed48a2000-06-02 20:12:24 +00001595 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001596 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001597
1598 e = fw_malloc(size + target->u.target_size);
1599 *e = *fw;
1600 e->target_offset = size;
1601 e->next_offset = size + target->u.target_size;
1602
1603 size = 0;
1604 for (m = matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001605 if (!m->used)
1606 continue;
1607
Rusty Russell5eed48a2000-06-02 20:12:24 +00001608 memcpy(e->elems + size, m->m, m->m->u.match_size);
1609 size += m->m->u.match_size;
1610 }
1611 memcpy(e->elems + size, target, target->u.target_size);
1612
1613 return e;
1614}
1615
1616int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1617{
1618 struct ip6t_entry fw, *e = NULL;
1619 int invert = 0;
1620 unsigned int nsaddrs = 0, ndaddrs = 0;
1621 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1622
1623 int c, verbose = 0;
1624 const char *chain = NULL;
1625 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1626 const char *policy = NULL, *newname = NULL;
1627 unsigned int rulenum = 0, options = 0, command = 0;
1628 int ret = 1;
1629 struct ip6tables_match *m;
1630 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001631 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001632 const char *jumpto = "";
1633 char *protocol = NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001634 const char *modprobe = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001635
1636 memset(&fw, 0, sizeof(fw));
1637
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001638 opts = original_opts;
1639 global_option_offset = 0;
1640
1641 /* re-set optind to 0 in case do_command gets called
1642 * a second time */
1643 optind = 0;
1644
1645 /* clear mflags in case do_command gets called a second time
1646 * (we clear the global list of all matches for security)*/
1647 for (m = ip6tables_matches; m; m = m->next) {
1648 m->mflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001649 m->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001650 }
1651
1652 for (t = ip6tables_targets; t; t = t->next) {
1653 t->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001654 t->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001655 }
1656
Rusty Russell5eed48a2000-06-02 20:12:24 +00001657 /* Suppress error messages: we may add new options if we
1658 demand-load a protocol. */
1659 opterr = 0;
1660
1661 while ((c = getopt_long(argc, argv,
1662 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1663 opts, NULL)) != -1) {
1664 switch (c) {
1665 /*
1666 * Command selection
1667 */
1668 case 'A':
1669 add_command(&command, CMD_APPEND, CMD_NONE,
1670 invert);
1671 chain = optarg;
1672 break;
1673
1674 case 'D':
1675 add_command(&command, CMD_DELETE, CMD_NONE,
1676 invert);
1677 chain = optarg;
1678 if (optind < argc && argv[optind][0] != '-'
1679 && argv[optind][0] != '!') {
1680 rulenum = parse_rulenumber(argv[optind++]);
1681 command = CMD_DELETE_NUM;
1682 }
1683 break;
1684
1685 case 'C':
1686 add_command(&command, CMD_CHECK, CMD_NONE,
1687 invert);
1688 chain = optarg;
1689 break;
1690
1691 case 'R':
1692 add_command(&command, CMD_REPLACE, CMD_NONE,
1693 invert);
1694 chain = optarg;
1695 if (optind < argc && argv[optind][0] != '-'
1696 && argv[optind][0] != '!')
1697 rulenum = parse_rulenumber(argv[optind++]);
1698 else
1699 exit_error(PARAMETER_PROBLEM,
1700 "-%c requires a rule number",
1701 cmd2char(CMD_REPLACE));
1702 break;
1703
1704 case 'I':
1705 add_command(&command, CMD_INSERT, CMD_NONE,
1706 invert);
1707 chain = optarg;
1708 if (optind < argc && argv[optind][0] != '-'
1709 && argv[optind][0] != '!')
1710 rulenum = parse_rulenumber(argv[optind++]);
1711 else rulenum = 1;
1712 break;
1713
1714 case 'L':
1715 add_command(&command, CMD_LIST, CMD_ZERO,
1716 invert);
1717 if (optarg) chain = optarg;
1718 else if (optind < argc && argv[optind][0] != '-'
1719 && argv[optind][0] != '!')
1720 chain = argv[optind++];
1721 break;
1722
1723 case 'F':
1724 add_command(&command, CMD_FLUSH, CMD_NONE,
1725 invert);
1726 if (optarg) chain = optarg;
1727 else if (optind < argc && argv[optind][0] != '-'
1728 && argv[optind][0] != '!')
1729 chain = argv[optind++];
1730 break;
1731
1732 case 'Z':
1733 add_command(&command, CMD_ZERO, CMD_LIST,
1734 invert);
1735 if (optarg) chain = optarg;
1736 else if (optind < argc && argv[optind][0] != '-'
1737 && argv[optind][0] != '!')
1738 chain = argv[optind++];
1739 break;
1740
1741 case 'N':
1742 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1743 invert);
1744 chain = optarg;
1745 break;
1746
1747 case 'X':
1748 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1749 invert);
1750 if (optarg) chain = optarg;
1751 else if (optind < argc && argv[optind][0] != '-'
1752 && argv[optind][0] != '!')
1753 chain = argv[optind++];
1754 break;
1755
1756 case 'E':
1757 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1758 invert);
1759 chain = optarg;
1760 if (optind < argc && argv[optind][0] != '-'
1761 && argv[optind][0] != '!')
1762 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001763 else
1764 exit_error(PARAMETER_PROBLEM,
1765 "-%c requires old-chain-name and "
1766 "new-chain-name",
1767 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001768 break;
1769
1770 case 'P':
1771 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1772 invert);
1773 chain = optarg;
1774 if (optind < argc && argv[optind][0] != '-'
1775 && argv[optind][0] != '!')
1776 policy = argv[optind++];
1777 else
1778 exit_error(PARAMETER_PROBLEM,
1779 "-%c requires a chain and a policy",
1780 cmd2char(CMD_SET_POLICY));
1781 break;
1782
1783 case 'h':
1784 if (!optarg)
1785 optarg = argv[optind];
1786
1787 /* iptables -p icmp -h */
1788 if (!ip6tables_matches && protocol)
1789 find_match(protocol, TRY_LOAD);
1790
1791 exit_printhelp();
1792
1793 /*
1794 * Option selection
1795 */
1796 case 'p':
1797 if (check_inverse(optarg, &invert))
1798 optind++;
1799 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1800 invert);
1801
1802 /* Canonicalize into lower case */
1803 for (protocol = argv[optind-1]; *protocol; protocol++)
1804 *protocol = tolower(*protocol);
1805
1806 protocol = argv[optind-1];
1807 fw.ipv6.proto = parse_protocol(protocol);
1808 fw.ipv6.flags |= IP6T_F_PROTO;
1809
1810 if (fw.ipv6.proto == 0
1811 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1812 exit_error(PARAMETER_PROBLEM,
1813 "rule would never match protocol");
1814 fw.nfcache |= NFC_IP6_PROTO;
1815 break;
1816
1817 case 's':
1818 if (check_inverse(optarg, &invert))
1819 optind++;
1820 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1821 invert);
1822 shostnetworkmask = argv[optind-1];
1823 fw.nfcache |= NFC_IP6_SRC;
1824 break;
1825
1826 case 'd':
1827 if (check_inverse(optarg, &invert))
1828 optind++;
1829 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1830 invert);
1831 dhostnetworkmask = argv[optind-1];
1832 fw.nfcache |= NFC_IP6_DST;
1833 break;
1834
1835 case 'j':
1836 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1837 invert);
1838 jumpto = parse_target(optarg);
1839 target = find_target(jumpto, TRY_LOAD);
1840
1841 if (target) {
1842 size_t size;
1843
1844 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1845 + target->size);
1846
1847 target->t = fw_calloc(1, size);
1848 target->t->u.target_size = size;
1849 strcpy(target->t->u.user.name, jumpto);
1850 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001851 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001852 }
1853 break;
1854
1855
1856 case 'i':
1857 if (check_inverse(optarg, &invert))
1858 optind++;
1859 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1860 invert);
1861 parse_interface(argv[optind-1],
1862 fw.ipv6.iniface,
1863 fw.ipv6.iniface_mask);
1864 fw.nfcache |= NFC_IP6_IF_IN;
1865 break;
1866
1867 case 'o':
1868 if (check_inverse(optarg, &invert))
1869 optind++;
1870 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1871 invert);
1872 parse_interface(argv[optind-1],
1873 fw.ipv6.outiface,
1874 fw.ipv6.outiface_mask);
1875 fw.nfcache |= NFC_IP6_IF_OUT;
1876 break;
1877
1878 case 'v':
1879 if (!verbose)
1880 set_option(&options, OPT_VERBOSE,
1881 &fw.ipv6.invflags, invert);
1882 verbose++;
1883 break;
1884
1885 case 'm': {
1886 size_t size;
1887
1888 if (invert)
1889 exit_error(PARAMETER_PROBLEM,
1890 "unexpected ! flag before --match");
1891
1892 m = find_match(optarg, LOAD_MUST_SUCCEED);
1893 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1894 + m->size);
1895 m->m = fw_calloc(1, size);
1896 m->m->u.match_size = size;
1897 strcpy(m->m->u.user.name, m->name);
1898 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001899 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001900 }
1901 break;
1902
1903 case 'n':
1904 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1905 invert);
1906 break;
1907
1908 case 't':
1909 if (invert)
1910 exit_error(PARAMETER_PROBLEM,
1911 "unexpected ! flag before --table");
1912 *table = argv[optind-1];
1913 break;
1914
1915 case 'x':
1916 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1917 invert);
1918 break;
1919
1920 case 'V':
1921 if (invert)
1922 printf("Not %s ;-)\n", program_version);
1923 else
1924 printf("%s v%s\n",
1925 program_name, program_version);
1926 exit(0);
1927
1928 case '0':
1929 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1930 invert);
1931 break;
1932
1933 case 1: /* non option */
1934 if (optarg[0] == '!' && optarg[1] == '\0') {
1935 if (invert)
1936 exit_error(PARAMETER_PROBLEM,
1937 "multiple consecutive ! not"
1938 " allowed");
1939 invert = TRUE;
1940 optarg[0] = '\0';
1941 continue;
1942 }
1943 printf("Bad argument `%s'\n", optarg);
1944 exit_tryhelp(2);
1945
1946 default:
1947 /* FIXME: This scheme doesn't allow two of the same
1948 matches --RR */
1949 if (!target
1950 || !(target->parse(c - target->option_offset,
1951 argv, invert,
1952 &target->tflags,
1953 &fw, &target->t))) {
1954 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001955 if (!m->used)
1956 continue;
1957
Rusty Russell5eed48a2000-06-02 20:12:24 +00001958 if (m->parse(c - m->option_offset,
1959 argv, invert,
1960 &m->mflags,
1961 &fw,
1962 &fw.nfcache,
1963 &m->m))
1964 break;
1965 }
1966
1967 /* If you listen carefully, you can
1968 actually hear this code suck. */
1969 if (m == NULL
1970 && protocol
1971 && !find_proto(protocol, DONT_LOAD,
1972 options&OPT_NUMERIC)
1973 && (m = find_proto(protocol, TRY_LOAD,
1974 options&OPT_NUMERIC))) {
1975 /* Try loading protocol */
1976 size_t size;
1977
1978 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1979 + m->size);
1980
1981 m->m = fw_calloc(1, size);
1982 m->m->u.match_size = size;
1983 strcpy(m->m->u.user.name, m->name);
1984 m->init(m->m, &fw.nfcache);
1985
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001986 opts = merge_options(opts,
1987 m->extra_opts, &m->option_offset);
1988
Rusty Russell5eed48a2000-06-02 20:12:24 +00001989 optind--;
1990 continue;
1991 }
1992 if (!m)
1993 exit_error(PARAMETER_PROBLEM,
1994 "Unknown arg `%s'",
1995 argv[optind-1]);
1996 }
1997 }
1998 invert = FALSE;
1999 }
2000
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002001 for (m = ip6tables_matches; m; m = m->next) {
2002 if (!m->used)
2003 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002004
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002005 m->final_check(m->mflags);
2006 }
2007
2008 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002009 target->final_check(target->tflags);
2010
2011 /* Fix me: must put inverse options checking here --MN */
2012
2013 if (optind < argc)
2014 exit_error(PARAMETER_PROBLEM,
2015 "unknown arguments found on commandline");
2016 if (!command)
2017 exit_error(PARAMETER_PROBLEM, "no command specified");
2018 if (invert)
2019 exit_error(PARAMETER_PROBLEM,
2020 "nothing appropriate following !");
2021
2022 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2023 CMD_CHECK)) {
2024 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002025 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002026 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002027 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002028 }
2029
2030 if (shostnetworkmask)
2031 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2032 &(fw.ipv6.smsk), &nsaddrs);
2033
2034 if (dhostnetworkmask)
2035 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2036 &(fw.ipv6.dmsk), &ndaddrs);
2037
2038 if ((nsaddrs > 1 || ndaddrs > 1) &&
2039 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2040 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2041 " source or destination IP addresses");
2042
2043 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2044 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2045 cmd2char(CMD_CHECK));
2046
2047 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2048 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2049 "specify a unique address");
2050
2051 generic_opt_check(command, options);
2052
2053 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2054 exit_error(PARAMETER_PROBLEM,
2055 "chain name `%s' too long (must be under %i chars)",
2056 chain, IP6T_FUNCTION_MAXNAMELEN);
2057
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002058 /* only allocate handle if we weren't called with a handle */
2059 if (!*handle)
2060 *handle = ip6tc_init(*table);
2061
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002062 if (!*handle) {
2063 /* try to insmod the module if iptc_init failed */
2064 ip6tables_insmod("ip6_tables", modprobe);
2065 *handle = ip6tc_init(*table);
2066 }
2067
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002068 if (!*handle)
2069 exit_error(VERSION_PROBLEM,
2070 "can't initialize ip6tables table `%s': %s",
2071 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002072
2073 if (command == CMD_CHECK
2074 || command == CMD_APPEND
2075 || command == CMD_DELETE
2076 || command == CMD_INSERT
2077 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002078 if (strcmp(chain, "PREROUTING") == 0
2079 || strcmp(chain, "INPUT") == 0) {
2080 /* -o not valid with incoming packets. */
2081 if (options & OPT_VIANAMEOUT)
2082 exit_error(PARAMETER_PROBLEM,
2083 "Can't use -%c with %s\n",
2084 opt2char(OPT_VIANAMEOUT),
2085 chain);
2086 /* -i required with -C */
2087 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2088 exit_error(PARAMETER_PROBLEM,
2089 "Need -%c with %s\n",
2090 opt2char(OPT_VIANAMEIN),
2091 chain);
2092 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002093
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002094 if (strcmp(chain, "POSTROUTING") == 0
2095 || strcmp(chain, "OUTPUT") == 0) {
2096 /* -i not valid with outgoing packets */
2097 if (options & OPT_VIANAMEIN)
2098 exit_error(PARAMETER_PROBLEM,
2099 "Can't use -%c with %s\n",
2100 opt2char(OPT_VIANAMEIN),
2101 chain);
2102 /* -o required with -C */
2103 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2104 exit_error(PARAMETER_PROBLEM,
2105 "Need -%c with %s\n",
2106 opt2char(OPT_VIANAMEOUT),
2107 chain);
2108 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002109
2110 if (target && ip6tc_is_chain(jumpto, *handle)) {
2111 printf("Warning: using chain %s, not extension\n",
2112 jumpto);
2113
2114 target = NULL;
2115 }
2116
2117 /* If they didn't specify a target, or it's a chain
2118 name, use standard. */
2119 if (!target
2120 && (strlen(jumpto) == 0
2121 || ip6tc_is_chain(jumpto, *handle))) {
2122 size_t size;
2123
2124 target = find_target(IP6T_STANDARD_TARGET,
2125 LOAD_MUST_SUCCEED);
2126
2127 size = sizeof(struct ip6t_entry_target)
2128 + target->size;
2129 target->t = fw_calloc(1, size);
2130 target->t->u.target_size = size;
2131 strcpy(target->t->u.user.name, jumpto);
2132 target->init(target->t, &fw.nfcache);
2133 }
2134
2135 if (!target) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002136#if 0
Rusty Russell5eed48a2000-06-02 20:12:24 +00002137 struct ip6t_entry_target unknown_target;
2138
2139 /* Don't know it. Must be extension with no
2140 options? */
2141 unknown_target.u.target_size = sizeof(unknown_target);
2142 strcpy(unknown_target.u.user.name, jumpto);
2143
2144 e = generate_entry(&fw, ip6tables_matches,
2145 &unknown_target);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002146#endif
2147 /* it is no chain, and we can't load a plugin.
2148 * We cannot know if the plugin is corrupt, non
2149 * existant OR if the user just misspelled a
2150 * chain. */
2151 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002152 } else {
2153 e = generate_entry(&fw, ip6tables_matches, target->t);
2154 }
2155 }
2156
2157 switch (command) {
2158 case CMD_APPEND:
2159 ret = append_entry(chain, e,
2160 nsaddrs, saddrs, ndaddrs, daddrs,
2161 options&OPT_VERBOSE,
2162 handle);
2163 break;
2164 case CMD_CHECK:
2165 ret = check_packet(chain, e,
2166 nsaddrs, saddrs, ndaddrs, daddrs,
2167 options&OPT_VERBOSE, handle);
2168 break;
2169 case CMD_DELETE:
2170 ret = delete_entry(chain, e,
2171 nsaddrs, saddrs, ndaddrs, daddrs,
2172 options&OPT_VERBOSE,
2173 handle);
2174 break;
2175 case CMD_DELETE_NUM:
2176 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2177 break;
2178 case CMD_REPLACE:
2179 ret = replace_entry(chain, e, rulenum - 1,
2180 saddrs, daddrs, options&OPT_VERBOSE,
2181 handle);
2182 break;
2183 case CMD_INSERT:
2184 ret = insert_entry(chain, e, rulenum - 1,
2185 nsaddrs, saddrs, ndaddrs, daddrs,
2186 options&OPT_VERBOSE,
2187 handle);
2188 break;
2189 case CMD_LIST:
2190 ret = list_entries(chain,
2191 options&OPT_VERBOSE,
2192 options&OPT_NUMERIC,
2193 options&OPT_EXPANDED,
2194 options&OPT_LINENUMBERS,
2195 handle);
2196 break;
2197 case CMD_FLUSH:
2198 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2199 break;
2200 case CMD_ZERO:
2201 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2202 break;
2203 case CMD_LIST|CMD_ZERO:
2204 ret = list_entries(chain,
2205 options&OPT_VERBOSE,
2206 options&OPT_NUMERIC,
2207 options&OPT_EXPANDED,
2208 options&OPT_LINENUMBERS,
2209 handle);
2210 if (ret)
2211 ret = zero_entries(chain,
2212 options&OPT_VERBOSE, handle);
2213 break;
2214 case CMD_NEW_CHAIN:
2215 ret = ip6tc_create_chain(chain, handle);
2216 break;
2217 case CMD_DELETE_CHAIN:
2218 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2219 break;
2220 case CMD_RENAME_CHAIN:
2221 ret = ip6tc_rename_chain(chain, newname, handle);
2222 break;
2223 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002224 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002225 break;
2226 default:
2227 /* We should never reach this... */
2228 exit_tryhelp(2);
2229 }
2230
2231 if (verbose > 1)
2232 dump_entries6(*handle);
2233
2234 return ret;
2235}