1 /* Code to take an iptables-style command line and do it. */
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
6 * (C) 2000-2002 by the netfilter coreteam <coreteam@netfilter.org>:
7 * Paul 'Rusty' Russell <rusty@rustcorp.com.au>
8 * Marc Boucher <marc+nf@mbsi.ca>
9 * James Morris <jmorris@intercode.com.au>
10 * Harald Welte <laforge@gnumonks.org>
11 * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <ip6tables.h>
39 #include <arpa/inet.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
54 #define IP6T_LIB_DIR "/usr/local/lib/iptables"
57 #ifndef PROC_SYS_MODPROBE
58 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
61 #define FMT_NUMERIC 0x0001
62 #define FMT_NOCOUNTS 0x0002
63 #define FMT_KILOMEGAGIGA 0x0004
64 #define FMT_OPTIONS 0x0008
65 #define FMT_NOTABLE 0x0010
66 #define FMT_NOTARGET 0x0020
67 #define FMT_VIA 0x0040
68 #define FMT_NONEWLINE 0x0080
69 #define FMT_LINENUMBERS 0x0100
71 #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
72 | FMT_NUMERIC | FMT_NOTABLE)
73 #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
76 #define CMD_NONE 0x0000U
77 #define CMD_INSERT 0x0001U
78 #define CMD_DELETE 0x0002U
79 #define CMD_DELETE_NUM 0x0004U
80 #define CMD_REPLACE 0x0008U
81 #define CMD_APPEND 0x0010U
82 #define CMD_LIST 0x0020U
83 #define CMD_FLUSH 0x0040U
84 #define CMD_ZERO 0x0080U
85 #define CMD_NEW_CHAIN 0x0100U
86 #define CMD_DELETE_CHAIN 0x0200U
87 #define CMD_SET_POLICY 0x0400U
88 #define CMD_CHECK 0x0800U
89 #define CMD_RENAME_CHAIN 0x1000U
90 #define NUMBER_OF_CMD 13
91 static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
94 #define OPTION_OFFSET 256
96 #define OPT_NONE 0x00000U
97 #define OPT_NUMERIC 0x00001U
98 #define OPT_SOURCE 0x00002U
99 #define OPT_DESTINATION 0x00004U
100 #define OPT_PROTOCOL 0x00008U
101 #define OPT_JUMP 0x00010U
102 #define OPT_VERBOSE 0x00020U
103 #define OPT_EXPANDED 0x00040U
104 #define OPT_VIANAMEIN 0x00080U
105 #define OPT_VIANAMEOUT 0x00100U
106 #define OPT_LINENUMBERS 0x00200U
107 #define OPT_COUNTERS 0x00400U
108 #define NUMBER_OF_OPT 11
109 static const char optflags[NUMBER_OF_OPT]
110 = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
112 static struct option original_opts[] = {
113 { "append", 1, 0, 'A' },
114 { "delete", 1, 0, 'D' },
115 { "insert", 1, 0, 'I' },
116 { "replace", 1, 0, 'R' },
117 { "list", 2, 0, 'L' },
118 { "flush", 2, 0, 'F' },
119 { "zero", 2, 0, 'Z' },
120 { "new-chain", 1, 0, 'N' },
121 { "delete-chain", 2, 0, 'X' },
122 { "rename-chain", 1, 0, 'E' },
123 { "policy", 1, 0, 'P' },
124 { "source", 1, 0, 's' },
125 { "destination", 1, 0, 'd' },
126 { "src", 1, 0, 's' }, /* synonym */
127 { "dst", 1, 0, 'd' }, /* synonym */
128 { "protocol", 1, 0, 'p' },
129 { "in-interface", 1, 0, 'i' },
130 { "jump", 1, 0, 'j' },
131 { "table", 1, 0, 't' },
132 { "match", 1, 0, 'm' },
133 { "numeric", 0, 0, 'n' },
134 { "out-interface", 1, 0, 'o' },
135 { "verbose", 0, 0, 'v' },
136 { "exact", 0, 0, 'x' },
137 { "version", 0, 0, 'V' },
138 { "help", 2, 0, 'h' },
139 { "line-numbers", 0, 0, '0' },
140 { "modprobe", 1, 0, 'M' },
141 { "set-counters", 1, 0, 'c' },
145 /* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
146 * current line of the input file, in order to give a more precise error
147 * message. ip6tables itself doesn't need this, so it is initialized to the
148 * magic number of -1 */
152 struct ip6t_entry_target *
153 ip6t_get_target(struct ip6t_entry *e)
155 return (void *)e + e->target_offset;
159 static struct option *opts = original_opts;
160 static unsigned int global_option_offset = 0;
162 /* Table of legal combinations of commands and options. If any of the
163 * given commands make an option legal, that option is legal (applies to
164 * CMD_LIST and CMD_ZERO only).
171 static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
172 /* Well, it's better than "Re: Linux vs FreeBSD" */
174 /* -n -s -d -p -j -v -x -i -o --line */
175 /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
176 /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
177 /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
178 /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
179 /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
180 /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
181 /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
182 /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
183 /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
184 /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
185 /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
186 /*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
187 /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
190 static int inverse_for_options[NUMBER_OF_OPT] =
193 /* -s */ IP6T_INV_SRCIP,
194 /* -d */ IP6T_INV_DSTIP,
195 /* -p */ IP6T_INV_PROTO,
199 /* -i */ IP6T_INV_VIA_IN,
200 /* -o */ IP6T_INV_VIA_OUT,
204 const char *program_version;
205 const char *program_name;
207 /* Keeping track of external matches and targets: linked lists. */
208 struct ip6tables_match *ip6tables_matches = NULL;
209 struct ip6tables_target *ip6tables_targets = NULL;
211 /* Extra debugging from libiptc */
212 extern void dump_entries6(const ip6tc_handle_t handle);
214 /* A few hardcoded protocols for 'all' and in case the user has no
221 /* Primitive headers... */
222 /* defined in netinet/in.h */
225 #define IPPROTO_ESP 50
228 #define IPPROTO_AH 51
232 static const struct pprot chain_protos[] = {
233 { "tcp", IPPROTO_TCP },
234 { "udp", IPPROTO_UDP },
235 { "icmpv6", IPPROTO_ICMPV6 },
236 { "esp", IPPROTO_ESP },
237 { "ah", IPPROTO_AH },
242 proto_to_name(u_int8_t proto, int nolookup)
246 if (proto && !nolookup) {
247 struct protoent *pent = getprotobynumber(proto);
252 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
253 if (chain_protos[i].num == proto)
254 return chain_protos[i].name;
260 in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
262 memcpy(dst, src, sizeof(struct in6_addr));
263 /* dst->s6_addr = src->s6_addr; */
267 exit_error(enum exittype status, char *msg, ...)
272 fprintf(stderr, "%s v%s: ", program_name, program_version);
273 vfprintf(stderr, msg, args);
275 fprintf(stderr, "\n");
276 if (status == PARAMETER_PROBLEM)
277 exit_tryhelp(status);
278 if (status == VERSION_PROBLEM)
280 "Perhaps iptables or your kernel needs to be upgraded.\n");
285 exit_tryhelp(int status)
288 fprintf(stderr, "Error occurred at line: %d\n", line);
289 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
290 program_name, program_name );
295 exit_printhelp(struct ip6tables_rule_match *matches)
297 struct ip6tables_rule_match *matchp = NULL;
298 struct ip6tables_target *t = NULL;
301 "Usage: %s -[AD] chain rule-specification [options]\n"
302 " %s -[RI] chain rulenum rule-specification [options]\n"
303 " %s -D chain rulenum [options]\n"
304 " %s -[LFZ] [chain] [options]\n"
306 " %s -E old-chain-name new-chain-name\n"
307 " %s -P chain target [options]\n"
308 " %s -h (print this help information)\n\n",
309 program_name, program_version, program_name, program_name,
310 program_name, program_name, program_name, program_name,
311 program_name, program_name);
315 "Either long or short options are allowed.\n"
316 " --append -A chain Append to chain\n"
317 " --delete -D chain Delete matching rule from chain\n"
318 " --delete -D chain rulenum\n"
319 " Delete rule rulenum (1 = first) from chain\n"
320 " --insert -I chain [rulenum]\n"
321 " Insert in chain as rulenum (default 1=first)\n"
322 " --replace -R chain rulenum\n"
323 " Replace rule rulenum (1 = first) in chain\n"
324 " --list -L [chain] List the rules in a chain or all chains\n"
325 " --flush -F [chain] Delete all rules in chain or all chains\n"
326 " --zero -Z [chain] Zero counters in chain or all chains\n"
327 " --new -N chain Create a new user-defined chain\n"
329 " -X [chain] Delete a user-defined chain\n"
330 " --policy -P chain target\n"
331 " Change policy on chain to target\n"
333 " -E old-chain new-chain\n"
334 " Change chain name, (moving any references)\n"
337 " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
338 " --source -s [!] address[/mask]\n"
339 " source specification\n"
340 " --destination -d [!] address[/mask]\n"
341 " destination specification\n"
342 " --in-interface -i [!] input name[+]\n"
343 " network interface name ([+] for wildcard)\n"
344 " --jump -j target\n"
345 " target for rule (may load target extension)\n"
346 " --match -m match\n"
347 " extended match (may load extension)\n"
348 " --numeric -n numeric output of addresses and ports\n"
349 " --out-interface -o [!] output name[+]\n"
350 " network interface name ([+] for wildcard)\n"
351 " --table -t table table to manipulate (default: `filter')\n"
352 " --verbose -v verbose mode\n"
353 " --line-numbers print line numbers when listing\n"
354 " --exact -x expand numbers (display exact values)\n"
355 /*"[!] --fragment -f match second or further fragments only\n"*/
356 " --modprobe=<command> try to insert modules using this command\n"
357 " --set-counters PKTS BYTES set the counter during insert/append\n"
358 "[!] --version -V print package version.\n");
360 /* Print out any special helps. A user might like to be able to add a --help
361 to the commandline, and see expected results. So we call help for all
362 specified matches & targets */
363 for (t = ip6tables_targets; t; t = t->next) {
369 for (matchp = matches; matchp; matchp = matchp->next) {
371 matchp->match->help();
377 generic_opt_check(int command, int options)
381 /* Check that commands are valid with options. Complicated by the
382 * fact that if an option is legal with *any* command given, it is
383 * legal overall (ie. -z and -l).
385 for (i = 0; i < NUMBER_OF_OPT; i++) {
386 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
388 for (j = 0; j < NUMBER_OF_CMD; j++) {
389 if (!(command & (1<<j)))
392 if (!(options & (1<<i))) {
393 if (commands_v_options[j][i] == '+')
394 exit_error(PARAMETER_PROBLEM,
395 "You need to supply the `-%c' "
396 "option for this command\n",
399 if (commands_v_options[j][i] != 'x')
406 exit_error(PARAMETER_PROBLEM,
407 "Illegal option `-%c' with this command\n",
416 for (ptr = optflags; option > 1; option >>= 1, ptr++);
425 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
431 add_command(int *cmd, const int newcmd, const int othercmds, int invert)
434 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
435 if (*cmd & (~othercmds))
436 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
437 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
442 check_inverse(const char option[], int *invert, int *optind, int argc)
444 if (option && strcmp(option, "!") == 0) {
446 exit_error(PARAMETER_PROBLEM,
447 "Multiple `!' flags not allowed");
451 if (argc && *optind > argc)
452 exit_error(PARAMETER_PROBLEM,
453 "no argument following `!'");
462 fw_calloc(size_t count, size_t size)
466 if ((p = calloc(count, size)) == NULL) {
467 perror("ip6tables: calloc failed");
474 fw_malloc(size_t size)
478 if ((p = malloc(size)) == NULL) {
479 perror("ip6tables: malloc failed");
486 addr_to_numeric(const struct in6_addr *addrp)
488 /* 0000:0000:0000:0000:0000:000.000.000.000
489 * 0000:0000:0000:0000:0000:0000:0000:0000 */
490 static char buf[50+1];
491 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
494 static struct in6_addr *
495 numeric_to_addr(const char *num)
497 static struct in6_addr ap;
499 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
502 fprintf(stderr, "\nnumeric2addr: %d\n", err);
504 return (struct in6_addr *)NULL;
508 static struct in6_addr *
509 host_to_addr(const char *name, unsigned int *naddr)
511 struct addrinfo hints;
512 struct addrinfo *res;
513 static struct in6_addr *addr;
516 memset(&hints, 0, sizeof(hints));
517 hints.ai_flags=AI_CANONNAME;
518 hints.ai_family=AF_INET6;
519 hints.ai_socktype=SOCK_RAW;
520 hints.ai_protocol=41;
524 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
526 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
528 return (struct in6_addr *) NULL;
530 if (res->ai_family != AF_INET6 ||
531 res->ai_addrlen != sizeof(struct sockaddr_in6))
532 return (struct in6_addr *) NULL;
535 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
536 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
538 /* Get the first element of the address-chain */
539 addr = fw_calloc(1, sizeof(struct in6_addr));
540 in6addrcpy(addr, (struct in6_addr *)
541 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
547 return (struct in6_addr *) NULL;
551 addr_to_host(const struct in6_addr *addr)
553 struct sockaddr_in6 saddr;
555 static char hostname[NI_MAXHOST];
557 memset(&saddr, 0, sizeof(struct sockaddr_in6));
558 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
559 saddr.sin6_family = AF_INET6;
561 if ( (err=getnameinfo((struct sockaddr *)&saddr,
562 sizeof(struct sockaddr_in6),
563 hostname, sizeof(hostname)-1,
566 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
568 return (char *) NULL;
571 fprintf (stderr, "\naddr2host: %s\n", hostname);
577 return (char *) NULL;
581 mask_to_numeric(const struct in6_addr *addrp)
583 static char buf[50+2];
584 int l = ipv6_prefix_length(addrp);
587 strcat(buf, addr_to_numeric(addrp));
590 sprintf(buf, "/%d", l);
594 static struct in6_addr *
595 network_to_addr(const char *name)
598 /* TODO: not implemented yet, but the exception breaks the
599 * name resolvation */
600 return (struct in6_addr *)NULL;
604 addr_to_anyname(const struct in6_addr *addr)
608 if ((name = addr_to_host(addr)) != NULL)
611 return addr_to_numeric(addr);
615 * All functions starting with "parse" should succeed, otherwise
617 * Most routines return pointers to static data that may change
618 * between calls to the same or other routines with a few exceptions:
619 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
620 * return global static data.
623 static struct in6_addr *
624 parse_hostnetwork(const char *name, unsigned int *naddrs)
626 struct in6_addr *addrp, *addrptmp;
628 if ((addrptmp = numeric_to_addr(name)) != NULL ||
629 (addrptmp = network_to_addr(name)) != NULL) {
630 addrp = fw_malloc(sizeof(struct in6_addr));
631 in6addrcpy(addrp, addrptmp);
635 if ((addrp = host_to_addr(name, naddrs)) != NULL)
638 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
641 static struct in6_addr *
642 parse_mask(char *mask)
644 static struct in6_addr maskaddr;
645 struct in6_addr *addrp;
649 /* no mask at all defaults to 128 bits */
650 memset(&maskaddr, 0xff, sizeof maskaddr);
653 if ((addrp = numeric_to_addr(mask)) != NULL)
655 if (string_to_number(mask, 0, 128, &bits) == -1)
656 exit_error(PARAMETER_PROBLEM,
657 "invalid mask `%s' specified", mask);
659 char *p = (char *)&maskaddr;
660 memset(p, 0xff, bits / 8);
661 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
662 p[bits / 8] = 0xff << (8 - (bits & 7));
666 memset(&maskaddr, 0, sizeof maskaddr);
671 parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
672 struct in6_addr *maskp, unsigned int *naddrs)
674 struct in6_addr *addrp;
679 strncpy(buf, name, sizeof(buf) - 1);
680 buf[sizeof(buf) - 1] = '\0';
681 if ((p = strrchr(buf, '/')) != NULL) {
683 addrp = parse_mask(p + 1);
685 addrp = parse_mask(NULL);
686 in6addrcpy(maskp, addrp);
688 /* if a null mask is given, the name is ignored, like in "any/0" */
689 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
692 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
694 for (i = 0, j = 0; i < n; i++) {
696 for (k = 0; k < 4; k++)
697 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
699 for (k = 0; k < j - 1; k++) {
700 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
709 struct ip6tables_match *
710 find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
712 struct ip6tables_match *ptr;
715 /* This is ugly as hell. Nonetheless, there is no way of changing
716 * this without hurting backwards compatibility */
717 if ( (strcmp(name,"icmpv6") == 0) ||
718 (strcmp(name,"ipv6-icmp") == 0) ||
719 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
722 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
723 if (strcmp(name, ptr->name) == 0)
727 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
728 if (strcmp("icmp6", ptr->name) == 0)
733 #ifndef NO_SHARED_LIBS
734 if (!ptr && tryload != DONT_LOAD) {
735 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
738 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
740 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
741 if (dlopen(path, RTLD_NOW)) {
742 /* Found library. If it didn't register itself,
743 maybe they specified target as match. */
744 ptr = find_match(name, DONT_LOAD, NULL);
747 exit_error(PARAMETER_PROBLEM,
748 "Couldn't load match `%s'\n",
750 } else if (tryload == LOAD_MUST_SUCCEED)
751 exit_error(PARAMETER_PROBLEM,
752 "Couldn't load match `%s':%s\n",
756 if (ptr && !ptr->loaded) {
757 if (tryload != DONT_LOAD)
762 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
763 exit_error(PARAMETER_PROBLEM,
764 "Couldn't find match `%s'\n", name);
768 if (ptr && matches) {
769 struct ip6tables_rule_match **i;
770 struct ip6tables_rule_match *newentry;
772 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
774 for (i = matches; *i; i = &(*i)->next);
775 newentry->match = ptr;
776 newentry->next = NULL;
783 /* Christophe Burki wants `-p 6' to imply `-m tcp'. */
784 static struct ip6tables_match *
785 find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
789 if (string_to_number(pname, 0, 255, &proto) != -1) {
790 char *protoname = proto_to_name(proto, nolookup);
793 return find_match(protoname, tryload, matches);
795 return find_match(pname, tryload, matches);
801 parse_protocol(const char *s)
805 if (string_to_number(s, 0, 255, &proto) == -1) {
806 struct protoent *pent;
808 if ((pent = getprotobyname(s)))
809 proto = pent->p_proto;
813 i < sizeof(chain_protos)/sizeof(struct pprot);
815 if (strcmp(s, chain_protos[i].name) == 0) {
816 proto = chain_protos[i].num;
820 if (i == sizeof(chain_protos)/sizeof(struct pprot))
821 exit_error(PARAMETER_PROBLEM,
822 "unknown protocol `%s' specified",
827 return (u_int16_t)proto;
831 parse_interface(const char *arg, char *vianame, unsigned char *mask)
833 int vialen = strlen(arg);
836 memset(mask, 0, IFNAMSIZ);
837 memset(vianame, 0, IFNAMSIZ);
839 if (vialen + 1 > IFNAMSIZ)
840 exit_error(PARAMETER_PROBLEM,
841 "interface name `%s' must be shorter than IFNAMSIZ"
842 " (%i)", arg, IFNAMSIZ-1);
844 strcpy(vianame, arg);
845 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
846 memset(mask, 0, IFNAMSIZ);
847 else if (vianame[vialen - 1] == '+') {
848 memset(mask, 0xFF, vialen - 1);
849 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
850 /* Don't remove `+' here! -HW */
852 /* Include nul-terminator in match */
853 memset(mask, 0xFF, vialen + 1);
854 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
855 for (i = 0; vianame[i]; i++) {
856 if (!isalnum(vianame[i])
858 && vianame[i] != '.') {
859 printf("Warning: wierd character in interface"
860 " `%s' (No aliases, :, ! or *).\n",
870 parse_rulenumber(const char *rule)
872 unsigned int rulenum;
874 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
875 exit_error(PARAMETER_PROBLEM,
876 "Invalid rule number `%s'", rule);
882 parse_target(const char *targetname)
886 if (strlen(targetname) < 1)
887 exit_error(PARAMETER_PROBLEM,
888 "Invalid target name (too short)");
890 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
891 exit_error(PARAMETER_PROBLEM,
892 "Invalid target name `%s' (%u chars max)",
893 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
895 for (ptr = targetname; *ptr; ptr++)
897 exit_error(PARAMETER_PROBLEM,
898 "Invalid target name `%s'", targetname);
903 string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
904 unsigned long long *ret)
906 unsigned long long number;
909 /* Handle hex, octal, etc. */
911 number = strtoull(s, &end, 0);
912 if (*end == '\0' && end != s) {
913 /* we parsed a number, let's see if we want this */
914 if (errno != ERANGE && min <= number && (!max || number <= max)) {
923 string_to_number_l(const char *s, unsigned long min, unsigned long max,
927 unsigned long long number;
929 result = string_to_number_ll(s, min, max, &number);
930 *ret = (unsigned long)number;
935 int string_to_number(const char *s, unsigned int min, unsigned int max,
939 unsigned long number;
941 result = string_to_number_l(s, min, max, &number);
942 *ret = (unsigned int)number;
948 set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
951 if (*options & option)
952 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
958 for (i = 0; 1 << i != option; i++);
960 if (!inverse_for_options[i])
961 exit_error(PARAMETER_PROBLEM,
962 "cannot have ! before -%c",
964 *invflg |= inverse_for_options[i];
968 struct ip6tables_target *
969 find_target(const char *name, enum ip6t_tryload tryload)
971 struct ip6tables_target *ptr;
973 /* Standard target? */
974 if (strcmp(name, "") == 0
975 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
976 || strcmp(name, IP6TC_LABEL_DROP) == 0
977 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
978 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
981 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
982 if (strcmp(name, ptr->name) == 0)
986 #ifndef NO_SHARED_LIBS
987 if (!ptr && tryload != DONT_LOAD) {
988 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
990 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
991 if (dlopen(path, RTLD_NOW)) {
992 /* Found library. If it didn't register itself,
993 maybe they specified match as a target. */
994 ptr = find_target(name, DONT_LOAD);
996 exit_error(PARAMETER_PROBLEM,
997 "Couldn't load target `%s'\n",
999 } else if (tryload == LOAD_MUST_SUCCEED)
1000 exit_error(PARAMETER_PROBLEM,
1001 "Couldn't load target `%s':%s\n",
1005 if (ptr && !ptr->loaded) {
1006 if (tryload != DONT_LOAD)
1011 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1012 exit_error(PARAMETER_PROBLEM,
1013 "Couldn't find target `%s'\n", name);
1023 static struct option *
1024 merge_options(struct option *oldopts, const struct option *newopts,
1025 unsigned int *option_offset)
1027 unsigned int num_old, num_new, i;
1028 struct option *merge;
1030 for (num_old = 0; oldopts[num_old].name; num_old++);
1031 for (num_new = 0; newopts[num_new].name; num_new++);
1033 global_option_offset += OPTION_OFFSET;
1034 *option_offset = global_option_offset;
1036 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1037 memcpy(merge, oldopts, num_old * sizeof(struct option));
1038 for (i = 0; i < num_new; i++) {
1039 merge[num_old + i] = newopts[i];
1040 merge[num_old + i].val += *option_offset;
1042 memset(merge + num_old + num_new, 0, sizeof(struct option));
1048 register_match6(struct ip6tables_match *me)
1050 struct ip6tables_match **i;
1052 if (strcmp(me->version, program_version) != 0) {
1053 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1054 program_name, me->name, me->version, program_version);
1058 if (find_match(me->name, DONT_LOAD, NULL)) {
1059 fprintf(stderr, "%s: match `%s' already registered.\n",
1060 program_name, me->name);
1064 if (me->size != IP6T_ALIGN(me->size)) {
1065 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1066 program_name, me->name, (unsigned int)me->size);
1070 /* Append to list. */
1071 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1080 register_target6(struct ip6tables_target *me)
1082 if (strcmp(me->version, program_version) != 0) {
1083 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1084 program_name, me->name, me->version, program_version);
1088 if (find_target(me->name, DONT_LOAD)) {
1089 fprintf(stderr, "%s: target `%s' already registered.\n",
1090 program_name, me->name);
1094 if (me->size != IP6T_ALIGN(me->size)) {
1095 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1096 program_name, me->name, (unsigned int)me->size);
1100 /* Prepend to list. */
1101 me->next = ip6tables_targets;
1102 ip6tables_targets = me;
1108 print_num(u_int64_t number, unsigned int format)
1110 if (format & FMT_KILOMEGAGIGA) {
1111 if (number > 99999) {
1112 number = (number + 500) / 1000;
1113 if (number > 9999) {
1114 number = (number + 500) / 1000;
1115 if (number > 9999) {
1116 number = (number + 500) / 1000;
1117 if (number > 9999) {
1118 number = (number + 500) / 1000;
1119 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
1121 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
1123 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
1125 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
1127 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
1129 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
1134 print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1136 struct ip6t_counters counters;
1137 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1138 printf("Chain %s", chain);
1140 printf(" (policy %s", pol);
1141 if (!(format & FMT_NOCOUNTS)) {
1143 print_num(counters.pcnt, (format|FMT_NOTABLE));
1144 fputs("packets, ", stdout);
1145 print_num(counters.bcnt, (format|FMT_NOTABLE));
1146 fputs("bytes", stdout);
1151 if (!ip6tc_get_references(&refs, chain, handle))
1152 printf(" (ERROR obtaining refs)\n");
1154 printf(" (%u references)\n", refs);
1157 if (format & FMT_LINENUMBERS)
1158 printf(FMT("%-4s ", "%s "), "num");
1159 if (!(format & FMT_NOCOUNTS)) {
1160 if (format & FMT_KILOMEGAGIGA) {
1161 printf(FMT("%5s ","%s "), "pkts");
1162 printf(FMT("%5s ","%s "), "bytes");
1164 printf(FMT("%8s ","%s "), "pkts");
1165 printf(FMT("%10s ","%s "), "bytes");
1168 if (!(format & FMT_NOTARGET))
1169 printf(FMT("%-9s ","%s "), "target");
1170 fputs(" prot ", stdout);
1171 if (format & FMT_OPTIONS)
1172 fputs("opt", stdout);
1173 if (format & FMT_VIA) {
1174 printf(FMT(" %-6s ","%s "), "in");
1175 printf(FMT("%-6s ","%s "), "out");
1177 printf(FMT(" %-19s ","%s "), "source");
1178 printf(FMT(" %-19s "," %s "), "destination");
1184 print_match(const struct ip6t_entry_match *m,
1185 const struct ip6t_ip6 *ip,
1188 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
1192 match->print(ip, m, numeric);
1194 printf("%s ", match->name);
1196 if (m->u.user.name[0])
1197 printf("UNKNOWN match `%s' ", m->u.user.name);
1199 /* Don't stop iterating. */
1203 /* e is called `fw' here for hysterical raisins */
1205 print_firewall(const struct ip6t_entry *fw,
1206 const char *targname,
1208 unsigned int format,
1209 const ip6tc_handle_t handle)
1211 struct ip6tables_target *target = NULL;
1212 const struct ip6t_entry_target *t;
1216 if (!ip6tc_is_chain(targname, handle))
1217 target = find_target(targname, TRY_LOAD);
1219 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1221 t = ip6t_get_target((struct ip6t_entry *)fw);
1222 flags = fw->ipv6.flags;
1224 if (format & FMT_LINENUMBERS)
1225 printf(FMT("%-4u ", "%u "), num+1);
1227 if (!(format & FMT_NOCOUNTS)) {
1228 print_num(fw->counters.pcnt, format);
1229 print_num(fw->counters.bcnt, format);
1232 if (!(format & FMT_NOTARGET))
1233 printf(FMT("%-9s ", "%s "), targname);
1235 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1237 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
1239 printf(FMT("%-5s", "%s "), pname);
1241 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1244 if (format & FMT_OPTIONS) {
1245 if (format & FMT_NOTABLE)
1246 fputs("opt ", stdout);
1247 fputc(' ', stdout); /* Invert flag of FRAG */
1248 fputc(' ', stdout); /* -f */
1252 if (format & FMT_VIA) {
1253 char iface[IFNAMSIZ+2];
1255 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1259 else iface[0] = '\0';
1261 if (fw->ipv6.iniface[0] != '\0') {
1262 strcat(iface, fw->ipv6.iniface);
1264 else if (format & FMT_NUMERIC) strcat(iface, "*");
1265 else strcat(iface, "any");
1266 printf(FMT(" %-6s ","in %s "), iface);
1268 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1272 else iface[0] = '\0';
1274 if (fw->ipv6.outiface[0] != '\0') {
1275 strcat(iface, fw->ipv6.outiface);
1277 else if (format & FMT_NUMERIC) strcat(iface, "*");
1278 else strcat(iface, "any");
1279 printf(FMT("%-6s ","out %s "), iface);
1282 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1283 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1284 && !(format & FMT_NUMERIC))
1285 printf(FMT("%-19s ","%s "), "anywhere");
1287 if (format & FMT_NUMERIC)
1288 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
1290 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
1291 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1292 printf(FMT("%-19s ","%s "), buf);
1295 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1296 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1297 && !(format & FMT_NUMERIC))
1298 printf(FMT("%-19s","-> %s"), "anywhere");
1300 if (format & FMT_NUMERIC)
1301 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
1303 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
1304 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1305 printf(FMT("%-19s","-> %s"), buf);
1308 if (format & FMT_NOTABLE)
1311 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1315 /* Print the target information. */
1316 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1317 } else if (t->u.target_size != sizeof(*t))
1318 printf("[%u bytes of unknown target data] ",
1319 (unsigned int)(t->u.target_size - sizeof(*t)));
1321 if (!(format & FMT_NONEWLINE))
1322 fputc('\n', stdout);
1326 print_firewall_line(const struct ip6t_entry *fw,
1327 const ip6tc_handle_t h)
1329 struct ip6t_entry_target *t;
1331 t = ip6t_get_target((struct ip6t_entry *)fw);
1332 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1336 append_entry(const ip6t_chainlabel chain,
1337 struct ip6t_entry *fw,
1338 unsigned int nsaddrs,
1339 const struct in6_addr saddrs[],
1340 unsigned int ndaddrs,
1341 const struct in6_addr daddrs[],
1343 ip6tc_handle_t *handle)
1348 for (i = 0; i < nsaddrs; i++) {
1349 fw->ipv6.src = saddrs[i];
1350 for (j = 0; j < ndaddrs; j++) {
1351 fw->ipv6.dst = daddrs[j];
1353 print_firewall_line(fw, *handle);
1354 ret &= ip6tc_append_entry(chain, fw, handle);
1362 replace_entry(const ip6t_chainlabel chain,
1363 struct ip6t_entry *fw,
1364 unsigned int rulenum,
1365 const struct in6_addr *saddr,
1366 const struct in6_addr *daddr,
1368 ip6tc_handle_t *handle)
1370 fw->ipv6.src = *saddr;
1371 fw->ipv6.dst = *daddr;
1374 print_firewall_line(fw, *handle);
1375 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1379 insert_entry(const ip6t_chainlabel chain,
1380 struct ip6t_entry *fw,
1381 unsigned int rulenum,
1382 unsigned int nsaddrs,
1383 const struct in6_addr saddrs[],
1384 unsigned int ndaddrs,
1385 const struct in6_addr daddrs[],
1387 ip6tc_handle_t *handle)
1392 for (i = 0; i < nsaddrs; i++) {
1393 fw->ipv6.src = saddrs[i];
1394 for (j = 0; j < ndaddrs; j++) {
1395 fw->ipv6.dst = daddrs[j];
1397 print_firewall_line(fw, *handle);
1398 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1405 static unsigned char *
1406 make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
1408 /* Establish mask for comparison */
1410 struct ip6tables_rule_match *matchp;
1411 unsigned char *mask, *mptr;
1413 size = sizeof(struct ip6t_entry);
1414 for (matchp = matches; matchp; matchp = matchp->next)
1415 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1417 mask = fw_calloc(1, size
1418 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1419 + ip6tables_targets->size);
1421 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1422 mptr = mask + sizeof(struct ip6t_entry);
1424 for (matchp = matches; matchp; matchp = matchp->next) {
1426 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1427 + matchp->match->userspacesize);
1428 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1432 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1433 + ip6tables_targets->userspacesize);
1439 delete_entry(const ip6t_chainlabel chain,
1440 struct ip6t_entry *fw,
1441 unsigned int nsaddrs,
1442 const struct in6_addr saddrs[],
1443 unsigned int ndaddrs,
1444 const struct in6_addr daddrs[],
1446 ip6tc_handle_t *handle,
1447 struct ip6tables_rule_match *matches)
1451 unsigned char *mask;
1453 mask = make_delete_mask(fw, matches);
1454 for (i = 0; i < nsaddrs; i++) {
1455 fw->ipv6.src = saddrs[i];
1456 for (j = 0; j < ndaddrs; j++) {
1457 fw->ipv6.dst = daddrs[j];
1459 print_firewall_line(fw, *handle);
1460 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
1469 for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1470 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1475 unsigned int i, chaincount = 0;
1477 chain = ip6tc_first_chain(handle);
1480 chain = ip6tc_next_chain(handle);
1483 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1485 chain = ip6tc_first_chain(handle);
1487 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1489 chain = ip6tc_next_chain(handle);
1492 for (i = 0; i < chaincount; i++) {
1494 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1497 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1505 flush_entries(const ip6t_chainlabel chain, int verbose,
1506 ip6tc_handle_t *handle)
1509 return for_each_chain(flush_entries, verbose, 1, handle);
1512 fprintf(stdout, "Flushing chain `%s'\n", chain);
1513 return ip6tc_flush_entries(chain, handle);
1517 zero_entries(const ip6t_chainlabel chain, int verbose,
1518 ip6tc_handle_t *handle)
1521 return for_each_chain(zero_entries, verbose, 1, handle);
1524 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1525 return ip6tc_zero_entries(chain, handle);
1529 delete_chain(const ip6t_chainlabel chain, int verbose,
1530 ip6tc_handle_t *handle)
1533 return for_each_chain(delete_chain, verbose, 0, handle);
1536 fprintf(stdout, "Deleting chain `%s'\n", chain);
1537 return ip6tc_delete_chain(chain, handle);
1541 list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1542 int expanded, int linenumbers, ip6tc_handle_t *handle)
1545 unsigned int format;
1548 format = FMT_OPTIONS;
1550 format |= FMT_NOCOUNTS;
1555 format |= FMT_NUMERIC;
1558 format |= FMT_KILOMEGAGIGA;
1561 format |= FMT_LINENUMBERS;
1563 for (this = ip6tc_first_chain(handle);
1565 this = ip6tc_next_chain(handle)) {
1566 const struct ip6t_entry *i;
1569 if (chain && strcmp(chain, this) != 0)
1572 if (found) printf("\n");
1574 print_header(format, this, handle);
1575 i = ip6tc_first_rule(this, handle);
1580 ip6tc_get_target(i, handle),
1584 i = ip6tc_next_rule(i, handle);
1593 static char *get_modprobe(void)
1598 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1604 switch (read(procfile, ret, 1024)) {
1606 case 1024: goto fail; /* Partial read. Wierd */
1608 if (ret[strlen(ret)-1]=='\n')
1609 ret[strlen(ret)-1]=0;
1619 int ip6tables_insmod(const char *modname, const char *modprobe)
1624 /* If they don't explicitly set it, read out of kernel */
1626 buf = get_modprobe();
1634 argv[0] = (char *)modprobe;
1635 argv[1] = (char *)modname;
1637 execv(argv[0], argv);
1639 /* not usually reached */
1644 default: /* parent */
1652 static struct ip6t_entry *
1653 generate_entry(const struct ip6t_entry *fw,
1654 struct ip6tables_rule_match *matches,
1655 struct ip6t_entry_target *target)
1658 struct ip6tables_rule_match *matchp;
1659 struct ip6t_entry *e;
1661 size = sizeof(struct ip6t_entry);
1662 for (matchp = matches; matchp; matchp = matchp->next)
1663 size += matchp->match->m->u.match_size;
1665 e = fw_malloc(size + target->u.target_size);
1667 e->target_offset = size;
1668 e->next_offset = size + target->u.target_size;
1671 for (matchp = matches; matchp; matchp = matchp->next) {
1672 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1673 size += matchp->match->m->u.match_size;
1675 memcpy(e->elems + size, target, target->u.target_size);
1680 void clear_rule_matches(struct ip6tables_rule_match **matches)
1682 struct ip6tables_rule_match *matchp, *tmp;
1684 for (matchp = *matches; matchp;) {
1686 if (matchp->match->m)
1687 free(matchp->match->m);
1695 int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1697 struct ip6t_entry fw, *e = NULL;
1699 unsigned int nsaddrs = 0, ndaddrs = 0;
1700 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1703 const char *chain = NULL;
1704 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1705 const char *policy = NULL, *newname = NULL;
1706 unsigned int rulenum = 0, options = 0, command = 0;
1707 const char *pcnt = NULL, *bcnt = NULL;
1709 struct ip6tables_match *m;
1710 struct ip6tables_rule_match *matches = NULL;
1711 struct ip6tables_rule_match *matchp;
1712 struct ip6tables_target *target = NULL;
1713 struct ip6tables_target *t;
1714 const char *jumpto = "";
1715 char *protocol = NULL;
1716 const char *modprobe = NULL;
1718 char icmp6p[] = "icmpv6";
1720 memset(&fw, 0, sizeof(fw));
1722 /* re-set optind to 0 in case do_command gets called
1726 /* clear mflags in case do_command gets called a second time
1727 * (we clear the global list of all matches for security)*/
1728 for (m = ip6tables_matches; m; m = m->next)
1731 for (t = ip6tables_targets; t; t = t->next) {
1736 /* Suppress error messages: we may add new options if we
1737 demand-load a protocol. */
1740 while ((c = getopt_long(argc, argv,
1741 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
1742 opts, NULL)) != -1) {
1748 add_command(&command, CMD_APPEND, CMD_NONE,
1754 add_command(&command, CMD_DELETE, CMD_NONE,
1757 if (optind < argc && argv[optind][0] != '-'
1758 && argv[optind][0] != '!') {
1759 rulenum = parse_rulenumber(argv[optind++]);
1760 command = CMD_DELETE_NUM;
1765 add_command(&command, CMD_REPLACE, CMD_NONE,
1768 if (optind < argc && argv[optind][0] != '-'
1769 && argv[optind][0] != '!')
1770 rulenum = parse_rulenumber(argv[optind++]);
1772 exit_error(PARAMETER_PROBLEM,
1773 "-%c requires a rule number",
1774 cmd2char(CMD_REPLACE));
1778 add_command(&command, CMD_INSERT, CMD_NONE,
1781 if (optind < argc && argv[optind][0] != '-'
1782 && argv[optind][0] != '!')
1783 rulenum = parse_rulenumber(argv[optind++]);
1788 add_command(&command, CMD_LIST, CMD_ZERO,
1790 if (optarg) chain = optarg;
1791 else if (optind < argc && argv[optind][0] != '-'
1792 && argv[optind][0] != '!')
1793 chain = argv[optind++];
1797 add_command(&command, CMD_FLUSH, CMD_NONE,
1799 if (optarg) chain = optarg;
1800 else if (optind < argc && argv[optind][0] != '-'
1801 && argv[optind][0] != '!')
1802 chain = argv[optind++];
1806 add_command(&command, CMD_ZERO, CMD_LIST,
1808 if (optarg) chain = optarg;
1809 else if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 chain = argv[optind++];
1815 if (optarg && *optarg == '-')
1816 exit_error(PARAMETER_PROBLEM,
1817 "chain name not allowed to start "
1819 if (find_target(optarg, TRY_LOAD))
1820 exit_error(PARAMETER_PROBLEM,
1821 "chain name may not clash "
1822 "with target name\n");
1823 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1829 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1831 if (optarg) chain = optarg;
1832 else if (optind < argc && argv[optind][0] != '-'
1833 && argv[optind][0] != '!')
1834 chain = argv[optind++];
1838 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1841 if (optind < argc && argv[optind][0] != '-'
1842 && argv[optind][0] != '!')
1843 newname = argv[optind++];
1845 exit_error(PARAMETER_PROBLEM,
1846 "-%c requires old-chain-name and "
1848 cmd2char(CMD_RENAME_CHAIN));
1852 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1855 if (optind < argc && argv[optind][0] != '-'
1856 && argv[optind][0] != '!')
1857 policy = argv[optind++];
1859 exit_error(PARAMETER_PROBLEM,
1860 "-%c requires a chain and a policy",
1861 cmd2char(CMD_SET_POLICY));
1866 optarg = argv[optind];
1868 /* iptables -p icmp -h */
1869 if (!matches && protocol)
1870 find_match(protocol, TRY_LOAD, &matches);
1872 exit_printhelp(matches);
1878 check_inverse(optarg, &invert, &optind, argc);
1879 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1882 /* Canonicalize into lower case */
1883 for (protocol = argv[optind-1]; *protocol; protocol++)
1884 *protocol = tolower(*protocol);
1886 protocol = argv[optind-1];
1887 if ( strcmp(protocol,"ipv6-icmp") == 0)
1889 fw.ipv6.proto = parse_protocol(protocol);
1890 fw.ipv6.flags |= IP6T_F_PROTO;
1892 if (fw.ipv6.proto == 0
1893 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1894 exit_error(PARAMETER_PROBLEM,
1895 "rule would never match protocol");
1896 fw.nfcache |= NFC_IP6_PROTO;
1900 check_inverse(optarg, &invert, &optind, argc);
1901 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1903 shostnetworkmask = argv[optind-1];
1904 fw.nfcache |= NFC_IP6_SRC;
1908 check_inverse(optarg, &invert, &optind, argc);
1909 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1911 dhostnetworkmask = argv[optind-1];
1912 fw.nfcache |= NFC_IP6_DST;
1916 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1918 jumpto = parse_target(optarg);
1919 /* TRY_LOAD (may be chain name) */
1920 target = find_target(jumpto, TRY_LOAD);
1925 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1928 target->t = fw_calloc(1, size);
1929 target->t->u.target_size = size;
1930 strcpy(target->t->u.user.name, jumpto);
1931 target->init(target->t, &fw.nfcache);
1932 opts = merge_options(opts, target->extra_opts, &target->option_offset);
1938 check_inverse(optarg, &invert, &optind, argc);
1939 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1941 parse_interface(argv[optind-1],
1943 fw.ipv6.iniface_mask);
1944 fw.nfcache |= NFC_IP6_IF_IN;
1948 check_inverse(optarg, &invert, &optind, argc);
1949 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1951 parse_interface(argv[optind-1],
1953 fw.ipv6.outiface_mask);
1954 fw.nfcache |= NFC_IP6_IF_OUT;
1959 set_option(&options, OPT_VERBOSE,
1960 &fw.ipv6.invflags, invert);
1968 exit_error(PARAMETER_PROBLEM,
1969 "unexpected ! flag before --match");
1971 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
1972 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1974 m->m = fw_calloc(1, size);
1975 m->m->u.match_size = size;
1976 strcpy(m->m->u.user.name, m->name);
1977 m->init(m->m, &fw.nfcache);
1978 opts = merge_options(opts, m->extra_opts, &m->option_offset);
1983 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1989 exit_error(PARAMETER_PROBLEM,
1990 "unexpected ! flag before --table");
1991 *table = argv[optind-1];
1995 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2001 printf("Not %s ;-)\n", program_version);
2004 program_name, program_version);
2008 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2018 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2021 if (optind < argc && argv[optind][0] != '-'
2022 && argv[optind][0] != '!')
2023 bcnt = argv[optind++];
2025 exit_error(PARAMETER_PROBLEM,
2026 "-%c requires packet and byte counter",
2027 opt2char(OPT_COUNTERS));
2029 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
2030 exit_error(PARAMETER_PROBLEM,
2031 "-%c packet counter not numeric",
2032 opt2char(OPT_COUNTERS));
2034 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
2035 exit_error(PARAMETER_PROBLEM,
2036 "-%c byte counter not numeric",
2037 opt2char(OPT_COUNTERS));
2042 case 1: /* non option */
2043 if (optarg[0] == '!' && optarg[1] == '\0') {
2045 exit_error(PARAMETER_PROBLEM,
2046 "multiple consecutive ! not"
2052 printf("Bad argument `%s'\n", optarg);
2056 /* FIXME: This scheme doesn't allow two of the same
2059 || !(target->parse(c - target->option_offset,
2062 &fw, &target->t))) {
2063 for (matchp = matches; matchp; matchp = matchp->next) {
2064 if (matchp->match->parse(c - matchp->match->option_offset,
2066 &matchp->match->mflags,
2072 m = matchp ? matchp->match : NULL;
2074 /* If you listen carefully, you can
2075 actually hear this code suck. */
2077 /* some explanations (after four different bugs
2078 * in 3 different releases): If we encountere a
2079 * parameter, that has not been parsed yet,
2080 * it's not an option of an explicitly loaded
2081 * match or a target. However, we support
2082 * implicit loading of the protocol match
2083 * extension. '-p tcp' means 'l4 proto 6' and
2084 * at the same time 'load tcp protocol match on
2085 * demand if we specify --dport'.
2087 * To make this work, we need to make sure:
2088 * - the parameter has not been parsed by
2090 * - a protocol has been specified
2091 * - the protocol extension has not been
2092 * loaded yet, or is loaded and unused
2093 * [think of iptables-restore!]
2094 * - the protocol extension can be successively
2099 && (!find_proto(protocol, DONT_LOAD,
2100 options&OPT_NUMERIC, NULL)
2101 || (find_proto(protocol, DONT_LOAD,
2102 options&OPT_NUMERIC, NULL)
2103 && (proto_used == 0))
2105 && (m = find_proto(protocol, TRY_LOAD,
2106 options&OPT_NUMERIC, &matches))) {
2107 /* Try loading protocol */
2112 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2115 m->m = fw_calloc(1, size);
2116 m->m->u.match_size = size;
2117 strcpy(m->m->u.user.name, m->name);
2118 m->init(m->m, &fw.nfcache);
2120 opts = merge_options(opts,
2121 m->extra_opts, &m->option_offset);
2128 exit_error(PARAMETER_PROBLEM,
2136 for (matchp = matches; matchp; matchp = matchp->next)
2137 matchp->match->final_check(matchp->match->mflags);
2140 target->final_check(target->tflags);
2142 /* Fix me: must put inverse options checking here --MN */
2145 exit_error(PARAMETER_PROBLEM,
2146 "unknown arguments found on commandline");
2148 exit_error(PARAMETER_PROBLEM, "no command specified");
2150 exit_error(PARAMETER_PROBLEM,
2151 "nothing appropriate following !");
2153 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
2154 if (!(options & OPT_DESTINATION))
2155 dhostnetworkmask = "::0/0";
2156 if (!(options & OPT_SOURCE))
2157 shostnetworkmask = "::0/0";
2160 if (shostnetworkmask)
2161 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2162 &(fw.ipv6.smsk), &nsaddrs);
2164 if (dhostnetworkmask)
2165 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2166 &(fw.ipv6.dmsk), &ndaddrs);
2168 if ((nsaddrs > 1 || ndaddrs > 1) &&
2169 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2170 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2171 " source or destination IP addresses");
2173 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2174 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2175 "specify a unique address");
2177 generic_opt_check(command, options);
2179 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2180 exit_error(PARAMETER_PROBLEM,
2181 "chain name `%s' too long (must be under %i chars)",
2182 chain, IP6T_FUNCTION_MAXNAMELEN);
2184 /* only allocate handle if we weren't called with a handle */
2186 *handle = ip6tc_init(*table);
2189 /* try to insmod the module if iptc_init failed */
2190 ip6tables_insmod("ip6_tables", modprobe);
2191 *handle = ip6tc_init(*table);
2195 exit_error(VERSION_PROBLEM,
2196 "can't initialize ip6tables table `%s': %s",
2197 *table, ip6tc_strerror(errno));
2199 if (command == CMD_APPEND
2200 || command == CMD_DELETE
2201 || command == CMD_INSERT
2202 || command == CMD_REPLACE) {
2203 if (strcmp(chain, "PREROUTING") == 0
2204 || strcmp(chain, "INPUT") == 0) {
2205 /* -o not valid with incoming packets. */
2206 if (options & OPT_VIANAMEOUT)
2207 exit_error(PARAMETER_PROBLEM,
2208 "Can't use -%c with %s\n",
2209 opt2char(OPT_VIANAMEOUT),
2213 if (strcmp(chain, "POSTROUTING") == 0
2214 || strcmp(chain, "OUTPUT") == 0) {
2215 /* -i not valid with outgoing packets */
2216 if (options & OPT_VIANAMEIN)
2217 exit_error(PARAMETER_PROBLEM,
2218 "Can't use -%c with %s\n",
2219 opt2char(OPT_VIANAMEIN),
2223 if (target && ip6tc_is_chain(jumpto, *handle)) {
2224 printf("Warning: using chain %s, not extension\n",
2233 /* If they didn't specify a target, or it's a chain
2234 name, use standard. */
2236 && (strlen(jumpto) == 0
2237 || ip6tc_is_chain(jumpto, *handle))) {
2240 target = find_target(IP6T_STANDARD_TARGET,
2243 size = sizeof(struct ip6t_entry_target)
2245 target->t = fw_calloc(1, size);
2246 target->t->u.target_size = size;
2247 strcpy(target->t->u.user.name, jumpto);
2248 target->init(target->t, &fw.nfcache);
2252 /* it is no chain, and we can't load a plugin.
2253 * We cannot know if the plugin is corrupt, non
2254 * existant OR if the user just misspelled a
2256 find_target(jumpto, LOAD_MUST_SUCCEED);
2258 e = generate_entry(&fw, matches, target->t);
2265 ret = append_entry(chain, e,
2266 nsaddrs, saddrs, ndaddrs, daddrs,
2267 options&OPT_VERBOSE,
2271 ret = delete_entry(chain, e,
2272 nsaddrs, saddrs, ndaddrs, daddrs,
2273 options&OPT_VERBOSE,
2276 case CMD_DELETE_NUM:
2277 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2280 ret = replace_entry(chain, e, rulenum - 1,
2281 saddrs, daddrs, options&OPT_VERBOSE,
2285 ret = insert_entry(chain, e, rulenum - 1,
2286 nsaddrs, saddrs, ndaddrs, daddrs,
2287 options&OPT_VERBOSE,
2291 ret = list_entries(chain,
2292 options&OPT_VERBOSE,
2293 options&OPT_NUMERIC,
2294 options&OPT_EXPANDED,
2295 options&OPT_LINENUMBERS,
2299 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2302 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2304 case CMD_LIST|CMD_ZERO:
2305 ret = list_entries(chain,
2306 options&OPT_VERBOSE,
2307 options&OPT_NUMERIC,
2308 options&OPT_EXPANDED,
2309 options&OPT_LINENUMBERS,
2312 ret = zero_entries(chain,
2313 options&OPT_VERBOSE, handle);
2316 ret = ip6tc_create_chain(chain, handle);
2318 case CMD_DELETE_CHAIN:
2319 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2321 case CMD_RENAME_CHAIN:
2322 ret = ip6tc_rename_chain(chain, newname, handle);
2324 case CMD_SET_POLICY:
2325 ret = ip6tc_set_policy(chain, policy, NULL, handle);
2328 /* We should never reach this... */
2333 dump_entries6(*handle);
2335 clear_rule_matches(&matches);
2342 for (c = 0; c < nsaddrs; c++)
2345 for (c = 0; c < ndaddrs; c++)
2348 if (opts != original_opts) {
2350 opts = original_opts;
2351 global_option_offset = 0;