blob: 27e7187c6e0bc22fff26e18bcd9cb082c6bbb7a1 [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;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001234 unsigned char *mask;
1235
1236 mask = make_delete_mask(fw);
1237 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001238 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001239 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001240 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241 if (verbose)
1242 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001243 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001244 }
1245 }
1246 return ret;
1247}
1248
1249static int
1250check_packet(const ip6t_chainlabel chain,
1251 struct ip6t_entry *fw,
1252 unsigned int nsaddrs,
1253 const struct in6_addr saddrs[],
1254 unsigned int ndaddrs,
1255 const struct in6_addr daddrs[],
1256 int verbose,
1257 ip6tc_handle_t *handle)
1258{
1259 int ret = 1;
1260 unsigned int i, j;
1261 struct ip6t_entry ipfw = *fw;
1262 const char *msg;
1263
1264 for (i = 0; i < nsaddrs; i++) {
1265 ipfw.ipv6.src = saddrs[i];
1266 for (j = 0; j < ndaddrs; j++) {
1267 ipfw.ipv6.dst = daddrs[j];
1268 if (verbose)
1269 print_firewall_line(fw, *handle);
1270 msg = ip6tc_check_packet(chain, &ipfw, handle);
1271 if (!msg) ret = 0;
1272 else printf("%s\n", msg);
1273 }
1274 }
1275
1276 return ret;
1277}
1278
1279static int
1280for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1281 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1282{
1283 int ret = 1;
1284 const char *chain;
1285 char *chains;
1286 unsigned int i, chaincount = 0;
1287
1288 chain = ip6tc_first_chain(handle);
1289 while (chain) {
1290 chaincount++;
1291 chain = ip6tc_next_chain(handle);
1292 }
1293
1294 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1295 i = 0;
1296 chain = ip6tc_first_chain(handle);
1297 while (chain) {
1298 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1299 i++;
1300 chain = ip6tc_next_chain(handle);
1301 }
1302
1303 for (i = 0; i < chaincount; i++) {
1304 if (!builtinstoo
1305 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1306 *handle))
1307 continue;
1308 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1309 }
1310
1311 free(chains);
1312 return ret;
1313}
1314
1315static int
1316flush_entries(const ip6t_chainlabel chain, int verbose,
1317 ip6tc_handle_t *handle)
1318{
1319 if (!chain)
1320 return for_each_chain(flush_entries, verbose, 1, handle);
1321
1322 if (verbose)
1323 fprintf(stdout, "Flushing chain `%s'\n", chain);
1324 return ip6tc_flush_entries(chain, handle);
1325}
1326
1327static int
1328zero_entries(const ip6t_chainlabel chain, int verbose,
1329 ip6tc_handle_t *handle)
1330{
1331 if (!chain)
1332 return for_each_chain(zero_entries, verbose, 1, handle);
1333
1334 if (verbose)
1335 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1336 return ip6tc_zero_entries(chain, handle);
1337}
1338
1339static int
1340delete_chain(const ip6t_chainlabel chain, int verbose,
1341 ip6tc_handle_t *handle)
1342{
1343 if (!chain)
1344 return for_each_chain(delete_chain, verbose, 0, handle);
1345
1346 if (verbose)
1347 fprintf(stdout, "Deleting chain `%s'\n", chain);
1348 return ip6tc_delete_chain(chain, handle);
1349}
1350
1351static int
1352list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1353 int expanded, int linenumbers, ip6tc_handle_t *handle)
1354{
1355 int found = 0;
1356 unsigned int format;
1357 const char *this;
1358
1359 format = FMT_OPTIONS;
1360 if (!verbose)
1361 format |= FMT_NOCOUNTS;
1362 else
1363 format |= FMT_VIA;
1364
1365 if (numeric)
1366 format |= FMT_NUMERIC;
1367
1368 if (!expanded)
1369 format |= FMT_KILOMEGAGIGA;
1370
1371 if (linenumbers)
1372 format |= FMT_LINENUMBERS;
1373
1374 for (this = ip6tc_first_chain(handle);
1375 this;
1376 this = ip6tc_next_chain(handle)) {
1377 const struct ip6t_entry *i;
1378 unsigned int num;
1379
1380 if (chain && strcmp(chain, this) != 0)
1381 continue;
1382
1383 if (found) printf("\n");
1384
1385 print_header(format, this, handle);
1386 i = ip6tc_first_rule(this, handle);
1387
1388 num = 0;
1389 while (i) {
1390 print_firewall(i,
1391 ip6tc_get_target(i, handle),
1392 num++,
1393 format,
1394 *handle);
1395 i = ip6tc_next_rule(i, handle);
1396 }
1397 found = 1;
1398 }
1399
1400 errno = ENOENT;
1401 return found;
1402}
1403
1404static struct ip6t_entry *
1405generate_entry(const struct ip6t_entry *fw,
1406 struct ip6tables_match *matches,
1407 struct ip6t_entry_target *target)
1408{
1409 unsigned int size;
1410 struct ip6tables_match *m;
1411 struct ip6t_entry *e;
1412
1413 size = sizeof(struct ip6t_entry);
1414 for (m = matches; m; m = m->next)
1415 size += m->m->u.match_size;
1416
1417 e = fw_malloc(size + target->u.target_size);
1418 *e = *fw;
1419 e->target_offset = size;
1420 e->next_offset = size + target->u.target_size;
1421
1422 size = 0;
1423 for (m = matches; m; m = m->next) {
1424 memcpy(e->elems + size, m->m, m->m->u.match_size);
1425 size += m->m->u.match_size;
1426 }
1427 memcpy(e->elems + size, target, target->u.target_size);
1428
1429 return e;
1430}
1431
1432int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1433{
1434 struct ip6t_entry fw, *e = NULL;
1435 int invert = 0;
1436 unsigned int nsaddrs = 0, ndaddrs = 0;
1437 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1438
1439 int c, verbose = 0;
1440 const char *chain = NULL;
1441 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1442 const char *policy = NULL, *newname = NULL;
1443 unsigned int rulenum = 0, options = 0, command = 0;
1444 int ret = 1;
1445 struct ip6tables_match *m;
1446 struct ip6tables_target *target = NULL;
1447 const char *jumpto = "";
1448 char *protocol = NULL;
1449
1450 memset(&fw, 0, sizeof(fw));
1451
1452 /* Suppress error messages: we may add new options if we
1453 demand-load a protocol. */
1454 opterr = 0;
1455
1456 while ((c = getopt_long(argc, argv,
1457 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:fbvnt:m:x",
1458 opts, NULL)) != -1) {
1459 switch (c) {
1460 /*
1461 * Command selection
1462 */
1463 case 'A':
1464 add_command(&command, CMD_APPEND, CMD_NONE,
1465 invert);
1466 chain = optarg;
1467 break;
1468
1469 case 'D':
1470 add_command(&command, CMD_DELETE, CMD_NONE,
1471 invert);
1472 chain = optarg;
1473 if (optind < argc && argv[optind][0] != '-'
1474 && argv[optind][0] != '!') {
1475 rulenum = parse_rulenumber(argv[optind++]);
1476 command = CMD_DELETE_NUM;
1477 }
1478 break;
1479
1480 case 'C':
1481 add_command(&command, CMD_CHECK, CMD_NONE,
1482 invert);
1483 chain = optarg;
1484 break;
1485
1486 case 'R':
1487 add_command(&command, CMD_REPLACE, CMD_NONE,
1488 invert);
1489 chain = optarg;
1490 if (optind < argc && argv[optind][0] != '-'
1491 && argv[optind][0] != '!')
1492 rulenum = parse_rulenumber(argv[optind++]);
1493 else
1494 exit_error(PARAMETER_PROBLEM,
1495 "-%c requires a rule number",
1496 cmd2char(CMD_REPLACE));
1497 break;
1498
1499 case 'I':
1500 add_command(&command, CMD_INSERT, CMD_NONE,
1501 invert);
1502 chain = optarg;
1503 if (optind < argc && argv[optind][0] != '-'
1504 && argv[optind][0] != '!')
1505 rulenum = parse_rulenumber(argv[optind++]);
1506 else rulenum = 1;
1507 break;
1508
1509 case 'L':
1510 add_command(&command, CMD_LIST, CMD_ZERO,
1511 invert);
1512 if (optarg) chain = optarg;
1513 else if (optind < argc && argv[optind][0] != '-'
1514 && argv[optind][0] != '!')
1515 chain = argv[optind++];
1516 break;
1517
1518 case 'F':
1519 add_command(&command, CMD_FLUSH, CMD_NONE,
1520 invert);
1521 if (optarg) chain = optarg;
1522 else if (optind < argc && argv[optind][0] != '-'
1523 && argv[optind][0] != '!')
1524 chain = argv[optind++];
1525 break;
1526
1527 case 'Z':
1528 add_command(&command, CMD_ZERO, CMD_LIST,
1529 invert);
1530 if (optarg) chain = optarg;
1531 else if (optind < argc && argv[optind][0] != '-'
1532 && argv[optind][0] != '!')
1533 chain = argv[optind++];
1534 break;
1535
1536 case 'N':
1537 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1538 invert);
1539 chain = optarg;
1540 break;
1541
1542 case 'X':
1543 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1544 invert);
1545 if (optarg) chain = optarg;
1546 else if (optind < argc && argv[optind][0] != '-'
1547 && argv[optind][0] != '!')
1548 chain = argv[optind++];
1549 break;
1550
1551 case 'E':
1552 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1553 invert);
1554 chain = optarg;
1555 if (optind < argc && argv[optind][0] != '-'
1556 && argv[optind][0] != '!')
1557 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001558 else
1559 exit_error(PARAMETER_PROBLEM,
1560 "-%c requires old-chain-name and "
1561 "new-chain-name",
1562 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563 break;
1564
1565 case 'P':
1566 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1567 invert);
1568 chain = optarg;
1569 if (optind < argc && argv[optind][0] != '-'
1570 && argv[optind][0] != '!')
1571 policy = argv[optind++];
1572 else
1573 exit_error(PARAMETER_PROBLEM,
1574 "-%c requires a chain and a policy",
1575 cmd2char(CMD_SET_POLICY));
1576 break;
1577
1578 case 'h':
1579 if (!optarg)
1580 optarg = argv[optind];
1581
1582 /* iptables -p icmp -h */
1583 if (!ip6tables_matches && protocol)
1584 find_match(protocol, TRY_LOAD);
1585
1586 exit_printhelp();
1587
1588 /*
1589 * Option selection
1590 */
1591 case 'p':
1592 if (check_inverse(optarg, &invert))
1593 optind++;
1594 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1595 invert);
1596
1597 /* Canonicalize into lower case */
1598 for (protocol = argv[optind-1]; *protocol; protocol++)
1599 *protocol = tolower(*protocol);
1600
1601 protocol = argv[optind-1];
1602 fw.ipv6.proto = parse_protocol(protocol);
1603 fw.ipv6.flags |= IP6T_F_PROTO;
1604
1605 if (fw.ipv6.proto == 0
1606 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1607 exit_error(PARAMETER_PROBLEM,
1608 "rule would never match protocol");
1609 fw.nfcache |= NFC_IP6_PROTO;
1610 break;
1611
1612 case 's':
1613 if (check_inverse(optarg, &invert))
1614 optind++;
1615 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1616 invert);
1617 shostnetworkmask = argv[optind-1];
1618 fw.nfcache |= NFC_IP6_SRC;
1619 break;
1620
1621 case 'd':
1622 if (check_inverse(optarg, &invert))
1623 optind++;
1624 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1625 invert);
1626 dhostnetworkmask = argv[optind-1];
1627 fw.nfcache |= NFC_IP6_DST;
1628 break;
1629
1630 case 'j':
1631 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1632 invert);
1633 jumpto = parse_target(optarg);
1634 target = find_target(jumpto, TRY_LOAD);
1635
1636 if (target) {
1637 size_t size;
1638
1639 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target)
1640 + target->size);
1641
1642 target->t = fw_calloc(1, size);
1643 target->t->u.target_size = size;
1644 strcpy(target->t->u.user.name, jumpto);
1645 target->init(target->t, &fw.nfcache);
1646 }
1647 break;
1648
1649
1650 case 'i':
1651 if (check_inverse(optarg, &invert))
1652 optind++;
1653 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1654 invert);
1655 parse_interface(argv[optind-1],
1656 fw.ipv6.iniface,
1657 fw.ipv6.iniface_mask);
1658 fw.nfcache |= NFC_IP6_IF_IN;
1659 break;
1660
1661 case 'o':
1662 if (check_inverse(optarg, &invert))
1663 optind++;
1664 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1665 invert);
1666 parse_interface(argv[optind-1],
1667 fw.ipv6.outiface,
1668 fw.ipv6.outiface_mask);
1669 fw.nfcache |= NFC_IP6_IF_OUT;
1670 break;
1671
1672 case 'v':
1673 if (!verbose)
1674 set_option(&options, OPT_VERBOSE,
1675 &fw.ipv6.invflags, invert);
1676 verbose++;
1677 break;
1678
1679 case 'm': {
1680 size_t size;
1681
1682 if (invert)
1683 exit_error(PARAMETER_PROBLEM,
1684 "unexpected ! flag before --match");
1685
1686 m = find_match(optarg, LOAD_MUST_SUCCEED);
1687 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1688 + m->size);
1689 m->m = fw_calloc(1, size);
1690 m->m->u.match_size = size;
1691 strcpy(m->m->u.user.name, m->name);
1692 m->init(m->m, &fw.nfcache);
1693 }
1694 break;
1695
1696 case 'n':
1697 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1698 invert);
1699 break;
1700
1701 case 't':
1702 if (invert)
1703 exit_error(PARAMETER_PROBLEM,
1704 "unexpected ! flag before --table");
1705 *table = argv[optind-1];
1706 break;
1707
1708 case 'x':
1709 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1710 invert);
1711 break;
1712
1713 case 'V':
1714 if (invert)
1715 printf("Not %s ;-)\n", program_version);
1716 else
1717 printf("%s v%s\n",
1718 program_name, program_version);
1719 exit(0);
1720
1721 case '0':
1722 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1723 invert);
1724 break;
1725
1726 case 1: /* non option */
1727 if (optarg[0] == '!' && optarg[1] == '\0') {
1728 if (invert)
1729 exit_error(PARAMETER_PROBLEM,
1730 "multiple consecutive ! not"
1731 " allowed");
1732 invert = TRUE;
1733 optarg[0] = '\0';
1734 continue;
1735 }
1736 printf("Bad argument `%s'\n", optarg);
1737 exit_tryhelp(2);
1738
1739 default:
1740 /* FIXME: This scheme doesn't allow two of the same
1741 matches --RR */
1742 if (!target
1743 || !(target->parse(c - target->option_offset,
1744 argv, invert,
1745 &target->tflags,
1746 &fw, &target->t))) {
1747 for (m = ip6tables_matches; m; m = m->next) {
1748 if (m->parse(c - m->option_offset,
1749 argv, invert,
1750 &m->mflags,
1751 &fw,
1752 &fw.nfcache,
1753 &m->m))
1754 break;
1755 }
1756
1757 /* If you listen carefully, you can
1758 actually hear this code suck. */
1759 if (m == NULL
1760 && protocol
1761 && !find_proto(protocol, DONT_LOAD,
1762 options&OPT_NUMERIC)
1763 && (m = find_proto(protocol, TRY_LOAD,
1764 options&OPT_NUMERIC))) {
1765 /* Try loading protocol */
1766 size_t size;
1767
1768 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)
1769 + m->size);
1770
1771 m->m = fw_calloc(1, size);
1772 m->m->u.match_size = size;
1773 strcpy(m->m->u.user.name, m->name);
1774 m->init(m->m, &fw.nfcache);
1775
1776 optind--;
1777 continue;
1778 }
1779 if (!m)
1780 exit_error(PARAMETER_PROBLEM,
1781 "Unknown arg `%s'",
1782 argv[optind-1]);
1783 }
1784 }
1785 invert = FALSE;
1786 }
1787
1788 for (m = ip6tables_matches; m; m = m->next)
1789 m->final_check(m->mflags);
1790 if (target)
1791 target->final_check(target->tflags);
1792
1793 /* Fix me: must put inverse options checking here --MN */
1794
1795 if (optind < argc)
1796 exit_error(PARAMETER_PROBLEM,
1797 "unknown arguments found on commandline");
1798 if (!command)
1799 exit_error(PARAMETER_PROBLEM, "no command specified");
1800 if (invert)
1801 exit_error(PARAMETER_PROBLEM,
1802 "nothing appropriate following !");
1803
1804 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
1805 CMD_CHECK)) {
1806 if (!(options & OPT_DESTINATION))
1807 dhostnetworkmask = "0.0.0.0/0";
1808 if (!(options & OPT_SOURCE))
1809 shostnetworkmask = "0.0.0.0/0";
1810 }
1811
1812 if (shostnetworkmask)
1813 parse_hostnetworkmask(shostnetworkmask, &saddrs,
1814 &(fw.ipv6.smsk), &nsaddrs);
1815
1816 if (dhostnetworkmask)
1817 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
1818 &(fw.ipv6.dmsk), &ndaddrs);
1819
1820 if ((nsaddrs > 1 || ndaddrs > 1) &&
1821 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
1822 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
1823 " source or destination IP addresses");
1824
1825 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
1826 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
1827 cmd2char(CMD_CHECK));
1828
1829 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
1830 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
1831 "specify a unique address");
1832
1833 generic_opt_check(command, options);
1834
1835 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
1836 exit_error(PARAMETER_PROBLEM,
1837 "chain name `%s' too long (must be under %i chars)",
1838 chain, IP6T_FUNCTION_MAXNAMELEN);
1839
1840 *handle = ip6tc_init(*table);
1841 if (!*handle)
1842 exit_error(VERSION_PROBLEM,
1843 "can't initialize iptables table `%s': %s",
1844 *table, ip6tc_strerror(errno));
1845
1846 if (command == CMD_CHECK
1847 || command == CMD_APPEND
1848 || command == CMD_DELETE
1849 || command == CMD_INSERT
1850 || command == CMD_REPLACE) {
1851 /* -o not valid with incoming packets. */
1852 if (options & OPT_VIANAMEOUT)
1853 if (strcmp(chain, "PREROUTING") == 0
1854 || strcmp(chain, "INPUT") == 0) {
1855 exit_error(PARAMETER_PROBLEM,
1856 "Can't use -%c with %s\n",
1857 opt2char(OPT_VIANAMEOUT),
1858 chain);
1859 }
1860
1861 /* -i not valid with outgoing packets */
1862 if (options & OPT_VIANAMEIN)
1863 if (strcmp(chain, "POSTROUTING") == 0
1864 || strcmp(chain, "OUTPUT") == 0) {
1865 exit_error(PARAMETER_PROBLEM,
1866 "Can't use -%c with %s\n",
1867 opt2char(OPT_VIANAMEIN),
1868 chain);
1869 }
1870
1871 if (target && ip6tc_is_chain(jumpto, *handle)) {
1872 printf("Warning: using chain %s, not extension\n",
1873 jumpto);
1874
1875 target = NULL;
1876 }
1877
1878 /* If they didn't specify a target, or it's a chain
1879 name, use standard. */
1880 if (!target
1881 && (strlen(jumpto) == 0
1882 || ip6tc_is_chain(jumpto, *handle))) {
1883 size_t size;
1884
1885 target = find_target(IP6T_STANDARD_TARGET,
1886 LOAD_MUST_SUCCEED);
1887
1888 size = sizeof(struct ip6t_entry_target)
1889 + target->size;
1890 target->t = fw_calloc(1, size);
1891 target->t->u.target_size = size;
1892 strcpy(target->t->u.user.name, jumpto);
1893 target->init(target->t, &fw.nfcache);
1894 }
1895
1896 if (!target) {
1897 struct ip6t_entry_target unknown_target;
1898
1899 /* Don't know it. Must be extension with no
1900 options? */
1901 unknown_target.u.target_size = sizeof(unknown_target);
1902 strcpy(unknown_target.u.user.name, jumpto);
1903
1904 e = generate_entry(&fw, ip6tables_matches,
1905 &unknown_target);
1906 } else {
1907 e = generate_entry(&fw, ip6tables_matches, target->t);
1908 }
1909 }
1910
1911 switch (command) {
1912 case CMD_APPEND:
1913 ret = append_entry(chain, e,
1914 nsaddrs, saddrs, ndaddrs, daddrs,
1915 options&OPT_VERBOSE,
1916 handle);
1917 break;
1918 case CMD_CHECK:
1919 ret = check_packet(chain, e,
1920 nsaddrs, saddrs, ndaddrs, daddrs,
1921 options&OPT_VERBOSE, handle);
1922 break;
1923 case CMD_DELETE:
1924 ret = delete_entry(chain, e,
1925 nsaddrs, saddrs, ndaddrs, daddrs,
1926 options&OPT_VERBOSE,
1927 handle);
1928 break;
1929 case CMD_DELETE_NUM:
1930 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
1931 break;
1932 case CMD_REPLACE:
1933 ret = replace_entry(chain, e, rulenum - 1,
1934 saddrs, daddrs, options&OPT_VERBOSE,
1935 handle);
1936 break;
1937 case CMD_INSERT:
1938 ret = insert_entry(chain, e, rulenum - 1,
1939 nsaddrs, saddrs, ndaddrs, daddrs,
1940 options&OPT_VERBOSE,
1941 handle);
1942 break;
1943 case CMD_LIST:
1944 ret = list_entries(chain,
1945 options&OPT_VERBOSE,
1946 options&OPT_NUMERIC,
1947 options&OPT_EXPANDED,
1948 options&OPT_LINENUMBERS,
1949 handle);
1950 break;
1951 case CMD_FLUSH:
1952 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
1953 break;
1954 case CMD_ZERO:
1955 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
1956 break;
1957 case CMD_LIST|CMD_ZERO:
1958 ret = list_entries(chain,
1959 options&OPT_VERBOSE,
1960 options&OPT_NUMERIC,
1961 options&OPT_EXPANDED,
1962 options&OPT_LINENUMBERS,
1963 handle);
1964 if (ret)
1965 ret = zero_entries(chain,
1966 options&OPT_VERBOSE, handle);
1967 break;
1968 case CMD_NEW_CHAIN:
1969 ret = ip6tc_create_chain(chain, handle);
1970 break;
1971 case CMD_DELETE_CHAIN:
1972 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
1973 break;
1974 case CMD_RENAME_CHAIN:
1975 ret = ip6tc_rename_chain(chain, newname, handle);
1976 break;
1977 case CMD_SET_POLICY:
1978 ret = ip6tc_set_policy(chain, policy, handle);
1979 break;
1980 default:
1981 /* We should never reach this... */
1982 exit_tryhelp(2);
1983 }
1984
1985 if (verbose > 1)
1986 dump_entries6(*handle);
1987
1988 return ret;
1989}