blob: 123d6a44367ce6d2dc89835c4c805b8e9b55db66 [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 *
138ip6t_get_target(struct ipt_entry *e)
139{
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"
335"[!] --fragment -f match second or further fragments only\n"
336"[!] --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
455static struct in6_addr *
456host_to_addr(const char *name, unsigned int *naddr)
457{
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000458#if 0
459 /* TODO: getipnodebyname() missing */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000460 struct hostent *host;
461 struct in6_addr *addr;
462 unsigned int i;
463
464 *naddr = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000465 if ((host = getipnodebyname(name, AF_INET6, 0, NULL)) != NULL) {
466 if (host->h_length != sizeof(struct in6_addr))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000467 return (struct in6_addr *) NULL;
468
469 while (host->h_addr_list[*naddr] != (char *) NULL)
470 (*naddr)++;
471 addr = fw_calloc(*naddr, sizeof(struct in6_addr));
472 for (i = 0; i < *naddr; i++)
473 in6addrcpy(&(addr[i]),
474 (struct in6_addr *) host->h_addr_list[i]);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000475 freehostent(host);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000476 return addr;
477 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000478#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000479
480 return (struct in6_addr *) NULL;
481}
482
483static char *
484addr_to_host(const struct in6_addr *addr)
485{
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000486#if 0
487 /* TODO: getipnodebyaddr missing */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000488 struct hostent *host;
489
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000490 if ((host = getipnodebyaddr((char *) addr,
491 sizeof(struct in6_addr), AF_INET6, NULL)) != NULL)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000492 return (char *) host->h_name;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000493 /*TODO: freehostent()*/
494#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000495
496 return (char *) NULL;
497}
498
499static char *
500addr_to_numeric(const struct in6_addr *addrp)
501{
András Kis-Szabó764316a2001-02-26 17:31:20 +0000502 // static char buf[20];
503 // ADDR tags + sep. + \0
504 // 0000:0000:0000:0000:0000:0000:0000:000.000.000.000
505 // (but inet_ntop() supports only
506 // 0000:0000:0000:0000:0000:0000:0000:0000 format now.)
507 static char buf[50+1];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000508 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof buf);
509}
510
511static struct in6_addr *
512numeric_to_addr(const char *num)
513{
514 static struct in6_addr ap;
515 if (inet_pton(AF_INET6, num, &ap) == 1)
516 return &ap;
517 return (struct in6_addr *)NULL;
518}
519
520static char *
521mask_to_numeric(const struct in6_addr *addrp)
522{
523 static char buf[20];
524 int l = ipv6_prefix_length(addrp);
525 if (l == -1)
526 return addr_to_numeric(addrp);
527 sprintf(buf, "%d", l);
528 return buf;
529}
530
531static struct in6_addr *
532network_to_addr(const char *name)
533{
534 abort();
535}
536
537static char *
538addr_to_anyname(const struct in6_addr *addr)
539{
540 char *name;
541
542 if ((name = addr_to_host(addr)) != NULL)
543 return name;
544
545 return addr_to_numeric(addr);
546}
547
548/*
549 * All functions starting with "parse" should succeed, otherwise
550 * the program fails.
551 * Most routines return pointers to static data that may change
552 * between calls to the same or other routines with a few exceptions:
553 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
554 * return global static data.
555*/
556
557static struct in6_addr *
558parse_hostnetwork(const char *name, unsigned int *naddrs)
559{
560 struct in6_addr *addrp, *addrptmp;
561
562 if ((addrptmp = numeric_to_addr(name)) != NULL ||
563 (addrptmp = network_to_addr(name)) != NULL) {
564 addrp = fw_malloc(sizeof(struct in6_addr));
565 in6addrcpy(addrp, addrptmp);
566 *naddrs = 1;
567 return addrp;
568 }
569 if ((addrp = host_to_addr(name, naddrs)) != NULL)
570 return addrp;
571
572 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
573}
574
575static struct in6_addr *
576parse_mask(char *mask)
577{
578 static struct in6_addr maskaddr;
579 struct in6_addr *addrp;
580 int bits;
581
582 if (mask == NULL) {
583 /* no mask at all defaults to 128 bits */
584 memset(&maskaddr, 0xff, sizeof maskaddr);
585 return &maskaddr;
586 }
587 if ((addrp = numeric_to_addr(mask)) != NULL)
588 return addrp;
589 if ((bits = string_to_number(mask, 0, 128)) == -1)
590 exit_error(PARAMETER_PROBLEM,
591 "invalid mask `%s' specified", mask);
592 if (bits != 0) {
593 char *p = (char *)&maskaddr;
594 memset(p, 0xff, bits / 8);
595 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
596 p[bits / 8] = 0xff << (8 - (bits & 7));
597 return &maskaddr;
598 }
599
600 memset(&maskaddr, 0, sizeof maskaddr);
601 return &maskaddr;
602}
603
604static void
605parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
606 struct in6_addr *maskp, unsigned int *naddrs)
607{
608 struct in6_addr *addrp;
609 char buf[256];
610 char *p;
611 int i, j, n;
612
613 strncpy(buf, name, sizeof(buf) - 1);
614 if ((p = strrchr(buf, '/')) != NULL) {
615 *p = '\0';
616 addrp = parse_mask(p + 1);
617 } else
618 addrp = parse_mask(NULL);
619 in6addrcpy(maskp, addrp);
620
621 /* if a null mask is given, the name is ignored, like in "any/0" */
622 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
623 strcpy(buf, "::");
624
625 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
626 n = *naddrs;
627 for (i = 0, j = 0; i < n; i++) {
628 int k;
629 for (k = 0; k < 4; k++)
630 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
631 j++;
632 for (k = 0; k < j - 1; k++) {
633 if (!memcmp(&addrp[k], &addrp[j - 1], sizeof(struct in6_addr))) {
634 (*naddrs)--;
635 j--;
636 break;
637 }
638 }
639 }
640}
641
642struct ip6tables_match *
643find_match(const char *name, enum ip6t_tryload tryload)
644{
645 struct ip6tables_match *ptr;
646
647 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
648 if (strcmp(name, ptr->name) == 0)
649 break;
650 }
651
652 if (!ptr && tryload != DONT_LOAD) {
653 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
654 + strlen(name)];
655 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
656 if (dlopen(path, RTLD_NOW)) {
657 /* Found library. If it didn't register itself,
658 maybe they specified target as match. */
659 ptr = find_match(name, DONT_LOAD);
660
661 if (!ptr)
662 exit_error(PARAMETER_PROBLEM,
663 "Couldn't load match `%s'\n",
664 name);
665 } else if (tryload == LOAD_MUST_SUCCEED)
666 exit_error(PARAMETER_PROBLEM,
667 "Couldn't load match `%s'\n", name);
668 }
669
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000670 if (ptr)
671 ptr->used = 1;
672
673
Rusty Russell5eed48a2000-06-02 20:12:24 +0000674 return ptr;
675}
676
677/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
678static struct ip6tables_match *
679find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
680{
681 int proto;
682
683 proto = string_to_number(pname, 0, 255);
684 if (proto != -1)
685 return find_match(proto_to_name(proto, nolookup), tryload);
686
687 return find_match(pname, tryload);
688}
689
690static u_int16_t
691parse_protocol(const char *s)
692{
693 int proto = string_to_number(s, 0, 255);
694
695 if (proto == -1) {
696 struct protoent *pent;
697
698 if ((pent = getprotobyname(s)))
699 proto = pent->p_proto;
700 else {
701 unsigned int i;
702 for (i = 0;
703 i < sizeof(chain_protos)/sizeof(struct pprot);
704 i++) {
705 if (strcmp(s, chain_protos[i].name) == 0) {
706 proto = chain_protos[i].num;
707 break;
708 }
709 }
710 if (i == sizeof(chain_protos)/sizeof(struct pprot))
711 exit_error(PARAMETER_PROBLEM,
712 "unknown protocol `%s' specified",
713 s);
714 }
715 }
716
717 return (u_int16_t)proto;
718}
719
720static void
721parse_interface(const char *arg, char *vianame, unsigned char *mask)
722{
723 int vialen = strlen(arg);
724 unsigned int i;
725
726 memset(mask, 0, IFNAMSIZ);
727 memset(vianame, 0, IFNAMSIZ);
728
729 if (vialen + 1 > IFNAMSIZ)
730 exit_error(PARAMETER_PROBLEM,
731 "interface name `%s' must be shorter than IFNAMSIZ"
732 " (%i)", arg, IFNAMSIZ-1);
733
734 strcpy(vianame, arg);
735 if (vialen == 0)
736 memset(mask, 0, IFNAMSIZ);
737 else if (vianame[vialen - 1] == '+') {
738 memset(mask, 0xFF, vialen - 1);
739 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
740 /* Remove `+' */
741 vianame[vialen - 1] = '\0';
742 } else {
743 /* Include nul-terminator in match */
744 memset(mask, 0xFF, vialen + 1);
745 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
746 }
747 for (i = 0; vianame[i]; i++) {
Harald Welte6a4542a2001-05-12 04:38:31 +0000748 if (!isalnum(vianame[i]) && vianame[i] != '_') {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 printf("Warning: wierd character in interface"
750 " `%s' (No aliases, :, ! or *).\n",
751 vianame);
752 break;
753 }
754 }
755}
756
757/* Can't be zero. */
758static int
759parse_rulenumber(const char *rule)
760{
761 int rulenum = string_to_number(rule, 1, INT_MAX);
762
763 if (rulenum == -1)
764 exit_error(PARAMETER_PROBLEM,
765 "Invalid rule number `%s'", rule);
766
767 return rulenum;
768}
769
770static const char *
771parse_target(const char *targetname)
772{
773 const char *ptr;
774
775 if (strlen(targetname) < 1)
776 exit_error(PARAMETER_PROBLEM,
777 "Invalid target name (too short)");
778
779 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
780 exit_error(PARAMETER_PROBLEM,
781 "Invalid target name `%s' (%i chars max)",
782 targetname, sizeof(ip6t_chainlabel)-1);
783
784 for (ptr = targetname; *ptr; ptr++)
785 if (isspace(*ptr))
786 exit_error(PARAMETER_PROBLEM,
787 "Invalid target name `%s'", targetname);
788 return targetname;
789}
790
791int
792string_to_number(const char *s, int min, int max)
793{
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000794 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000795 char *end;
796
797 /* Handle hex, octal, etc. */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000798 errno = 0;
799 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800 if (*end == '\0' && end != s) {
801 /* we parsed a number, let's see if we want this */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000802 if (errno != ERANGE && min <= number && number <= max)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803 return number;
804 }
805 return -1;
806}
807
808static void
809set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
810 int invert)
811{
812 if (*options & option)
813 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
814 opt2char(option));
815 *options |= option;
816
817 if (invert) {
818 unsigned int i;
819 for (i = 0; 1 << i != option; i++);
820
821 if (!inverse_for_options[i])
822 exit_error(PARAMETER_PROBLEM,
823 "cannot have ! before -%c",
824 opt2char(option));
825 *invflg |= inverse_for_options[i];
826 }
827}
828
829struct ip6tables_target *
830find_target(const char *name, enum ip6t_tryload tryload)
831{
832 struct ip6tables_target *ptr;
833
834 /* Standard target? */
835 if (strcmp(name, "") == 0
836 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
837 || strcmp(name, IP6TC_LABEL_DROP) == 0
838 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
839 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
840 name = "standard";
841
842 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
843 if (strcmp(name, ptr->name) == 0)
844 break;
845 }
846
847 if (!ptr && tryload != DONT_LOAD) {
848 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
849 + strlen(name)];
850 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
851 if (dlopen(path, RTLD_NOW)) {
852 /* Found library. If it didn't register itself,
853 maybe they specified match as a target. */
854 ptr = find_target(name, DONT_LOAD);
855 if (!ptr)
856 exit_error(PARAMETER_PROBLEM,
857 "Couldn't load target `%s'\n",
858 name);
859 } else if (tryload == LOAD_MUST_SUCCEED)
860 exit_error(PARAMETER_PROBLEM,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000861 "Couldn't load target `%s'%s\n",
862 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000863 }
864
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000865 if (ptr)
866 ptr->used = 1;
867
Rusty Russell5eed48a2000-06-02 20:12:24 +0000868 return ptr;
869}
870
871static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000872merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000873 unsigned int *option_offset)
874{
875 unsigned int num_old, num_new, i;
876 struct option *merge;
877
878 for (num_old = 0; oldopts[num_old].name; num_old++);
879 for (num_new = 0; newopts[num_new].name; num_new++);
880
881 global_option_offset += OPTION_OFFSET;
882 *option_offset = global_option_offset;
883
884 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
885 memcpy(merge, oldopts, num_old * sizeof(struct option));
886 for (i = 0; i < num_new; i++) {
887 merge[num_old + i] = newopts[i];
888 merge[num_old + i].val += *option_offset;
889 }
890 memset(merge + num_old + num_new, 0, sizeof(struct option));
891
892 return merge;
893}
894
895void
896register_match6(struct ip6tables_match *me)
897{
898 if (strcmp(me->version, program_version) != 0) {
899 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
900 program_name, me->name, me->version, program_version);
901 exit(1);
902 }
903
904 if (find_match(me->name, DONT_LOAD)) {
905 fprintf(stderr, "%s: match `%s' already registered.\n",
906 program_name, me->name);
907 exit(1);
908 }
909
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000910 if (me->size != IP6T_ALIGN(me->size)) {
911 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
912 program_name, me->name, me->size);
913 exit(1);
914 }
915
Rusty Russell5eed48a2000-06-02 20:12:24 +0000916 /* Prepend to list. */
917 me->next = ip6tables_matches;
918 ip6tables_matches = me;
919 me->m = NULL;
920 me->mflags = 0;
921
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000922 /* opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +0000923}
924
925void
926register_target6(struct ip6tables_target *me)
927{
928 if (strcmp(me->version, program_version) != 0) {
929 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
930 program_name, me->name, me->version, program_version);
931 exit(1);
932 }
933
934 if (find_target(me->name, DONT_LOAD)) {
935 fprintf(stderr, "%s: target `%s' already registered.\n",
936 program_name, me->name);
937 exit(1);
938 }
939
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000940 if (me->size != IP6T_ALIGN(me->size)) {
941 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
942 program_name, me->name, me->size);
943 exit(1);
944 }
945
Rusty Russell5eed48a2000-06-02 20:12:24 +0000946 /* Prepend to list. */
947 me->next = ip6tables_targets;
948 ip6tables_targets = me;
949 me->t = NULL;
950 me->tflags = 0;
951
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000952 /*opts = merge_options(opts, me->extra_opts, &me->option_offset);*/
953}
954
955static void
956print_num(u_int64_t number, unsigned int format)
957{
958 if (format & FMT_KILOMEGAGIGA) {
959 if (number > 99999) {
960 number = (number + 500) / 1000;
961 if (number > 9999) {
962 number = (number + 500) / 1000;
963 if (number > 9999) {
964 number = (number + 500) / 1000;
965 printf(FMT("%4lluG ","%lluG "),number);
966 }
967 else printf(FMT("%4lluM ","%lluM "), number);
968 } else
969 printf(FMT("%4lluK ","%lluK "), number);
970 } else
971 printf(FMT("%5llu ","%llu "), number);
972 } else
973 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000974}
975
976static void
977print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
978{
979 struct ip6t_counters counters;
980 const char *pol = ip6tc_get_policy(chain, &counters, handle);
981 printf("Chain %s", chain);
982 if (pol) {
983 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000984 if (!(format & FMT_NOCOUNTS)) {
985 /*printf(" %llu packets, %llu bytes",
986 counters.pcnt, counters.bcnt);*/
987 fputc(' ', stdout);
988 print_num(counters.pcnt, (format|FMT_NOTABLE));
989 fputs("packets, ", stdout);
990 print_num(counters.bcnt, (format|FMT_NOTABLE));
991 fputs("bytes", stdout);
992 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993 printf(")\n");
994 } else {
995 unsigned int refs;
996 if (!ip6tc_get_references(&refs, chain, handle))
997 printf(" (ERROR obtaining refs)\n");
998 else
999 printf(" (%u references)\n", refs);
1000 }
1001
1002 if (format & FMT_LINENUMBERS)
1003 printf(FMT("%-4s ", "%s "), "num");
1004 if (!(format & FMT_NOCOUNTS)) {
1005 if (format & FMT_KILOMEGAGIGA) {
1006 printf(FMT("%5s ","%s "), "pkts");
1007 printf(FMT("%5s ","%s "), "bytes");
1008 } else {
1009 printf(FMT("%8s ","%s "), "pkts");
1010 printf(FMT("%10s ","%s "), "bytes");
1011 }
1012 }
1013 if (!(format & FMT_NOTARGET))
1014 printf(FMT("%-9s ","%s "), "target");
1015 fputs(" prot ", stdout);
1016 if (format & FMT_OPTIONS)
1017 fputs("opt", stdout);
1018 if (format & FMT_VIA) {
1019 printf(FMT(" %-6s ","%s "), "in");
1020 printf(FMT("%-6s ","%s "), "out");
1021 }
1022 printf(FMT(" %-19s ","%s "), "source");
1023 printf(FMT(" %-19s "," %s "), "destination");
1024 printf("\n");
1025}
1026
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027static int
1028print_match(const struct ip6t_entry_match *m,
1029 const struct ip6t_ip6 *ip,
1030 int numeric)
1031{
1032 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1033
1034 if (match) {
1035 if (match->print)
1036 match->print(ip, m, numeric);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001037 else
1038 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001039 } else {
1040 if (m->u.user.name[0])
1041 printf("UNKNOWN match `%s' ", m->u.user.name);
1042 }
1043 /* Don't stop iterating. */
1044 return 0;
1045}
1046
1047/* e is called `fw' here for hysterical raisins */
1048static void
1049print_firewall(const struct ip6t_entry *fw,
1050 const char *targname,
1051 unsigned int num,
1052 unsigned int format,
1053 const ip6tc_handle_t handle)
1054{
1055 struct ip6tables_target *target = NULL;
1056 const struct ip6t_entry_target *t;
1057 u_int8_t flags;
1058 char buf[BUFSIZ];
1059
1060 /* User creates a chain called "REJECT": this overrides the
1061 `REJECT' target module. Keep feeding them rope until the
1062 revolution... Bwahahahahah */
1063 if (!ip6tc_is_chain(targname, handle))
1064 target = find_target(targname, TRY_LOAD);
1065 else
1066 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1067
1068 t = ip6t_get_target((struct ip6t_entry *)fw);
1069 flags = fw->ipv6.flags;
1070
1071 if (format & FMT_LINENUMBERS)
1072 printf(FMT("%-4u ", "%u "), num+1);
1073
1074 if (!(format & FMT_NOCOUNTS)) {
1075 print_num(fw->counters.pcnt, format);
1076 print_num(fw->counters.bcnt, format);
1077 }
1078
1079 if (!(format & FMT_NOTARGET))
1080 printf(FMT("%-9s ", "%s "), targname);
1081
1082 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1083 {
1084 char *pname = proto_to_name(fw->ipv6.proto,
1085 format&FMT_NUMERIC);
1086 if (pname)
1087 printf(FMT("%-5s", "%s "), pname);
1088 else
1089 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1090 }
1091
1092 if (format & FMT_OPTIONS) {
1093 if (format & FMT_NOTABLE)
1094 fputs("opt ", stdout);
1095 fputc(' ', stdout);
1096 fputc(' ', stdout);
1097 fputc(' ', stdout);
1098 }
1099
1100 if (format & FMT_VIA) {
1101 char iface[IFNAMSIZ+2];
1102
1103 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1104 iface[0] = '!';
1105 iface[1] = '\0';
1106 }
1107 else iface[0] = '\0';
1108
1109 if (fw->ipv6.iniface[0] != '\0') {
1110 strcat(iface, fw->ipv6.iniface);
1111 /* If it doesn't compare the nul-term, it's a
1112 wildcard. */
1113 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1114 strcat(iface, "+");
1115 }
1116 else if (format & FMT_NUMERIC) strcat(iface, "*");
1117 else strcat(iface, "any");
1118 printf(FMT(" %-6s ","in %s "), iface);
1119
1120 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1121 iface[0] = '!';
1122 iface[1] = '\0';
1123 }
1124 else iface[0] = '\0';
1125
1126 if (fw->ipv6.outiface[0] != '\0') {
1127 strcat(iface, fw->ipv6.outiface);
1128 /* If it doesn't compare the nul-term, it's a
1129 wildcard. */
1130 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1131 strcat(iface, "+");
1132 }
1133 else if (format & FMT_NUMERIC) strcat(iface, "*");
1134 else strcat(iface, "any");
1135 printf(FMT("%-6s ","out %s "), iface);
1136 }
1137
1138 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1139 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1140 && !(format & FMT_NUMERIC))
1141 printf(FMT("%-19s ","%s "), "anywhere");
1142 else {
1143 if (format & FMT_NUMERIC)
1144 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1145 else
1146 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1147 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1148 printf(FMT("%-19s ","%s "), buf);
1149 }
1150
1151 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1152 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1153 && !(format & FMT_NUMERIC))
1154 printf(FMT("%-19s","-> %s"), "anywhere");
1155 else {
1156 if (format & FMT_NUMERIC)
1157 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1158 else
1159 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1160 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1161 printf(FMT("%-19s","-> %s"), buf);
1162 }
1163
1164 if (format & FMT_NOTABLE)
1165 fputs(" ", stdout);
1166
1167 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1168
1169 if (target) {
1170 if (target->print)
1171 /* Print the target information. */
1172 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1173 } else if (t->u.target_size != sizeof(*t))
1174 printf("[%u bytes of unknown target data] ",
1175 t->u.target_size - sizeof(*t));
1176
1177 if (!(format & FMT_NONEWLINE))
1178 fputc('\n', stdout);
1179}
1180
1181static void
1182print_firewall_line(const struct ip6t_entry *fw,
1183 const ip6tc_handle_t h)
1184{
1185 struct ip6t_entry_target *t;
1186
1187 t = ip6t_get_target((struct ip6t_entry *)fw);
1188 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1189}
1190
1191static int
1192append_entry(const ip6t_chainlabel chain,
1193 struct ip6t_entry *fw,
1194 unsigned int nsaddrs,
1195 const struct in6_addr saddrs[],
1196 unsigned int ndaddrs,
1197 const struct in6_addr daddrs[],
1198 int verbose,
1199 ip6tc_handle_t *handle)
1200{
1201 unsigned int i, j;
1202 int ret = 1;
1203
1204 for (i = 0; i < nsaddrs; i++) {
1205 fw->ipv6.src = saddrs[i];
1206 for (j = 0; j < ndaddrs; j++) {
1207 fw->ipv6.dst = daddrs[j];
1208 if (verbose)
1209 print_firewall_line(fw, *handle);
1210 ret &= ip6tc_append_entry(chain, fw, handle);
1211 }
1212 }
1213
1214 return ret;
1215}
1216
1217static int
1218replace_entry(const ip6t_chainlabel chain,
1219 struct ip6t_entry *fw,
1220 unsigned int rulenum,
1221 const struct in6_addr *saddr,
1222 const struct in6_addr *daddr,
1223 int verbose,
1224 ip6tc_handle_t *handle)
1225{
1226 fw->ipv6.src = *saddr;
1227 fw->ipv6.dst = *daddr;
1228
1229 if (verbose)
1230 print_firewall_line(fw, *handle);
1231 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1232}
1233
1234static int
1235insert_entry(const ip6t_chainlabel chain,
1236 struct ip6t_entry *fw,
1237 unsigned int rulenum,
1238 unsigned int nsaddrs,
1239 const struct in6_addr saddrs[],
1240 unsigned int ndaddrs,
1241 const struct in6_addr daddrs[],
1242 int verbose,
1243 ip6tc_handle_t *handle)
1244{
1245 unsigned int i, j;
1246 int ret = 1;
1247
1248 for (i = 0; i < nsaddrs; i++) {
1249 fw->ipv6.src = saddrs[i];
1250 for (j = 0; j < ndaddrs; j++) {
1251 fw->ipv6.dst = daddrs[j];
1252 if (verbose)
1253 print_firewall_line(fw, *handle);
1254 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1255 }
1256 }
1257
1258 return ret;
1259}
1260
1261static unsigned char *
1262make_delete_mask(struct ip6t_entry *fw)
1263{
1264 /* Establish mask for comparison */
1265 unsigned int size;
1266 struct ip6tables_match *m;
1267 unsigned char *mask, *mptr;
1268
1269 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001270 for (m = ip6tables_matches; m; m = m->next) {
1271 if (!m->used)
1272 continue;
1273
1274 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1275 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001276
1277 mask = fw_calloc(1, size
1278 + sizeof(struct ip6t_entry_target)
1279 + ip6tables_targets->size);
1280
1281 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1282 mptr = mask + sizeof(struct ip6t_entry);
1283
1284 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001285 if (!m->used)
1286 continue;
1287
Rusty Russell5eed48a2000-06-02 20:12:24 +00001288 memset(mptr, 0xFF,
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001289 IP6T_ALIGN(sizeof(struct ip6t_entry_match)) +
1290 m->userspacesize);
1291 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001292 }
1293
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001294 memset(mptr, 0xFF,
1295 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1296 + ip6tables_targets->userspacesize);
1297 /*mptr += sizeof(struct ip6t_entry_target);
1298 memset(mptr, 0xFF, ip6tables_targets->userspacesize);*/
Rusty Russell5eed48a2000-06-02 20:12:24 +00001299
1300 return mask;
1301}
1302
1303static int
1304delete_entry(const ip6t_chainlabel chain,
1305 struct ip6t_entry *fw,
1306 unsigned int nsaddrs,
1307 const struct in6_addr saddrs[],
1308 unsigned int ndaddrs,
1309 const struct in6_addr daddrs[],
1310 int verbose,
1311 ip6tc_handle_t *handle)
1312{
1313 unsigned int i, j;
1314 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001315 unsigned char *mask;
1316
1317 mask = make_delete_mask(fw);
1318 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001319 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001320 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001321 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001322 if (verbose)
1323 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001324 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001325 }
1326 }
1327 return ret;
1328}
1329
1330static int
1331check_packet(const ip6t_chainlabel chain,
1332 struct ip6t_entry *fw,
1333 unsigned int nsaddrs,
1334 const struct in6_addr saddrs[],
1335 unsigned int ndaddrs,
1336 const struct in6_addr daddrs[],
1337 int verbose,
1338 ip6tc_handle_t *handle)
1339{
1340 int ret = 1;
1341 unsigned int i, j;
1342 struct ip6t_entry ipfw = *fw;
1343 const char *msg;
1344
1345 for (i = 0; i < nsaddrs; i++) {
1346 ipfw.ipv6.src = saddrs[i];
1347 for (j = 0; j < ndaddrs; j++) {
1348 ipfw.ipv6.dst = daddrs[j];
1349 if (verbose)
1350 print_firewall_line(fw, *handle);
1351 msg = ip6tc_check_packet(chain, &ipfw, handle);
1352 if (!msg) ret = 0;
1353 else printf("%s\n", msg);
1354 }
1355 }
1356
1357 return ret;
1358}
1359
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001360/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001361int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1363 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1364{
1365 int ret = 1;
1366 const char *chain;
1367 char *chains;
1368 unsigned int i, chaincount = 0;
1369
1370 chain = ip6tc_first_chain(handle);
1371 while (chain) {
1372 chaincount++;
1373 chain = ip6tc_next_chain(handle);
1374 }
1375
1376 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1377 i = 0;
1378 chain = ip6tc_first_chain(handle);
1379 while (chain) {
1380 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1381 i++;
1382 chain = ip6tc_next_chain(handle);
1383 }
1384
1385 for (i = 0; i < chaincount; i++) {
1386 if (!builtinstoo
1387 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1388 *handle))
1389 continue;
1390 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1391 }
1392
1393 free(chains);
1394 return ret;
1395}
1396
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001397/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001398int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001399flush_entries(const ip6t_chainlabel chain, int verbose,
1400 ip6tc_handle_t *handle)
1401{
1402 if (!chain)
1403 return for_each_chain(flush_entries, verbose, 1, handle);
1404
1405 if (verbose)
1406 fprintf(stdout, "Flushing chain `%s'\n", chain);
1407 return ip6tc_flush_entries(chain, handle);
1408}
1409
1410static int
1411zero_entries(const ip6t_chainlabel chain, int verbose,
1412 ip6tc_handle_t *handle)
1413{
1414 if (!chain)
1415 return for_each_chain(zero_entries, verbose, 1, handle);
1416
1417 if (verbose)
1418 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1419 return ip6tc_zero_entries(chain, handle);
1420}
1421
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001422/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001423int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001424delete_chain(const ip6t_chainlabel chain, int verbose,
1425 ip6tc_handle_t *handle)
1426{
1427 if (!chain)
1428 return for_each_chain(delete_chain, verbose, 0, handle);
1429
1430 if (verbose)
1431 fprintf(stdout, "Deleting chain `%s'\n", chain);
1432 return ip6tc_delete_chain(chain, handle);
1433}
1434
1435static int
1436list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1437 int expanded, int linenumbers, ip6tc_handle_t *handle)
1438{
1439 int found = 0;
1440 unsigned int format;
1441 const char *this;
1442
1443 format = FMT_OPTIONS;
1444 if (!verbose)
1445 format |= FMT_NOCOUNTS;
1446 else
1447 format |= FMT_VIA;
1448
1449 if (numeric)
1450 format |= FMT_NUMERIC;
1451
1452 if (!expanded)
1453 format |= FMT_KILOMEGAGIGA;
1454
1455 if (linenumbers)
1456 format |= FMT_LINENUMBERS;
1457
1458 for (this = ip6tc_first_chain(handle);
1459 this;
1460 this = ip6tc_next_chain(handle)) {
1461 const struct ip6t_entry *i;
1462 unsigned int num;
1463
1464 if (chain && strcmp(chain, this) != 0)
1465 continue;
1466
1467 if (found) printf("\n");
1468
1469 print_header(format, this, handle);
1470 i = ip6tc_first_rule(this, handle);
1471
1472 num = 0;
1473 while (i) {
1474 print_firewall(i,
1475 ip6tc_get_target(i, handle),
1476 num++,
1477 format,
1478 *handle);
1479 i = ip6tc_next_rule(i, handle);
1480 }
1481 found = 1;
1482 }
1483
1484 errno = ENOENT;
1485 return found;
1486}
1487
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001488static char *get_modprobe(void)
1489{
1490 int procfile;
1491 char *ret;
1492
1493 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1494 if (procfile < 0)
1495 return NULL;
1496
1497 ret = malloc(1024);
1498 if (ret) {
1499 switch (read(procfile, ret, 1024)) {
1500 case -1: goto fail;
1501 case 1024: goto fail; /* Partial read. Wierd */
1502 }
1503 if (ret[strlen(ret)-1]=='\n')
1504 ret[strlen(ret)-1]=0;
1505 close(procfile);
1506 return ret;
1507 }
1508 fail:
1509 free(ret);
1510 close(procfile);
1511 return NULL;
1512}
1513
1514static int ip6tables_insmod(const char *modname, const char *modprobe)
1515{
1516 char *buf = NULL;
1517 char *argv[3];
1518
1519 /* If they don't explicitly set it, read out of kernel */
1520 if (!modprobe) {
1521 buf = get_modprobe();
1522 if (!buf)
1523 return -1;
1524 modprobe = buf;
1525 }
1526
1527 switch (fork()) {
1528 case 0:
1529 argv[0] = (char *)modprobe;
1530 argv[1] = (char *)modname;
1531 argv[2] = NULL;
1532 execv(argv[0], argv);
1533
1534 /* not usually reached */
1535 exit(0);
1536 case -1:
1537 return -1;
1538
1539 default: /* parent */
1540 wait(NULL);
1541 }
1542
1543 free(buf);
1544 return 0;
1545}
1546
Rusty Russell5eed48a2000-06-02 20:12:24 +00001547static struct ip6t_entry *
1548generate_entry(const struct ip6t_entry *fw,
1549 struct ip6tables_match *matches,
1550 struct ip6t_entry_target *target)
1551{
1552 unsigned int size;
1553 struct ip6tables_match *m;
1554 struct ip6t_entry *e;
1555
1556 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001557 for (m = matches; m; m = m->next) {
1558 if (!m->used)
1559 continue;
1560
Rusty Russell5eed48a2000-06-02 20:12:24 +00001561 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001562 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563
1564 e = fw_malloc(size + target->u.target_size);
1565 *e = *fw;
1566 e->target_offset = size;
1567 e->next_offset = size + target->u.target_size;
1568
1569 size = 0;
1570 for (m = matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001571 if (!m->used)
1572 continue;
1573
Rusty Russell5eed48a2000-06-02 20:12:24 +00001574 memcpy(e->elems + size, m->m, m->m->u.match_size);
1575 size += m->m->u.match_size;
1576 }
1577 memcpy(e->elems + size, target, target->u.target_size);
1578
1579 return e;
1580}
1581
1582int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1583{
1584 struct ip6t_entry fw, *e = NULL;
1585 int invert = 0;
1586 unsigned int nsaddrs = 0, ndaddrs = 0;
1587 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1588
1589 int c, verbose = 0;
1590 const char *chain = NULL;
1591 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1592 const char *policy = NULL, *newname = NULL;
1593 unsigned int rulenum = 0, options = 0, command = 0;
1594 int ret = 1;
1595 struct ip6tables_match *m;
1596 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001597 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001598 const char *jumpto = "";
1599 char *protocol = NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001600 const char *modprobe = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001601
1602 memset(&fw, 0, sizeof(fw));
1603
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001604 opts = original_opts;
1605 global_option_offset = 0;
1606
1607 /* re-set optind to 0 in case do_command gets called
1608 * a second time */
1609 optind = 0;
1610
1611 /* clear mflags in case do_command gets called a second time
1612 * (we clear the global list of all matches for security)*/
1613 for (m = ip6tables_matches; m; m = m->next) {
1614 m->mflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001615 m->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001616 }
1617
1618 for (t = ip6tables_targets; t; t = t->next) {
1619 t->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001620 t->used = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001621 }
1622
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623 /* Suppress error messages: we may add new options if we
1624 demand-load a protocol. */
1625 opterr = 0;
1626
1627 while ((c = getopt_long(argc, argv,
1628 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1629 opts, NULL)) != -1) {
1630 switch (c) {
1631 /*
1632 * Command selection
1633 */
1634 case 'A':
1635 add_command(&command, CMD_APPEND, CMD_NONE,
1636 invert);
1637 chain = optarg;
1638 break;
1639
1640 case 'D':
1641 add_command(&command, CMD_DELETE, CMD_NONE,
1642 invert);
1643 chain = optarg;
1644 if (optind < argc && argv[optind][0] != '-'
1645 && argv[optind][0] != '!') {
1646 rulenum = parse_rulenumber(argv[optind++]);
1647 command = CMD_DELETE_NUM;
1648 }
1649 break;
1650
1651 case 'C':
1652 add_command(&command, CMD_CHECK, CMD_NONE,
1653 invert);
1654 chain = optarg;
1655 break;
1656
1657 case 'R':
1658 add_command(&command, CMD_REPLACE, CMD_NONE,
1659 invert);
1660 chain = optarg;
1661 if (optind < argc && argv[optind][0] != '-'
1662 && argv[optind][0] != '!')
1663 rulenum = parse_rulenumber(argv[optind++]);
1664 else
1665 exit_error(PARAMETER_PROBLEM,
1666 "-%c requires a rule number",
1667 cmd2char(CMD_REPLACE));
1668 break;
1669
1670 case 'I':
1671 add_command(&command, CMD_INSERT, CMD_NONE,
1672 invert);
1673 chain = optarg;
1674 if (optind < argc && argv[optind][0] != '-'
1675 && argv[optind][0] != '!')
1676 rulenum = parse_rulenumber(argv[optind++]);
1677 else rulenum = 1;
1678 break;
1679
1680 case 'L':
1681 add_command(&command, CMD_LIST, CMD_ZERO,
1682 invert);
1683 if (optarg) chain = optarg;
1684 else if (optind < argc && argv[optind][0] != '-'
1685 && argv[optind][0] != '!')
1686 chain = argv[optind++];
1687 break;
1688
1689 case 'F':
1690 add_command(&command, CMD_FLUSH, CMD_NONE,
1691 invert);
1692 if (optarg) chain = optarg;
1693 else if (optind < argc && argv[optind][0] != '-'
1694 && argv[optind][0] != '!')
1695 chain = argv[optind++];
1696 break;
1697
1698 case 'Z':
1699 add_command(&command, CMD_ZERO, CMD_LIST,
1700 invert);
1701 if (optarg) chain = optarg;
1702 else if (optind < argc && argv[optind][0] != '-'
1703 && argv[optind][0] != '!')
1704 chain = argv[optind++];
1705 break;
1706
1707 case 'N':
1708 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1709 invert);
1710 chain = optarg;
1711 break;
1712
1713 case 'X':
1714 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1715 invert);
1716 if (optarg) chain = optarg;
1717 else if (optind < argc && argv[optind][0] != '-'
1718 && argv[optind][0] != '!')
1719 chain = argv[optind++];
1720 break;
1721
1722 case 'E':
1723 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1724 invert);
1725 chain = optarg;
1726 if (optind < argc && argv[optind][0] != '-'
1727 && argv[optind][0] != '!')
1728 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001729 else
1730 exit_error(PARAMETER_PROBLEM,
1731 "-%c requires old-chain-name and "
1732 "new-chain-name",
1733 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001734 break;
1735
1736 case 'P':
1737 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1738 invert);
1739 chain = optarg;
1740 if (optind < argc && argv[optind][0] != '-'
1741 && argv[optind][0] != '!')
1742 policy = argv[optind++];
1743 else
1744 exit_error(PARAMETER_PROBLEM,
1745 "-%c requires a chain and a policy",
1746 cmd2char(CMD_SET_POLICY));
1747 break;
1748
1749 case 'h':
1750 if (!optarg)
1751 optarg = argv[optind];
1752
1753 /* iptables -p icmp -h */
1754 if (!ip6tables_matches && protocol)
1755 find_match(protocol, TRY_LOAD);
1756
1757 exit_printhelp();
1758
1759 /*
1760 * Option selection
1761 */
1762 case 'p':
1763 if (check_inverse(optarg, &invert))
1764 optind++;
1765 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1766 invert);
1767
1768 /* Canonicalize into lower case */
1769 for (protocol = argv[optind-1]; *protocol; protocol++)
1770 *protocol = tolower(*protocol);
1771
1772 protocol = argv[optind-1];
1773 fw.ipv6.proto = parse_protocol(protocol);
1774 fw.ipv6.flags |= IP6T_F_PROTO;
1775
1776 if (fw.ipv6.proto == 0
1777 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1778 exit_error(PARAMETER_PROBLEM,
1779 "rule would never match protocol");
1780 fw.nfcache |= NFC_IP6_PROTO;
1781 break;
1782
1783 case 's':
1784 if (check_inverse(optarg, &invert))
1785 optind++;
1786 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1787 invert);
1788 shostnetworkmask = argv[optind-1];
1789 fw.nfcache |= NFC_IP6_SRC;
1790 break;
1791
1792 case 'd':
1793 if (check_inverse(optarg, &invert))
1794 optind++;
1795 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1796 invert);
1797 dhostnetworkmask = argv[optind-1];
1798 fw.nfcache |= NFC_IP6_DST;
1799 break;
1800
1801 case 'j':
1802 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1803 invert);
1804 jumpto = parse_target(optarg);
1805 target = find_target(jumpto, TRY_LOAD);
1806
1807 if (target) {
1808 size_t size;
1809
1810 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1811 + target->size);
1812
1813 target->t = fw_calloc(1, size);
1814 target->t->u.target_size = size;
1815 strcpy(target->t->u.user.name, jumpto);
1816 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001817 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001818 }
1819 break;
1820
1821
1822 case 'i':
1823 if (check_inverse(optarg, &invert))
1824 optind++;
1825 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1826 invert);
1827 parse_interface(argv[optind-1],
1828 fw.ipv6.iniface,
1829 fw.ipv6.iniface_mask);
1830 fw.nfcache |= NFC_IP6_IF_IN;
1831 break;
1832
1833 case 'o':
1834 if (check_inverse(optarg, &invert))
1835 optind++;
1836 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1837 invert);
1838 parse_interface(argv[optind-1],
1839 fw.ipv6.outiface,
1840 fw.ipv6.outiface_mask);
1841 fw.nfcache |= NFC_IP6_IF_OUT;
1842 break;
1843
1844 case 'v':
1845 if (!verbose)
1846 set_option(&options, OPT_VERBOSE,
1847 &fw.ipv6.invflags, invert);
1848 verbose++;
1849 break;
1850
1851 case 'm': {
1852 size_t size;
1853
1854 if (invert)
1855 exit_error(PARAMETER_PROBLEM,
1856 "unexpected ! flag before --match");
1857
1858 m = find_match(optarg, LOAD_MUST_SUCCEED);
1859 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1860 + m->size);
1861 m->m = fw_calloc(1, size);
1862 m->m->u.match_size = size;
1863 strcpy(m->m->u.user.name, m->name);
1864 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001865 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001866 }
1867 break;
1868
1869 case 'n':
1870 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1871 invert);
1872 break;
1873
1874 case 't':
1875 if (invert)
1876 exit_error(PARAMETER_PROBLEM,
1877 "unexpected ! flag before --table");
1878 *table = argv[optind-1];
1879 break;
1880
1881 case 'x':
1882 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1883 invert);
1884 break;
1885
1886 case 'V':
1887 if (invert)
1888 printf("Not %s ;-)\n", program_version);
1889 else
1890 printf("%s v%s\n",
1891 program_name, program_version);
1892 exit(0);
1893
1894 case '0':
1895 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1896 invert);
1897 break;
1898
1899 case 1: /* non option */
1900 if (optarg[0] == '!' && optarg[1] == '\0') {
1901 if (invert)
1902 exit_error(PARAMETER_PROBLEM,
1903 "multiple consecutive ! not"
1904 " allowed");
1905 invert = TRUE;
1906 optarg[0] = '\0';
1907 continue;
1908 }
1909 printf("Bad argument `%s'\n", optarg);
1910 exit_tryhelp(2);
1911
1912 default:
1913 /* FIXME: This scheme doesn't allow two of the same
1914 matches --RR */
1915 if (!target
1916 || !(target->parse(c - target->option_offset,
1917 argv, invert,
1918 &target->tflags,
1919 &fw, &target->t))) {
1920 for (m = ip6tables_matches; m; m = m->next) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001921 if (!m->used)
1922 continue;
1923
Rusty Russell5eed48a2000-06-02 20:12:24 +00001924 if (m->parse(c - m->option_offset,
1925 argv, invert,
1926 &m->mflags,
1927 &fw,
1928 &fw.nfcache,
1929 &m->m))
1930 break;
1931 }
1932
1933 /* If you listen carefully, you can
1934 actually hear this code suck. */
1935 if (m == NULL
1936 && protocol
1937 && !find_proto(protocol, DONT_LOAD,
1938 options&OPT_NUMERIC)
1939 && (m = find_proto(protocol, TRY_LOAD,
1940 options&OPT_NUMERIC))) {
1941 /* Try loading protocol */
1942 size_t size;
1943
1944 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1945 + m->size);
1946
1947 m->m = fw_calloc(1, size);
1948 m->m->u.match_size = size;
1949 strcpy(m->m->u.user.name, m->name);
1950 m->init(m->m, &fw.nfcache);
1951
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001952 opts = merge_options(opts,
1953 m->extra_opts, &m->option_offset);
1954
Rusty Russell5eed48a2000-06-02 20:12:24 +00001955 optind--;
1956 continue;
1957 }
1958 if (!m)
1959 exit_error(PARAMETER_PROBLEM,
1960 "Unknown arg `%s'",
1961 argv[optind-1]);
1962 }
1963 }
1964 invert = FALSE;
1965 }
1966
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001967 for (m = ip6tables_matches; m; m = m->next) {
1968 if (!m->used)
1969 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001970
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001971 m->final_check(m->mflags);
1972 }
1973
1974 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001975 target->final_check(target->tflags);
1976
1977 /* Fix me: must put inverse options checking here --MN */
1978
1979 if (optind < argc)
1980 exit_error(PARAMETER_PROBLEM,
1981 "unknown arguments found on commandline");
1982 if (!command)
1983 exit_error(PARAMETER_PROBLEM, "no command specified");
1984 if (invert)
1985 exit_error(PARAMETER_PROBLEM,
1986 "nothing appropriate following !");
1987
1988 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1989 CMD_CHECK)) {
1990 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001991 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001992 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00001993 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001994 }
1995
1996 if (shostnetworkmask)
1997 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1998 &(fw.ipv6.smsk), &nsaddrs);
1999
2000 if (dhostnetworkmask)
2001 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2002 &(fw.ipv6.dmsk), &ndaddrs);
2003
2004 if ((nsaddrs > 1 || ndaddrs > 1) &&
2005 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2006 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2007 " source or destination IP addresses");
2008
2009 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2010 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2011 cmd2char(CMD_CHECK));
2012
2013 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2014 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2015 "specify a unique address");
2016
2017 generic_opt_check(command, options);
2018
2019 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2020 exit_error(PARAMETER_PROBLEM,
2021 "chain name `%s' too long (must be under %i chars)",
2022 chain, IP6T_FUNCTION_MAXNAMELEN);
2023
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002024 /* only allocate handle if we weren't called with a handle */
2025 if (!*handle)
2026 *handle = ip6tc_init(*table);
2027
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002028 if (!*handle) {
2029 /* try to insmod the module if iptc_init failed */
2030 ip6tables_insmod("ip6_tables", modprobe);
2031 *handle = ip6tc_init(*table);
2032 }
2033
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002034 if (!*handle)
2035 exit_error(VERSION_PROBLEM,
2036 "can't initialize ip6tables table `%s': %s",
2037 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002038
2039 if (command == CMD_CHECK
2040 || command == CMD_APPEND
2041 || command == CMD_DELETE
2042 || command == CMD_INSERT
2043 || command == CMD_REPLACE) {
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002044 if (strcmp(chain, "PREROUTING") == 0
2045 || strcmp(chain, "INPUT") == 0) {
2046 /* -o not valid with incoming packets. */
2047 if (options & OPT_VIANAMEOUT)
2048 exit_error(PARAMETER_PROBLEM,
2049 "Can't use -%c with %s\n",
2050 opt2char(OPT_VIANAMEOUT),
2051 chain);
2052 /* -i required with -C */
2053 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2054 exit_error(PARAMETER_PROBLEM,
2055 "Need -%c with %s\n",
2056 opt2char(OPT_VIANAMEIN),
2057 chain);
2058 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002059
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002060 if (strcmp(chain, "POSTROUTING") == 0
2061 || strcmp(chain, "OUTPUT") == 0) {
2062 /* -i not valid with outgoing packets */
2063 if (options & OPT_VIANAMEIN)
2064 exit_error(PARAMETER_PROBLEM,
2065 "Can't use -%c with %s\n",
2066 opt2char(OPT_VIANAMEIN),
2067 chain);
2068 /* -o required with -C */
2069 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2070 exit_error(PARAMETER_PROBLEM,
2071 "Need -%c with %s\n",
2072 opt2char(OPT_VIANAMEOUT),
2073 chain);
2074 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002075
2076 if (target && ip6tc_is_chain(jumpto, *handle)) {
2077 printf("Warning: using chain %s, not extension\n",
2078 jumpto);
2079
2080 target = NULL;
2081 }
2082
2083 /* If they didn't specify a target, or it's a chain
2084 name, use standard. */
2085 if (!target
2086 && (strlen(jumpto) == 0
2087 || ip6tc_is_chain(jumpto, *handle))) {
2088 size_t size;
2089
2090 target = find_target(IP6T_STANDARD_TARGET,
2091 LOAD_MUST_SUCCEED);
2092
2093 size = sizeof(struct ip6t_entry_target)
2094 + target->size;
2095 target->t = fw_calloc(1, size);
2096 target->t->u.target_size = size;
2097 strcpy(target->t->u.user.name, jumpto);
2098 target->init(target->t, &fw.nfcache);
2099 }
2100
2101 if (!target) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002102#if 0
Rusty Russell5eed48a2000-06-02 20:12:24 +00002103 struct ip6t_entry_target unknown_target;
2104
2105 /* Don't know it. Must be extension with no
2106 options? */
2107 unknown_target.u.target_size = sizeof(unknown_target);
2108 strcpy(unknown_target.u.user.name, jumpto);
2109
2110 e = generate_entry(&fw, ip6tables_matches,
2111 &unknown_target);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002112#endif
2113 /* it is no chain, and we can't load a plugin.
2114 * We cannot know if the plugin is corrupt, non
2115 * existant OR if the user just misspelled a
2116 * chain. */
2117 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002118 } else {
2119 e = generate_entry(&fw, ip6tables_matches, target->t);
2120 }
2121 }
2122
2123 switch (command) {
2124 case CMD_APPEND:
2125 ret = append_entry(chain, e,
2126 nsaddrs, saddrs, ndaddrs, daddrs,
2127 options&OPT_VERBOSE,
2128 handle);
2129 break;
2130 case CMD_CHECK:
2131 ret = check_packet(chain, e,
2132 nsaddrs, saddrs, ndaddrs, daddrs,
2133 options&OPT_VERBOSE, handle);
2134 break;
2135 case CMD_DELETE:
2136 ret = delete_entry(chain, e,
2137 nsaddrs, saddrs, ndaddrs, daddrs,
2138 options&OPT_VERBOSE,
2139 handle);
2140 break;
2141 case CMD_DELETE_NUM:
2142 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2143 break;
2144 case CMD_REPLACE:
2145 ret = replace_entry(chain, e, rulenum - 1,
2146 saddrs, daddrs, options&OPT_VERBOSE,
2147 handle);
2148 break;
2149 case CMD_INSERT:
2150 ret = insert_entry(chain, e, rulenum - 1,
2151 nsaddrs, saddrs, ndaddrs, daddrs,
2152 options&OPT_VERBOSE,
2153 handle);
2154 break;
2155 case CMD_LIST:
2156 ret = list_entries(chain,
2157 options&OPT_VERBOSE,
2158 options&OPT_NUMERIC,
2159 options&OPT_EXPANDED,
2160 options&OPT_LINENUMBERS,
2161 handle);
2162 break;
2163 case CMD_FLUSH:
2164 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2165 break;
2166 case CMD_ZERO:
2167 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2168 break;
2169 case CMD_LIST|CMD_ZERO:
2170 ret = list_entries(chain,
2171 options&OPT_VERBOSE,
2172 options&OPT_NUMERIC,
2173 options&OPT_EXPANDED,
2174 options&OPT_LINENUMBERS,
2175 handle);
2176 if (ret)
2177 ret = zero_entries(chain,
2178 options&OPT_VERBOSE, handle);
2179 break;
2180 case CMD_NEW_CHAIN:
2181 ret = ip6tc_create_chain(chain, handle);
2182 break;
2183 case CMD_DELETE_CHAIN:
2184 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2185 break;
2186 case CMD_RENAME_CHAIN:
2187 ret = ip6tc_rename_chain(chain, newname, handle);
2188 break;
2189 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002190 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002191 break;
2192 default:
2193 /* We should never reach this... */
2194 exit_tryhelp(2);
2195 }
2196
2197 if (verbose > 1)
2198 dump_entries6(*handle);
2199
2200 return ret;
2201}