blob: 045b4baa2645c85e50672c06585a2fdb2d9dff77 [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>
33
34#ifndef TRUE
35#define TRUE 1
36#endif
37#ifndef FALSE
38#define FALSE 0
39#endif
40
41#ifndef IP6T_LIB_DIR
42#define IP6T_LIB_DIR "/usr/local/lib/iptables"
43#endif
44
45#define FMT_NUMERIC 0x0001
46#define FMT_NOCOUNTS 0x0002
47#define FMT_KILOMEGAGIGA 0x0004
48#define FMT_OPTIONS 0x0008
49#define FMT_NOTABLE 0x0010
50#define FMT_NOTARGET 0x0020
51#define FMT_VIA 0x0040
52#define FMT_NONEWLINE 0x0080
53#define FMT_LINENUMBERS 0x0100
54
55#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
56 | FMT_NUMERIC | FMT_NOTABLE)
57#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
58
59
60#define CMD_NONE 0x0000U
61#define CMD_INSERT 0x0001U
62#define CMD_DELETE 0x0002U
63#define CMD_DELETE_NUM 0x0004U
64#define CMD_REPLACE 0x0008U
65#define CMD_APPEND 0x0010U
66#define CMD_LIST 0x0020U
67#define CMD_FLUSH 0x0040U
68#define CMD_ZERO 0x0080U
69#define CMD_NEW_CHAIN 0x0100U
70#define CMD_DELETE_CHAIN 0x0200U
71#define CMD_SET_POLICY 0x0400U
72#define CMD_CHECK 0x0800U
73#define CMD_RENAME_CHAIN 0x1000U
74#define NUMBER_OF_CMD 13
75static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
76 'N', 'X', 'P', 'C', 'E' };
77
78#define OPTION_OFFSET 256
79
80#define OPT_NONE 0x00000U
81#define OPT_NUMERIC 0x00001U
82#define OPT_SOURCE 0x00002U
83#define OPT_DESTINATION 0x00004U
84#define OPT_PROTOCOL 0x00008U
85#define OPT_JUMP 0x00010U
86#define OPT_VERBOSE 0x00020U
87#define OPT_EXPANDED 0x00040U
88#define OPT_VIANAMEIN 0x00080U
89#define OPT_VIANAMEOUT 0x00100U
90#define OPT_LINENUMBERS 0x00200U
91#define NUMBER_OF_OPT 10
92static const char optflags[NUMBER_OF_OPT]
93= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3'};
94
95static struct option original_opts[] = {
96 { "append", 1, 0, 'A' },
97 { "delete", 1, 0, 'D' },
98 { "insert", 1, 0, 'I' },
99 { "replace", 1, 0, 'R' },
100 { "list", 2, 0, 'L' },
101 { "flush", 2, 0, 'F' },
102 { "zero", 2, 0, 'Z' },
103 { "check", 1, 0, 'C' },
104 { "new-chain", 1, 0, 'N' },
105 { "delete-chain", 2, 0, 'X' },
106 { "rename-chain", 2, 0, 'E' },
107 { "policy", 1, 0, 'P' },
108 { "source", 1, 0, 's' },
109 { "destination", 1, 0, 'd' },
110 { "src", 1, 0, 's' }, /* synonym */
111 { "dst", 1, 0, 'd' }, /* synonym */
112 { "protocol", 1, 0, 'p' },
113 { "in-interface", 1, 0, 'i' },
114 { "jump", 1, 0, 'j' },
115 { "table", 1, 0, 't' },
116 { "match", 1, 0, 'm' },
117 { "numeric", 0, 0, 'n' },
118 { "out-interface", 1, 0, 'o' },
119 { "verbose", 0, 0, 'v' },
120 { "exact", 0, 0, 'x' },
121 { "version", 0, 0, 'V' },
122 { "help", 2, 0, 'h' },
123 { "line-numbers", 0, 0, '0' },
124 { 0 }
125};
126
127static struct option *opts = original_opts;
128static unsigned int global_option_offset = 0;
129
130/* Table of legal combinations of commands and options. If any of the
131 * given commands make an option legal, that option is legal (applies to
132 * CMD_LIST and CMD_ZERO only).
133 * Key:
134 * + compulsory
135 * x illegal
136 * optional
137 */
138
139static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
140/* Well, it's better than "Re: Linux vs FreeBSD" */
141{
142 /* -n -s -d -p -j -v -x -i -o --line */
143/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
144/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
145/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
146/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
147/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
148/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
149/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
150/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
151/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
152/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
153/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
154/*CHECK*/ {'x','+','+','+','x',' ','x','+','+','x'},
155/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
156};
157
158static int inverse_for_options[NUMBER_OF_OPT] =
159{
160/* -n */ 0,
161/* -s */ IP6T_INV_SRCIP,
162/* -d */ IP6T_INV_DSTIP,
163/* -p */ IP6T_INV_PROTO,
164/* -j */ 0,
165/* -v */ 0,
166/* -x */ 0,
167/* -i */ IP6T_INV_VIA_IN,
168/* -o */ IP6T_INV_VIA_OUT,
169/*--line*/ 0
170};
171
172const char *program_version;
173const char *program_name;
174
175/* Keeping track of external matches and targets: linked lists. */
176struct ip6tables_match *ip6tables_matches = NULL;
177struct ip6tables_target *ip6tables_targets = NULL;
178
179/* Extra debugging from libiptc */
180extern void dump_entries6(const ip6tc_handle_t handle);
181
182/* A few hardcoded protocols for 'all' and in case the user has no
183 /etc/protocols */
184struct pprot {
185 char *name;
186 u_int8_t num;
187};
188
189static const struct pprot chain_protos[] = {
190 { "tcp", IPPROTO_TCP },
191 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000192 { "icmpv6", IPPROTO_ICMPV6 },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000193 { "all", 0 },
194};
195
196static char *
197proto_to_name(u_int8_t proto, int nolookup)
198{
199 unsigned int i;
200
201 if (proto && !nolookup) {
202 struct protoent *pent = getprotobynumber(proto);
203 if (pent)
204 return pent->p_name;
205 }
206
207 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
208 if (chain_protos[i].num == proto)
209 return chain_protos[i].name;
210
211 return NULL;
212}
213
214static void
215in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
216{
217 memcpy(dst, src, sizeof(struct in6_addr));
218}
219
220void
221exit_error(enum exittype status, char *msg, ...)
222{
223 va_list args;
224
225 va_start(args, msg);
226 fprintf(stderr, "%s v%s: ", program_name, program_version);
227 vfprintf(stderr, msg, args);
228 va_end(args);
229 fprintf(stderr, "\n");
230 if (status == PARAMETER_PROBLEM)
231 exit_tryhelp(status);
232 if (status == VERSION_PROBLEM)
233 fprintf(stderr,
234 "Perhaps iptables or your kernel needs to be upgraded.\n");
235 exit(status);
236}
237
238void
239exit_tryhelp(int status)
240{
241 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
242 program_name, program_name );
243 exit(status);
244}
245
246void
247exit_printhelp(void)
248{
249 struct ip6tables_match *m = NULL;
250 struct ip6tables_target *t = NULL;
251
252 printf("%s v%s\n\n"
253"Usage: %s -[ADC] chain rule-specification [options]\n"
254" %s -[RI] chain rulenum rule-specification [options]\n"
255" %s -D chain rulenum [options]\n"
256" %s -[LFZ] [chain] [options]\n"
257" %s -[NX] chain\n"
258" %s -E old-chain-name new-chain-name\n"
259" %s -P chain target [options]\n"
260" %s -h (print this help information)\n\n",
261 program_name, program_version, program_name, program_name,
262 program_name, program_name, program_name, program_name,
263 program_name, program_name);
264
265 printf(
266"Commands:\n"
267"Either long or short options are allowed.\n"
268" --append -A chain Append to chain\n"
269" --delete -D chain Delete matching rule from chain\n"
270" --delete -D chain rulenum\n"
271" Delete rule rulenum (1 = first) from chain\n"
272" --insert -I chain [rulenum]\n"
273" Insert in chain as rulenum (default 1=first)\n"
274" --replace -R chain rulenum\n"
275" Replace rule rulenum (1 = first) in chain\n"
276" --list -L [chain] List the rules in a chain or all chains\n"
277" --flush -F [chain] Delete all rules in chain or all chains\n"
278" --zero -Z [chain] Zero counters in chain or all chains\n"
279" --check -C chain Test this packet on chain\n"
280" --new -N chain Create a new user-defined chain\n"
281" --delete-chain\n"
282" -X [chain] Delete a user-defined chain\n"
283" --policy -P chain target\n"
284" Change policy on chain to target\n"
285" --rename-chain\n"
286" -E old-chain new-chain\n"
287" Change chain name, (moving any references)\n"
288
289"Options:\n"
290" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
291" --source -s [!] address[/mask]\n"
292" source specification\n"
293" --destination -d [!] address[/mask]\n"
294" destination specification\n"
295" --in-interface -i [!] input name[+]\n"
296" network interface name ([+] for wildcard)\n"
297" --jump -j target\n"
298" target for rule\n"
299" --numeric -n numeric output of addresses and ports\n"
300" --out-interface -o [!] output name[+]\n"
301" network interface name ([+] for wildcard)\n"
302" --table -t table table to manipulate (default: `filter')\n"
303" --verbose -v verbose mode\n"
304" --exact -x expand numbers (display exact values)\n"
305"[!] --fragment -f match second or further fragments only\n"
306"[!] --version -V print package version.\n");
307
308 /* Print out any special helps. A user might like to be able to add a --help
309 to the commandline, and see expected results. So we call help for all
310 matches & targets */
311 for (t=ip6tables_targets;t;t=t->next) {
312 printf("\n");
313 t->help();
314 }
315 for (m=ip6tables_matches;m;m=m->next) {
316 printf("\n");
317 m->help();
318 }
319 exit(0);
320}
321
322static void
323generic_opt_check(int command, int options)
324{
325 int i, j, legal = 0;
326
327 /* Check that commands are valid with options. Complicated by the
328 * fact that if an option is legal with *any* command given, it is
329 * legal overall (ie. -z and -l).
330 */
331 for (i = 0; i < NUMBER_OF_OPT; i++) {
332 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
333
334 for (j = 0; j < NUMBER_OF_CMD; j++) {
335 if (!(command & (1<<j)))
336 continue;
337
338 if (!(options & (1<<i))) {
339 if (commands_v_options[j][i] == '+')
340 exit_error(PARAMETER_PROBLEM,
341 "You need to supply the `-%c' "
342 "option for this command\n",
343 optflags[i]);
344 } else {
345 if (commands_v_options[j][i] != 'x')
346 legal = 1;
347 else if (legal == 0)
348 legal = -1;
349 }
350 }
351 if (legal == -1)
352 exit_error(PARAMETER_PROBLEM,
353 "Illegal option `-%c' with this command\n",
354 optflags[i]);
355 }
356}
357
358static char
359opt2char(int option)
360{
361 const char *ptr;
362 for (ptr = optflags; option > 1; option >>= 1, ptr++);
363
364 return *ptr;
365}
366
367static char
368cmd2char(int option)
369{
370 const char *ptr;
371 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
372
373 return *ptr;
374}
375
376static void
377add_command(int *cmd, const int newcmd, const int othercmds, int invert)
378{
379 if (invert)
380 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
381 if (*cmd & (~othercmds))
382 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
383 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
384 *cmd |= newcmd;
385}
386
387int
388check_inverse(const char option[], int *invert)
389{
390 if (option && strcmp(option, "!") == 0) {
391 if (*invert)
392 exit_error(PARAMETER_PROBLEM,
393 "Multiple `!' flags not allowed");
394
395 *invert = TRUE;
396 return TRUE;
397 }
398 return FALSE;
399}
400
401static void *
402fw_calloc(size_t count, size_t size)
403{
404 void *p;
405
406 if ((p = calloc(count, size)) == NULL) {
407 perror("iptables: calloc failed");
408 exit(1);
409 }
410 return p;
411}
412
413static void *
414fw_malloc(size_t size)
415{
416 void *p;
417
418 if ((p = malloc(size)) == NULL) {
419 perror("iptables: malloc failed");
420 exit(1);
421 }
422 return p;
423}
424
425static struct in6_addr *
426host_to_addr(const char *name, unsigned int *naddr)
427{
428 struct hostent *host;
429 struct in6_addr *addr;
430 unsigned int i;
431
432 *naddr = 0;
433 if ((host = gethostbyname2(name, AF_INET6)) != NULL) {
434 if (host->h_addrtype != AF_INET6 ||
435 host->h_length != sizeof(struct in6_addr))
436 return (struct in6_addr *) NULL;
437
438 while (host->h_addr_list[*naddr] != (char *) NULL)
439 (*naddr)++;
440 addr = fw_calloc(*naddr, sizeof(struct in6_addr));
441 for (i = 0; i < *naddr; i++)
442 in6addrcpy(&(addr[i]),
443 (struct in6_addr *) host->h_addr_list[i]);
444 return addr;
445 }
446
447 return (struct in6_addr *) NULL;
448}
449
450static char *
451addr_to_host(const struct in6_addr *addr)
452{
453 struct hostent *host;
454
455 if ((host = gethostbyaddr((char *) addr,
456 sizeof(struct in_addr), AF_INET6)) != NULL)
457 return (char *) host->h_name;
458
459 return (char *) NULL;
460}
461
462static char *
463addr_to_numeric(const struct in6_addr *addrp)
464{
465 static char buf[20];
466 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof buf);
467}
468
469static struct in6_addr *
470numeric_to_addr(const char *num)
471{
472 static struct in6_addr ap;
473 if (inet_pton(AF_INET6, num, &ap) == 1)
474 return &ap;
475 return (struct in6_addr *)NULL;
476}
477
478static char *
479mask_to_numeric(const struct in6_addr *addrp)
480{
481 static char buf[20];
482 int l = ipv6_prefix_length(addrp);
483 if (l == -1)
484 return addr_to_numeric(addrp);
485 sprintf(buf, "%d", l);
486 return buf;
487}
488
489static struct in6_addr *
490network_to_addr(const char *name)
491{
492 abort();
493}
494
495static char *
496addr_to_anyname(const struct in6_addr *addr)
497{
498 char *name;
499
500 if ((name = addr_to_host(addr)) != NULL)
501 return name;
502
503 return addr_to_numeric(addr);
504}
505
506/*
507 * All functions starting with "parse" should succeed, otherwise
508 * the program fails.
509 * Most routines return pointers to static data that may change
510 * between calls to the same or other routines with a few exceptions:
511 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
512 * return global static data.
513*/
514
515static struct in6_addr *
516parse_hostnetwork(const char *name, unsigned int *naddrs)
517{
518 struct in6_addr *addrp, *addrptmp;
519
520 if ((addrptmp = numeric_to_addr(name)) != NULL ||
521 (addrptmp = network_to_addr(name)) != NULL) {
522 addrp = fw_malloc(sizeof(struct in6_addr));
523 in6addrcpy(addrp, addrptmp);
524 *naddrs = 1;
525 return addrp;
526 }
527 if ((addrp = host_to_addr(name, naddrs)) != NULL)
528 return addrp;
529
530 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
531}
532
533static struct in6_addr *
534parse_mask(char *mask)
535{
536 static struct in6_addr maskaddr;
537 struct in6_addr *addrp;
538 int bits;
539
540 if (mask == NULL) {
541 /* no mask at all defaults to 128 bits */
542 memset(&maskaddr, 0xff, sizeof maskaddr);
543 return &maskaddr;
544 }
545 if ((addrp = numeric_to_addr(mask)) != NULL)
546 return addrp;
547 if ((bits = string_to_number(mask, 0, 128)) == -1)
548 exit_error(PARAMETER_PROBLEM,
549 "invalid mask `%s' specified", mask);
550 if (bits != 0) {
551 char *p = (char *)&maskaddr;
552 memset(p, 0xff, bits / 8);
553 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
554 p[bits / 8] = 0xff << (8 - (bits & 7));
555 return &maskaddr;
556 }
557
558 memset(&maskaddr, 0, sizeof maskaddr);
559 return &maskaddr;
560}
561
562static void
563parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
564 struct in6_addr *maskp, unsigned int *naddrs)
565{
566 struct in6_addr *addrp;
567 char buf[256];
568 char *p;
569 int i, j, n;
570
571 strncpy(buf, name, sizeof(buf) - 1);
572 if ((p = strrchr(buf, '/')) != NULL) {
573 *p = '\0';
574 addrp = parse_mask(p + 1);
575 } else
576 addrp = parse_mask(NULL);
577 in6addrcpy(maskp, addrp);
578
579 /* if a null mask is given, the name is ignored, like in "any/0" */
580 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
581 strcpy(buf, "::");
582
583 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
584 n = *naddrs;
585 for (i = 0, j = 0; i < n; i++) {
586 int k;
587 for (k = 0; k < 4; k++)
588 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
589 j++;
590 for (k = 0; k < j - 1; k++) {
591 if (!memcmp(&addrp[k], &addrp[j - 1], sizeof(struct in6_addr))) {
592 (*naddrs)--;
593 j--;
594 break;
595 }
596 }
597 }
598}
599
600struct ip6tables_match *
601find_match(const char *name, enum ip6t_tryload tryload)
602{
603 struct ip6tables_match *ptr;
604
605 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
606 if (strcmp(name, ptr->name) == 0)
607 break;
608 }
609
610 if (!ptr && tryload != DONT_LOAD) {
611 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
612 + strlen(name)];
613 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
614 if (dlopen(path, RTLD_NOW)) {
615 /* Found library. If it didn't register itself,
616 maybe they specified target as match. */
617 ptr = find_match(name, DONT_LOAD);
618
619 if (!ptr)
620 exit_error(PARAMETER_PROBLEM,
621 "Couldn't load match `%s'\n",
622 name);
623 } else if (tryload == LOAD_MUST_SUCCEED)
624 exit_error(PARAMETER_PROBLEM,
625 "Couldn't load match `%s'\n", name);
626 }
627
628 return ptr;
629}
630
631/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
632static struct ip6tables_match *
633find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
634{
635 int proto;
636
637 proto = string_to_number(pname, 0, 255);
638 if (proto != -1)
639 return find_match(proto_to_name(proto, nolookup), tryload);
640
641 return find_match(pname, tryload);
642}
643
644static u_int16_t
645parse_protocol(const char *s)
646{
647 int proto = string_to_number(s, 0, 255);
648
649 if (proto == -1) {
650 struct protoent *pent;
651
652 if ((pent = getprotobyname(s)))
653 proto = pent->p_proto;
654 else {
655 unsigned int i;
656 for (i = 0;
657 i < sizeof(chain_protos)/sizeof(struct pprot);
658 i++) {
659 if (strcmp(s, chain_protos[i].name) == 0) {
660 proto = chain_protos[i].num;
661 break;
662 }
663 }
664 if (i == sizeof(chain_protos)/sizeof(struct pprot))
665 exit_error(PARAMETER_PROBLEM,
666 "unknown protocol `%s' specified",
667 s);
668 }
669 }
670
671 return (u_int16_t)proto;
672}
673
674static void
675parse_interface(const char *arg, char *vianame, unsigned char *mask)
676{
677 int vialen = strlen(arg);
678 unsigned int i;
679
680 memset(mask, 0, IFNAMSIZ);
681 memset(vianame, 0, IFNAMSIZ);
682
683 if (vialen + 1 > IFNAMSIZ)
684 exit_error(PARAMETER_PROBLEM,
685 "interface name `%s' must be shorter than IFNAMSIZ"
686 " (%i)", arg, IFNAMSIZ-1);
687
688 strcpy(vianame, arg);
689 if (vialen == 0)
690 memset(mask, 0, IFNAMSIZ);
691 else if (vianame[vialen - 1] == '+') {
692 memset(mask, 0xFF, vialen - 1);
693 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
694 /* Remove `+' */
695 vianame[vialen - 1] = '\0';
696 } else {
697 /* Include nul-terminator in match */
698 memset(mask, 0xFF, vialen + 1);
699 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
700 }
701 for (i = 0; vianame[i]; i++) {
702 if (!isalnum(vianame[i])) {
703 printf("Warning: wierd character in interface"
704 " `%s' (No aliases, :, ! or *).\n",
705 vianame);
706 break;
707 }
708 }
709}
710
711/* Can't be zero. */
712static int
713parse_rulenumber(const char *rule)
714{
715 int rulenum = string_to_number(rule, 1, INT_MAX);
716
717 if (rulenum == -1)
718 exit_error(PARAMETER_PROBLEM,
719 "Invalid rule number `%s'", rule);
720
721 return rulenum;
722}
723
724static const char *
725parse_target(const char *targetname)
726{
727 const char *ptr;
728
729 if (strlen(targetname) < 1)
730 exit_error(PARAMETER_PROBLEM,
731 "Invalid target name (too short)");
732
733 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
734 exit_error(PARAMETER_PROBLEM,
735 "Invalid target name `%s' (%i chars max)",
736 targetname, sizeof(ip6t_chainlabel)-1);
737
738 for (ptr = targetname; *ptr; ptr++)
739 if (isspace(*ptr))
740 exit_error(PARAMETER_PROBLEM,
741 "Invalid target name `%s'", targetname);
742 return targetname;
743}
744
745int
746string_to_number(const char *s, int min, int max)
747{
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000748 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000749 char *end;
750
751 /* Handle hex, octal, etc. */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000752 errno = 0;
753 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000754 if (*end == '\0' && end != s) {
755 /* we parsed a number, let's see if we want this */
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000756 if (errno != ERANGE && min <= number && number <= max)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000757 return number;
758 }
759 return -1;
760}
761
762static void
763set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
764 int invert)
765{
766 if (*options & option)
767 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
768 opt2char(option));
769 *options |= option;
770
771 if (invert) {
772 unsigned int i;
773 for (i = 0; 1 << i != option; i++);
774
775 if (!inverse_for_options[i])
776 exit_error(PARAMETER_PROBLEM,
777 "cannot have ! before -%c",
778 opt2char(option));
779 *invflg |= inverse_for_options[i];
780 }
781}
782
783struct ip6tables_target *
784find_target(const char *name, enum ip6t_tryload tryload)
785{
786 struct ip6tables_target *ptr;
787
788 /* Standard target? */
789 if (strcmp(name, "") == 0
790 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
791 || strcmp(name, IP6TC_LABEL_DROP) == 0
792 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
793 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
794 name = "standard";
795
796 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
797 if (strcmp(name, ptr->name) == 0)
798 break;
799 }
800
801 if (!ptr && tryload != DONT_LOAD) {
802 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
803 + strlen(name)];
804 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
805 if (dlopen(path, RTLD_NOW)) {
806 /* Found library. If it didn't register itself,
807 maybe they specified match as a target. */
808 ptr = find_target(name, DONT_LOAD);
809 if (!ptr)
810 exit_error(PARAMETER_PROBLEM,
811 "Couldn't load target `%s'\n",
812 name);
813 } else if (tryload == LOAD_MUST_SUCCEED)
814 exit_error(PARAMETER_PROBLEM,
815 "Couldn't load target `%s'\n", name);
816 }
817
818 return ptr;
819}
820
821static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000822merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000823 unsigned int *option_offset)
824{
825 unsigned int num_old, num_new, i;
826 struct option *merge;
827
828 for (num_old = 0; oldopts[num_old].name; num_old++);
829 for (num_new = 0; newopts[num_new].name; num_new++);
830
831 global_option_offset += OPTION_OFFSET;
832 *option_offset = global_option_offset;
833
834 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
835 memcpy(merge, oldopts, num_old * sizeof(struct option));
836 for (i = 0; i < num_new; i++) {
837 merge[num_old + i] = newopts[i];
838 merge[num_old + i].val += *option_offset;
839 }
840 memset(merge + num_old + num_new, 0, sizeof(struct option));
841
842 return merge;
843}
844
845void
846register_match6(struct ip6tables_match *me)
847{
848 if (strcmp(me->version, program_version) != 0) {
849 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
850 program_name, me->name, me->version, program_version);
851 exit(1);
852 }
853
854 if (find_match(me->name, DONT_LOAD)) {
855 fprintf(stderr, "%s: match `%s' already registered.\n",
856 program_name, me->name);
857 exit(1);
858 }
859
860 /* Prepend to list. */
861 me->next = ip6tables_matches;
862 ip6tables_matches = me;
863 me->m = NULL;
864 me->mflags = 0;
865
866 opts = merge_options(opts, me->extra_opts, &me->option_offset);
867}
868
869void
870register_target6(struct ip6tables_target *me)
871{
872 if (strcmp(me->version, program_version) != 0) {
873 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
874 program_name, me->name, me->version, program_version);
875 exit(1);
876 }
877
878 if (find_target(me->name, DONT_LOAD)) {
879 fprintf(stderr, "%s: target `%s' already registered.\n",
880 program_name, me->name);
881 exit(1);
882 }
883
884 /* Prepend to list. */
885 me->next = ip6tables_targets;
886 ip6tables_targets = me;
887 me->t = NULL;
888 me->tflags = 0;
889
890 opts = merge_options(opts, me->extra_opts, &me->option_offset);
891}
892
893static void
894print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
895{
896 struct ip6t_counters counters;
897 const char *pol = ip6tc_get_policy(chain, &counters, handle);
898 printf("Chain %s", chain);
899 if (pol) {
900 printf(" (policy %s", pol);
901 if (!(format & FMT_NOCOUNTS))
902 printf(" %llu packets, %llu bytes",
903 counters.pcnt, counters.bcnt);
904 printf(")\n");
905 } else {
906 unsigned int refs;
907 if (!ip6tc_get_references(&refs, chain, handle))
908 printf(" (ERROR obtaining refs)\n");
909 else
910 printf(" (%u references)\n", refs);
911 }
912
913 if (format & FMT_LINENUMBERS)
914 printf(FMT("%-4s ", "%s "), "num");
915 if (!(format & FMT_NOCOUNTS)) {
916 if (format & FMT_KILOMEGAGIGA) {
917 printf(FMT("%5s ","%s "), "pkts");
918 printf(FMT("%5s ","%s "), "bytes");
919 } else {
920 printf(FMT("%8s ","%s "), "pkts");
921 printf(FMT("%10s ","%s "), "bytes");
922 }
923 }
924 if (!(format & FMT_NOTARGET))
925 printf(FMT("%-9s ","%s "), "target");
926 fputs(" prot ", stdout);
927 if (format & FMT_OPTIONS)
928 fputs("opt", stdout);
929 if (format & FMT_VIA) {
930 printf(FMT(" %-6s ","%s "), "in");
931 printf(FMT("%-6s ","%s "), "out");
932 }
933 printf(FMT(" %-19s ","%s "), "source");
934 printf(FMT(" %-19s "," %s "), "destination");
935 printf("\n");
936}
937
938static void
939print_num(u_int64_t number, unsigned int format)
940{
941 if (format & FMT_KILOMEGAGIGA) {
942 if (number > 99999) {
943 number = (number + 500) / 1000;
944 if (number > 9999) {
945 number = (number + 500) / 1000;
946 if (number > 9999) {
947 number = (number + 500) / 1000;
948 printf(FMT("%4lluG ","%lluG "),number);
949 }
950 else printf(FMT("%4lluM ","%lluM "), number);
951 } else
952 printf(FMT("%4lluK ","%lluK "), number);
953 } else
954 printf(FMT("%5llu ","%llu "), number);
955 } else
956 printf(FMT("%8llu ","%llu "), number);
957}
958
959static int
960print_match(const struct ip6t_entry_match *m,
961 const struct ip6t_ip6 *ip,
962 int numeric)
963{
964 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
965
966 if (match) {
967 if (match->print)
968 match->print(ip, m, numeric);
969 } else {
970 if (m->u.user.name[0])
971 printf("UNKNOWN match `%s' ", m->u.user.name);
972 }
973 /* Don't stop iterating. */
974 return 0;
975}
976
977/* e is called `fw' here for hysterical raisins */
978static void
979print_firewall(const struct ip6t_entry *fw,
980 const char *targname,
981 unsigned int num,
982 unsigned int format,
983 const ip6tc_handle_t handle)
984{
985 struct ip6tables_target *target = NULL;
986 const struct ip6t_entry_target *t;
987 u_int8_t flags;
988 char buf[BUFSIZ];
989
990 /* User creates a chain called "REJECT": this overrides the
991 `REJECT' target module. Keep feeding them rope until the
992 revolution... Bwahahahahah */
993 if (!ip6tc_is_chain(targname, handle))
994 target = find_target(targname, TRY_LOAD);
995 else
996 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
997
998 t = ip6t_get_target((struct ip6t_entry *)fw);
999 flags = fw->ipv6.flags;
1000
1001 if (format & FMT_LINENUMBERS)
1002 printf(FMT("%-4u ", "%u "), num+1);
1003
1004 if (!(format & FMT_NOCOUNTS)) {
1005 print_num(fw->counters.pcnt, format);
1006 print_num(fw->counters.bcnt, format);
1007 }
1008
1009 if (!(format & FMT_NOTARGET))
1010 printf(FMT("%-9s ", "%s "), targname);
1011
1012 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1013 {
1014 char *pname = proto_to_name(fw->ipv6.proto,
1015 format&FMT_NUMERIC);
1016 if (pname)
1017 printf(FMT("%-5s", "%s "), pname);
1018 else
1019 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1020 }
1021
1022 if (format & FMT_OPTIONS) {
1023 if (format & FMT_NOTABLE)
1024 fputs("opt ", stdout);
1025 fputc(' ', stdout);
1026 fputc(' ', stdout);
1027 fputc(' ', stdout);
1028 }
1029
1030 if (format & FMT_VIA) {
1031 char iface[IFNAMSIZ+2];
1032
1033 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1034 iface[0] = '!';
1035 iface[1] = '\0';
1036 }
1037 else iface[0] = '\0';
1038
1039 if (fw->ipv6.iniface[0] != '\0') {
1040 strcat(iface, fw->ipv6.iniface);
1041 /* If it doesn't compare the nul-term, it's a
1042 wildcard. */
1043 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1044 strcat(iface, "+");
1045 }
1046 else if (format & FMT_NUMERIC) strcat(iface, "*");
1047 else strcat(iface, "any");
1048 printf(FMT(" %-6s ","in %s "), iface);
1049
1050 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1051 iface[0] = '!';
1052 iface[1] = '\0';
1053 }
1054 else iface[0] = '\0';
1055
1056 if (fw->ipv6.outiface[0] != '\0') {
1057 strcat(iface, fw->ipv6.outiface);
1058 /* If it doesn't compare the nul-term, it's a
1059 wildcard. */
1060 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1061 strcat(iface, "+");
1062 }
1063 else if (format & FMT_NUMERIC) strcat(iface, "*");
1064 else strcat(iface, "any");
1065 printf(FMT("%-6s ","out %s "), iface);
1066 }
1067
1068 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1069 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1070 && !(format & FMT_NUMERIC))
1071 printf(FMT("%-19s ","%s "), "anywhere");
1072 else {
1073 if (format & FMT_NUMERIC)
1074 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1075 else
1076 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1077 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1078 printf(FMT("%-19s ","%s "), buf);
1079 }
1080
1081 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1082 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1083 && !(format & FMT_NUMERIC))
1084 printf(FMT("%-19s","-> %s"), "anywhere");
1085 else {
1086 if (format & FMT_NUMERIC)
1087 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1088 else
1089 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1090 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1091 printf(FMT("%-19s","-> %s"), buf);
1092 }
1093
1094 if (format & FMT_NOTABLE)
1095 fputs(" ", stdout);
1096
1097 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1098
1099 if (target) {
1100 if (target->print)
1101 /* Print the target information. */
1102 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1103 } else if (t->u.target_size != sizeof(*t))
1104 printf("[%u bytes of unknown target data] ",
1105 t->u.target_size - sizeof(*t));
1106
1107 if (!(format & FMT_NONEWLINE))
1108 fputc('\n', stdout);
1109}
1110
1111static void
1112print_firewall_line(const struct ip6t_entry *fw,
1113 const ip6tc_handle_t h)
1114{
1115 struct ip6t_entry_target *t;
1116
1117 t = ip6t_get_target((struct ip6t_entry *)fw);
1118 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1119}
1120
1121static int
1122append_entry(const ip6t_chainlabel chain,
1123 struct ip6t_entry *fw,
1124 unsigned int nsaddrs,
1125 const struct in6_addr saddrs[],
1126 unsigned int ndaddrs,
1127 const struct in6_addr daddrs[],
1128 int verbose,
1129 ip6tc_handle_t *handle)
1130{
1131 unsigned int i, j;
1132 int ret = 1;
1133
1134 for (i = 0; i < nsaddrs; i++) {
1135 fw->ipv6.src = saddrs[i];
1136 for (j = 0; j < ndaddrs; j++) {
1137 fw->ipv6.dst = daddrs[j];
1138 if (verbose)
1139 print_firewall_line(fw, *handle);
1140 ret &= ip6tc_append_entry(chain, fw, handle);
1141 }
1142 }
1143
1144 return ret;
1145}
1146
1147static int
1148replace_entry(const ip6t_chainlabel chain,
1149 struct ip6t_entry *fw,
1150 unsigned int rulenum,
1151 const struct in6_addr *saddr,
1152 const struct in6_addr *daddr,
1153 int verbose,
1154 ip6tc_handle_t *handle)
1155{
1156 fw->ipv6.src = *saddr;
1157 fw->ipv6.dst = *daddr;
1158
1159 if (verbose)
1160 print_firewall_line(fw, *handle);
1161 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1162}
1163
1164static int
1165insert_entry(const ip6t_chainlabel chain,
1166 struct ip6t_entry *fw,
1167 unsigned int rulenum,
1168 unsigned int nsaddrs,
1169 const struct in6_addr saddrs[],
1170 unsigned int ndaddrs,
1171 const struct in6_addr daddrs[],
1172 int verbose,
1173 ip6tc_handle_t *handle)
1174{
1175 unsigned int i, j;
1176 int ret = 1;
1177
1178 for (i = 0; i < nsaddrs; i++) {
1179 fw->ipv6.src = saddrs[i];
1180 for (j = 0; j < ndaddrs; j++) {
1181 fw->ipv6.dst = daddrs[j];
1182 if (verbose)
1183 print_firewall_line(fw, *handle);
1184 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1185 }
1186 }
1187
1188 return ret;
1189}
1190
1191static unsigned char *
1192make_delete_mask(struct ip6t_entry *fw)
1193{
1194 /* Establish mask for comparison */
1195 unsigned int size;
1196 struct ip6tables_match *m;
1197 unsigned char *mask, *mptr;
1198
1199 size = sizeof(struct ip6t_entry);
1200 for (m = ip6tables_matches; m; m = m->next)
1201 size += sizeof(struct ip6t_entry_match) + m->size;
1202
1203 mask = fw_calloc(1, size
1204 + sizeof(struct ip6t_entry_target)
1205 + ip6tables_targets->size);
1206
1207 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1208 mptr = mask + sizeof(struct ip6t_entry);
1209
1210 for (m = ip6tables_matches; m; m = m->next) {
1211 memset(mptr, 0xFF,
1212 sizeof(struct ip6t_entry_match) + m->userspacesize);
1213 mptr += sizeof(struct ip6t_entry_match) + m->size;
1214 }
1215
1216 memset(mptr, 0xFF, sizeof(struct ip6t_entry_target));
1217 mptr += sizeof(struct ip6t_entry_target);
1218 memset(mptr, 0xFF, ip6tables_targets->userspacesize);
1219
1220 return mask;
1221}
1222
1223static int
1224delete_entry(const ip6t_chainlabel chain,
1225 struct ip6t_entry *fw,
1226 unsigned int nsaddrs,
1227 const struct in6_addr saddrs[],
1228 unsigned int ndaddrs,
1229 const struct in6_addr daddrs[],
1230 int verbose,
1231 ip6tc_handle_t *handle)
1232{
1233 unsigned int i, j;
1234 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001235 unsigned char *mask;
1236
1237 mask = make_delete_mask(fw);
1238 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001239 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001241 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242 if (verbose)
1243 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001244 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001245 }
1246 }
1247 return ret;
1248}
1249
1250static int
1251check_packet(const ip6t_chainlabel chain,
1252 struct ip6t_entry *fw,
1253 unsigned int nsaddrs,
1254 const struct in6_addr saddrs[],
1255 unsigned int ndaddrs,
1256 const struct in6_addr daddrs[],
1257 int verbose,
1258 ip6tc_handle_t *handle)
1259{
1260 int ret = 1;
1261 unsigned int i, j;
1262 struct ip6t_entry ipfw = *fw;
1263 const char *msg;
1264
1265 for (i = 0; i < nsaddrs; i++) {
1266 ipfw.ipv6.src = saddrs[i];
1267 for (j = 0; j < ndaddrs; j++) {
1268 ipfw.ipv6.dst = daddrs[j];
1269 if (verbose)
1270 print_firewall_line(fw, *handle);
1271 msg = ip6tc_check_packet(chain, &ipfw, handle);
1272 if (!msg) ret = 0;
1273 else printf("%s\n", msg);
1274 }
1275 }
1276
1277 return ret;
1278}
1279
1280static int
1281for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1282 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1283{
1284 int ret = 1;
1285 const char *chain;
1286 char *chains;
1287 unsigned int i, chaincount = 0;
1288
1289 chain = ip6tc_first_chain(handle);
1290 while (chain) {
1291 chaincount++;
1292 chain = ip6tc_next_chain(handle);
1293 }
1294
1295 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1296 i = 0;
1297 chain = ip6tc_first_chain(handle);
1298 while (chain) {
1299 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1300 i++;
1301 chain = ip6tc_next_chain(handle);
1302 }
1303
1304 for (i = 0; i < chaincount; i++) {
1305 if (!builtinstoo
1306 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1307 *handle))
1308 continue;
1309 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1310 }
1311
1312 free(chains);
1313 return ret;
1314}
1315
1316static int
1317flush_entries(const ip6t_chainlabel chain, int verbose,
1318 ip6tc_handle_t *handle)
1319{
1320 if (!chain)
1321 return for_each_chain(flush_entries, verbose, 1, handle);
1322
1323 if (verbose)
1324 fprintf(stdout, "Flushing chain `%s'\n", chain);
1325 return ip6tc_flush_entries(chain, handle);
1326}
1327
1328static int
1329zero_entries(const ip6t_chainlabel chain, int verbose,
1330 ip6tc_handle_t *handle)
1331{
1332 if (!chain)
1333 return for_each_chain(zero_entries, verbose, 1, handle);
1334
1335 if (verbose)
1336 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1337 return ip6tc_zero_entries(chain, handle);
1338}
1339
1340static int
1341delete_chain(const ip6t_chainlabel chain, int verbose,
1342 ip6tc_handle_t *handle)
1343{
1344 if (!chain)
1345 return for_each_chain(delete_chain, verbose, 0, handle);
1346
1347 if (verbose)
1348 fprintf(stdout, "Deleting chain `%s'\n", chain);
1349 return ip6tc_delete_chain(chain, handle);
1350}
1351
1352static int
1353list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1354 int expanded, int linenumbers, ip6tc_handle_t *handle)
1355{
1356 int found = 0;
1357 unsigned int format;
1358 const char *this;
1359
1360 format = FMT_OPTIONS;
1361 if (!verbose)
1362 format |= FMT_NOCOUNTS;
1363 else
1364 format |= FMT_VIA;
1365
1366 if (numeric)
1367 format |= FMT_NUMERIC;
1368
1369 if (!expanded)
1370 format |= FMT_KILOMEGAGIGA;
1371
1372 if (linenumbers)
1373 format |= FMT_LINENUMBERS;
1374
1375 for (this = ip6tc_first_chain(handle);
1376 this;
1377 this = ip6tc_next_chain(handle)) {
1378 const struct ip6t_entry *i;
1379 unsigned int num;
1380
1381 if (chain && strcmp(chain, this) != 0)
1382 continue;
1383
1384 if (found) printf("\n");
1385
1386 print_header(format, this, handle);
1387 i = ip6tc_first_rule(this, handle);
1388
1389 num = 0;
1390 while (i) {
1391 print_firewall(i,
1392 ip6tc_get_target(i, handle),
1393 num++,
1394 format,
1395 *handle);
1396 i = ip6tc_next_rule(i, handle);
1397 }
1398 found = 1;
1399 }
1400
1401 errno = ENOENT;
1402 return found;
1403}
1404
1405static struct ip6t_entry *
1406generate_entry(const struct ip6t_entry *fw,
1407 struct ip6tables_match *matches,
1408 struct ip6t_entry_target *target)
1409{
1410 unsigned int size;
1411 struct ip6tables_match *m;
1412 struct ip6t_entry *e;
1413
1414 size = sizeof(struct ip6t_entry);
1415 for (m = matches; m; m = m->next)
1416 size += m->m->u.match_size;
1417
1418 e = fw_malloc(size + target->u.target_size);
1419 *e = *fw;
1420 e->target_offset = size;
1421 e->next_offset = size + target->u.target_size;
1422
1423 size = 0;
1424 for (m = matches; m; m = m->next) {
1425 memcpy(e->elems + size, m->m, m->m->u.match_size);
1426 size += m->m->u.match_size;
1427 }
1428 memcpy(e->elems + size, target, target->u.target_size);
1429
1430 return e;
1431}
1432
1433int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1434{
1435 struct ip6t_entry fw, *e = NULL;
1436 int invert = 0;
1437 unsigned int nsaddrs = 0, ndaddrs = 0;
1438 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1439
1440 int c, verbose = 0;
1441 const char *chain = NULL;
1442 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1443 const char *policy = NULL, *newname = NULL;
1444 unsigned int rulenum = 0, options = 0, command = 0;
1445 int ret = 1;
1446 struct ip6tables_match *m;
1447 struct ip6tables_target *target = NULL;
1448 const char *jumpto = "";
1449 char *protocol = NULL;
1450
1451 memset(&fw, 0, sizeof(fw));
1452
1453 /* Suppress error messages: we may add new options if we
1454 demand-load a protocol. */
1455 opterr = 0;
1456
1457 while ((c = getopt_long(argc, argv,
1458 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1459 opts, NULL)) != -1) {
1460 switch (c) {
1461 /*
1462 * Command selection
1463 */
1464 case 'A':
1465 add_command(&command, CMD_APPEND, CMD_NONE,
1466 invert);
1467 chain = optarg;
1468 break;
1469
1470 case 'D':
1471 add_command(&command, CMD_DELETE, CMD_NONE,
1472 invert);
1473 chain = optarg;
1474 if (optind < argc && argv[optind][0] != '-'
1475 && argv[optind][0] != '!') {
1476 rulenum = parse_rulenumber(argv[optind++]);
1477 command = CMD_DELETE_NUM;
1478 }
1479 break;
1480
1481 case 'C':
1482 add_command(&command, CMD_CHECK, CMD_NONE,
1483 invert);
1484 chain = optarg;
1485 break;
1486
1487 case 'R':
1488 add_command(&command, CMD_REPLACE, CMD_NONE,
1489 invert);
1490 chain = optarg;
1491 if (optind < argc && argv[optind][0] != '-'
1492 && argv[optind][0] != '!')
1493 rulenum = parse_rulenumber(argv[optind++]);
1494 else
1495 exit_error(PARAMETER_PROBLEM,
1496 "-%c requires a rule number",
1497 cmd2char(CMD_REPLACE));
1498 break;
1499
1500 case 'I':
1501 add_command(&command, CMD_INSERT, CMD_NONE,
1502 invert);
1503 chain = optarg;
1504 if (optind < argc && argv[optind][0] != '-'
1505 && argv[optind][0] != '!')
1506 rulenum = parse_rulenumber(argv[optind++]);
1507 else rulenum = 1;
1508 break;
1509
1510 case 'L':
1511 add_command(&command, CMD_LIST, CMD_ZERO,
1512 invert);
1513 if (optarg) chain = optarg;
1514 else if (optind < argc && argv[optind][0] != '-'
1515 && argv[optind][0] != '!')
1516 chain = argv[optind++];
1517 break;
1518
1519 case 'F':
1520 add_command(&command, CMD_FLUSH, CMD_NONE,
1521 invert);
1522 if (optarg) chain = optarg;
1523 else if (optind < argc && argv[optind][0] != '-'
1524 && argv[optind][0] != '!')
1525 chain = argv[optind++];
1526 break;
1527
1528 case 'Z':
1529 add_command(&command, CMD_ZERO, CMD_LIST,
1530 invert);
1531 if (optarg) chain = optarg;
1532 else if (optind < argc && argv[optind][0] != '-'
1533 && argv[optind][0] != '!')
1534 chain = argv[optind++];
1535 break;
1536
1537 case 'N':
1538 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1539 invert);
1540 chain = optarg;
1541 break;
1542
1543 case 'X':
1544 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1545 invert);
1546 if (optarg) chain = optarg;
1547 else if (optind < argc && argv[optind][0] != '-'
1548 && argv[optind][0] != '!')
1549 chain = argv[optind++];
1550 break;
1551
1552 case 'E':
1553 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1554 invert);
1555 chain = optarg;
1556 if (optind < argc && argv[optind][0] != '-'
1557 && argv[optind][0] != '!')
1558 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001559 else
1560 exit_error(PARAMETER_PROBLEM,
1561 "-%c requires old-chain-name and "
1562 "new-chain-name",
1563 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001564 break;
1565
1566 case 'P':
1567 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1568 invert);
1569 chain = optarg;
1570 if (optind < argc && argv[optind][0] != '-'
1571 && argv[optind][0] != '!')
1572 policy = argv[optind++];
1573 else
1574 exit_error(PARAMETER_PROBLEM,
1575 "-%c requires a chain and a policy",
1576 cmd2char(CMD_SET_POLICY));
1577 break;
1578
1579 case 'h':
1580 if (!optarg)
1581 optarg = argv[optind];
1582
1583 /* iptables -p icmp -h */
1584 if (!ip6tables_matches && protocol)
1585 find_match(protocol, TRY_LOAD);
1586
1587 exit_printhelp();
1588
1589 /*
1590 * Option selection
1591 */
1592 case 'p':
1593 if (check_inverse(optarg, &invert))
1594 optind++;
1595 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1596 invert);
1597
1598 /* Canonicalize into lower case */
1599 for (protocol = argv[optind-1]; *protocol; protocol++)
1600 *protocol = tolower(*protocol);
1601
1602 protocol = argv[optind-1];
1603 fw.ipv6.proto = parse_protocol(protocol);
1604 fw.ipv6.flags |= IP6T_F_PROTO;
1605
1606 if (fw.ipv6.proto == 0
1607 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1608 exit_error(PARAMETER_PROBLEM,
1609 "rule would never match protocol");
1610 fw.nfcache |= NFC_IP6_PROTO;
1611 break;
1612
1613 case 's':
1614 if (check_inverse(optarg, &invert))
1615 optind++;
1616 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1617 invert);
1618 shostnetworkmask = argv[optind-1];
1619 fw.nfcache |= NFC_IP6_SRC;
1620 break;
1621
1622 case 'd':
1623 if (check_inverse(optarg, &invert))
1624 optind++;
1625 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1626 invert);
1627 dhostnetworkmask = argv[optind-1];
1628 fw.nfcache |= NFC_IP6_DST;
1629 break;
1630
1631 case 'j':
1632 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1633 invert);
1634 jumpto = parse_target(optarg);
1635 target = find_target(jumpto, TRY_LOAD);
1636
1637 if (target) {
1638 size_t size;
1639
1640 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1641 + target->size);
1642
1643 target->t = fw_calloc(1, size);
1644 target->t->u.target_size = size;
1645 strcpy(target->t->u.user.name, jumpto);
1646 target->init(target->t, &fw.nfcache);
1647 }
1648 break;
1649
1650
1651 case 'i':
1652 if (check_inverse(optarg, &invert))
1653 optind++;
1654 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1655 invert);
1656 parse_interface(argv[optind-1],
1657 fw.ipv6.iniface,
1658 fw.ipv6.iniface_mask);
1659 fw.nfcache |= NFC_IP6_IF_IN;
1660 break;
1661
1662 case 'o':
1663 if (check_inverse(optarg, &invert))
1664 optind++;
1665 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1666 invert);
1667 parse_interface(argv[optind-1],
1668 fw.ipv6.outiface,
1669 fw.ipv6.outiface_mask);
1670 fw.nfcache |= NFC_IP6_IF_OUT;
1671 break;
1672
1673 case 'v':
1674 if (!verbose)
1675 set_option(&options, OPT_VERBOSE,
1676 &fw.ipv6.invflags, invert);
1677 verbose++;
1678 break;
1679
1680 case 'm': {
1681 size_t size;
1682
1683 if (invert)
1684 exit_error(PARAMETER_PROBLEM,
1685 "unexpected ! flag before --match");
1686
1687 m = find_match(optarg, LOAD_MUST_SUCCEED);
1688 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1689 + m->size);
1690 m->m = fw_calloc(1, size);
1691 m->m->u.match_size = size;
1692 strcpy(m->m->u.user.name, m->name);
1693 m->init(m->m, &fw.nfcache);
1694 }
1695 break;
1696
1697 case 'n':
1698 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1699 invert);
1700 break;
1701
1702 case 't':
1703 if (invert)
1704 exit_error(PARAMETER_PROBLEM,
1705 "unexpected ! flag before --table");
1706 *table = argv[optind-1];
1707 break;
1708
1709 case 'x':
1710 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1711 invert);
1712 break;
1713
1714 case 'V':
1715 if (invert)
1716 printf("Not %s ;-)\n", program_version);
1717 else
1718 printf("%s v%s\n",
1719 program_name, program_version);
1720 exit(0);
1721
1722 case '0':
1723 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1724 invert);
1725 break;
1726
1727 case 1: /* non option */
1728 if (optarg[0] == '!' && optarg[1] == '\0') {
1729 if (invert)
1730 exit_error(PARAMETER_PROBLEM,
1731 "multiple consecutive ! not"
1732 " allowed");
1733 invert = TRUE;
1734 optarg[0] = '\0';
1735 continue;
1736 }
1737 printf("Bad argument `%s'\n", optarg);
1738 exit_tryhelp(2);
1739
1740 default:
1741 /* FIXME: This scheme doesn't allow two of the same
1742 matches --RR */
1743 if (!target
1744 || !(target->parse(c - target->option_offset,
1745 argv, invert,
1746 &target->tflags,
1747 &fw, &target->t))) {
1748 for (m = ip6tables_matches; m; m = m->next) {
1749 if (m->parse(c - m->option_offset,
1750 argv, invert,
1751 &m->mflags,
1752 &fw,
1753 &fw.nfcache,
1754 &m->m))
1755 break;
1756 }
1757
1758 /* If you listen carefully, you can
1759 actually hear this code suck. */
1760 if (m == NULL
1761 && protocol
1762 && !find_proto(protocol, DONT_LOAD,
1763 options&OPT_NUMERIC)
1764 && (m = find_proto(protocol, TRY_LOAD,
1765 options&OPT_NUMERIC))) {
1766 /* Try loading protocol */
1767 size_t size;
1768
1769 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1770 + m->size);
1771
1772 m->m = fw_calloc(1, size);
1773 m->m->u.match_size = size;
1774 strcpy(m->m->u.user.name, m->name);
1775 m->init(m->m, &fw.nfcache);
1776
1777 optind--;
1778 continue;
1779 }
1780 if (!m)
1781 exit_error(PARAMETER_PROBLEM,
1782 "Unknown arg `%s'",
1783 argv[optind-1]);
1784 }
1785 }
1786 invert = FALSE;
1787 }
1788
1789 for (m = ip6tables_matches; m; m = m->next)
1790 m->final_check(m->mflags);
1791 if (target)
1792 target->final_check(target->tflags);
1793
1794 /* Fix me: must put inverse options checking here --MN */
1795
1796 if (optind < argc)
1797 exit_error(PARAMETER_PROBLEM,
1798 "unknown arguments found on commandline");
1799 if (!command)
1800 exit_error(PARAMETER_PROBLEM, "no command specified");
1801 if (invert)
1802 exit_error(PARAMETER_PROBLEM,
1803 "nothing appropriate following !");
1804
1805 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1806 CMD_CHECK)) {
1807 if (!(options & OPT_DESTINATION))
1808 dhostnetworkmask = "0.0.0.0/0";
1809 if (!(options & OPT_SOURCE))
1810 shostnetworkmask = "0.0.0.0/0";
1811 }
1812
1813 if (shostnetworkmask)
1814 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1815 &(fw.ipv6.smsk), &nsaddrs);
1816
1817 if (dhostnetworkmask)
1818 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1819 &(fw.ipv6.dmsk), &ndaddrs);
1820
1821 if ((nsaddrs > 1 || ndaddrs > 1) &&
1822 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1823 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1824 " source or destination IP addresses");
1825
1826 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
1827 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1828 cmd2char(CMD_CHECK));
1829
1830 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1831 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1832 "specify a unique address");
1833
1834 generic_opt_check(command, options);
1835
1836 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1837 exit_error(PARAMETER_PROBLEM,
1838 "chain name `%s' too long (must be under %i chars)",
1839 chain, IP6T_FUNCTION_MAXNAMELEN);
1840
1841 *handle = ip6tc_init(*table);
1842 if (!*handle)
1843 exit_error(VERSION_PROBLEM,
1844 "can't initialize iptables table `%s': %s",
1845 *table, ip6tc_strerror(errno));
1846
1847 if (command == CMD_CHECK
1848 || command == CMD_APPEND
1849 || command == CMD_DELETE
1850 || command == CMD_INSERT
1851 || command == CMD_REPLACE) {
1852 /* -o not valid with incoming packets. */
1853 if (options & OPT_VIANAMEOUT)
1854 if (strcmp(chain, "PREROUTING") == 0
1855 || strcmp(chain, "INPUT") == 0) {
1856 exit_error(PARAMETER_PROBLEM,
1857 "Can't use -%c with %s\n",
1858 opt2char(OPT_VIANAMEOUT),
1859 chain);
1860 }
1861
1862 /* -i not valid with outgoing packets */
1863 if (options & OPT_VIANAMEIN)
1864 if (strcmp(chain, "POSTROUTING") == 0
1865 || strcmp(chain, "OUTPUT") == 0) {
1866 exit_error(PARAMETER_PROBLEM,
1867 "Can't use -%c with %s\n",
1868 opt2char(OPT_VIANAMEIN),
1869 chain);
1870 }
1871
1872 if (target && ip6tc_is_chain(jumpto, *handle)) {
1873 printf("Warning: using chain %s, not extension\n",
1874 jumpto);
1875
1876 target = NULL;
1877 }
1878
1879 /* If they didn't specify a target, or it's a chain
1880 name, use standard. */
1881 if (!target
1882 && (strlen(jumpto) == 0
1883 || ip6tc_is_chain(jumpto, *handle))) {
1884 size_t size;
1885
1886 target = find_target(IP6T_STANDARD_TARGET,
1887 LOAD_MUST_SUCCEED);
1888
1889 size = sizeof(struct ip6t_entry_target)
1890 + target->size;
1891 target->t = fw_calloc(1, size);
1892 target->t->u.target_size = size;
1893 strcpy(target->t->u.user.name, jumpto);
1894 target->init(target->t, &fw.nfcache);
1895 }
1896
1897 if (!target) {
1898 struct ip6t_entry_target unknown_target;
1899
1900 /* Don't know it. Must be extension with no
1901 options? */
1902 unknown_target.u.target_size = sizeof(unknown_target);
1903 strcpy(unknown_target.u.user.name, jumpto);
1904
1905 e = generate_entry(&fw, ip6tables_matches,
1906 &unknown_target);
1907 } else {
1908 e = generate_entry(&fw, ip6tables_matches, target->t);
1909 }
1910 }
1911
1912 switch (command) {
1913 case CMD_APPEND:
1914 ret = append_entry(chain, e,
1915 nsaddrs, saddrs, ndaddrs, daddrs,
1916 options&OPT_VERBOSE,
1917 handle);
1918 break;
1919 case CMD_CHECK:
1920 ret = check_packet(chain, e,
1921 nsaddrs, saddrs, ndaddrs, daddrs,
1922 options&OPT_VERBOSE, handle);
1923 break;
1924 case CMD_DELETE:
1925 ret = delete_entry(chain, e,
1926 nsaddrs, saddrs, ndaddrs, daddrs,
1927 options&OPT_VERBOSE,
1928 handle);
1929 break;
1930 case CMD_DELETE_NUM:
1931 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1932 break;
1933 case CMD_REPLACE:
1934 ret = replace_entry(chain, e, rulenum - 1,
1935 saddrs, daddrs, options&OPT_VERBOSE,
1936 handle);
1937 break;
1938 case CMD_INSERT:
1939 ret = insert_entry(chain, e, rulenum - 1,
1940 nsaddrs, saddrs, ndaddrs, daddrs,
1941 options&OPT_VERBOSE,
1942 handle);
1943 break;
1944 case CMD_LIST:
1945 ret = list_entries(chain,
1946 options&OPT_VERBOSE,
1947 options&OPT_NUMERIC,
1948 options&OPT_EXPANDED,
1949 options&OPT_LINENUMBERS,
1950 handle);
1951 break;
1952 case CMD_FLUSH:
1953 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1954 break;
1955 case CMD_ZERO:
1956 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1957 break;
1958 case CMD_LIST|CMD_ZERO:
1959 ret = list_entries(chain,
1960 options&OPT_VERBOSE,
1961 options&OPT_NUMERIC,
1962 options&OPT_EXPANDED,
1963 options&OPT_LINENUMBERS,
1964 handle);
1965 if (ret)
1966 ret = zero_entries(chain,
1967 options&OPT_VERBOSE, handle);
1968 break;
1969 case CMD_NEW_CHAIN:
1970 ret = ip6tc_create_chain(chain, handle);
1971 break;
1972 case CMD_DELETE_CHAIN:
1973 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1974 break;
1975 case CMD_RENAME_CHAIN:
1976 ret = ip6tc_rename_chain(chain, newname, handle);
1977 break;
1978 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00001979 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001980 break;
1981 default:
1982 /* We should never reach this... */
1983 exit_tryhelp(2);
1984 }
1985
1986 if (verbose > 1)
1987 dump_entries6(*handle);
1988
1989 return ret;
1990}