Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / ip6tables.c
blob1fdbd4590ef7a2e229101937d550650a410b9cdc
1 /* Code to take an ip6tables-style command line and do it. */
3 /*
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.
28 #include <getopt.h>
29 #include <string.h>
30 #include <netdb.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <dlfcn.h>
35 #include <ctype.h>
36 #include <stdarg.h>
37 #include <limits.h>
38 #include <ip6tables.h>
39 #include <arpa/inet.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <sys/wait.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #ifndef TRUE
47 #define TRUE 1
48 #endif
49 #ifndef FALSE
50 #define FALSE 0
51 #endif
53 #ifndef PROC_SYS_MODPROBE
54 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55 #endif
57 #define FMT_NUMERIC 0x0001
58 #define FMT_NOCOUNTS 0x0002
59 #define FMT_KILOMEGAGIGA 0x0004
60 #define FMT_OPTIONS 0x0008
61 #define FMT_NOTABLE 0x0010
62 #define FMT_NOTARGET 0x0020
63 #define FMT_VIA 0x0040
64 #define FMT_NONEWLINE 0x0080
65 #define FMT_LINENUMBERS 0x0100
67 #define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
68 | FMT_NUMERIC | FMT_NOTABLE)
69 #define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
72 #define CMD_NONE 0x0000U
73 #define CMD_INSERT 0x0001U
74 #define CMD_DELETE 0x0002U
75 #define CMD_DELETE_NUM 0x0004U
76 #define CMD_REPLACE 0x0008U
77 #define CMD_APPEND 0x0010U
78 #define CMD_LIST 0x0020U
79 #define CMD_FLUSH 0x0040U
80 #define CMD_ZERO 0x0080U
81 #define CMD_NEW_CHAIN 0x0100U
82 #define CMD_DELETE_CHAIN 0x0200U
83 #define CMD_SET_POLICY 0x0400U
84 #define CMD_RENAME_CHAIN 0x0800U
85 #define NUMBER_OF_CMD 13
86 static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
87 'N', 'X', 'P', 'E' };
89 #define OPTION_OFFSET 256
91 #define OPT_NONE 0x00000U
92 #define OPT_NUMERIC 0x00001U
93 #define OPT_SOURCE 0x00002U
94 #define OPT_DESTINATION 0x00004U
95 #define OPT_PROTOCOL 0x00008U
96 #define OPT_JUMP 0x00010U
97 #define OPT_VERBOSE 0x00020U
98 #define OPT_EXPANDED 0x00040U
99 #define OPT_VIANAMEIN 0x00080U
100 #define OPT_VIANAMEOUT 0x00100U
101 #define OPT_LINENUMBERS 0x00200U
102 #define OPT_COUNTERS 0x00400U
103 #define NUMBER_OF_OPT 11
104 static const char optflags[NUMBER_OF_OPT]
105 = { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
107 static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
117 { "rename-chain", 1, 0, 'E' },
118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
123 { "protocol", 1, 0, 'p' },
124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
135 { "modprobe", 1, 0, 'M' },
136 { "set-counters", 1, 0, 'c' },
137 { "goto", 1, 0, 'g' },
138 { 0 }
141 /* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. ip6tables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145 int line = -1;
147 static struct option *opts = original_opts;
148 static unsigned int global_option_offset = 0;
150 /* Table of legal combinations of commands and options. If any of the
151 * given commands make an option legal, that option is legal (applies to
152 * CMD_LIST and CMD_ZERO only).
153 * Key:
154 * + compulsory
155 * x illegal
156 * optional
159 static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160 /* Well, it's better than "Re: Linux vs FreeBSD" */
162 /* -n -s -d -p -j -v -x -i -o --line -c */
163 /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
164 /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
165 /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
166 /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167 /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
168 /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
169 /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170 /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171 /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172 /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
173 /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
174 /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
177 static int inverse_for_options[NUMBER_OF_OPT] =
179 /* -n */ 0,
180 /* -s */ IP6T_INV_SRCIP,
181 /* -d */ IP6T_INV_DSTIP,
182 /* -p */ IP6T_INV_PROTO,
183 /* -j */ 0,
184 /* -v */ 0,
185 /* -x */ 0,
186 /* -i */ IP6T_INV_VIA_IN,
187 /* -o */ IP6T_INV_VIA_OUT,
188 /*--line*/ 0,
189 /* -c */ 0,
192 const char *program_version;
193 const char *program_name;
194 char *lib_dir;
196 /* the path to command to load kernel module */
197 const char *modprobe = NULL;
199 /* Keeping track of external matches and targets: linked lists. */
200 struct ip6tables_match *ip6tables_matches = NULL;
201 struct ip6tables_target *ip6tables_targets = NULL;
203 /* Extra debugging from libiptc */
204 extern void dump_entries6(const ip6tc_handle_t handle);
206 /* A few hardcoded protocols for 'all' and in case the user has no
207 /etc/protocols */
208 struct pprot {
209 char *name;
210 u_int8_t num;
213 /* Primitive headers... */
214 /* defined in netinet/in.h */
215 #if 0
216 #ifndef IPPROTO_ESP
217 #define IPPROTO_ESP 50
218 #endif
219 #ifndef IPPROTO_AH
220 #define IPPROTO_AH 51
221 #endif
222 #endif
223 #ifndef IPPROTO_MH
224 #define IPPROTO_MH 135
225 #endif
227 static const struct pprot chain_protos[] = {
228 { "tcp", IPPROTO_TCP },
229 { "udp", IPPROTO_UDP },
230 { "udplite", IPPROTO_UDPLITE },
231 { "icmpv6", IPPROTO_ICMPV6 },
232 { "ipv6-icmp", IPPROTO_ICMPV6 },
233 { "esp", IPPROTO_ESP },
234 { "ah", IPPROTO_AH },
235 { "ipv6-mh", IPPROTO_MH },
236 { "mh", IPPROTO_MH },
237 { "all", 0 },
240 static char *
241 proto_to_name(u_int8_t proto, int nolookup)
243 unsigned int i;
245 if (proto && !nolookup) {
246 struct protoent *pent = getprotobynumber(proto);
247 if (pent)
248 return pent->p_name;
251 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
252 if (chain_protos[i].num == proto)
253 return chain_protos[i].name;
255 return NULL;
259 service_to_port(const char *name, const char *proto)
261 struct servent *service;
263 if ((service = getservbyname(name, proto)) != NULL)
264 return ntohs((unsigned short) service->s_port);
266 return -1;
269 u_int16_t
270 parse_port(const char *port, const char *proto)
272 unsigned int portnum;
274 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
275 (portnum = service_to_port(port, proto)) != -1)
276 return (u_int16_t)portnum;
278 exit_error(PARAMETER_PROBLEM,
279 "invalid port/service `%s' specified", port);
282 static void
283 in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
285 memcpy(dst, src, sizeof(struct in6_addr));
286 /* dst->s6_addr = src->s6_addr; */
289 static void free_opts(int reset_offset)
291 if (opts != original_opts) {
292 free(opts);
293 opts = original_opts;
294 if (reset_offset)
295 global_option_offset = 0;
299 void
300 exit_error(enum exittype status, char *msg, ...)
302 va_list args;
304 va_start(args, msg);
305 fprintf(stderr, "%s v%s: ", program_name, program_version);
306 vfprintf(stderr, msg, args);
307 va_end(args);
308 fprintf(stderr, "\n");
309 if (status == PARAMETER_PROBLEM)
310 exit_tryhelp(status);
311 if (status == VERSION_PROBLEM)
312 fprintf(stderr,
313 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
314 /* On error paths, make sure that we don't leak memory */
315 free_opts(1);
316 exit(status);
319 void
320 exit_tryhelp(int status)
322 if (line != -1)
323 fprintf(stderr, "Error occurred at line: %d\n", line);
324 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
325 program_name, program_name );
326 free_opts(1);
327 exit(status);
330 void
331 exit_printhelp(struct ip6tables_rule_match *matches)
333 struct ip6tables_rule_match *matchp = NULL;
334 struct ip6tables_target *t = NULL;
336 printf("%s v%s\n\n"
337 "Usage: %s -[AD] chain rule-specification [options]\n"
338 " %s -[RI] chain rulenum rule-specification [options]\n"
339 " %s -D chain rulenum [options]\n"
340 " %s -[LFZ] [chain] [options]\n"
341 " %s -[NX] chain\n"
342 " %s -E old-chain-name new-chain-name\n"
343 " %s -P chain target [options]\n"
344 " %s -h (print this help information)\n\n",
345 program_name, program_version, program_name, program_name,
346 program_name, program_name, program_name, program_name,
347 program_name, program_name);
349 printf(
350 "Commands:\n"
351 "Either long or short options are allowed.\n"
352 " --append -A chain Append to chain\n"
353 " --delete -D chain Delete matching rule from chain\n"
354 " --delete -D chain rulenum\n"
355 " Delete rule rulenum (1 = first) from chain\n"
356 " --insert -I chain [rulenum]\n"
357 " Insert in chain as rulenum (default 1=first)\n"
358 " --replace -R chain rulenum\n"
359 " Replace rule rulenum (1 = first) in chain\n"
360 " --list -L [chain] List the rules in a chain or all chains\n"
361 " --flush -F [chain] Delete all rules in chain or all chains\n"
362 " --zero -Z [chain] Zero counters in chain or all chains\n"
363 " --new -N chain Create a new user-defined chain\n"
364 " --delete-chain\n"
365 " -X [chain] Delete a user-defined chain\n"
366 " --policy -P chain target\n"
367 " Change policy on chain to target\n"
368 " --rename-chain\n"
369 " -E old-chain new-chain\n"
370 " Change chain name, (moving any references)\n"
372 "Options:\n"
373 " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
374 " --source -s [!] address[/mask]\n"
375 " source specification\n"
376 " --destination -d [!] address[/mask]\n"
377 " destination specification\n"
378 " --in-interface -i [!] input name[+]\n"
379 " network interface name ([+] for wildcard)\n"
380 " --jump -j target\n"
381 " target for rule (may load target extension)\n"
382 #ifdef IP6T_F_GOTO
383 " --goto -g chain\n"
384 " jump to chain with no return\n"
385 #endif
386 " --match -m match\n"
387 " extended match (may load extension)\n"
388 " --numeric -n numeric output of addresses and ports\n"
389 " --out-interface -o [!] output name[+]\n"
390 " network interface name ([+] for wildcard)\n"
391 " --table -t table table to manipulate (default: `filter')\n"
392 " --verbose -v verbose mode\n"
393 " --line-numbers print line numbers when listing\n"
394 " --exact -x expand numbers (display exact values)\n"
395 /*"[!] --fragment -f match second or further fragments only\n"*/
396 " --modprobe=<command> try to insert modules using this command\n"
397 " --set-counters PKTS BYTES set the counter during insert/append\n"
398 "[!] --version -V print package version.\n");
400 /* Print out any special helps. A user might like to be able to add a --help
401 to the commandline, and see expected results. So we call help for all
402 specified matches & targets */
403 for (t = ip6tables_targets; t; t = t->next) {
404 if (t->used) {
405 printf("\n");
406 t->help();
409 for (matchp = matches; matchp; matchp = matchp->next) {
410 printf("\n");
411 matchp->match->help();
413 exit(0);
416 static void
417 generic_opt_check(int command, int options)
419 int i, j, legal = 0;
421 /* Check that commands are valid with options. Complicated by the
422 * fact that if an option is legal with *any* command given, it is
423 * legal overall (ie. -z and -l).
425 for (i = 0; i < NUMBER_OF_OPT; i++) {
426 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
428 for (j = 0; j < NUMBER_OF_CMD; j++) {
429 if (!(command & (1<<j)))
430 continue;
432 if (!(options & (1<<i))) {
433 if (commands_v_options[j][i] == '+')
434 exit_error(PARAMETER_PROBLEM,
435 "You need to supply the `-%c' "
436 "option for this command\n",
437 optflags[i]);
438 } else {
439 if (commands_v_options[j][i] != 'x')
440 legal = 1;
441 else if (legal == 0)
442 legal = -1;
445 if (legal == -1)
446 exit_error(PARAMETER_PROBLEM,
447 "Illegal option `-%c' with this command\n",
448 optflags[i]);
452 static char
453 opt2char(int option)
455 const char *ptr;
456 for (ptr = optflags; option > 1; option >>= 1, ptr++);
458 return *ptr;
461 static char
462 cmd2char(int option)
464 const char *ptr;
465 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
467 return *ptr;
470 static void
471 add_command(unsigned int *cmd, const int newcmd, const int othercmds,
472 int invert)
474 if (invert)
475 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
476 if (*cmd & (~othercmds))
477 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
478 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
479 *cmd |= newcmd;
483 check_inverse(const char option[], int *invert, int *optind, int argc)
485 if (option && strcmp(option, "!") == 0) {
486 if (*invert)
487 exit_error(PARAMETER_PROBLEM,
488 "Multiple `!' flags not allowed");
489 *invert = TRUE;
490 if (optind) {
491 *optind = *optind+1;
492 if (argc && *optind > argc)
493 exit_error(PARAMETER_PROBLEM,
494 "no argument following `!'");
497 return TRUE;
499 return FALSE;
502 static void *
503 fw_calloc(size_t count, size_t size)
505 void *p;
507 if ((p = calloc(count, size)) == NULL) {
508 perror("ip6tables: calloc failed");
509 exit(1);
511 return p;
514 static void *
515 fw_malloc(size_t size)
517 void *p;
519 if ((p = malloc(size)) == NULL) {
520 perror("ip6tables: malloc failed");
521 exit(1);
523 return p;
526 static char *
527 addr_to_numeric(const struct in6_addr *addrp)
529 /* 0000:0000:0000:0000:0000:000.000.000.000
530 * 0000:0000:0000:0000:0000:0000:0000:0000 */
531 static char buf[50+1];
532 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
535 static struct in6_addr *
536 numeric_to_addr(const char *num)
538 static struct in6_addr ap;
539 int err;
540 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
541 return &ap;
542 #ifdef DEBUG
543 fprintf(stderr, "\nnumeric2addr: %d\n", err);
544 #endif
545 return (struct in6_addr *)NULL;
549 static struct in6_addr *
550 host_to_addr(const char *name, unsigned int *naddr)
552 struct addrinfo hints;
553 struct addrinfo *res;
554 struct addrinfo *p;
555 struct in6_addr *addr;
556 int err;
557 unsigned int i;
559 memset(&hints, 0, sizeof(hints));
560 hints.ai_flags=AI_CANONNAME;
561 hints.ai_family=AF_INET6;
562 hints.ai_socktype=SOCK_RAW;
564 *naddr = 0;
565 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
566 #ifdef DEBUG
567 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
568 #endif
569 return (struct in6_addr *) NULL;
570 } else {
571 /* Find length of address-chain */
572 for(p = res; p != NULL; p = p->ai_next)
573 (*naddr)++;
574 #ifdef DEBUG
575 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
576 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
577 #endif
578 addr = fw_calloc(*naddr, sizeof(struct in6_addr));
579 i = 0;
580 for(p = res; p != NULL; p = p->ai_next)
581 in6addrcpy(&(addr[i++]), (struct in6_addr *)
582 &((struct sockaddr_in6 *)p->ai_addr)->sin6_addr);
583 freeaddrinfo(res);
584 return addr;
587 return (struct in6_addr *) NULL;
590 static char *
591 addr_to_host(const struct in6_addr *addr)
593 struct sockaddr_in6 saddr;
594 int err;
595 static char hostname[NI_MAXHOST];
597 memset(&saddr, 0, sizeof(struct sockaddr_in6));
598 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
599 saddr.sin6_family = AF_INET6;
601 if ( (err=getnameinfo((struct sockaddr *)&saddr,
602 sizeof(struct sockaddr_in6),
603 hostname, sizeof(hostname)-1,
604 NULL, 0, 0)) != 0 ){
605 #ifdef DEBUG
606 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
607 #endif
608 return (char *) NULL;
609 } else {
610 #ifdef DEBUG
611 fprintf (stderr, "\naddr2host: %s\n", hostname);
612 #endif
614 return hostname;
617 return (char *) NULL;
620 static char *
621 mask_to_numeric(const struct in6_addr *addrp)
623 static char buf[50+2];
624 int l = ipv6_prefix_length(addrp);
625 if (l == -1) {
626 strcpy(buf, "/");
627 strcat(buf, addr_to_numeric(addrp));
628 return buf;
630 sprintf(buf, "/%d", l);
631 return buf;
634 static struct in6_addr *
635 network_to_addr(const char *name)
637 /* abort();*/
638 /* TODO: not implemented yet, but the exception breaks the
639 * name resolvation */
640 return (struct in6_addr *)NULL;
643 static char *
644 addr_to_anyname(const struct in6_addr *addr)
646 char *name;
648 if ((name = addr_to_host(addr)) != NULL)
649 return name;
651 return addr_to_numeric(addr);
655 * All functions starting with "parse" should succeed, otherwise
656 * the program fails.
657 * Most routines return pointers to static data that may change
658 * between calls to the same or other routines with a few exceptions:
659 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
660 * return global static data.
663 static struct in6_addr *
664 parse_hostnetwork(const char *name, unsigned int *naddrs)
666 struct in6_addr *addrp, *addrptmp;
668 if ((addrptmp = numeric_to_addr(name)) != NULL ||
669 (addrptmp = network_to_addr(name)) != NULL) {
670 addrp = fw_malloc(sizeof(struct in6_addr));
671 in6addrcpy(addrp, addrptmp);
672 *naddrs = 1;
673 return addrp;
675 if ((addrp = host_to_addr(name, naddrs)) != NULL)
676 return addrp;
678 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
681 static struct in6_addr *
682 parse_mask(char *mask)
684 static struct in6_addr maskaddr;
685 struct in6_addr *addrp;
686 unsigned int bits;
688 if (mask == NULL) {
689 /* no mask at all defaults to 128 bits */
690 memset(&maskaddr, 0xff, sizeof maskaddr);
691 return &maskaddr;
693 if ((addrp = numeric_to_addr(mask)) != NULL)
694 return addrp;
695 if (string_to_number(mask, 0, 128, &bits) == -1)
696 exit_error(PARAMETER_PROBLEM,
697 "invalid mask `%s' specified", mask);
698 if (bits != 0) {
699 char *p = (char *)&maskaddr;
700 memset(p, 0xff, bits / 8);
701 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
702 p[bits / 8] = 0xff << (8 - (bits & 7));
703 return &maskaddr;
706 memset(&maskaddr, 0, sizeof maskaddr);
707 return &maskaddr;
710 void
711 parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
712 struct in6_addr *maskp, unsigned int *naddrs)
714 struct in6_addr *addrp;
715 char buf[256];
716 char *p;
717 int i, j, n;
719 strncpy(buf, name, sizeof(buf) - 1);
720 buf[sizeof(buf) - 1] = '\0';
721 if ((p = strrchr(buf, '/')) != NULL) {
722 *p = '\0';
723 addrp = parse_mask(p + 1);
724 } else
725 addrp = parse_mask(NULL);
726 in6addrcpy(maskp, addrp);
728 /* if a null mask is given, the name is ignored, like in "any/0" */
729 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
730 strcpy(buf, "::");
732 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
733 n = *naddrs;
734 for (i = 0, j = 0; i < n; i++) {
735 int k;
736 for (k = 0; k < 4; k++)
737 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
738 j++;
739 for (k = 0; k < j - 1; k++) {
740 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
741 in6addrcpy( &addrp[--j], &addrp[--(*naddrs)] );
742 break;
748 struct ip6tables_match *
749 find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
751 struct ip6tables_match *ptr;
752 const char *icmp6 = "icmp6";
753 const char *name;
755 /* This is ugly as hell. Nonetheless, there is no way of changing
756 * this without hurting backwards compatibility */
757 if ( (strcmp(match_name,"icmpv6") == 0) ||
758 (strcmp(match_name,"ipv6-icmp") == 0) ||
759 (strcmp(match_name,"icmp6") == 0) )
760 name = icmp6;
761 else
762 name = match_name;
764 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
765 if (strcmp(name, ptr->name) == 0) {
766 struct ip6tables_match *clone;
768 /* First match of this type: */
769 if (ptr->m == NULL)
770 break;
772 /* Second and subsequent clones */
773 clone = fw_malloc(sizeof(struct ip6tables_match));
774 memcpy(clone, ptr, sizeof(struct ip6tables_match));
775 clone->mflags = 0;
776 /* This is a clone: */
777 clone->next = clone;
779 ptr = clone;
780 break;
784 #ifndef NO_SHARED_LIBS
785 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
786 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
787 + strlen(name)];
788 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
789 if (dlopen(path, RTLD_NOW)) {
790 /* Found library. If it didn't register itself,
791 maybe they specified target as match. */
792 ptr = find_match(name, DONT_LOAD, NULL);
794 if (!ptr)
795 exit_error(PARAMETER_PROBLEM,
796 "Couldn't load match `%s'\n",
797 name);
798 } else if (tryload == LOAD_MUST_SUCCEED)
799 exit_error(PARAMETER_PROBLEM,
800 "Couldn't load match `%s':%s\n",
801 name, dlerror());
803 #else
804 if (ptr && !ptr->loaded) {
805 if (tryload != DONT_LOAD)
806 ptr->loaded = 1;
807 else
808 ptr = NULL;
810 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
811 exit_error(PARAMETER_PROBLEM,
812 "Couldn't find match `%s'\n", name);
814 #endif
816 if (ptr && matches) {
817 struct ip6tables_rule_match **i;
818 struct ip6tables_rule_match *newentry;
820 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
822 for (i = matches; *i; i = &(*i)->next) {
823 if (strcmp(name, (*i)->match->name) == 0)
824 (*i)->completed = 1;
826 newentry->match = ptr;
827 newentry->completed = 0;
828 newentry->next = NULL;
829 *i = newentry;
832 return ptr;
835 /* Christophe Burki wants `-p 6' to imply `-m tcp'. */
836 static struct ip6tables_match *
837 find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
839 unsigned int proto;
841 if (string_to_number(pname, 0, 255, &proto) != -1) {
842 char *protoname = proto_to_name(proto, nolookup);
844 if (protoname)
845 return find_match(protoname, tryload, matches);
846 } else
847 return find_match(pname, tryload, matches);
849 return NULL;
852 u_int16_t
853 parse_protocol(const char *s)
855 unsigned int proto;
857 if (string_to_number(s, 0, 255, &proto) == -1) {
858 struct protoent *pent;
860 /* first deal with the special case of 'all' to prevent
861 * people from being able to redefine 'all' in nsswitch
862 * and/or provoke expensive [not working] ldap/nis/...
863 * lookups */
864 if (!strcmp(s, "all"))
865 return 0;
867 if ((pent = getprotobyname(s)))
868 proto = pent->p_proto;
869 else {
870 unsigned int i;
871 for (i = 0;
872 i < sizeof(chain_protos)/sizeof(struct pprot);
873 i++) {
874 if (chain_protos[i].name == NULL)
875 continue;
877 if (strcmp(s, chain_protos[i].name) == 0) {
878 proto = chain_protos[i].num;
879 break;
882 if (i == sizeof(chain_protos)/sizeof(struct pprot))
883 exit_error(PARAMETER_PROBLEM,
884 "unknown protocol `%s' specified",
889 return (u_int16_t)proto;
892 /* These are invalid numbers as upper layer protocol */
893 static int is_exthdr(u_int16_t proto)
895 return (proto == IPPROTO_ROUTING ||
896 proto == IPPROTO_FRAGMENT ||
897 proto == IPPROTO_AH ||
898 proto == IPPROTO_DSTOPTS);
901 void parse_interface(const char *arg, char *vianame, unsigned char *mask)
903 int vialen = strlen(arg);
904 unsigned int i;
906 memset(mask, 0, IFNAMSIZ);
907 memset(vianame, 0, IFNAMSIZ);
909 if (vialen + 1 > IFNAMSIZ)
910 exit_error(PARAMETER_PROBLEM,
911 "interface name `%s' must be shorter than IFNAMSIZ"
912 " (%i)", arg, IFNAMSIZ-1);
914 strcpy(vianame, arg);
915 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
916 memset(mask, 0, IFNAMSIZ);
917 else if (vianame[vialen - 1] == '+') {
918 memset(mask, 0xFF, vialen - 1);
919 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
920 /* Don't remove `+' here! -HW */
921 } else {
922 /* Include nul-terminator in match */
923 memset(mask, 0xFF, vialen + 1);
924 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
925 for (i = 0; vianame[i]; i++) {
926 if (vianame[i] == ':' ||
927 vianame[i] == '!' ||
928 vianame[i] == '*') {
929 printf("Warning: weird character in interface"
930 " `%s' (No aliases, :, ! or *).\n",
931 vianame);
932 break;
938 /* Can't be zero. */
939 static int
940 parse_rulenumber(const char *rule)
942 unsigned int rulenum;
944 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
945 exit_error(PARAMETER_PROBLEM,
946 "Invalid rule number `%s'", rule);
948 return rulenum;
951 static const char *
952 parse_target(const char *targetname)
954 const char *ptr;
956 if (strlen(targetname) < 1)
957 exit_error(PARAMETER_PROBLEM,
958 "Invalid target name (too short)");
960 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
961 exit_error(PARAMETER_PROBLEM,
962 "Invalid target name `%s' (%u chars max)",
963 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
965 for (ptr = targetname; *ptr; ptr++)
966 if (isspace(*ptr))
967 exit_error(PARAMETER_PROBLEM,
968 "Invalid target name `%s'", targetname);
969 return targetname;
973 string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
974 unsigned long long *ret)
976 unsigned long long number;
977 char *end;
979 /* Handle hex, octal, etc. */
980 errno = 0;
981 number = strtoull(s, &end, 0);
982 if (*end == '\0' && end != s) {
983 /* we parsed a number, let's see if we want this */
984 if (errno != ERANGE && min <= number && (!max || number <= max)) {
985 *ret = number;
986 return 0;
989 return -1;
993 string_to_number_l(const char *s, unsigned long min, unsigned long max,
994 unsigned long *ret)
996 int result;
997 unsigned long long number;
999 result = string_to_number_ll(s, min, max, &number);
1000 *ret = (unsigned long)number;
1002 return result;
1005 int string_to_number(const char *s, unsigned int min, unsigned int max,
1006 unsigned int *ret)
1008 int result;
1009 unsigned long number;
1011 result = string_to_number_l(s, min, max, &number);
1012 *ret = (unsigned int)number;
1014 return result;
1017 static void
1018 set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1019 int invert)
1021 if (*options & option)
1022 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1023 opt2char(option));
1024 *options |= option;
1026 if (invert) {
1027 unsigned int i;
1028 for (i = 0; 1 << i != option; i++);
1030 if (!inverse_for_options[i])
1031 exit_error(PARAMETER_PROBLEM,
1032 "cannot have ! before -%c",
1033 opt2char(option));
1034 *invflg |= inverse_for_options[i];
1038 struct ip6tables_target *
1039 find_target(const char *name, enum ip6t_tryload tryload)
1041 struct ip6tables_target *ptr;
1043 /* Standard target? */
1044 if (strcmp(name, "") == 0
1045 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1046 || strcmp(name, IP6TC_LABEL_DROP) == 0
1047 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1048 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1049 name = "standard";
1051 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1052 if (strcmp(name, ptr->name) == 0)
1053 break;
1056 #ifndef NO_SHARED_LIBS
1057 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
1058 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
1059 + strlen(name)];
1060 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
1061 if (dlopen(path, RTLD_NOW)) {
1062 /* Found library. If it didn't register itself,
1063 maybe they specified match as a target. */
1064 ptr = find_target(name, DONT_LOAD);
1065 if (!ptr)
1066 exit_error(PARAMETER_PROBLEM,
1067 "Couldn't load target `%s'\n",
1068 name);
1069 } else if (tryload == LOAD_MUST_SUCCEED)
1070 exit_error(PARAMETER_PROBLEM,
1071 "Couldn't load target `%s':%s\n",
1072 name, dlerror());
1074 #else
1075 if (ptr && !ptr->loaded) {
1076 if (tryload != DONT_LOAD)
1077 ptr->loaded = 1;
1078 else
1079 ptr = NULL;
1081 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1082 exit_error(PARAMETER_PROBLEM,
1083 "Couldn't find target `%s'\n", name);
1085 #endif
1087 if (ptr)
1088 ptr->used = 1;
1090 return ptr;
1093 static struct option *
1094 merge_options(struct option *oldopts, const struct option *newopts,
1095 unsigned int *option_offset)
1097 unsigned int num_old, num_new, i;
1098 struct option *merge;
1100 if (newopts == NULL)
1101 return oldopts;
1103 for (num_old = 0; oldopts[num_old].name; num_old++);
1104 for (num_new = 0; newopts[num_new].name; num_new++);
1106 global_option_offset += OPTION_OFFSET;
1107 *option_offset = global_option_offset;
1109 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1110 memcpy(merge, oldopts, num_old * sizeof(struct option));
1111 free_opts(0); /* Release previous options merged if any */
1112 for (i = 0; i < num_new; i++) {
1113 merge[num_old + i] = newopts[i];
1114 merge[num_old + i].val += *option_offset;
1116 memset(merge + num_old + num_new, 0, sizeof(struct option));
1118 return merge;
1121 static int compatible_revision(const char *name, u_int8_t revision, int opt)
1123 struct ip6t_get_revision rev;
1124 socklen_t s = sizeof(rev);
1125 int max_rev, sockfd;
1127 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1128 if (sockfd < 0) {
1129 fprintf(stderr, "Could not open socket to kernel: %s\n",
1130 strerror(errno));
1131 exit(1);
1134 strcpy(rev.name, name);
1135 rev.revision = revision;
1137 load_ip6tables_ko(modprobe, 1);
1139 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1140 if (max_rev < 0) {
1141 /* Definitely don't support this? */
1142 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
1143 close(sockfd);
1144 return 0;
1145 } else if (errno == ENOPROTOOPT) {
1146 close(sockfd);
1147 /* Assume only revision 0 support (old kernel) */
1148 return (revision == 0);
1149 } else {
1150 fprintf(stderr, "getsockopt failed strangely: %s\n",
1151 strerror(errno));
1152 exit(1);
1155 close(sockfd);
1156 return 1;
1159 static int compatible_match_revision(const char *name, u_int8_t revision)
1161 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1164 void
1165 register_match6(struct ip6tables_match *me)
1167 struct ip6tables_match **i, *old;
1169 if (strcmp(me->version, program_version) != 0) {
1170 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1171 program_name, me->name, me->version, program_version);
1172 exit(1);
1175 /* Revision field stole a char from name. */
1176 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1177 fprintf(stderr, "%s: target `%s' has invalid name\n",
1178 program_name, me->name);
1179 exit(1);
1182 old = find_match(me->name, DURING_LOAD, NULL);
1183 if (old) {
1184 if (old->revision == me->revision) {
1185 fprintf(stderr,
1186 "%s: match `%s' already registered.\n",
1187 program_name, me->name);
1188 exit(1);
1191 /* Now we have two (or more) options, check compatibility. */
1192 if (compatible_match_revision(old->name, old->revision)
1193 && old->revision > me->revision)
1194 return;
1196 /* Replace if compatible. */
1197 if (!compatible_match_revision(me->name, me->revision))
1198 return;
1200 /* Delete old one. */
1201 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1202 *i = old->next;
1205 if (me->size != IP6T_ALIGN(me->size)) {
1206 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1207 program_name, me->name, (unsigned int)me->size);
1208 exit(1);
1211 /* Append to list. */
1212 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1213 me->next = NULL;
1214 *i = me;
1216 me->m = NULL;
1217 me->mflags = 0;
1220 void
1221 register_target6(struct ip6tables_target *me)
1223 if (strcmp(me->version, program_version) != 0) {
1224 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1225 program_name, me->name, me->version, program_version);
1226 exit(1);
1229 if (find_target(me->name, DURING_LOAD)) {
1230 fprintf(stderr, "%s: target `%s' already registered.\n",
1231 program_name, me->name);
1232 exit(1);
1235 if (me->size != IP6T_ALIGN(me->size)) {
1236 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1237 program_name, me->name, (unsigned int)me->size);
1238 exit(1);
1241 /* Prepend to list. */
1242 me->next = ip6tables_targets;
1243 ip6tables_targets = me;
1244 me->t = NULL;
1245 me->tflags = 0;
1248 static void
1249 print_num(u_int64_t number, unsigned int format)
1251 if (format & FMT_KILOMEGAGIGA) {
1252 if (number > 99999) {
1253 number = (number + 500) / 1000;
1254 if (number > 9999) {
1255 number = (number + 500) / 1000;
1256 if (number > 9999) {
1257 number = (number + 500) / 1000;
1258 if (number > 9999) {
1259 number = (number + 500) / 1000;
1260 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
1262 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
1264 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
1265 } else
1266 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
1267 } else
1268 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
1269 } else
1270 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
1274 static void
1275 print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1277 struct ip6t_counters counters;
1278 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1279 printf("Chain %s", chain);
1280 if (pol) {
1281 printf(" (policy %s", pol);
1282 if (!(format & FMT_NOCOUNTS)) {
1283 fputc(' ', stdout);
1284 print_num(counters.pcnt, (format|FMT_NOTABLE));
1285 fputs("packets, ", stdout);
1286 print_num(counters.bcnt, (format|FMT_NOTABLE));
1287 fputs("bytes", stdout);
1289 printf(")\n");
1290 } else {
1291 unsigned int refs;
1292 if (!ip6tc_get_references(&refs, chain, handle))
1293 printf(" (ERROR obtaining refs)\n");
1294 else
1295 printf(" (%u references)\n", refs);
1298 if (format & FMT_LINENUMBERS)
1299 printf(FMT("%-4s ", "%s "), "num");
1300 if (!(format & FMT_NOCOUNTS)) {
1301 if (format & FMT_KILOMEGAGIGA) {
1302 printf(FMT("%5s ","%s "), "pkts");
1303 printf(FMT("%5s ","%s "), "bytes");
1304 } else {
1305 printf(FMT("%8s ","%s "), "pkts");
1306 printf(FMT("%10s ","%s "), "bytes");
1309 if (!(format & FMT_NOTARGET))
1310 printf(FMT("%-9s ","%s "), "target");
1311 fputs(" prot ", stdout);
1312 if (format & FMT_OPTIONS)
1313 fputs("opt", stdout);
1314 if (format & FMT_VIA) {
1315 printf(FMT(" %-6s ","%s "), "in");
1316 printf(FMT("%-6s ","%s "), "out");
1318 printf(FMT(" %-19s ","%s "), "source");
1319 printf(FMT(" %-19s "," %s "), "destination");
1320 printf("\n");
1324 static int
1325 print_match(const struct ip6t_entry_match *m,
1326 const struct ip6t_ip6 *ip,
1327 int numeric)
1329 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
1331 if (match) {
1332 if (match->print)
1333 match->print(ip, m, numeric);
1334 else
1335 printf("%s ", match->name);
1336 } else {
1337 if (m->u.user.name[0])
1338 printf("UNKNOWN match `%s' ", m->u.user.name);
1340 /* Don't stop iterating. */
1341 return 0;
1344 /* e is called `fw' here for hysterical raisins */
1345 static void
1346 print_firewall(const struct ip6t_entry *fw,
1347 const char *targname,
1348 unsigned int num,
1349 unsigned int format,
1350 const ip6tc_handle_t handle)
1352 struct ip6tables_target *target = NULL;
1353 const struct ip6t_entry_target *t;
1354 u_int8_t flags;
1355 char buf[BUFSIZ];
1357 if (!ip6tc_is_chain(targname, handle))
1358 target = find_target(targname, TRY_LOAD);
1359 else
1360 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1362 t = ip6t_get_target((struct ip6t_entry *)fw);
1363 flags = fw->ipv6.flags;
1365 if (format & FMT_LINENUMBERS)
1366 printf(FMT("%-4u ", "%u "), num+1);
1368 if (!(format & FMT_NOCOUNTS)) {
1369 print_num(fw->counters.pcnt, format);
1370 print_num(fw->counters.bcnt, format);
1373 if (!(format & FMT_NOTARGET))
1374 printf(FMT("%-9s ", "%s "), targname);
1376 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1378 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
1379 if (pname)
1380 printf(FMT("%-5s", "%s "), pname);
1381 else
1382 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1385 if (format & FMT_OPTIONS) {
1386 if (format & FMT_NOTABLE)
1387 fputs("opt ", stdout);
1388 fputc(' ', stdout); /* Invert flag of FRAG */
1389 fputc(' ', stdout); /* -f */
1390 fputc(' ', stdout);
1393 if (format & FMT_VIA) {
1394 char iface[IFNAMSIZ+2];
1396 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1397 iface[0] = '!';
1398 iface[1] = '\0';
1400 else iface[0] = '\0';
1402 if (fw->ipv6.iniface[0] != '\0') {
1403 strcat(iface, fw->ipv6.iniface);
1405 else if (format & FMT_NUMERIC) strcat(iface, "*");
1406 else strcat(iface, "any");
1407 printf(FMT(" %-6s ","in %s "), iface);
1409 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1410 iface[0] = '!';
1411 iface[1] = '\0';
1413 else iface[0] = '\0';
1415 if (fw->ipv6.outiface[0] != '\0') {
1416 strcat(iface, fw->ipv6.outiface);
1418 else if (format & FMT_NUMERIC) strcat(iface, "*");
1419 else strcat(iface, "any");
1420 printf(FMT("%-6s ","out %s "), iface);
1423 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1424 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1425 && !(format & FMT_NUMERIC))
1426 printf(FMT("%-19s ","%s "), "anywhere");
1427 else {
1428 if (format & FMT_NUMERIC)
1429 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
1430 else
1431 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
1432 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1433 printf(FMT("%-19s ","%s "), buf);
1436 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1437 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1438 && !(format & FMT_NUMERIC))
1439 printf(FMT("%-19s ","-> %s"), "anywhere");
1440 else {
1441 if (format & FMT_NUMERIC)
1442 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
1443 else
1444 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
1445 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1446 printf(FMT("%-19s ","-> %s"), buf);
1449 if (format & FMT_NOTABLE)
1450 fputs(" ", stdout);
1452 #ifdef IP6T_F_GOTO
1453 if(fw->ipv6.flags & IP6T_F_GOTO)
1454 printf("[goto] ");
1455 #endif
1457 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1459 if (target) {
1460 if (target->print)
1461 /* Print the target information. */
1462 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1463 } else if (t->u.target_size != sizeof(*t))
1464 printf("[%u bytes of unknown target data] ",
1465 (unsigned int)(t->u.target_size - sizeof(*t)));
1467 if (!(format & FMT_NONEWLINE))
1468 fputc('\n', stdout);
1471 static void
1472 print_firewall_line(const struct ip6t_entry *fw,
1473 const ip6tc_handle_t h)
1475 struct ip6t_entry_target *t;
1477 t = ip6t_get_target((struct ip6t_entry *)fw);
1478 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1481 static int
1482 append_entry(const ip6t_chainlabel chain,
1483 struct ip6t_entry *fw,
1484 unsigned int nsaddrs,
1485 const struct in6_addr saddrs[],
1486 unsigned int ndaddrs,
1487 const struct in6_addr daddrs[],
1488 int verbose,
1489 ip6tc_handle_t *handle)
1491 unsigned int i, j;
1492 int ret = 1;
1494 for (i = 0; i < nsaddrs; i++) {
1495 fw->ipv6.src = saddrs[i];
1496 for (j = 0; j < ndaddrs; j++) {
1497 fw->ipv6.dst = daddrs[j];
1498 if (verbose)
1499 print_firewall_line(fw, *handle);
1500 ret &= ip6tc_append_entry(chain, fw, handle);
1504 return ret;
1507 static int
1508 replace_entry(const ip6t_chainlabel chain,
1509 struct ip6t_entry *fw,
1510 unsigned int rulenum,
1511 const struct in6_addr *saddr,
1512 const struct in6_addr *daddr,
1513 int verbose,
1514 ip6tc_handle_t *handle)
1516 fw->ipv6.src = *saddr;
1517 fw->ipv6.dst = *daddr;
1519 if (verbose)
1520 print_firewall_line(fw, *handle);
1521 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1524 static int
1525 insert_entry(const ip6t_chainlabel chain,
1526 struct ip6t_entry *fw,
1527 unsigned int rulenum,
1528 unsigned int nsaddrs,
1529 const struct in6_addr saddrs[],
1530 unsigned int ndaddrs,
1531 const struct in6_addr daddrs[],
1532 int verbose,
1533 ip6tc_handle_t *handle)
1535 unsigned int i, j;
1536 int ret = 1;
1538 for (i = 0; i < nsaddrs; i++) {
1539 fw->ipv6.src = saddrs[i];
1540 for (j = 0; j < ndaddrs; j++) {
1541 fw->ipv6.dst = daddrs[j];
1542 if (verbose)
1543 print_firewall_line(fw, *handle);
1544 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1548 return ret;
1551 static unsigned char *
1552 make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches,
1553 const struct ip6tables_target *target)
1555 /* Establish mask for comparison */
1556 unsigned int size;
1557 struct ip6tables_rule_match *matchp;
1558 unsigned char *mask, *mptr;
1560 size = sizeof(struct ip6t_entry);
1561 for (matchp = matches; matchp; matchp = matchp->next)
1562 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1564 mask = fw_calloc(1, size
1565 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1566 + target->size);
1568 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1569 mptr = mask + sizeof(struct ip6t_entry);
1571 for (matchp = matches; matchp; matchp = matchp->next) {
1572 memset(mptr, 0xFF,
1573 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1574 + matchp->match->userspacesize);
1575 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1578 memset(mptr, 0xFF,
1579 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1580 + target->userspacesize);
1582 return mask;
1585 static int
1586 delete_entry(const ip6t_chainlabel chain,
1587 struct ip6t_entry *fw,
1588 unsigned int nsaddrs,
1589 const struct in6_addr saddrs[],
1590 unsigned int ndaddrs,
1591 const struct in6_addr daddrs[],
1592 int verbose,
1593 ip6tc_handle_t *handle,
1594 struct ip6tables_rule_match *matches,
1595 const struct ip6tables_target *target)
1597 unsigned int i, j;
1598 int ret = 1;
1599 unsigned char *mask;
1601 mask = make_delete_mask(fw, matches, target);
1602 for (i = 0; i < nsaddrs; i++) {
1603 fw->ipv6.src = saddrs[i];
1604 for (j = 0; j < ndaddrs; j++) {
1605 fw->ipv6.dst = daddrs[j];
1606 if (verbose)
1607 print_firewall_line(fw, *handle);
1608 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
1611 free(mask);
1613 return ret;
1617 for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1618 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1620 int ret = 1;
1621 const char *chain;
1622 char *chains;
1623 unsigned int i, chaincount = 0;
1625 chain = ip6tc_first_chain(handle);
1626 while (chain) {
1627 chaincount++;
1628 chain = ip6tc_next_chain(handle);
1631 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1632 i = 0;
1633 chain = ip6tc_first_chain(handle);
1634 while (chain) {
1635 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1636 i++;
1637 chain = ip6tc_next_chain(handle);
1640 for (i = 0; i < chaincount; i++) {
1641 if (!builtinstoo
1642 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1643 *handle) == 1)
1644 continue;
1645 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1648 free(chains);
1649 return ret;
1653 flush_entries(const ip6t_chainlabel chain, int verbose,
1654 ip6tc_handle_t *handle)
1656 if (!chain)
1657 return for_each_chain(flush_entries, verbose, 1, handle);
1659 if (verbose)
1660 fprintf(stdout, "Flushing chain `%s'\n", chain);
1661 return ip6tc_flush_entries(chain, handle);
1664 static int
1665 zero_entries(const ip6t_chainlabel chain, int verbose,
1666 ip6tc_handle_t *handle)
1668 if (!chain)
1669 return for_each_chain(zero_entries, verbose, 1, handle);
1671 if (verbose)
1672 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1673 return ip6tc_zero_entries(chain, handle);
1677 delete_chain(const ip6t_chainlabel chain, int verbose,
1678 ip6tc_handle_t *handle)
1680 if (!chain)
1681 return for_each_chain(delete_chain, verbose, 0, handle);
1683 if (verbose)
1684 fprintf(stdout, "Deleting chain `%s'\n", chain);
1685 return ip6tc_delete_chain(chain, handle);
1688 static int
1689 list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1690 int expanded, int linenumbers, ip6tc_handle_t *handle)
1692 int found = 0;
1693 unsigned int format;
1694 const char *this;
1696 format = FMT_OPTIONS;
1697 if (!verbose)
1698 format |= FMT_NOCOUNTS;
1699 else
1700 format |= FMT_VIA;
1702 if (numeric)
1703 format |= FMT_NUMERIC;
1705 if (!expanded)
1706 format |= FMT_KILOMEGAGIGA;
1708 if (linenumbers)
1709 format |= FMT_LINENUMBERS;
1711 for (this = ip6tc_first_chain(handle);
1712 this;
1713 this = ip6tc_next_chain(handle)) {
1714 const struct ip6t_entry *i;
1715 unsigned int num;
1717 if (chain && strcmp(chain, this) != 0)
1718 continue;
1720 if (found) printf("\n");
1722 print_header(format, this, handle);
1723 i = ip6tc_first_rule(this, handle);
1725 num = 0;
1726 while (i) {
1727 print_firewall(i,
1728 ip6tc_get_target(i, handle),
1729 num++,
1730 format,
1731 *handle);
1732 i = ip6tc_next_rule(i, handle);
1734 found = 1;
1737 errno = ENOENT;
1738 return found;
1741 static char *get_modprobe(void)
1743 int procfile;
1744 char *ret;
1746 #define PROCFILE_BUFSIZ 1024
1747 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1748 if (procfile < 0)
1749 return NULL;
1751 ret = malloc(PROCFILE_BUFSIZ);
1752 if (ret) {
1753 memset(ret, 0, PROCFILE_BUFSIZ);
1754 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
1755 case -1: goto fail;
1756 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
1758 if (ret[strlen(ret)-1]=='\n')
1759 ret[strlen(ret)-1]=0;
1760 close(procfile);
1761 return ret;
1763 fail:
1764 free(ret);
1765 close(procfile);
1766 return NULL;
1769 int ip6tables_insmod(const char *modname, const char *modprobe, int quiet)
1771 char *buf = NULL;
1772 char *argv[4];
1773 int status;
1775 /* If they don't explicitly set it, read out of kernel */
1776 if (!modprobe) {
1777 buf = get_modprobe();
1778 if (!buf)
1779 return -1;
1780 modprobe = buf;
1783 switch (fork()) {
1784 case 0:
1785 argv[0] = (char *)modprobe;
1786 argv[1] = (char *)modname;
1787 if (quiet) {
1788 argv[2] = "-q";
1789 argv[3] = NULL;
1790 } else {
1791 argv[2] = NULL;
1792 argv[3] = NULL;
1794 execv(argv[0], argv);
1796 /* not usually reached */
1797 exit(1);
1798 case -1:
1799 return -1;
1801 default: /* parent */
1802 wait(&status);
1805 free(buf);
1806 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1807 return 0;
1808 return -1;
1811 int load_ip6tables_ko(const char *modprobe, int quiet)
1813 static int loaded = 0;
1814 static int ret = -1;
1816 if (!loaded) {
1817 ret = ip6tables_insmod("ip6_tables", modprobe, quiet);
1818 loaded = (ret == 0);
1821 return ret;
1824 static struct ip6t_entry *
1825 generate_entry(const struct ip6t_entry *fw,
1826 struct ip6tables_rule_match *matches,
1827 struct ip6t_entry_target *target)
1829 unsigned int size;
1830 struct ip6tables_rule_match *matchp;
1831 struct ip6t_entry *e;
1833 size = sizeof(struct ip6t_entry);
1834 for (matchp = matches; matchp; matchp = matchp->next)
1835 size += matchp->match->m->u.match_size;
1837 e = fw_malloc(size + target->u.target_size);
1838 *e = *fw;
1839 e->target_offset = size;
1840 e->next_offset = size + target->u.target_size;
1842 size = 0;
1843 for (matchp = matches; matchp; matchp = matchp->next) {
1844 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1845 size += matchp->match->m->u.match_size;
1847 memcpy(e->elems + size, target, target->u.target_size);
1849 return e;
1852 void clear_rule_matches(struct ip6tables_rule_match **matches)
1854 struct ip6tables_rule_match *matchp, *tmp;
1856 for (matchp = *matches; matchp;) {
1857 tmp = matchp->next;
1858 if (matchp->match->m) {
1859 free(matchp->match->m);
1860 matchp->match->m = NULL;
1862 if (matchp->match == matchp->match->next) {
1863 free(matchp->match);
1864 matchp->match = NULL;
1866 free(matchp);
1867 matchp = tmp;
1870 *matches = NULL;
1873 static void set_revision(char *name, u_int8_t revision)
1875 /* Old kernel sources don't have ".revision" field,
1876 but we stole a byte from name. */
1877 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1878 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1881 int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1883 struct ip6t_entry fw, *e = NULL;
1884 int invert = 0;
1885 unsigned int nsaddrs = 0, ndaddrs = 0;
1886 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1888 int c, verbose = 0;
1889 const char *chain = NULL;
1890 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1891 const char *policy = NULL, *newname = NULL;
1892 unsigned int rulenum = 0, options = 0, command = 0;
1893 const char *pcnt = NULL, *bcnt = NULL;
1894 int ret = 1;
1895 struct ip6tables_match *m;
1896 struct ip6tables_rule_match *matches = NULL;
1897 struct ip6tables_rule_match *matchp;
1898 struct ip6tables_target *target = NULL;
1899 struct ip6tables_target *t;
1900 const char *jumpto = "";
1901 char *protocol = NULL;
1902 int proto_used = 0;
1903 unsigned long long cnt;
1905 memset(&fw, 0, sizeof(fw));
1907 /* re-set optind to 0 in case do_command gets called
1908 * a second time */
1909 optind = 0;
1911 /* clear mflags in case do_command gets called a second time
1912 * (we clear the global list of all matches for security)*/
1913 for (m = ip6tables_matches; m; m = m->next)
1914 m->mflags = 0;
1916 for (t = ip6tables_targets; t; t = t->next) {
1917 t->tflags = 0;
1918 t->used = 0;
1921 /* Suppress error messages: we may add new options if we
1922 demand-load a protocol. */
1923 opterr = 0;
1925 while ((c = getopt_long(argc, argv,
1926 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:g:",
1927 opts, NULL)) != -1) {
1928 switch (c) {
1930 * Command selection
1932 case 'A':
1933 add_command(&command, CMD_APPEND, CMD_NONE,
1934 invert);
1935 chain = optarg;
1936 break;
1938 case 'D':
1939 add_command(&command, CMD_DELETE, CMD_NONE,
1940 invert);
1941 chain = optarg;
1942 if (optind < argc && argv[optind][0] != '-'
1943 && argv[optind][0] != '!') {
1944 rulenum = parse_rulenumber(argv[optind++]);
1945 command = CMD_DELETE_NUM;
1947 break;
1949 case 'R':
1950 add_command(&command, CMD_REPLACE, CMD_NONE,
1951 invert);
1952 chain = optarg;
1953 if (optind < argc && argv[optind][0] != '-'
1954 && argv[optind][0] != '!')
1955 rulenum = parse_rulenumber(argv[optind++]);
1956 else
1957 exit_error(PARAMETER_PROBLEM,
1958 "-%c requires a rule number",
1959 cmd2char(CMD_REPLACE));
1960 break;
1962 case 'I':
1963 add_command(&command, CMD_INSERT, CMD_NONE,
1964 invert);
1965 chain = optarg;
1966 if (optind < argc && argv[optind][0] != '-'
1967 && argv[optind][0] != '!')
1968 rulenum = parse_rulenumber(argv[optind++]);
1969 else rulenum = 1;
1970 break;
1972 case 'L':
1973 add_command(&command, CMD_LIST, CMD_ZERO,
1974 invert);
1975 if (optarg) chain = optarg;
1976 else if (optind < argc && argv[optind][0] != '-'
1977 && argv[optind][0] != '!')
1978 chain = argv[optind++];
1979 break;
1981 case 'F':
1982 add_command(&command, CMD_FLUSH, CMD_NONE,
1983 invert);
1984 if (optarg) chain = optarg;
1985 else if (optind < argc && argv[optind][0] != '-'
1986 && argv[optind][0] != '!')
1987 chain = argv[optind++];
1988 break;
1990 case 'Z':
1991 add_command(&command, CMD_ZERO, CMD_LIST,
1992 invert);
1993 if (optarg) chain = optarg;
1994 else if (optind < argc && argv[optind][0] != '-'
1995 && argv[optind][0] != '!')
1996 chain = argv[optind++];
1997 break;
1999 case 'N':
2000 if (optarg && (*optarg == '-' || *optarg == '!'))
2001 exit_error(PARAMETER_PROBLEM,
2002 "chain name not allowed to start "
2003 "with `%c'\n", *optarg);
2004 if (find_target(optarg, TRY_LOAD))
2005 exit_error(PARAMETER_PROBLEM,
2006 "chain name may not clash "
2007 "with target name\n");
2008 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
2009 invert);
2010 chain = optarg;
2011 break;
2013 case 'X':
2014 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
2015 invert);
2016 if (optarg) chain = optarg;
2017 else if (optind < argc && argv[optind][0] != '-'
2018 && argv[optind][0] != '!')
2019 chain = argv[optind++];
2020 break;
2022 case 'E':
2023 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
2024 invert);
2025 chain = optarg;
2026 if (optind < argc && argv[optind][0] != '-'
2027 && argv[optind][0] != '!')
2028 newname = argv[optind++];
2029 else
2030 exit_error(PARAMETER_PROBLEM,
2031 "-%c requires old-chain-name and "
2032 "new-chain-name",
2033 cmd2char(CMD_RENAME_CHAIN));
2034 break;
2036 case 'P':
2037 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2038 invert);
2039 chain = optarg;
2040 if (optind < argc && argv[optind][0] != '-'
2041 && argv[optind][0] != '!')
2042 policy = argv[optind++];
2043 else
2044 exit_error(PARAMETER_PROBLEM,
2045 "-%c requires a chain and a policy",
2046 cmd2char(CMD_SET_POLICY));
2047 break;
2049 case 'h':
2050 if (!optarg)
2051 optarg = argv[optind];
2053 /* ip6tables -p icmp -h */
2054 if (!matches && protocol)
2055 find_match(protocol, TRY_LOAD, &matches);
2057 exit_printhelp(matches);
2060 * Option selection
2062 case 'p':
2063 check_inverse(optarg, &invert, &optind, argc);
2064 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2065 invert);
2067 /* Canonicalize into lower case */
2068 for (protocol = argv[optind-1]; *protocol; protocol++)
2069 *protocol = tolower(*protocol);
2071 protocol = argv[optind-1];
2072 fw.ipv6.proto = parse_protocol(protocol);
2073 fw.ipv6.flags |= IP6T_F_PROTO;
2075 if (fw.ipv6.proto == 0
2076 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2077 exit_error(PARAMETER_PROBLEM,
2078 "rule would never match protocol");
2080 if (is_exthdr(fw.ipv6.proto)
2081 && (fw.ipv6.invflags & IP6T_INV_PROTO) == 0)
2082 printf("Warning: never matched protocol: %s. "
2083 "use extension match instead.\n",
2084 protocol);
2085 break;
2087 case 's':
2088 check_inverse(optarg, &invert, &optind, argc);
2089 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2090 invert);
2091 shostnetworkmask = argv[optind-1];
2092 break;
2094 case 'd':
2095 check_inverse(optarg, &invert, &optind, argc);
2096 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2097 invert);
2098 dhostnetworkmask = argv[optind-1];
2099 break;
2101 #ifdef IP6T_F_GOTO
2102 case 'g':
2103 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2104 invert);
2105 fw.ipv6.flags |= IP6T_F_GOTO;
2106 jumpto = parse_target(optarg);
2107 break;
2108 #endif
2110 case 'j':
2111 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2112 invert);
2113 jumpto = parse_target(optarg);
2114 /* TRY_LOAD (may be chain name) */
2115 target = find_target(jumpto, TRY_LOAD);
2117 if (target) {
2118 size_t size;
2120 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2121 + target->size;
2123 target->t = fw_calloc(1, size);
2124 target->t->u.target_size = size;
2125 strcpy(target->t->u.user.name, jumpto);
2126 if (target->init != NULL)
2127 target->init(target->t, &fw.nfcache);
2128 opts = merge_options(opts,
2129 target->extra_opts,
2130 &target->option_offset);
2131 if (opts == NULL)
2132 exit_error(OTHER_PROBLEM,
2133 "can't alloc memory!");
2135 break;
2138 case 'i':
2139 check_inverse(optarg, &invert, &optind, argc);
2140 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2141 invert);
2142 parse_interface(argv[optind-1],
2143 fw.ipv6.iniface,
2144 fw.ipv6.iniface_mask);
2145 break;
2147 case 'o':
2148 check_inverse(optarg, &invert, &optind, argc);
2149 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2150 invert);
2151 parse_interface(argv[optind-1],
2152 fw.ipv6.outiface,
2153 fw.ipv6.outiface_mask);
2154 break;
2156 case 'v':
2157 if (!verbose)
2158 set_option(&options, OPT_VERBOSE,
2159 &fw.ipv6.invflags, invert);
2160 verbose++;
2161 break;
2163 case 'm': {
2164 size_t size;
2166 if (invert)
2167 exit_error(PARAMETER_PROBLEM,
2168 "unexpected ! flag before --match");
2170 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
2171 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2172 + m->size;
2173 m->m = fw_calloc(1, size);
2174 m->m->u.match_size = size;
2175 strcpy(m->m->u.user.name, m->name);
2176 set_revision(m->m->u.user.name, m->revision);
2177 if (m->init != NULL)
2178 m->init(m->m, &fw.nfcache);
2179 if (m != m->next)
2180 /* Merge options for non-cloned matches */
2181 opts = merge_options(opts, m->extra_opts, &m->option_offset);
2183 break;
2185 case 'n':
2186 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2187 invert);
2188 break;
2190 case 't':
2191 if (invert)
2192 exit_error(PARAMETER_PROBLEM,
2193 "unexpected ! flag before --table");
2194 *table = optarg;
2195 break;
2197 case 'x':
2198 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2199 invert);
2200 break;
2202 case 'V':
2203 if (invert)
2204 printf("Not %s ;-)\n", program_version);
2205 else
2206 printf("%s v%s\n",
2207 program_name, program_version);
2208 exit(0);
2210 case '0':
2211 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2212 invert);
2213 break;
2215 case 'M':
2216 modprobe = optarg;
2217 break;
2219 case 'c':
2221 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2222 invert);
2223 pcnt = optarg;
2224 if (optind < argc && argv[optind][0] != '-'
2225 && argv[optind][0] != '!')
2226 bcnt = argv[optind++];
2227 else
2228 exit_error(PARAMETER_PROBLEM,
2229 "-%c requires packet and byte counter",
2230 opt2char(OPT_COUNTERS));
2232 if (sscanf(pcnt, "%llu", (unsigned long long *)&cnt) != 1)
2233 exit_error(PARAMETER_PROBLEM,
2234 "-%c packet counter not numeric",
2235 opt2char(OPT_COUNTERS));
2236 fw.counters.pcnt = cnt;
2238 if (sscanf(bcnt, "%llu", (unsigned long long *)&cnt) != 1)
2239 exit_error(PARAMETER_PROBLEM,
2240 "-%c byte counter not numeric",
2241 opt2char(OPT_COUNTERS));
2242 fw.counters.bcnt = cnt;
2244 break;
2247 case 1: /* non option */
2248 if (optarg[0] == '!' && optarg[1] == '\0') {
2249 if (invert)
2250 exit_error(PARAMETER_PROBLEM,
2251 "multiple consecutive ! not"
2252 " allowed");
2253 invert = TRUE;
2254 optarg[0] = '\0';
2255 continue;
2257 printf("Bad argument `%s'\n", optarg);
2258 exit_tryhelp(2);
2260 default:
2261 if (target == NULL || target->parse == NULL ||
2262 c < target->option_offset ||
2263 c >= target->option_offset + OPTION_OFFSET ||
2264 !(target->parse(c - target->option_offset,
2265 argv, invert,
2266 &target->tflags,
2267 &fw, &target->t))) {
2268 for (matchp = matches; matchp; matchp = matchp->next) {
2269 if (matchp->completed ||
2270 matchp->match->parse == NULL)
2271 continue;
2272 if (c < matchp->match->option_offset ||
2273 c >= matchp->match->option_offset + OPTION_OFFSET)
2274 continue;
2275 if (matchp->match->parse(c - matchp->match->option_offset,
2276 argv, invert,
2277 &matchp->match->mflags,
2278 &fw,
2279 &fw.nfcache,
2280 &matchp->match->m))
2281 break;
2283 m = matchp ? matchp->match : NULL;
2285 /* If you listen carefully, you can
2286 actually hear this code suck. */
2288 /* some explanations (after four different bugs
2289 * in 3 different releases): If we encounter a
2290 * parameter, that has not been parsed yet,
2291 * it's not an option of an explicitly loaded
2292 * match or a target. However, we support
2293 * implicit loading of the protocol match
2294 * extension. '-p tcp' means 'l4 proto 6' and
2295 * at the same time 'load tcp protocol match on
2296 * demand if we specify --dport'.
2298 * To make this work, we need to make sure:
2299 * - the parameter has not been parsed by
2300 * a match (m above)
2301 * - a protocol has been specified
2302 * - the protocol extension has not been
2303 * loaded yet, or is loaded and unused
2304 * [think of ip6tables-restore!]
2305 * - the protocol extension can be successively
2306 * loaded
2308 if (m == NULL
2309 && protocol
2310 && (!find_proto(protocol, DONT_LOAD,
2311 options&OPT_NUMERIC, NULL)
2312 || (find_proto(protocol, DONT_LOAD,
2313 options&OPT_NUMERIC, NULL)
2314 && (proto_used == 0))
2316 && (m = find_proto(protocol, TRY_LOAD,
2317 options&OPT_NUMERIC, &matches))) {
2318 /* Try loading protocol */
2319 size_t size;
2321 proto_used = 1;
2323 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2324 + m->size;
2326 m->m = fw_calloc(1, size);
2327 m->m->u.match_size = size;
2328 strcpy(m->m->u.user.name, m->name);
2329 set_revision(m->m->u.user.name,
2330 m->revision);
2331 if (m->init != NULL)
2332 m->init(m->m, &fw.nfcache);
2334 opts = merge_options(opts,
2335 m->extra_opts, &m->option_offset);
2337 optind--;
2338 continue;
2341 if (!m)
2342 exit_error(PARAMETER_PROBLEM,
2343 "Unknown arg `%s'", optarg);
2346 invert = FALSE;
2349 for (matchp = matches; matchp; matchp = matchp->next)
2350 matchp->match->final_check(matchp->match->mflags);
2352 if (target)
2353 target->final_check(target->tflags);
2355 /* Fix me: must put inverse options checking here --MN */
2357 if (optind < argc)
2358 exit_error(PARAMETER_PROBLEM,
2359 "unknown arguments found on commandline");
2360 if (!command)
2361 exit_error(PARAMETER_PROBLEM, "no command specified");
2362 if (invert)
2363 exit_error(PARAMETER_PROBLEM,
2364 "nothing appropriate following !");
2366 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
2367 if (!(options & OPT_DESTINATION))
2368 dhostnetworkmask = "::0/0";
2369 if (!(options & OPT_SOURCE))
2370 shostnetworkmask = "::0/0";
2373 if (shostnetworkmask)
2374 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2375 &(fw.ipv6.smsk), &nsaddrs);
2377 if (dhostnetworkmask)
2378 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2379 &(fw.ipv6.dmsk), &ndaddrs);
2381 if ((nsaddrs > 1 || ndaddrs > 1) &&
2382 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2383 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2384 " source or destination IP addresses");
2386 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2387 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2388 "specify a unique address");
2390 generic_opt_check(command, options);
2392 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2393 exit_error(PARAMETER_PROBLEM,
2394 "chain name `%s' too long (must be under %i chars)",
2395 chain, IP6T_FUNCTION_MAXNAMELEN);
2397 /* only allocate handle if we weren't called with a handle */
2398 if (!*handle)
2399 *handle = ip6tc_init(*table);
2401 /* try to insmod the module if iptc_init failed */
2402 if (!*handle && load_ip6tables_ko(modprobe, 0) != -1)
2403 *handle = ip6tc_init(*table);
2405 if (!*handle)
2406 exit_error(VERSION_PROBLEM,
2407 "can't initialize ip6tables table `%s': %s",
2408 *table, ip6tc_strerror(errno));
2410 if (command == CMD_APPEND
2411 || command == CMD_DELETE
2412 || command == CMD_INSERT
2413 || command == CMD_REPLACE) {
2414 if (strcmp(chain, "PREROUTING") == 0
2415 || strcmp(chain, "INPUT") == 0) {
2416 /* -o not valid with incoming packets. */
2417 if (options & OPT_VIANAMEOUT)
2418 exit_error(PARAMETER_PROBLEM,
2419 "Can't use -%c with %s\n",
2420 opt2char(OPT_VIANAMEOUT),
2421 chain);
2424 if (strcmp(chain, "POSTROUTING") == 0
2425 || strcmp(chain, "OUTPUT") == 0) {
2426 /* -i not valid with outgoing packets */
2427 if (options & OPT_VIANAMEIN)
2428 exit_error(PARAMETER_PROBLEM,
2429 "Can't use -%c with %s\n",
2430 opt2char(OPT_VIANAMEIN),
2431 chain);
2434 if (target && ip6tc_is_chain(jumpto, *handle)) {
2435 printf("Warning: using chain %s, not extension\n",
2436 jumpto);
2438 if (target->t)
2439 free(target->t);
2441 target = NULL;
2444 /* If they didn't specify a target, or it's a chain
2445 name, use standard. */
2446 if (!target
2447 && (strlen(jumpto) == 0
2448 || ip6tc_is_chain(jumpto, *handle))) {
2449 size_t size;
2451 target = find_target(IP6T_STANDARD_TARGET,
2452 LOAD_MUST_SUCCEED);
2454 size = sizeof(struct ip6t_entry_target)
2455 + target->size;
2456 target->t = fw_calloc(1, size);
2457 target->t->u.target_size = size;
2458 strcpy(target->t->u.user.name, jumpto);
2459 if (target->init != NULL)
2460 target->init(target->t, &fw.nfcache);
2463 if (!target) {
2464 /* it is no chain, and we can't load a plugin.
2465 * We cannot know if the plugin is corrupt, non
2466 * existant OR if the user just misspelled a
2467 * chain. */
2468 #ifdef IP6T_F_GOTO
2469 if (fw.ipv6.flags & IP6T_F_GOTO)
2470 exit_error(PARAMETER_PROBLEM,
2471 "goto '%s' is not a chain\n", jumpto);
2472 #endif
2473 find_target(jumpto, LOAD_MUST_SUCCEED);
2474 } else {
2475 e = generate_entry(&fw, matches, target->t);
2476 free(target->t);
2480 switch (command) {
2481 case CMD_APPEND:
2482 ret = append_entry(chain, e,
2483 nsaddrs, saddrs, ndaddrs, daddrs,
2484 options&OPT_VERBOSE,
2485 handle);
2486 break;
2487 case CMD_DELETE:
2488 ret = delete_entry(chain, e,
2489 nsaddrs, saddrs, ndaddrs, daddrs,
2490 options&OPT_VERBOSE,
2491 handle, matches, target);
2492 break;
2493 case CMD_DELETE_NUM:
2494 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2495 break;
2496 case CMD_REPLACE:
2497 ret = replace_entry(chain, e, rulenum - 1,
2498 saddrs, daddrs, options&OPT_VERBOSE,
2499 handle);
2500 break;
2501 case CMD_INSERT:
2502 ret = insert_entry(chain, e, rulenum - 1,
2503 nsaddrs, saddrs, ndaddrs, daddrs,
2504 options&OPT_VERBOSE,
2505 handle);
2506 break;
2507 case CMD_LIST:
2508 ret = list_entries(chain,
2509 options&OPT_VERBOSE,
2510 options&OPT_NUMERIC,
2511 options&OPT_EXPANDED,
2512 options&OPT_LINENUMBERS,
2513 handle);
2514 break;
2515 case CMD_FLUSH:
2516 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2517 break;
2518 case CMD_ZERO:
2519 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2520 break;
2521 case CMD_LIST|CMD_ZERO:
2522 ret = list_entries(chain,
2523 options&OPT_VERBOSE,
2524 options&OPT_NUMERIC,
2525 options&OPT_EXPANDED,
2526 options&OPT_LINENUMBERS,
2527 handle);
2528 if (ret)
2529 ret = zero_entries(chain,
2530 options&OPT_VERBOSE, handle);
2531 break;
2532 case CMD_NEW_CHAIN:
2533 ret = ip6tc_create_chain(chain, handle);
2534 break;
2535 case CMD_DELETE_CHAIN:
2536 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2537 break;
2538 case CMD_RENAME_CHAIN:
2539 ret = ip6tc_rename_chain(chain, newname, handle);
2540 break;
2541 case CMD_SET_POLICY:
2542 ret = ip6tc_set_policy(chain, policy, NULL, handle);
2543 break;
2544 default:
2545 /* We should never reach this... */
2546 exit_tryhelp(2);
2549 if (verbose > 1)
2550 dump_entries6(*handle);
2552 clear_rule_matches(&matches);
2554 if (e != NULL) {
2555 free(e);
2556 e = NULL;
2559 free(saddrs);
2560 free(daddrs);
2561 free_opts(1);
2563 return ret;