blob: 84215b9d8e9518514b534134969a5ce6049ff3a8 [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 },
192 { "icmp", IPPROTO_ICMP },
193 { "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{
748 int number;
749 char *end;
750
751 /* Handle hex, octal, etc. */
752 number = (int)strtol(s, &end, 0);
753 if (*end == '\0' && end != s) {
754 /* we parsed a number, let's see if we want this */
755 if (min <= number && number <= max)
756 return number;
757 }
758 return -1;
759}
760
761static void
762set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
763 int invert)
764{
765 if (*options & option)
766 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
767 opt2char(option));
768 *options |= option;
769
770 if (invert) {
771 unsigned int i;
772 for (i = 0; 1 << i != option; i++);
773
774 if (!inverse_for_options[i])
775 exit_error(PARAMETER_PROBLEM,
776 "cannot have ! before -%c",
777 opt2char(option));
778 *invflg |= inverse_for_options[i];
779 }
780}
781
782struct ip6tables_target *
783find_target(const char *name, enum ip6t_tryload tryload)
784{
785 struct ip6tables_target *ptr;
786
787 /* Standard target? */
788 if (strcmp(name, "") == 0
789 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
790 || strcmp(name, IP6TC_LABEL_DROP) == 0
791 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
792 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
793 name = "standard";
794
795 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
796 if (strcmp(name, ptr->name) == 0)
797 break;
798 }
799
800 if (!ptr && tryload != DONT_LOAD) {
801 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
802 + strlen(name)];
803 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
804 if (dlopen(path, RTLD_NOW)) {
805 /* Found library. If it didn't register itself,
806 maybe they specified match as a target. */
807 ptr = find_target(name, DONT_LOAD);
808 if (!ptr)
809 exit_error(PARAMETER_PROBLEM,
810 "Couldn't load target `%s'\n",
811 name);
812 } else if (tryload == LOAD_MUST_SUCCEED)
813 exit_error(PARAMETER_PROBLEM,
814 "Couldn't load target `%s'\n", name);
815 }
816
817 return ptr;
818}
819
820static struct option *
821merge_options(struct option *oldopts, struct option *newopts,
822 unsigned int *option_offset)
823{
824 unsigned int num_old, num_new, i;
825 struct option *merge;
826
827 for (num_old = 0; oldopts[num_old].name; num_old++);
828 for (num_new = 0; newopts[num_new].name; num_new++);
829
830 global_option_offset += OPTION_OFFSET;
831 *option_offset = global_option_offset;
832
833 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
834 memcpy(merge, oldopts, num_old * sizeof(struct option));
835 for (i = 0; i < num_new; i++) {
836 merge[num_old + i] = newopts[i];
837 merge[num_old + i].val += *option_offset;
838 }
839 memset(merge + num_old + num_new, 0, sizeof(struct option));
840
841 return merge;
842}
843
844void
845register_match6(struct ip6tables_match *me)
846{
847 if (strcmp(me->version, program_version) != 0) {
848 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
849 program_name, me->name, me->version, program_version);
850 exit(1);
851 }
852
853 if (find_match(me->name, DONT_LOAD)) {
854 fprintf(stderr, "%s: match `%s' already registered.\n",
855 program_name, me->name);
856 exit(1);
857 }
858
859 /* Prepend to list. */
860 me->next = ip6tables_matches;
861 ip6tables_matches = me;
862 me->m = NULL;
863 me->mflags = 0;
864
865 opts = merge_options(opts, me->extra_opts, &me->option_offset);
866}
867
868void
869register_target6(struct ip6tables_target *me)
870{
871 if (strcmp(me->version, program_version) != 0) {
872 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
873 program_name, me->name, me->version, program_version);
874 exit(1);
875 }
876
877 if (find_target(me->name, DONT_LOAD)) {
878 fprintf(stderr, "%s: target `%s' already registered.\n",
879 program_name, me->name);
880 exit(1);
881 }
882
883 /* Prepend to list. */
884 me->next = ip6tables_targets;
885 ip6tables_targets = me;
886 me->t = NULL;
887 me->tflags = 0;
888
889 opts = merge_options(opts, me->extra_opts, &me->option_offset);
890}
891
892static void
893print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
894{
895 struct ip6t_counters counters;
896 const char *pol = ip6tc_get_policy(chain, &counters, handle);
897 printf("Chain %s", chain);
898 if (pol) {
899 printf(" (policy %s", pol);
900 if (!(format & FMT_NOCOUNTS))
901 printf(" %llu packets, %llu bytes",
902 counters.pcnt, counters.bcnt);
903 printf(")\n");
904 } else {
905 unsigned int refs;
906 if (!ip6tc_get_references(&refs, chain, handle))
907 printf(" (ERROR obtaining refs)\n");
908 else
909 printf(" (%u references)\n", refs);
910 }
911
912 if (format & FMT_LINENUMBERS)
913 printf(FMT("%-4s ", "%s "), "num");
914 if (!(format & FMT_NOCOUNTS)) {
915 if (format & FMT_KILOMEGAGIGA) {
916 printf(FMT("%5s ","%s "), "pkts");
917 printf(FMT("%5s ","%s "), "bytes");
918 } else {
919 printf(FMT("%8s ","%s "), "pkts");
920 printf(FMT("%10s ","%s "), "bytes");
921 }
922 }
923 if (!(format & FMT_NOTARGET))
924 printf(FMT("%-9s ","%s "), "target");
925 fputs(" prot ", stdout);
926 if (format & FMT_OPTIONS)
927 fputs("opt", stdout);
928 if (format & FMT_VIA) {
929 printf(FMT(" %-6s ","%s "), "in");
930 printf(FMT("%-6s ","%s "), "out");
931 }
932 printf(FMT(" %-19s ","%s "), "source");
933 printf(FMT(" %-19s "," %s "), "destination");
934 printf("\n");
935}
936
937static void
938print_num(u_int64_t number, unsigned int format)
939{
940 if (format & FMT_KILOMEGAGIGA) {
941 if (number > 99999) {
942 number = (number + 500) / 1000;
943 if (number > 9999) {
944 number = (number + 500) / 1000;
945 if (number > 9999) {
946 number = (number + 500) / 1000;
947 printf(FMT("%4lluG ","%lluG "),number);
948 }
949 else printf(FMT("%4lluM ","%lluM "), number);
950 } else
951 printf(FMT("%4lluK ","%lluK "), number);
952 } else
953 printf(FMT("%5llu ","%llu "), number);
954 } else
955 printf(FMT("%8llu ","%llu "), number);
956}
957
958static int
959print_match(const struct ip6t_entry_match *m,
960 const struct ip6t_ip6 *ip,
961 int numeric)
962{
963 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
964
965 if (match) {
966 if (match->print)
967 match->print(ip, m, numeric);
968 } else {
969 if (m->u.user.name[0])
970 printf("UNKNOWN match `%s' ", m->u.user.name);
971 }
972 /* Don't stop iterating. */
973 return 0;
974}
975
976/* e is called `fw' here for hysterical raisins */
977static void
978print_firewall(const struct ip6t_entry *fw,
979 const char *targname,
980 unsigned int num,
981 unsigned int format,
982 const ip6tc_handle_t handle)
983{
984 struct ip6tables_target *target = NULL;
985 const struct ip6t_entry_target *t;
986 u_int8_t flags;
987 char buf[BUFSIZ];
988
989 /* User creates a chain called "REJECT": this overrides the
990 `REJECT' target module. Keep feeding them rope until the
991 revolution... Bwahahahahah */
992 if (!ip6tc_is_chain(targname, handle))
993 target = find_target(targname, TRY_LOAD);
994 else
995 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
996
997 t = ip6t_get_target((struct ip6t_entry *)fw);
998 flags = fw->ipv6.flags;
999
1000 if (format & FMT_LINENUMBERS)
1001 printf(FMT("%-4u ", "%u "), num+1);
1002
1003 if (!(format & FMT_NOCOUNTS)) {
1004 print_num(fw->counters.pcnt, format);
1005 print_num(fw->counters.bcnt, format);
1006 }
1007
1008 if (!(format & FMT_NOTARGET))
1009 printf(FMT("%-9s ", "%s "), targname);
1010
1011 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1012 {
1013 char *pname = proto_to_name(fw->ipv6.proto,
1014 format&FMT_NUMERIC);
1015 if (pname)
1016 printf(FMT("%-5s", "%s "), pname);
1017 else
1018 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1019 }
1020
1021 if (format & FMT_OPTIONS) {
1022 if (format & FMT_NOTABLE)
1023 fputs("opt ", stdout);
1024 fputc(' ', stdout);
1025 fputc(' ', stdout);
1026 fputc(' ', stdout);
1027 }
1028
1029 if (format & FMT_VIA) {
1030 char iface[IFNAMSIZ+2];
1031
1032 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1033 iface[0] = '!';
1034 iface[1] = '\0';
1035 }
1036 else iface[0] = '\0';
1037
1038 if (fw->ipv6.iniface[0] != '\0') {
1039 strcat(iface, fw->ipv6.iniface);
1040 /* If it doesn't compare the nul-term, it's a
1041 wildcard. */
1042 if (fw->ipv6.iniface_mask[strlen(fw->ipv6.iniface)] == 0)
1043 strcat(iface, "+");
1044 }
1045 else if (format & FMT_NUMERIC) strcat(iface, "*");
1046 else strcat(iface, "any");
1047 printf(FMT(" %-6s ","in %s "), iface);
1048
1049 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1050 iface[0] = '!';
1051 iface[1] = '\0';
1052 }
1053 else iface[0] = '\0';
1054
1055 if (fw->ipv6.outiface[0] != '\0') {
1056 strcat(iface, fw->ipv6.outiface);
1057 /* If it doesn't compare the nul-term, it's a
1058 wildcard. */
1059 if (fw->ipv6.outiface_mask[strlen(fw->ipv6.outiface)] == 0)
1060 strcat(iface, "+");
1061 }
1062 else if (format & FMT_NUMERIC) strcat(iface, "*");
1063 else strcat(iface, "any");
1064 printf(FMT("%-6s ","out %s "), iface);
1065 }
1066
1067 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1068 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1069 && !(format & FMT_NUMERIC))
1070 printf(FMT("%-19s ","%s "), "anywhere");
1071 else {
1072 if (format & FMT_NUMERIC)
1073 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.src)));
1074 else
1075 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.src)));
1076 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1077 printf(FMT("%-19s ","%s "), buf);
1078 }
1079
1080 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1081 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1082 && !(format & FMT_NUMERIC))
1083 printf(FMT("%-19s","-> %s"), "anywhere");
1084 else {
1085 if (format & FMT_NUMERIC)
1086 sprintf(buf, "%s/", addr_to_numeric(&(fw->ipv6.dst)));
1087 else
1088 sprintf(buf, "%s/", addr_to_anyname(&(fw->ipv6.dst)));
1089 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1090 printf(FMT("%-19s","-> %s"), buf);
1091 }
1092
1093 if (format & FMT_NOTABLE)
1094 fputs(" ", stdout);
1095
1096 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1097
1098 if (target) {
1099 if (target->print)
1100 /* Print the target information. */
1101 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1102 } else if (t->u.target_size != sizeof(*t))
1103 printf("[%u bytes of unknown target data] ",
1104 t->u.target_size - sizeof(*t));
1105
1106 if (!(format & FMT_NONEWLINE))
1107 fputc('\n', stdout);
1108}
1109
1110static void
1111print_firewall_line(const struct ip6t_entry *fw,
1112 const ip6tc_handle_t h)
1113{
1114 struct ip6t_entry_target *t;
1115
1116 t = ip6t_get_target((struct ip6t_entry *)fw);
1117 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1118}
1119
1120static int
1121append_entry(const ip6t_chainlabel chain,
1122 struct ip6t_entry *fw,
1123 unsigned int nsaddrs,
1124 const struct in6_addr saddrs[],
1125 unsigned int ndaddrs,
1126 const struct in6_addr daddrs[],
1127 int verbose,
1128 ip6tc_handle_t *handle)
1129{
1130 unsigned int i, j;
1131 int ret = 1;
1132
1133 for (i = 0; i < nsaddrs; i++) {
1134 fw->ipv6.src = saddrs[i];
1135 for (j = 0; j < ndaddrs; j++) {
1136 fw->ipv6.dst = daddrs[j];
1137 if (verbose)
1138 print_firewall_line(fw, *handle);
1139 ret &= ip6tc_append_entry(chain, fw, handle);
1140 }
1141 }
1142
1143 return ret;
1144}
1145
1146static int
1147replace_entry(const ip6t_chainlabel chain,
1148 struct ip6t_entry *fw,
1149 unsigned int rulenum,
1150 const struct in6_addr *saddr,
1151 const struct in6_addr *daddr,
1152 int verbose,
1153 ip6tc_handle_t *handle)
1154{
1155 fw->ipv6.src = *saddr;
1156 fw->ipv6.dst = *daddr;
1157
1158 if (verbose)
1159 print_firewall_line(fw, *handle);
1160 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1161}
1162
1163static int
1164insert_entry(const ip6t_chainlabel chain,
1165 struct ip6t_entry *fw,
1166 unsigned int rulenum,
1167 unsigned int nsaddrs,
1168 const struct in6_addr saddrs[],
1169 unsigned int ndaddrs,
1170 const struct in6_addr daddrs[],
1171 int verbose,
1172 ip6tc_handle_t *handle)
1173{
1174 unsigned int i, j;
1175 int ret = 1;
1176
1177 for (i = 0; i < nsaddrs; i++) {
1178 fw->ipv6.src = saddrs[i];
1179 for (j = 0; j < ndaddrs; j++) {
1180 fw->ipv6.dst = daddrs[j];
1181 if (verbose)
1182 print_firewall_line(fw, *handle);
1183 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1184 }
1185 }
1186
1187 return ret;
1188}
1189
1190static unsigned char *
1191make_delete_mask(struct ip6t_entry *fw)
1192{
1193 /* Establish mask for comparison */
1194 unsigned int size;
1195 struct ip6tables_match *m;
1196 unsigned char *mask, *mptr;
1197
1198 size = sizeof(struct ip6t_entry);
1199 for (m = ip6tables_matches; m; m = m->next)
1200 size += sizeof(struct ip6t_entry_match) + m->size;
1201
1202 mask = fw_calloc(1, size
1203 + sizeof(struct ip6t_entry_target)
1204 + ip6tables_targets->size);
1205
1206 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1207 mptr = mask + sizeof(struct ip6t_entry);
1208
1209 for (m = ip6tables_matches; m; m = m->next) {
1210 memset(mptr, 0xFF,
1211 sizeof(struct ip6t_entry_match) + m->userspacesize);
1212 mptr += sizeof(struct ip6t_entry_match) + m->size;
1213 }
1214
1215 memset(mptr, 0xFF, sizeof(struct ip6t_entry_target));
1216 mptr += sizeof(struct ip6t_entry_target);
1217 memset(mptr, 0xFF, ip6tables_targets->userspacesize);
1218
1219 return mask;
1220}
1221
1222static int
1223delete_entry(const ip6t_chainlabel chain,
1224 struct ip6t_entry *fw,
1225 unsigned int nsaddrs,
1226 const struct in6_addr saddrs[],
1227 unsigned int ndaddrs,
1228 const struct in6_addr daddrs[],
1229 int verbose,
1230 ip6tc_handle_t *handle)
1231{
1232 unsigned int i, j;
1233 int ret = 1;
1234 struct ip6t_entry ipfw = *fw;
1235 unsigned char *mask;
1236
1237 mask = make_delete_mask(fw);
1238 for (i = 0; i < nsaddrs; i++) {
1239 ipfw.ipv6.src = saddrs[i];
1240 for (j = 0; j < ndaddrs; j++) {
1241 ipfw.ipv6.dst = daddrs[j];
1242 if (verbose)
1243 print_firewall_line(fw, *handle);
1244 ret &= ip6tc_delete_entry(chain, &ipfw, mask, handle);
1245 }
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++];
1559 break;
1560
1561 case 'P':
1562 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1563 invert);
1564 chain = optarg;
1565 if (optind < argc && argv[optind][0] != '-'
1566 && argv[optind][0] != '!')
1567 policy = argv[optind++];
1568 else
1569 exit_error(PARAMETER_PROBLEM,
1570 "-%c requires a chain and a policy",
1571 cmd2char(CMD_SET_POLICY));
1572 break;
1573
1574 case 'h':
1575 if (!optarg)
1576 optarg = argv[optind];
1577
1578 /* iptables -p icmp -h */
1579 if (!ip6tables_matches && protocol)
1580 find_match(protocol, TRY_LOAD);
1581
1582 exit_printhelp();
1583
1584 /*
1585 * Option selection
1586 */
1587 case 'p':
1588 if (check_inverse(optarg, &invert))
1589 optind++;
1590 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1591 invert);
1592
1593 /* Canonicalize into lower case */
1594 for (protocol = argv[optind-1]; *protocol; protocol++)
1595 *protocol = tolower(*protocol);
1596
1597 protocol = argv[optind-1];
1598 fw.ipv6.proto = parse_protocol(protocol);
1599 fw.ipv6.flags |= IP6T_F_PROTO;
1600
1601 if (fw.ipv6.proto == 0
1602 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1603 exit_error(PARAMETER_PROBLEM,
1604 "rule would never match protocol");
1605 fw.nfcache |= NFC_IP6_PROTO;
1606 break;
1607
1608 case 's':
1609 if (check_inverse(optarg, &invert))
1610 optind++;
1611 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1612 invert);
1613 shostnetworkmask = argv[optind-1];
1614 fw.nfcache |= NFC_IP6_SRC;
1615 break;
1616
1617 case 'd':
1618 if (check_inverse(optarg, &invert))
1619 optind++;
1620 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1621 invert);
1622 dhostnetworkmask = argv[optind-1];
1623 fw.nfcache |= NFC_IP6_DST;
1624 break;
1625
1626 case 'j':
1627 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1628 invert);
1629 jumpto = parse_target(optarg);
1630 target = find_target(jumpto, TRY_LOAD);
1631
1632 if (target) {
1633 size_t size;
1634
1635 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1636 + target->size);
1637
1638 target->t = fw_calloc(1, size);
1639 target->t->u.target_size = size;
1640 strcpy(target->t->u.user.name, jumpto);
1641 target->init(target->t, &fw.nfcache);
1642 }
1643 break;
1644
1645
1646 case 'i':
1647 if (check_inverse(optarg, &invert))
1648 optind++;
1649 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1650 invert);
1651 parse_interface(argv[optind-1],
1652 fw.ipv6.iniface,
1653 fw.ipv6.iniface_mask);
1654 fw.nfcache |= NFC_IP6_IF_IN;
1655 break;
1656
1657 case 'o':
1658 if (check_inverse(optarg, &invert))
1659 optind++;
1660 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1661 invert);
1662 parse_interface(argv[optind-1],
1663 fw.ipv6.outiface,
1664 fw.ipv6.outiface_mask);
1665 fw.nfcache |= NFC_IP6_IF_OUT;
1666 break;
1667
1668 case 'v':
1669 if (!verbose)
1670 set_option(&options, OPT_VERBOSE,
1671 &fw.ipv6.invflags, invert);
1672 verbose++;
1673 break;
1674
1675 case 'm': {
1676 size_t size;
1677
1678 if (invert)
1679 exit_error(PARAMETER_PROBLEM,
1680 "unexpected ! flag before --match");
1681
1682 m = find_match(optarg, LOAD_MUST_SUCCEED);
1683 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1684 + m->size);
1685 m->m = fw_calloc(1, size);
1686 m->m->u.match_size = size;
1687 strcpy(m->m->u.user.name, m->name);
1688 m->init(m->m, &fw.nfcache);
1689 }
1690 break;
1691
1692 case 'n':
1693 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1694 invert);
1695 break;
1696
1697 case 't':
1698 if (invert)
1699 exit_error(PARAMETER_PROBLEM,
1700 "unexpected ! flag before --table");
1701 *table = argv[optind-1];
1702 break;
1703
1704 case 'x':
1705 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1706 invert);
1707 break;
1708
1709 case 'V':
1710 if (invert)
1711 printf("Not %s ;-)\n", program_version);
1712 else
1713 printf("%s v%s\n",
1714 program_name, program_version);
1715 exit(0);
1716
1717 case '0':
1718 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1719 invert);
1720 break;
1721
1722 case 1: /* non option */
1723 if (optarg[0] == '!' && optarg[1] == '\0') {
1724 if (invert)
1725 exit_error(PARAMETER_PROBLEM,
1726 "multiple consecutive ! not"
1727 " allowed");
1728 invert = TRUE;
1729 optarg[0] = '\0';
1730 continue;
1731 }
1732 printf("Bad argument `%s'\n", optarg);
1733 exit_tryhelp(2);
1734
1735 default:
1736 /* FIXME: This scheme doesn't allow two of the same
1737 matches --RR */
1738 if (!target
1739 || !(target->parse(c - target->option_offset,
1740 argv, invert,
1741 &target->tflags,
1742 &fw, &target->t))) {
1743 for (m = ip6tables_matches; m; m = m->next) {
1744 if (m->parse(c - m->option_offset,
1745 argv, invert,
1746 &m->mflags,
1747 &fw,
1748 &fw.nfcache,
1749 &m->m))
1750 break;
1751 }
1752
1753 /* If you listen carefully, you can
1754 actually hear this code suck. */
1755 if (m == NULL
1756 && protocol
1757 && !find_proto(protocol, DONT_LOAD,
1758 options&OPT_NUMERIC)
1759 && (m = find_proto(protocol, TRY_LOAD,
1760 options&OPT_NUMERIC))) {
1761 /* Try loading protocol */
1762 size_t size;
1763
1764 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1765 + m->size);
1766
1767 m->m = fw_calloc(1, size);
1768 m->m->u.match_size = size;
1769 strcpy(m->m->u.user.name, m->name);
1770 m->init(m->m, &fw.nfcache);
1771
1772 optind--;
1773 continue;
1774 }
1775 if (!m)
1776 exit_error(PARAMETER_PROBLEM,
1777 "Unknown arg `%s'",
1778 argv[optind-1]);
1779 }
1780 }
1781 invert = FALSE;
1782 }
1783
1784 for (m = ip6tables_matches; m; m = m->next)
1785 m->final_check(m->mflags);
1786 if (target)
1787 target->final_check(target->tflags);
1788
1789 /* Fix me: must put inverse options checking here --MN */
1790
1791 if (optind < argc)
1792 exit_error(PARAMETER_PROBLEM,
1793 "unknown arguments found on commandline");
1794 if (!command)
1795 exit_error(PARAMETER_PROBLEM, "no command specified");
1796 if (invert)
1797 exit_error(PARAMETER_PROBLEM,
1798 "nothing appropriate following !");
1799
1800 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1801 CMD_CHECK)) {
1802 if (!(options & OPT_DESTINATION))
1803 dhostnetworkmask = "0.0.0.0/0";
1804 if (!(options & OPT_SOURCE))
1805 shostnetworkmask = "0.0.0.0/0";
1806 }
1807
1808 if (shostnetworkmask)
1809 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1810 &(fw.ipv6.smsk), &nsaddrs);
1811
1812 if (dhostnetworkmask)
1813 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1814 &(fw.ipv6.dmsk), &ndaddrs);
1815
1816 if ((nsaddrs > 1 || ndaddrs > 1) &&
1817 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1818 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1819 " source or destination IP addresses");
1820
1821 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
1822 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1823 cmd2char(CMD_CHECK));
1824
1825 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1826 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1827 "specify a unique address");
1828
1829 generic_opt_check(command, options);
1830
1831 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1832 exit_error(PARAMETER_PROBLEM,
1833 "chain name `%s' too long (must be under %i chars)",
1834 chain, IP6T_FUNCTION_MAXNAMELEN);
1835
1836 *handle = ip6tc_init(*table);
1837 if (!*handle)
1838 exit_error(VERSION_PROBLEM,
1839 "can't initialize iptables table `%s': %s",
1840 *table, ip6tc_strerror(errno));
1841
1842 if (command == CMD_CHECK
1843 || command == CMD_APPEND
1844 || command == CMD_DELETE
1845 || command == CMD_INSERT
1846 || command == CMD_REPLACE) {
1847 /* -o not valid with incoming packets. */
1848 if (options & OPT_VIANAMEOUT)
1849 if (strcmp(chain, "PREROUTING") == 0
1850 || strcmp(chain, "INPUT") == 0) {
1851 exit_error(PARAMETER_PROBLEM,
1852 "Can't use -%c with %s\n",
1853 opt2char(OPT_VIANAMEOUT),
1854 chain);
1855 }
1856
1857 /* -i not valid with outgoing packets */
1858 if (options & OPT_VIANAMEIN)
1859 if (strcmp(chain, "POSTROUTING") == 0
1860 || strcmp(chain, "OUTPUT") == 0) {
1861 exit_error(PARAMETER_PROBLEM,
1862 "Can't use -%c with %s\n",
1863 opt2char(OPT_VIANAMEIN),
1864 chain);
1865 }
1866
1867 if (target && ip6tc_is_chain(jumpto, *handle)) {
1868 printf("Warning: using chain %s, not extension\n",
1869 jumpto);
1870
1871 target = NULL;
1872 }
1873
1874 /* If they didn't specify a target, or it's a chain
1875 name, use standard. */
1876 if (!target
1877 && (strlen(jumpto) == 0
1878 || ip6tc_is_chain(jumpto, *handle))) {
1879 size_t size;
1880
1881 target = find_target(IP6T_STANDARD_TARGET,
1882 LOAD_MUST_SUCCEED);
1883
1884 size = sizeof(struct ip6t_entry_target)
1885 + target->size;
1886 target->t = fw_calloc(1, size);
1887 target->t->u.target_size = size;
1888 strcpy(target->t->u.user.name, jumpto);
1889 target->init(target->t, &fw.nfcache);
1890 }
1891
1892 if (!target) {
1893 struct ip6t_entry_target unknown_target;
1894
1895 /* Don't know it. Must be extension with no
1896 options? */
1897 unknown_target.u.target_size = sizeof(unknown_target);
1898 strcpy(unknown_target.u.user.name, jumpto);
1899
1900 e = generate_entry(&fw, ip6tables_matches,
1901 &unknown_target);
1902 } else {
1903 e = generate_entry(&fw, ip6tables_matches, target->t);
1904 }
1905 }
1906
1907 switch (command) {
1908 case CMD_APPEND:
1909 ret = append_entry(chain, e,
1910 nsaddrs, saddrs, ndaddrs, daddrs,
1911 options&OPT_VERBOSE,
1912 handle);
1913 break;
1914 case CMD_CHECK:
1915 ret = check_packet(chain, e,
1916 nsaddrs, saddrs, ndaddrs, daddrs,
1917 options&OPT_VERBOSE, handle);
1918 break;
1919 case CMD_DELETE:
1920 ret = delete_entry(chain, e,
1921 nsaddrs, saddrs, ndaddrs, daddrs,
1922 options&OPT_VERBOSE,
1923 handle);
1924 break;
1925 case CMD_DELETE_NUM:
1926 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1927 break;
1928 case CMD_REPLACE:
1929 ret = replace_entry(chain, e, rulenum - 1,
1930 saddrs, daddrs, options&OPT_VERBOSE,
1931 handle);
1932 break;
1933 case CMD_INSERT:
1934 ret = insert_entry(chain, e, rulenum - 1,
1935 nsaddrs, saddrs, ndaddrs, daddrs,
1936 options&OPT_VERBOSE,
1937 handle);
1938 break;
1939 case CMD_LIST:
1940 ret = list_entries(chain,
1941 options&OPT_VERBOSE,
1942 options&OPT_NUMERIC,
1943 options&OPT_EXPANDED,
1944 options&OPT_LINENUMBERS,
1945 handle);
1946 break;
1947 case CMD_FLUSH:
1948 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1949 break;
1950 case CMD_ZERO:
1951 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1952 break;
1953 case CMD_LIST|CMD_ZERO:
1954 ret = list_entries(chain,
1955 options&OPT_VERBOSE,
1956 options&OPT_NUMERIC,
1957 options&OPT_EXPANDED,
1958 options&OPT_LINENUMBERS,
1959 handle);
1960 if (ret)
1961 ret = zero_entries(chain,
1962 options&OPT_VERBOSE, handle);
1963 break;
1964 case CMD_NEW_CHAIN:
1965 ret = ip6tc_create_chain(chain, handle);
1966 break;
1967 case CMD_DELETE_CHAIN:
1968 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1969 break;
1970 case CMD_RENAME_CHAIN:
1971 ret = ip6tc_rename_chain(chain, newname, handle);
1972 break;
1973 case CMD_SET_POLICY:
1974 ret = ip6tc_set_policy(chain, policy, handle);
1975 break;
1976 default:
1977 /* We should never reach this... */
1978 exit_tryhelp(2);
1979 }
1980
1981 if (verbose > 1)
1982 dump_entries6(*handle);
1983
1984 return ret;
1985}