1 /* Code to take an ip6tables-style command line and do it. */
2
3 /*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
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>
12 *
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.
17 *
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.
22 *
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.
26 */
27
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>
45
46 #ifndef TRUE
47 #define TRUE 1
48 #endif
49 #ifndef FALSE
50 #define FALSE 0
51 #endif
52
53 #ifndef PROC_SYS_MODPROBE
54 #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55 #endif
56
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
66
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))
70
71
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' };
88
89 #define OPTION_OFFSET 256
90
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'};
106
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 { 0 }
138 };
139
140 /* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144 int line = -1;
145
146 static struct option *opts = original_opts;
147 static unsigned int global_option_offset = 0;
148
149 /* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158 static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159 /* Well, it's better than "Re: Linux vs FreeBSD" */
160 {
161 /* -n -s -d -p -j -v -x -i -o --line -c */
162 /*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163 /*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164 /*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165 /*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166 /*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167 /*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168 /*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169 /*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170 /*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171 /*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172 /*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
173 /*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
174 };
175
176 static int inverse_for_options[NUMBER_OF_OPT] =
177 {
178 /* -n */ 0,
179 /* -s */ IP6T_INV_SRCIP,
180 /* -d */ IP6T_INV_DSTIP,
181 /* -p */ IP6T_INV_PROTO,
182 /* -j */ 0,
183 /* -v */ 0,
184 /* -x */ 0,
185 /* -i */ IP6T_INV_VIA_IN,
186 /* -o */ IP6T_INV_VIA_OUT,
187 /*--line*/ 0,
188 /* -c */ 0,
189 };
190
191 const char *program_version;
192 const char *program_name;
193 char *lib_dir;
194
195 /* the path to command to load kernel module */
196 const char *modprobe = NULL;
197
198 /* Keeping track of external matches and targets: linked lists. */
199 struct ip6tables_match *ip6tables_matches = NULL;
200 struct ip6tables_target *ip6tables_targets = NULL;
201
202 /* Extra debugging from libiptc */
203 extern void dump_entries6(const ip6tc_handle_t handle);
204
205 /* A few hardcoded protocols for 'all' and in case the user has no
206 /etc/protocols */
207 struct pprot {
208 char *name;
209 u_int8_t num;
210 };
211
212 /* Primitive headers... */
213 /* defined in netinet/in.h */
214 #if 0
215 #ifndef IPPROTO_ESP
216 #define IPPROTO_ESP 50
217 #endif
218 #ifndef IPPROTO_AH
219 #define IPPROTO_AH 51
220 #endif
221 #endif
222
223 static const struct pprot chain_protos[] = {
224 { "tcp", IPPROTO_TCP },
225 { "udp", IPPROTO_UDP },
226 { "udplite", IPPROTO_UDPLITE },
227 { "icmpv6", IPPROTO_ICMPV6 },
228 { "ipv6-icmp", IPPROTO_ICMPV6 },
229 { "esp", IPPROTO_ESP },
230 { "ah", IPPROTO_AH },
231 };
232
233 static char *
proto_to_name(u_int8_t proto,int nolookup)234 proto_to_name(u_int8_t proto, int nolookup)
235 {
236 unsigned int i;
237
238 if (proto && !nolookup) {
239 struct protoent *pent = getprotobynumber(proto);
240 if (pent)
241 return pent->p_name;
242 }
243
244 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
245 if (chain_protos[i].num == proto)
246 return chain_protos[i].name;
247
248 return NULL;
249 }
250
251 int
service_to_port(const char * name,const char * proto)252 service_to_port(const char *name, const char *proto)
253 {
254 struct servent *service;
255
256 if ((service = getservbyname(name, proto)) != NULL)
257 return ntohs((unsigned short) service->s_port);
258
259 return -1;
260 }
261
262 u_int16_t
parse_port(const char * port,const char * proto)263 parse_port(const char *port, const char *proto)
264 {
265 unsigned int portnum;
266
267 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
268 (portnum = service_to_port(port, proto)) != -1)
269 return (u_int16_t)portnum;
270
271 exit_error(PARAMETER_PROBLEM,
272 "invalid port/service `%s' specified", port);
273 }
274
275 static void
in6addrcpy(struct in6_addr * dst,struct in6_addr * src)276 in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
277 {
278 memcpy(dst, src, sizeof(struct in6_addr));
279 /* dst->s6_addr = src->s6_addr; */
280 }
281
free_opts(int reset_offset)282 static void free_opts(int reset_offset)
283 {
284 if (opts != original_opts) {
285 free(opts);
286 opts = original_opts;
287 if (reset_offset)
288 global_option_offset = 0;
289 }
290 }
291
292 void
exit_error(enum exittype status,char * msg,...)293 exit_error(enum exittype status, char *msg, ...)
294 {
295 va_list args;
296
297 va_start(args, msg);
298 fprintf(stderr, "%s v%s: ", program_name, program_version);
299 vfprintf(stderr, msg, args);
300 va_end(args);
301 fprintf(stderr, "\n");
302 if (status == PARAMETER_PROBLEM)
303 exit_tryhelp(status);
304 if (status == VERSION_PROBLEM)
305 fprintf(stderr,
306 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
307 /* On error paths, make sure that we don't leak memory */
308 free_opts(1);
309 exit(status);
310 }
311
312 void
exit_tryhelp(int status)313 exit_tryhelp(int status)
314 {
315 if (line != -1)
316 fprintf(stderr, "Error occurred at line: %d\n", line);
317 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
318 program_name, program_name );
319 free_opts(1);
320 exit(status);
321 }
322
323 void
exit_printhelp(struct ip6tables_rule_match * matches)324 exit_printhelp(struct ip6tables_rule_match *matches)
325 {
326 struct ip6tables_rule_match *matchp = NULL;
327 struct ip6tables_target *t = NULL;
328
329 printf("%s v%s\n\n"
330 "Usage: %s -[AD] chain rule-specification [options]\n"
331 " %s -[RI] chain rulenum rule-specification [options]\n"
332 " %s -D chain rulenum [options]\n"
333 " %s -[LFZ] [chain] [options]\n"
334 " %s -[NX] chain\n"
335 " %s -E old-chain-name new-chain-name\n"
336 " %s -P chain target [options]\n"
337 " %s -h (print this help information)\n\n",
338 program_name, program_version, program_name, program_name,
339 program_name, program_name, program_name, program_name,
340 program_name, program_name);
341
342 printf(
343 "Commands:\n"
344 "Either long or short options are allowed.\n"
345 " --append -A chain Append to chain\n"
346 " --delete -D chain Delete matching rule from chain\n"
347 " --delete -D chain rulenum\n"
348 " Delete rule rulenum (1 = first) from chain\n"
349 " --insert -I chain [rulenum]\n"
350 " Insert in chain as rulenum (default 1=first)\n"
351 " --replace -R chain rulenum\n"
352 " Replace rule rulenum (1 = first) in chain\n"
353 " --list -L [chain] List the rules in a chain or all chains\n"
354 " --flush -F [chain] Delete all rules in chain or all chains\n"
355 " --zero -Z [chain] Zero counters in chain or all chains\n"
356 " --new -N chain Create a new user-defined chain\n"
357 " --delete-chain\n"
358 " -X [chain] Delete a user-defined chain\n"
359 " --policy -P chain target\n"
360 " Change policy on chain to target\n"
361 " --rename-chain\n"
362 " -E old-chain new-chain\n"
363 " Change chain name, (moving any references)\n"
364
365 "Options:\n"
366 " --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
367 " --source -s [!] address[/mask]\n"
368 " source specification\n"
369 " --destination -d [!] address[/mask]\n"
370 " destination specification\n"
371 " --in-interface -i [!] input name[+]\n"
372 " network interface name ([+] for wildcard)\n"
373 " --jump -j target\n"
374 " target for rule (may load target extension)\n"
375 " --match -m match\n"
376 " extended match (may load extension)\n"
377 " --numeric -n numeric output of addresses and ports\n"
378 " --out-interface -o [!] output name[+]\n"
379 " network interface name ([+] for wildcard)\n"
380 " --table -t table table to manipulate (default: `filter')\n"
381 " --verbose -v verbose mode\n"
382 " --line-numbers print line numbers when listing\n"
383 " --exact -x expand numbers (display exact values)\n"
384 /*"[!] --fragment -f match second or further fragments only\n"*/
385 " --modprobe=<command> try to insert modules using this command\n"
386 " --set-counters PKTS BYTES set the counter during insert/append\n"
387 "[!] --version -V print package version.\n");
388
389 /* Print out any special helps. A user might like to be able to add a --help
390 to the commandline, and see expected results. So we call help for all
391 specified matches & targets */
392 for (t = ip6tables_targets; t; t = t->next) {
393 if (t->used) {
394 printf("\n");
395 t->help();
396 }
397 }
398 for (matchp = matches; matchp; matchp = matchp->next) {
399 printf("\n");
400 matchp->match->help();
401 }
402 exit(0);
403 }
404
405 static void
generic_opt_check(int command,int options)406 generic_opt_check(int command, int options)
407 {
408 int i, j, legal = 0;
409
410 /* Check that commands are valid with options. Complicated by the
411 * fact that if an option is legal with *any* command given, it is
412 * legal overall (ie. -z and -l).
413 */
414 for (i = 0; i < NUMBER_OF_OPT; i++) {
415 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
416
417 for (j = 0; j < NUMBER_OF_CMD; j++) {
418 if (!(command & (1<<j)))
419 continue;
420
421 if (!(options & (1<<i))) {
422 if (commands_v_options[j][i] == '+')
423 exit_error(PARAMETER_PROBLEM,
424 "You need to supply the `-%c' "
425 "option for this command\n",
426 optflags[i]);
427 } else {
428 if (commands_v_options[j][i] != 'x')
429 legal = 1;
430 else if (legal == 0)
431 legal = -1;
432 }
433 }
434 if (legal == -1)
435 exit_error(PARAMETER_PROBLEM,
436 "Illegal option `-%c' with this command\n",
437 optflags[i]);
438 }
439 }
440
441 static char
opt2char(int option)442 opt2char(int option)
443 {
444 const char *ptr;
445 for (ptr = optflags; option > 1; option >>= 1, ptr++);
446
447 return *ptr;
448 }
449
450 static char
cmd2char(int option)451 cmd2char(int option)
452 {
453 const char *ptr;
454 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
455
456 return *ptr;
457 }
458
459 static void
add_command(unsigned int * cmd,const int newcmd,const int othercmds,int invert)460 add_command(unsigned int *cmd, const int newcmd, const int othercmds,
461 int invert)
462 {
463 if (invert)
464 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
465 if (*cmd & (~othercmds))
466 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
467 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
468 *cmd |= newcmd;
469 }
470
471 int
check_inverse(const char option[],int * invert,int * optind,int argc)472 check_inverse(const char option[], int *invert, int *optind, int argc)
473 {
474 if (option && strcmp(option, "!") == 0) {
475 if (*invert)
476 exit_error(PARAMETER_PROBLEM,
477 "Multiple `!' flags not allowed");
478 *invert = TRUE;
479 if (optind) {
480 *optind = *optind+1;
481 if (argc && *optind > argc)
482 exit_error(PARAMETER_PROBLEM,
483 "no argument following `!'");
484 }
485
486 return TRUE;
487 }
488 return FALSE;
489 }
490
491 static void *
fw_calloc(size_t count,size_t size)492 fw_calloc(size_t count, size_t size)
493 {
494 void *p;
495
496 if ((p = calloc(count, size)) == NULL) {
497 perror("ip6tables: calloc failed");
498 exit(1);
499 }
500 return p;
501 }
502
503 static void *
fw_malloc(size_t size)504 fw_malloc(size_t size)
505 {
506 void *p;
507
508 if ((p = malloc(size)) == NULL) {
509 perror("ip6tables: malloc failed");
510 exit(1);
511 }
512 return p;
513 }
514
515 static char *
addr_to_numeric(const struct in6_addr * addrp)516 addr_to_numeric(const struct in6_addr *addrp)
517 {
518 /* 0000:0000:0000:0000:0000:000.000.000.000
519 * 0000:0000:0000:0000:0000:0000:0000:0000 */
520 static char buf[50+1];
521 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
522 }
523
524 static struct in6_addr *
numeric_to_addr(const char * num)525 numeric_to_addr(const char *num)
526 {
527 static struct in6_addr ap;
528 int err;
529 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
530 return ≈
531 #ifdef DEBUG
532 fprintf(stderr, "\nnumeric2addr: %d\n", err);
533 #endif
534 return (struct in6_addr *)NULL;
535 }
536
537
538 static struct in6_addr *
host_to_addr(const char * name,unsigned int * naddr)539 host_to_addr(const char *name, unsigned int *naddr)
540 {
541 struct addrinfo hints;
542 struct addrinfo *res;
543 static struct in6_addr *addr;
544 int err;
545
546 memset(&hints, 0, sizeof(hints));
547 hints.ai_flags=AI_CANONNAME;
548 hints.ai_family=AF_INET6;
549 hints.ai_socktype=SOCK_RAW;
550 hints.ai_protocol=41;
551 hints.ai_next=NULL;
552
553 *naddr = 0;
554 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
555 #ifdef DEBUG
556 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
557 #endif
558 return (struct in6_addr *) NULL;
559 } else {
560 if (res->ai_family != AF_INET6 ||
561 res->ai_addrlen != sizeof(struct sockaddr_in6))
562 return (struct in6_addr *) NULL;
563
564 #ifdef DEBUG
565 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
566 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
567 #endif
568 /* Get the first element of the address-chain */
569 addr = fw_calloc(1, sizeof(struct in6_addr));
570 in6addrcpy(addr, (struct in6_addr *)
571 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
572 freeaddrinfo(res);
573 *naddr = 1;
574 return addr;
575 }
576
577 return (struct in6_addr *) NULL;
578 }
579
580 static char *
addr_to_host(const struct in6_addr * addr)581 addr_to_host(const struct in6_addr *addr)
582 {
583 struct sockaddr_in6 saddr;
584 int err;
585 static char hostname[NI_MAXHOST];
586
587 memset(&saddr, 0, sizeof(struct sockaddr_in6));
588 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
589 saddr.sin6_family = AF_INET6;
590
591 if ( (err=getnameinfo((struct sockaddr *)&saddr,
592 sizeof(struct sockaddr_in6),
593 hostname, sizeof(hostname)-1,
594 NULL, 0, 0)) != 0 ){
595 #ifdef DEBUG
596 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
597 #endif
598 return (char *) NULL;
599 } else {
600 #ifdef DEBUG
601 fprintf (stderr, "\naddr2host: %s\n", hostname);
602 #endif
603
604 return hostname;
605 }
606
607 return (char *) NULL;
608 }
609
610 static char *
mask_to_numeric(const struct in6_addr * addrp)611 mask_to_numeric(const struct in6_addr *addrp)
612 {
613 static char buf[50+2];
614 int l = ipv6_prefix_length(addrp);
615 if (l == -1) {
616 strcpy(buf, "/");
617 strcat(buf, addr_to_numeric(addrp));
618 return buf;
619 }
620 sprintf(buf, "/%d", l);
621 return buf;
622 }
623
624 static struct in6_addr *
network_to_addr(const char * name)625 network_to_addr(const char *name)
626 {
627 /* abort();*/
628 /* TODO: not implemented yet, but the exception breaks the
629 * name resolvation */
630 return (struct in6_addr *)NULL;
631 }
632
633 static char *
addr_to_anyname(const struct in6_addr * addr)634 addr_to_anyname(const struct in6_addr *addr)
635 {
636 char *name;
637
638 if ((name = addr_to_host(addr)) != NULL)
639 return name;
640
641 return addr_to_numeric(addr);
642 }
643
644 /*
645 * All functions starting with "parse" should succeed, otherwise
646 * the program fails.
647 * Most routines return pointers to static data that may change
648 * between calls to the same or other routines with a few exceptions:
649 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
650 * return global static data.
651 */
652
653 static struct in6_addr *
parse_hostnetwork(const char * name,unsigned int * naddrs)654 parse_hostnetwork(const char *name, unsigned int *naddrs)
655 {
656 struct in6_addr *addrp, *addrptmp;
657
658 if ((addrptmp = numeric_to_addr(name)) != NULL ||
659 (addrptmp = network_to_addr(name)) != NULL) {
660 addrp = fw_malloc(sizeof(struct in6_addr));
661 in6addrcpy(addrp, addrptmp);
662 *naddrs = 1;
663 return addrp;
664 }
665 if ((addrp = host_to_addr(name, naddrs)) != NULL)
666 return addrp;
667
668 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
669 }
670
671 static struct in6_addr *
parse_mask(char * mask)672 parse_mask(char *mask)
673 {
674 static struct in6_addr maskaddr;
675 struct in6_addr *addrp;
676 unsigned int bits;
677
678 if (mask == NULL) {
679 /* no mask at all defaults to 128 bits */
680 memset(&maskaddr, 0xff, sizeof maskaddr);
681 return &maskaddr;
682 }
683 if ((addrp = numeric_to_addr(mask)) != NULL)
684 return addrp;
685 if (string_to_number(mask, 0, 128, &bits) == -1)
686 exit_error(PARAMETER_PROBLEM,
687 "invalid mask `%s' specified", mask);
688 if (bits != 0) {
689 char *p = (char *)&maskaddr;
690 memset(p, 0xff, bits / 8);
691 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
692 p[bits / 8] = 0xff << (8 - (bits & 7));
693 return &maskaddr;
694 }
695
696 memset(&maskaddr, 0, sizeof maskaddr);
697 return &maskaddr;
698 }
699
700 void
parse_hostnetworkmask(const char * name,struct in6_addr ** addrpp,struct in6_addr * maskp,unsigned int * naddrs)701 parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
702 struct in6_addr *maskp, unsigned int *naddrs)
703 {
704 struct in6_addr *addrp;
705 char buf[256];
706 char *p;
707 int i, j, n;
708
709 strncpy(buf, name, sizeof(buf) - 1);
710 buf[sizeof(buf) - 1] = '\0';
711 if ((p = strrchr(buf, '/')) != NULL) {
712 *p = '\0';
713 addrp = parse_mask(p + 1);
714 } else
715 addrp = parse_mask(NULL);
716 in6addrcpy(maskp, addrp);
717
718 /* if a null mask is given, the name is ignored, like in "any/0" */
719 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
720 strcpy(buf, "::");
721
722 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
723 n = *naddrs;
724 for (i = 0, j = 0; i < n; i++) {
725 int k;
726 for (k = 0; k < 4; k++)
727 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
728 j++;
729 for (k = 0; k < j - 1; k++) {
730 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
731 (*naddrs)--;
732 j--;
733 break;
734 }
735 }
736 }
737 }
738
739 struct ip6tables_match *
find_match(const char * match_name,enum ip6t_tryload tryload,struct ip6tables_rule_match ** matches)740 find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
741 {
742 struct ip6tables_match *ptr;
743 const char *icmp6 = "icmp6";
744 const char *name;
745
746 /* This is ugly as hell. Nonetheless, there is no way of changing
747 * this without hurting backwards compatibility */
748 if ( (strcmp(match_name,"icmpv6") == 0) ||
749 (strcmp(match_name,"ipv6-icmp") == 0) ||
750 (strcmp(match_name,"icmp6") == 0) )
751 name = icmp6;
752 else
753 name = match_name;
754
755 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
756 if (strcmp(name, ptr->name) == 0) {
757 struct ip6tables_match *clone;
758
759 /* First match of this type: */
760 if (ptr->m == NULL)
761 break;
762
763 /* Second and subsequent clones */
764 clone = fw_malloc(sizeof(struct ip6tables_match));
765 memcpy(clone, ptr, sizeof(struct ip6tables_match));
766 clone->mflags = 0;
767 /* This is a clone: */
768 clone->next = clone;
769
770 ptr = clone;
771 break;
772 }
773 }
774
775 #ifndef NO_SHARED_LIBS
776 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
777 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
778 + strlen(name)];
779 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
780 if (dlopen(path, RTLD_NOW)) {
781 /* Found library. If it didn't register itself,
782 maybe they specified target as match. */
783 ptr = find_match(name, DONT_LOAD, NULL);
784
785 if (!ptr)
786 exit_error(PARAMETER_PROBLEM,
787 "Couldn't load match `%s'\n",
788 name);
789 } else if (tryload == LOAD_MUST_SUCCEED)
790 exit_error(PARAMETER_PROBLEM,
791 "Couldn't load match `%s':%s\n",
792 name, dlerror());
793 }
794 #else
795 if (ptr && !ptr->loaded) {
796 if (tryload != DONT_LOAD)
797 ptr->loaded = 1;
798 else
799 ptr = NULL;
800 }
801 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
802 exit_error(PARAMETER_PROBLEM,
803 "Couldn't find match `%s'\n", name);
804 }
805 #endif
806
807 if (ptr && matches) {
808 struct ip6tables_rule_match **i;
809 struct ip6tables_rule_match *newentry;
810
811 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
812
813 for (i = matches; *i; i = &(*i)->next) {
814 if (strcmp(name, (*i)->match->name) == 0)
815 (*i)->completed = 1;
816 }
817 newentry->match = ptr;
818 newentry->completed = 0;
819 newentry->next = NULL;
820 *i = newentry;
821 }
822
823 return ptr;
824 }
825
826 /* Christophe Burki wants `-p 6' to imply `-m tcp'. */
827 static struct ip6tables_match *
find_proto(const char * pname,enum ip6t_tryload tryload,int nolookup,struct ip6tables_rule_match ** matches)828 find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
829 {
830 unsigned int proto;
831
832 if (string_to_number(pname, 0, 255, &proto) != -1) {
833 char *protoname = proto_to_name(proto, nolookup);
834
835 if (protoname)
836 return find_match(protoname, tryload, matches);
837 } else
838 return find_match(pname, tryload, matches);
839
840 return NULL;
841 }
842
843 u_int16_t
parse_protocol(const char * s)844 parse_protocol(const char *s)
845 {
846 unsigned int proto;
847
848 if (string_to_number(s, 0, 255, &proto) == -1) {
849 struct protoent *pent;
850
851 /* first deal with the special case of 'all' to prevent
852 * people from being able to redefine 'all' in nsswitch
853 * and/or provoke expensive [not working] ldap/nis/...
854 * lookups */
855 if (!strcmp(s, "all"))
856 return 0;
857
858 if ((pent = getprotobyname(s)))
859 proto = pent->p_proto;
860 else {
861 unsigned int i;
862 for (i = 0;
863 i < sizeof(chain_protos)/sizeof(struct pprot);
864 i++) {
865 if (strcmp(s, chain_protos[i].name) == 0) {
866 proto = chain_protos[i].num;
867 break;
868 }
869 }
870 if (i == sizeof(chain_protos)/sizeof(struct pprot))
871 exit_error(PARAMETER_PROBLEM,
872 "unknown protocol `%s' specified",
873 s);
874 }
875 }
876
877 return (u_int16_t)proto;
878 }
879
880 /* proto means IPv6 extension header ? */
is_exthdr(u_int16_t proto)881 static int is_exthdr(u_int16_t proto)
882 {
883 return (proto == IPPROTO_HOPOPTS ||
884 proto == IPPROTO_ROUTING ||
885 proto == IPPROTO_FRAGMENT ||
886 proto == IPPROTO_ESP ||
887 proto == IPPROTO_AH ||
888 proto == IPPROTO_DSTOPTS);
889 }
890
parse_interface(const char * arg,char * vianame,unsigned char * mask)891 void parse_interface(const char *arg, char *vianame, unsigned char *mask)
892 {
893 int vialen = strlen(arg);
894 unsigned int i;
895
896 memset(mask, 0, IFNAMSIZ);
897 memset(vianame, 0, IFNAMSIZ);
898
899 if (vialen + 1 > IFNAMSIZ)
900 exit_error(PARAMETER_PROBLEM,
901 "interface name `%s' must be shorter than IFNAMSIZ"
902 " (%i)", arg, IFNAMSIZ-1);
903
904 strcpy(vianame, arg);
905 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
906 memset(mask, 0, IFNAMSIZ);
907 else if (vianame[vialen - 1] == '+') {
908 memset(mask, 0xFF, vialen - 1);
909 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
910 /* Don't remove `+' here! -HW */
911 } else {
912 /* Include nul-terminator in match */
913 memset(mask, 0xFF, vialen + 1);
914 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
915 for (i = 0; vianame[i]; i++) {
916 if (vianame[i] == ':' ||
917 vianame[i] == '!' ||
918 vianame[i] == '*') {
919 printf("Warning: weird character in interface"
920 " `%s' (No aliases, :, ! or *).\n",
921 vianame);
922 break;
923 }
924 }
925 }
926 }
927
928 /* Can't be zero. */
929 static int
parse_rulenumber(const char * rule)930 parse_rulenumber(const char *rule)
931 {
932 unsigned int rulenum;
933
934 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
935 exit_error(PARAMETER_PROBLEM,
936 "Invalid rule number `%s'", rule);
937
938 return rulenum;
939 }
940
941 static const char *
parse_target(const char * targetname)942 parse_target(const char *targetname)
943 {
944 const char *ptr;
945
946 if (strlen(targetname) < 1)
947 exit_error(PARAMETER_PROBLEM,
948 "Invalid target name (too short)");
949
950 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
951 exit_error(PARAMETER_PROBLEM,
952 "Invalid target name `%s' (%u chars max)",
953 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
954
955 for (ptr = targetname; *ptr; ptr++)
956 if (isspace(*ptr))
957 exit_error(PARAMETER_PROBLEM,
958 "Invalid target name `%s'", targetname);
959 return targetname;
960 }
961
962 int
string_to_number_ll(const char * s,unsigned long long min,unsigned long long max,unsigned long long * ret)963 string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
964 unsigned long long *ret)
965 {
966 unsigned long long number;
967 char *end;
968
969 /* Handle hex, octal, etc. */
970 errno = 0;
971 number = strtoull(s, &end, 0);
972 if (*end == '\0' && end != s) {
973 /* we parsed a number, let's see if we want this */
974 if (errno != ERANGE && min <= number && (!max || number <= max)) {
975 *ret = number;
976 return 0;
977 }
978 }
979 return -1;
980 }
981
982 int
string_to_number_l(const char * s,unsigned long min,unsigned long max,unsigned long * ret)983 string_to_number_l(const char *s, unsigned long min, unsigned long max,
984 unsigned long *ret)
985 {
986 int result;
987 unsigned long long number;
988
989 result = string_to_number_ll(s, min, max, &number);
990 *ret = (unsigned long)number;
991
992 return result;
993 }
994
string_to_number(const char * s,unsigned int min,unsigned int max,unsigned int * ret)995 int string_to_number(const char *s, unsigned int min, unsigned int max,
996 unsigned int *ret)
997 {
998 int result;
999 unsigned long number;
1000
1001 result = string_to_number_l(s, min, max, &number);
1002 *ret = (unsigned int)number;
1003
1004 return result;
1005 }
1006
1007 static void
set_option(unsigned int * options,unsigned int option,u_int8_t * invflg,int invert)1008 set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1009 int invert)
1010 {
1011 if (*options & option)
1012 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1013 opt2char(option));
1014 *options |= option;
1015
1016 if (invert) {
1017 unsigned int i;
1018 for (i = 0; 1 << i != option; i++);
1019
1020 if (!inverse_for_options[i])
1021 exit_error(PARAMETER_PROBLEM,
1022 "cannot have ! before -%c",
1023 opt2char(option));
1024 *invflg |= inverse_for_options[i];
1025 }
1026 }
1027
1028 struct ip6tables_target *
find_target(const char * name,enum ip6t_tryload tryload)1029 find_target(const char *name, enum ip6t_tryload tryload)
1030 {
1031 struct ip6tables_target *ptr;
1032
1033 /* Standard target? */
1034 if (strcmp(name, "") == 0
1035 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1036 || strcmp(name, IP6TC_LABEL_DROP) == 0
1037 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1038 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1039 name = "standard";
1040
1041 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1042 if (strcmp(name, ptr->name) == 0)
1043 break;
1044 }
1045
1046 #ifndef NO_SHARED_LIBS
1047 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
1048 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
1049 + strlen(name)];
1050 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
1051 if (dlopen(path, RTLD_NOW)) {
1052 /* Found library. If it didn't register itself,
1053 maybe they specified match as a target. */
1054 ptr = find_target(name, DONT_LOAD);
1055 if (!ptr)
1056 exit_error(PARAMETER_PROBLEM,
1057 "Couldn't load target `%s'\n",
1058 name);
1059 } else if (tryload == LOAD_MUST_SUCCEED)
1060 exit_error(PARAMETER_PROBLEM,
1061 "Couldn't load target `%s':%s\n",
1062 name, dlerror());
1063 }
1064 #else
1065 if (ptr && !ptr->loaded) {
1066 if (tryload != DONT_LOAD)
1067 ptr->loaded = 1;
1068 else
1069 ptr = NULL;
1070 }
1071 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1072 exit_error(PARAMETER_PROBLEM,
1073 "Couldn't find target `%s'\n", name);
1074 }
1075 #endif
1076
1077 if (ptr)
1078 ptr->used = 1;
1079
1080 return ptr;
1081 }
1082
1083 static struct option *
merge_options(struct option * oldopts,const struct option * newopts,unsigned int * option_offset)1084 merge_options(struct option *oldopts, const struct option *newopts,
1085 unsigned int *option_offset)
1086 {
1087 unsigned int num_old, num_new, i;
1088 struct option *merge;
1089
1090 for (num_old = 0; oldopts[num_old].name; num_old++);
1091 for (num_new = 0; newopts[num_new].name; num_new++);
1092
1093 global_option_offset += OPTION_OFFSET;
1094 *option_offset = global_option_offset;
1095
1096 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1097 memcpy(merge, oldopts, num_old * sizeof(struct option));
1098 free_opts(0); /* Release previous options merged if any */
1099 for (i = 0; i < num_new; i++) {
1100 merge[num_old + i] = newopts[i];
1101 merge[num_old + i].val += *option_offset;
1102 }
1103 memset(merge + num_old + num_new, 0, sizeof(struct option));
1104
1105 return merge;
1106 }
1107
compatible_revision(const char * name,u_int8_t revision,int opt)1108 static int compatible_revision(const char *name, u_int8_t revision, int opt)
1109 {
1110 struct ip6t_get_revision rev;
1111 socklen_t s = sizeof(rev);
1112 int max_rev, sockfd;
1113
1114 sockfd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW);
1115 if (sockfd < 0) {
1116 fprintf(stderr, "Could not open socket to kernel: %s\n",
1117 strerror(errno));
1118 exit(1);
1119 }
1120
1121 strcpy(rev.name, name);
1122 rev.revision = revision;
1123
1124 load_ip6tables_ko(modprobe);
1125
1126 max_rev = getsockopt(sockfd, IPPROTO_IPV6, opt, &rev, &s);
1127 if (max_rev < 0) {
1128 /* Definitely don't support this? */
1129 if (errno == EPROTONOSUPPORT) {
1130 close(sockfd);
1131 return 0;
1132 } else if (errno == ENOPROTOOPT) {
1133 close(sockfd);
1134 /* Assume only revision 0 support (old kernel) */
1135 return (revision == 0);
1136 } else {
1137 fprintf(stderr, "getsockopt failed strangely: %s\n",
1138 strerror(errno));
1139 exit(1);
1140 }
1141 }
1142 close(sockfd);
1143 return 1;
1144 }
1145
compatible_match_revision(const char * name,u_int8_t revision)1146 static int compatible_match_revision(const char *name, u_int8_t revision)
1147 {
1148 return compatible_revision(name, revision, IP6T_SO_GET_REVISION_MATCH);
1149 }
1150
1151 void
register_match6(struct ip6tables_match * me)1152 register_match6(struct ip6tables_match *me)
1153 {
1154 struct ip6tables_match **i, *old;
1155
1156 if (strcmp(me->version, program_version) != 0) {
1157 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1158 program_name, me->name, me->version, program_version);
1159 exit(1);
1160 }
1161
1162 /* Revision field stole a char from name. */
1163 if (strlen(me->name) >= IP6T_FUNCTION_MAXNAMELEN-1) {
1164 fprintf(stderr, "%s: target `%s' has invalid name\n",
1165 program_name, me->name);
1166 exit(1);
1167 }
1168
1169 old = find_match(me->name, DURING_LOAD, NULL);
1170 if (old) {
1171 if (old->revision == me->revision) {
1172 fprintf(stderr,
1173 "%s: match `%s' already registered.\n",
1174 program_name, me->name);
1175 exit(1);
1176 }
1177
1178 /* Now we have two (or more) options, check compatibility. */
1179 if (compatible_match_revision(old->name, old->revision)
1180 && old->revision > me->revision)
1181 return;
1182
1183 /* Replace if compatible. */
1184 if (!compatible_match_revision(me->name, me->revision))
1185 return;
1186
1187 /* Delete old one. */
1188 for (i = &ip6tables_matches; *i!=old; i = &(*i)->next);
1189 *i = old->next;
1190 }
1191
1192 if (me->size != IP6T_ALIGN(me->size)) {
1193 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1194 program_name, me->name, (unsigned int)me->size);
1195 exit(1);
1196 }
1197
1198 /* Append to list. */
1199 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1200 me->next = NULL;
1201 *i = me;
1202
1203 me->m = NULL;
1204 me->mflags = 0;
1205 }
1206
1207 void
register_target6(struct ip6tables_target * me)1208 register_target6(struct ip6tables_target *me)
1209 {
1210 if (strcmp(me->version, program_version) != 0) {
1211 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1212 program_name, me->name, me->version, program_version);
1213 exit(1);
1214 }
1215
1216 if (find_target(me->name, DURING_LOAD)) {
1217 fprintf(stderr, "%s: target `%s' already registered.\n",
1218 program_name, me->name);
1219 exit(1);
1220 }
1221
1222 if (me->size != IP6T_ALIGN(me->size)) {
1223 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1224 program_name, me->name, (unsigned int)me->size);
1225 exit(1);
1226 }
1227
1228 /* Prepend to list. */
1229 me->next = ip6tables_targets;
1230 ip6tables_targets = me;
1231 me->t = NULL;
1232 me->tflags = 0;
1233 }
1234
1235 static void
print_num(u_int64_t number,unsigned int format)1236 print_num(u_int64_t number, unsigned int format)
1237 {
1238 if (format & FMT_KILOMEGAGIGA) {
1239 if (number > 99999) {
1240 number = (number + 500) / 1000;
1241 if (number > 9999) {
1242 number = (number + 500) / 1000;
1243 if (number > 9999) {
1244 number = (number + 500) / 1000;
1245 if (number > 9999) {
1246 number = (number + 500) / 1000;
1247 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
1248 }
1249 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
1250 }
1251 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
1252 } else
1253 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
1254 } else
1255 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
1256 } else
1257 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
1258 }
1259
1260
1261 static void
print_header(unsigned int format,const char * chain,ip6tc_handle_t * handle)1262 print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1263 {
1264 struct ip6t_counters counters;
1265 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1266 printf("Chain %s", chain);
1267 if (pol) {
1268 printf(" (policy %s", pol);
1269 if (!(format & FMT_NOCOUNTS)) {
1270 fputc(' ', stdout);
1271 print_num(counters.pcnt, (format|FMT_NOTABLE));
1272 fputs("packets, ", stdout);
1273 print_num(counters.bcnt, (format|FMT_NOTABLE));
1274 fputs("bytes", stdout);
1275 }
1276 printf(")\n");
1277 } else {
1278 unsigned int refs;
1279 if (!ip6tc_get_references(&refs, chain, handle))
1280 printf(" (ERROR obtaining refs)\n");
1281 else
1282 printf(" (%u references)\n", refs);
1283 }
1284
1285 if (format & FMT_LINENUMBERS)
1286 printf(FMT("%-4s ", "%s "), "num");
1287 if (!(format & FMT_NOCOUNTS)) {
1288 if (format & FMT_KILOMEGAGIGA) {
1289 printf(FMT("%5s ","%s "), "pkts");
1290 printf(FMT("%5s ","%s "), "bytes");
1291 } else {
1292 printf(FMT("%8s ","%s "), "pkts");
1293 printf(FMT("%10s ","%s "), "bytes");
1294 }
1295 }
1296 if (!(format & FMT_NOTARGET))
1297 printf(FMT("%-9s ","%s "), "target");
1298 fputs(" prot ", stdout);
1299 if (format & FMT_OPTIONS)
1300 fputs("opt", stdout);
1301 if (format & FMT_VIA) {
1302 printf(FMT(" %-6s ","%s "), "in");
1303 printf(FMT("%-6s ","%s "), "out");
1304 }
1305 printf(FMT(" %-19s ","%s "), "source");
1306 printf(FMT(" %-19s "," %s "), "destination");
1307 printf("\n");
1308 }
1309
1310
1311 static int
print_match(const struct ip6t_entry_match * m,const struct ip6t_ip6 * ip,int numeric)1312 print_match(const struct ip6t_entry_match *m,
1313 const struct ip6t_ip6 *ip,
1314 int numeric)
1315 {
1316 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
1317
1318 if (match) {
1319 if (match->print)
1320 match->print(ip, m, numeric);
1321 else
1322 printf("%s ", match->name);
1323 } else {
1324 if (m->u.user.name[0])
1325 printf("UNKNOWN match `%s' ", m->u.user.name);
1326 }
1327 /* Don't stop iterating. */
1328 return 0;
1329 }
1330
1331 /* e is called `fw' here for hysterical raisins */
1332 static void
print_firewall(const struct ip6t_entry * fw,const char * targname,unsigned int num,unsigned int format,const ip6tc_handle_t handle)1333 print_firewall(const struct ip6t_entry *fw,
1334 const char *targname,
1335 unsigned int num,
1336 unsigned int format,
1337 const ip6tc_handle_t handle)
1338 {
1339 struct ip6tables_target *target = NULL;
1340 const struct ip6t_entry_target *t;
1341 u_int8_t flags;
1342 char buf[BUFSIZ];
1343
1344 if (!ip6tc_is_chain(targname, handle))
1345 target = find_target(targname, TRY_LOAD);
1346 else
1347 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1348
1349 t = ip6t_get_target((struct ip6t_entry *)fw);
1350 flags = fw->ipv6.flags;
1351
1352 if (format & FMT_LINENUMBERS)
1353 printf(FMT("%-4u ", "%u "), num+1);
1354
1355 if (!(format & FMT_NOCOUNTS)) {
1356 print_num(fw->counters.pcnt, format);
1357 print_num(fw->counters.bcnt, format);
1358 }
1359
1360 if (!(format & FMT_NOTARGET))
1361 printf(FMT("%-9s ", "%s "), targname);
1362
1363 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1364 {
1365 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
1366 if (pname)
1367 printf(FMT("%-5s", "%s "), pname);
1368 else
1369 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1370 }
1371
1372 if (format & FMT_OPTIONS) {
1373 if (format & FMT_NOTABLE)
1374 fputs("opt ", stdout);
1375 fputc(' ', stdout); /* Invert flag of FRAG */
1376 fputc(' ', stdout); /* -f */
1377 fputc(' ', stdout);
1378 }
1379
1380 if (format & FMT_VIA) {
1381 char iface[IFNAMSIZ+2];
1382
1383 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1384 iface[0] = '!';
1385 iface[1] = '\0';
1386 }
1387 else iface[0] = '\0';
1388
1389 if (fw->ipv6.iniface[0] != '\0') {
1390 strcat(iface, fw->ipv6.iniface);
1391 }
1392 else if (format & FMT_NUMERIC) strcat(iface, "*");
1393 else strcat(iface, "any");
1394 printf(FMT(" %-6s ","in %s "), iface);
1395
1396 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1397 iface[0] = '!';
1398 iface[1] = '\0';
1399 }
1400 else iface[0] = '\0';
1401
1402 if (fw->ipv6.outiface[0] != '\0') {
1403 strcat(iface, fw->ipv6.outiface);
1404 }
1405 else if (format & FMT_NUMERIC) strcat(iface, "*");
1406 else strcat(iface, "any");
1407 printf(FMT("%-6s ","out %s "), iface);
1408 }
1409
1410 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1411 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1412 && !(format & FMT_NUMERIC))
1413 printf(FMT("%-19s ","%s "), "anywhere");
1414 else {
1415 if (format & FMT_NUMERIC)
1416 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
1417 else
1418 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
1419 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1420 printf(FMT("%-19s ","%s "), buf);
1421 }
1422
1423 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1424 if (!memcmp(&fw->ipv6.dmsk, &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.dst)));
1430 else
1431 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
1432 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1433 printf(FMT("%-19s","-> %s"), buf);
1434 }
1435
1436 if (format & FMT_NOTABLE)
1437 fputs(" ", stdout);
1438
1439 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1440
1441 if (target) {
1442 if (target->print)
1443 /* Print the target information. */
1444 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1445 } else if (t->u.target_size != sizeof(*t))
1446 printf("[%u bytes of unknown target data] ",
1447 (unsigned int)(t->u.target_size - sizeof(*t)));
1448
1449 if (!(format & FMT_NONEWLINE))
1450 fputc('\n', stdout);
1451 }
1452
1453 static void
print_firewall_line(const struct ip6t_entry * fw,const ip6tc_handle_t h)1454 print_firewall_line(const struct ip6t_entry *fw,
1455 const ip6tc_handle_t h)
1456 {
1457 struct ip6t_entry_target *t;
1458
1459 t = ip6t_get_target((struct ip6t_entry *)fw);
1460 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1461 }
1462
1463 static int
append_entry(const ip6t_chainlabel chain,struct ip6t_entry * fw,unsigned int nsaddrs,const struct in6_addr saddrs[],unsigned int ndaddrs,const struct in6_addr daddrs[],int verbose,ip6tc_handle_t * handle)1464 append_entry(const ip6t_chainlabel chain,
1465 struct ip6t_entry *fw,
1466 unsigned int nsaddrs,
1467 const struct in6_addr saddrs[],
1468 unsigned int ndaddrs,
1469 const struct in6_addr daddrs[],
1470 int verbose,
1471 ip6tc_handle_t *handle)
1472 {
1473 unsigned int i, j;
1474 int ret = 1;
1475
1476 for (i = 0; i < nsaddrs; i++) {
1477 fw->ipv6.src = saddrs[i];
1478 for (j = 0; j < ndaddrs; j++) {
1479 fw->ipv6.dst = daddrs[j];
1480 if (verbose)
1481 print_firewall_line(fw, *handle);
1482 ret &= ip6tc_append_entry(chain, fw, handle);
1483 }
1484 }
1485
1486 return ret;
1487 }
1488
1489 static int
replace_entry(const ip6t_chainlabel chain,struct ip6t_entry * fw,unsigned int rulenum,const struct in6_addr * saddr,const struct in6_addr * daddr,int verbose,ip6tc_handle_t * handle)1490 replace_entry(const ip6t_chainlabel chain,
1491 struct ip6t_entry *fw,
1492 unsigned int rulenum,
1493 const struct in6_addr *saddr,
1494 const struct in6_addr *daddr,
1495 int verbose,
1496 ip6tc_handle_t *handle)
1497 {
1498 fw->ipv6.src = *saddr;
1499 fw->ipv6.dst = *daddr;
1500
1501 if (verbose)
1502 print_firewall_line(fw, *handle);
1503 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1504 }
1505
1506 static int
insert_entry(const ip6t_chainlabel chain,struct ip6t_entry * fw,unsigned int rulenum,unsigned int nsaddrs,const struct in6_addr saddrs[],unsigned int ndaddrs,const struct in6_addr daddrs[],int verbose,ip6tc_handle_t * handle)1507 insert_entry(const ip6t_chainlabel chain,
1508 struct ip6t_entry *fw,
1509 unsigned int rulenum,
1510 unsigned int nsaddrs,
1511 const struct in6_addr saddrs[],
1512 unsigned int ndaddrs,
1513 const struct in6_addr daddrs[],
1514 int verbose,
1515 ip6tc_handle_t *handle)
1516 {
1517 unsigned int i, j;
1518 int ret = 1;
1519
1520 for (i = 0; i < nsaddrs; i++) {
1521 fw->ipv6.src = saddrs[i];
1522 for (j = 0; j < ndaddrs; j++) {
1523 fw->ipv6.dst = daddrs[j];
1524 if (verbose)
1525 print_firewall_line(fw, *handle);
1526 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1527 }
1528 }
1529
1530 return ret;
1531 }
1532
1533 static unsigned char *
make_delete_mask(struct ip6t_entry * fw,struct ip6tables_rule_match * matches)1534 make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
1535 {
1536 /* Establish mask for comparison */
1537 unsigned int size;
1538 struct ip6tables_rule_match *matchp;
1539 unsigned char *mask, *mptr;
1540
1541 size = sizeof(struct ip6t_entry);
1542 for (matchp = matches; matchp; matchp = matchp->next)
1543 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1544
1545 mask = fw_calloc(1, size
1546 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1547 + ip6tables_targets->size);
1548
1549 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1550 mptr = mask + sizeof(struct ip6t_entry);
1551
1552 for (matchp = matches; matchp; matchp = matchp->next) {
1553 memset(mptr, 0xFF,
1554 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1555 + matchp->match->userspacesize);
1556 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
1557 }
1558
1559 memset(mptr, 0xFF,
1560 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1561 + ip6tables_targets->userspacesize);
1562
1563 return mask;
1564 }
1565
1566 static int
delete_entry(const ip6t_chainlabel chain,struct ip6t_entry * fw,unsigned int nsaddrs,const struct in6_addr saddrs[],unsigned int ndaddrs,const struct in6_addr daddrs[],int verbose,ip6tc_handle_t * handle,struct ip6tables_rule_match * matches)1567 delete_entry(const ip6t_chainlabel chain,
1568 struct ip6t_entry *fw,
1569 unsigned int nsaddrs,
1570 const struct in6_addr saddrs[],
1571 unsigned int ndaddrs,
1572 const struct in6_addr daddrs[],
1573 int verbose,
1574 ip6tc_handle_t *handle,
1575 struct ip6tables_rule_match *matches)
1576 {
1577 unsigned int i, j;
1578 int ret = 1;
1579 unsigned char *mask;
1580
1581 mask = make_delete_mask(fw, matches);
1582 for (i = 0; i < nsaddrs; i++) {
1583 fw->ipv6.src = saddrs[i];
1584 for (j = 0; j < ndaddrs; j++) {
1585 fw->ipv6.dst = daddrs[j];
1586 if (verbose)
1587 print_firewall_line(fw, *handle);
1588 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
1589 }
1590 }
1591 free(mask);
1592
1593 return ret;
1594 }
1595
1596 int
for_each_chain(int (* fn)(const ip6t_chainlabel,int,ip6tc_handle_t *),int verbose,int builtinstoo,ip6tc_handle_t * handle)1597 for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1598 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1599 {
1600 int ret = 1;
1601 const char *chain;
1602 char *chains;
1603 unsigned int i, chaincount = 0;
1604
1605 chain = ip6tc_first_chain(handle);
1606 while (chain) {
1607 chaincount++;
1608 chain = ip6tc_next_chain(handle);
1609 }
1610
1611 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1612 i = 0;
1613 chain = ip6tc_first_chain(handle);
1614 while (chain) {
1615 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1616 i++;
1617 chain = ip6tc_next_chain(handle);
1618 }
1619
1620 for (i = 0; i < chaincount; i++) {
1621 if (!builtinstoo
1622 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1623 *handle) == 1)
1624 continue;
1625 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1626 }
1627
1628 free(chains);
1629 return ret;
1630 }
1631
1632 int
flush_entries(const ip6t_chainlabel chain,int verbose,ip6tc_handle_t * handle)1633 flush_entries(const ip6t_chainlabel chain, int verbose,
1634 ip6tc_handle_t *handle)
1635 {
1636 if (!chain)
1637 return for_each_chain(flush_entries, verbose, 1, handle);
1638
1639 if (verbose)
1640 fprintf(stdout, "Flushing chain `%s'\n", chain);
1641 return ip6tc_flush_entries(chain, handle);
1642 }
1643
1644 static int
zero_entries(const ip6t_chainlabel chain,int verbose,ip6tc_handle_t * handle)1645 zero_entries(const ip6t_chainlabel chain, int verbose,
1646 ip6tc_handle_t *handle)
1647 {
1648 if (!chain)
1649 return for_each_chain(zero_entries, verbose, 1, handle);
1650
1651 if (verbose)
1652 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1653 return ip6tc_zero_entries(chain, handle);
1654 }
1655
1656 int
delete_chain(const ip6t_chainlabel chain,int verbose,ip6tc_handle_t * handle)1657 delete_chain(const ip6t_chainlabel chain, int verbose,
1658 ip6tc_handle_t *handle)
1659 {
1660 if (!chain)
1661 return for_each_chain(delete_chain, verbose, 0, handle);
1662
1663 if (verbose)
1664 fprintf(stdout, "Deleting chain `%s'\n", chain);
1665 return ip6tc_delete_chain(chain, handle);
1666 }
1667
1668 static int
list_entries(const ip6t_chainlabel chain,int verbose,int numeric,int expanded,int linenumbers,ip6tc_handle_t * handle)1669 list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1670 int expanded, int linenumbers, ip6tc_handle_t *handle)
1671 {
1672 int found = 0;
1673 unsigned int format;
1674 const char *this;
1675
1676 format = FMT_OPTIONS;
1677 if (!verbose)
1678 format |= FMT_NOCOUNTS;
1679 else
1680 format |= FMT_VIA;
1681
1682 if (numeric)
1683 format |= FMT_NUMERIC;
1684
1685 if (!expanded)
1686 format |= FMT_KILOMEGAGIGA;
1687
1688 if (linenumbers)
1689 format |= FMT_LINENUMBERS;
1690
1691 for (this = ip6tc_first_chain(handle);
1692 this;
1693 this = ip6tc_next_chain(handle)) {
1694 const struct ip6t_entry *i;
1695 unsigned int num;
1696
1697 if (chain && strcmp(chain, this) != 0)
1698 continue;
1699
1700 if (found) printf("\n");
1701
1702 print_header(format, this, handle);
1703 i = ip6tc_first_rule(this, handle);
1704
1705 num = 0;
1706 while (i) {
1707 print_firewall(i,
1708 ip6tc_get_target(i, handle),
1709 num++,
1710 format,
1711 *handle);
1712 i = ip6tc_next_rule(i, handle);
1713 }
1714 found = 1;
1715 }
1716
1717 errno = ENOENT;
1718 return found;
1719 }
1720
get_modprobe(void)1721 static char *get_modprobe(void)
1722 {
1723 int procfile;
1724 char *ret;
1725
1726 #define PROCFILE_BUFSIZ 1024
1727 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1728 if (procfile < 0)
1729 return NULL;
1730
1731 ret = malloc(PROCFILE_BUFSIZ);
1732 if (ret) {
1733 memset(ret, 0, PROCFILE_BUFSIZ);
1734 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
1735 case -1: goto fail;
1736 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
1737 }
1738 if (ret[strlen(ret)-1]=='\n')
1739 ret[strlen(ret)-1]=0;
1740 close(procfile);
1741 return ret;
1742 }
1743 fail:
1744 free(ret);
1745 close(procfile);
1746 return NULL;
1747 }
1748
ip6tables_insmod(const char * modname,const char * modprobe)1749 int ip6tables_insmod(const char *modname, const char *modprobe)
1750 {
1751 char *buf = NULL;
1752 char *argv[3];
1753 int status;
1754
1755 /* If they don't explicitly set it, read out of kernel */
1756 if (!modprobe) {
1757 buf = get_modprobe();
1758 if (!buf)
1759 return -1;
1760 modprobe = buf;
1761 }
1762
1763 switch (fork()) {
1764 case 0:
1765 argv[0] = (char *)modprobe;
1766 argv[1] = (char *)modname;
1767 argv[2] = NULL;
1768 execv(argv[0], argv);
1769
1770 /* not usually reached */
1771 exit(1);
1772 case -1:
1773 return -1;
1774
1775 default: /* parent */
1776 wait(&status);
1777 }
1778
1779 free(buf);
1780 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1781 return 0;
1782 return -1;
1783 }
1784
load_ip6tables_ko(const char * modprobe)1785 int load_ip6tables_ko(const char *modprobe)
1786 {
1787 static int loaded = 0;
1788 static int ret = -1;
1789
1790 if (!loaded) {
1791 ret = ip6tables_insmod("ip6_tables", modprobe);
1792 loaded = 1;
1793 }
1794
1795 return ret;
1796 }
1797
1798 static struct ip6t_entry *
generate_entry(const struct ip6t_entry * fw,struct ip6tables_rule_match * matches,struct ip6t_entry_target * target)1799 generate_entry(const struct ip6t_entry *fw,
1800 struct ip6tables_rule_match *matches,
1801 struct ip6t_entry_target *target)
1802 {
1803 unsigned int size;
1804 struct ip6tables_rule_match *matchp;
1805 struct ip6t_entry *e;
1806
1807 size = sizeof(struct ip6t_entry);
1808 for (matchp = matches; matchp; matchp = matchp->next)
1809 size += matchp->match->m->u.match_size;
1810
1811 e = fw_malloc(size + target->u.target_size);
1812 *e = *fw;
1813 e->target_offset = size;
1814 e->next_offset = size + target->u.target_size;
1815
1816 size = 0;
1817 for (matchp = matches; matchp; matchp = matchp->next) {
1818 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1819 size += matchp->match->m->u.match_size;
1820 }
1821 memcpy(e->elems + size, target, target->u.target_size);
1822
1823 return e;
1824 }
1825
clear_rule_matches(struct ip6tables_rule_match ** matches)1826 void clear_rule_matches(struct ip6tables_rule_match **matches)
1827 {
1828 struct ip6tables_rule_match *matchp, *tmp;
1829
1830 for (matchp = *matches; matchp;) {
1831 tmp = matchp->next;
1832 if (matchp->match->m) {
1833 free(matchp->match->m);
1834 matchp->match->m = NULL;
1835 }
1836 if (matchp->match == matchp->match->next) {
1837 free(matchp->match);
1838 matchp->match = NULL;
1839 }
1840 free(matchp);
1841 matchp = tmp;
1842 }
1843
1844 *matches = NULL;
1845 }
1846
set_revision(char * name,u_int8_t revision)1847 static void set_revision(char *name, u_int8_t revision)
1848 {
1849 /* Old kernel sources don't have ".revision" field,
1850 but we stole a byte from name. */
1851 name[IP6T_FUNCTION_MAXNAMELEN - 2] = '\0';
1852 name[IP6T_FUNCTION_MAXNAMELEN - 1] = revision;
1853 }
1854
do_command6(int argc,char * argv[],char ** table,ip6tc_handle_t * handle)1855 int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1856 {
1857 struct ip6t_entry fw, *e = NULL;
1858 int invert = 0;
1859 unsigned int nsaddrs = 0, ndaddrs = 0;
1860 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1861
1862 int c, verbose = 0;
1863 const char *chain = NULL;
1864 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1865 const char *policy = NULL, *newname = NULL;
1866 unsigned int rulenum = 0, options = 0, command = 0;
1867 const char *pcnt = NULL, *bcnt = NULL;
1868 int ret = 1;
1869 struct ip6tables_match *m;
1870 struct ip6tables_rule_match *matches = NULL;
1871 struct ip6tables_rule_match *matchp;
1872 struct ip6tables_target *target = NULL;
1873 struct ip6tables_target *t;
1874 const char *jumpto = "";
1875 char *protocol = NULL;
1876 int proto_used = 0;
1877
1878 memset(&fw, 0, sizeof(fw));
1879
1880 /* re-set optind to 0 in case do_command gets called
1881 * a second time */
1882 optind = 0;
1883
1884 /* clear mflags in case do_command gets called a second time
1885 * (we clear the global list of all matches for security)*/
1886 for (m = ip6tables_matches; m; m = m->next)
1887 m->mflags = 0;
1888
1889 for (t = ip6tables_targets; t; t = t->next) {
1890 t->tflags = 0;
1891 t->used = 0;
1892 }
1893
1894 /* Suppress error messages: we may add new options if we
1895 demand-load a protocol. */
1896 opterr = 0;
1897
1898 while ((c = getopt_long(argc, argv,
1899 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
1900 opts, NULL)) != -1) {
1901 switch (c) {
1902 /*
1903 * Command selection
1904 */
1905 case 'A':
1906 add_command(&command, CMD_APPEND, CMD_NONE,
1907 invert);
1908 chain = optarg;
1909 break;
1910
1911 case 'D':
1912 add_command(&command, CMD_DELETE, CMD_NONE,
1913 invert);
1914 chain = optarg;
1915 if (optind < argc && argv[optind][0] != '-'
1916 && argv[optind][0] != '!') {
1917 rulenum = parse_rulenumber(argv[optind++]);
1918 command = CMD_DELETE_NUM;
1919 }
1920 break;
1921
1922 case 'R':
1923 add_command(&command, CMD_REPLACE, CMD_NONE,
1924 invert);
1925 chain = optarg;
1926 if (optind < argc && argv[optind][0] != '-'
1927 && argv[optind][0] != '!')
1928 rulenum = parse_rulenumber(argv[optind++]);
1929 else
1930 exit_error(PARAMETER_PROBLEM,
1931 "-%c requires a rule number",
1932 cmd2char(CMD_REPLACE));
1933 break;
1934
1935 case 'I':
1936 add_command(&command, CMD_INSERT, CMD_NONE,
1937 invert);
1938 chain = optarg;
1939 if (optind < argc && argv[optind][0] != '-'
1940 && argv[optind][0] != '!')
1941 rulenum = parse_rulenumber(argv[optind++]);
1942 else rulenum = 1;
1943 break;
1944
1945 case 'L':
1946 add_command(&command, CMD_LIST, CMD_ZERO,
1947 invert);
1948 if (optarg) chain = optarg;
1949 else if (optind < argc && argv[optind][0] != '-'
1950 && argv[optind][0] != '!')
1951 chain = argv[optind++];
1952 break;
1953
1954 case 'F':
1955 add_command(&command, CMD_FLUSH, CMD_NONE,
1956 invert);
1957 if (optarg) chain = optarg;
1958 else if (optind < argc && argv[optind][0] != '-'
1959 && argv[optind][0] != '!')
1960 chain = argv[optind++];
1961 break;
1962
1963 case 'Z':
1964 add_command(&command, CMD_ZERO, CMD_LIST,
1965 invert);
1966 if (optarg) chain = optarg;
1967 else if (optind < argc && argv[optind][0] != '-'
1968 && argv[optind][0] != '!')
1969 chain = argv[optind++];
1970 break;
1971
1972 case 'N':
1973 if (optarg && (*optarg == '-' || *optarg == '!'))
1974 exit_error(PARAMETER_PROBLEM,
1975 "chain name not allowed to start "
1976 "with `%c'\n", *optarg);
1977 if (find_target(optarg, TRY_LOAD))
1978 exit_error(PARAMETER_PROBLEM,
1979 "chain name may not clash "
1980 "with target name\n");
1981 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1982 invert);
1983 chain = optarg;
1984 break;
1985
1986 case 'X':
1987 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1988 invert);
1989 if (optarg) chain = optarg;
1990 else if (optind < argc && argv[optind][0] != '-'
1991 && argv[optind][0] != '!')
1992 chain = argv[optind++];
1993 break;
1994
1995 case 'E':
1996 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1997 invert);
1998 chain = optarg;
1999 if (optind < argc && argv[optind][0] != '-'
2000 && argv[optind][0] != '!')
2001 newname = argv[optind++];
2002 else
2003 exit_error(PARAMETER_PROBLEM,
2004 "-%c requires old-chain-name and "
2005 "new-chain-name",
2006 cmd2char(CMD_RENAME_CHAIN));
2007 break;
2008
2009 case 'P':
2010 add_command(&command, CMD_SET_POLICY, CMD_NONE,
2011 invert);
2012 chain = optarg;
2013 if (optind < argc && argv[optind][0] != '-'
2014 && argv[optind][0] != '!')
2015 policy = argv[optind++];
2016 else
2017 exit_error(PARAMETER_PROBLEM,
2018 "-%c requires a chain and a policy",
2019 cmd2char(CMD_SET_POLICY));
2020 break;
2021
2022 case 'h':
2023 if (!optarg)
2024 optarg = argv[optind];
2025
2026 /* ip6tables -p icmp -h */
2027 if (!matches && protocol)
2028 find_match(protocol, TRY_LOAD, &matches);
2029
2030 exit_printhelp(matches);
2031
2032 /*
2033 * Option selection
2034 */
2035 case 'p':
2036 check_inverse(optarg, &invert, &optind, argc);
2037 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
2038 invert);
2039
2040 /* Canonicalize into lower case */
2041 for (protocol = argv[optind-1]; *protocol; protocol++)
2042 *protocol = tolower(*protocol);
2043
2044 protocol = argv[optind-1];
2045 fw.ipv6.proto = parse_protocol(protocol);
2046 fw.ipv6.flags |= IP6T_F_PROTO;
2047
2048 if (fw.ipv6.proto == 0
2049 && (fw.ipv6.invflags & IP6T_INV_PROTO))
2050 exit_error(PARAMETER_PROBLEM,
2051 "rule would never match protocol");
2052
2053 if (fw.ipv6.proto != IPPROTO_ESP &&
2054 is_exthdr(fw.ipv6.proto))
2055 printf("Warning: never matched protocol: %s. "
2056 "use exension match instead.", protocol);
2057 break;
2058
2059 case 's':
2060 check_inverse(optarg, &invert, &optind, argc);
2061 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
2062 invert);
2063 shostnetworkmask = argv[optind-1];
2064 break;
2065
2066 case 'd':
2067 check_inverse(optarg, &invert, &optind, argc);
2068 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
2069 invert);
2070 dhostnetworkmask = argv[optind-1];
2071 break;
2072
2073 case 'j':
2074 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
2075 invert);
2076 jumpto = parse_target(optarg);
2077 /* TRY_LOAD (may be chain name) */
2078 target = find_target(jumpto, TRY_LOAD);
2079
2080 if (target) {
2081 size_t size;
2082
2083 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
2084 + target->size;
2085
2086 target->t = fw_calloc(1, size);
2087 target->t->u.target_size = size;
2088 strcpy(target->t->u.user.name, jumpto);
2089 if (target->init != NULL)
2090 target->init(target->t, &fw.nfcache);
2091 opts = merge_options(opts, target->extra_opts, &target->option_offset);
2092 }
2093 break;
2094
2095
2096 case 'i':
2097 check_inverse(optarg, &invert, &optind, argc);
2098 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2099 invert);
2100 parse_interface(argv[optind-1],
2101 fw.ipv6.iniface,
2102 fw.ipv6.iniface_mask);
2103 break;
2104
2105 case 'o':
2106 check_inverse(optarg, &invert, &optind, argc);
2107 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2108 invert);
2109 parse_interface(argv[optind-1],
2110 fw.ipv6.outiface,
2111 fw.ipv6.outiface_mask);
2112 break;
2113
2114 case 'v':
2115 if (!verbose)
2116 set_option(&options, OPT_VERBOSE,
2117 &fw.ipv6.invflags, invert);
2118 verbose++;
2119 break;
2120
2121 case 'm': {
2122 size_t size;
2123
2124 if (invert)
2125 exit_error(PARAMETER_PROBLEM,
2126 "unexpected ! flag before --match");
2127
2128 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
2129 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2130 + m->size;
2131 m->m = fw_calloc(1, size);
2132 m->m->u.match_size = size;
2133 strcpy(m->m->u.user.name, m->name);
2134 set_revision(m->m->u.user.name, m->revision);
2135 if (m->init != NULL)
2136 m->init(m->m, &fw.nfcache);
2137 if (m != m->next)
2138 /* Merge options for non-cloned matches */
2139 opts = merge_options(opts, m->extra_opts, &m->option_offset);
2140 }
2141 break;
2142
2143 case 'n':
2144 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2145 invert);
2146 break;
2147
2148 case 't':
2149 if (invert)
2150 exit_error(PARAMETER_PROBLEM,
2151 "unexpected ! flag before --table");
2152 *table = argv[optind-1];
2153 break;
2154
2155 case 'x':
2156 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2157 invert);
2158 break;
2159
2160 case 'V':
2161 if (invert)
2162 printf("Not %s ;-)\n", program_version);
2163 else
2164 printf("%s v%s\n",
2165 program_name, program_version);
2166 exit(0);
2167
2168 case '0':
2169 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2170 invert);
2171 break;
2172
2173 case 'M':
2174 modprobe = optarg;
2175 break;
2176
2177 case 'c':
2178
2179 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2180 invert);
2181 pcnt = optarg;
2182 if (optind < argc && argv[optind][0] != '-'
2183 && argv[optind][0] != '!')
2184 bcnt = argv[optind++];
2185 else
2186 exit_error(PARAMETER_PROBLEM,
2187 "-%c requires packet and byte counter",
2188 opt2char(OPT_COUNTERS));
2189
2190 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
2191 exit_error(PARAMETER_PROBLEM,
2192 "-%c packet counter not numeric",
2193 opt2char(OPT_COUNTERS));
2194
2195 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
2196 exit_error(PARAMETER_PROBLEM,
2197 "-%c byte counter not numeric",
2198 opt2char(OPT_COUNTERS));
2199
2200 break;
2201
2202
2203 case 1: /* non option */
2204 if (optarg[0] == '!' && optarg[1] == '\0') {
2205 if (invert)
2206 exit_error(PARAMETER_PROBLEM,
2207 "multiple consecutive ! not"
2208 " allowed");
2209 invert = TRUE;
2210 optarg[0] = '\0';
2211 continue;
2212 }
2213 printf("Bad argument `%s'\n", optarg);
2214 exit_tryhelp(2);
2215
2216 default:
2217 if (!target
2218 || !(target->parse(c - target->option_offset,
2219 argv, invert,
2220 &target->tflags,
2221 &fw, &target->t))) {
2222 for (matchp = matches; matchp; matchp = matchp->next) {
2223 if (matchp->completed)
2224 continue;
2225 if (matchp->match->parse(c - matchp->match->option_offset,
2226 argv, invert,
2227 &matchp->match->mflags,
2228 &fw,
2229 &fw.nfcache,
2230 &matchp->match->m))
2231 break;
2232 }
2233 m = matchp ? matchp->match : NULL;
2234
2235 /* If you listen carefully, you can
2236 actually hear this code suck. */
2237
2238 /* some explanations (after four different bugs
2239 * in 3 different releases): If we encounter a
2240 * parameter, that has not been parsed yet,
2241 * it's not an option of an explicitly loaded
2242 * match or a target. However, we support
2243 * implicit loading of the protocol match
2244 * extension. '-p tcp' means 'l4 proto 6' and
2245 * at the same time 'load tcp protocol match on
2246 * demand if we specify --dport'.
2247 *
2248 * To make this work, we need to make sure:
2249 * - the parameter has not been parsed by
2250 * a match (m above)
2251 * - a protocol has been specified
2252 * - the protocol extension has not been
2253 * loaded yet, or is loaded and unused
2254 * [think of ip6tables-restore!]
2255 * - the protocol extension can be successively
2256 * loaded
2257 */
2258 if (m == NULL
2259 && protocol
2260 && (!find_proto(protocol, DONT_LOAD,
2261 options&OPT_NUMERIC, NULL)
2262 || (find_proto(protocol, DONT_LOAD,
2263 options&OPT_NUMERIC, NULL)
2264 && (proto_used == 0))
2265 )
2266 && (m = find_proto(protocol, TRY_LOAD,
2267 options&OPT_NUMERIC, &matches))) {
2268 /* Try loading protocol */
2269 size_t size;
2270
2271 proto_used = 1;
2272
2273 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2274 + m->size;
2275
2276 m->m = fw_calloc(1, size);
2277 m->m->u.match_size = size;
2278 strcpy(m->m->u.user.name, m->name);
2279 set_revision(m->m->u.user.name,
2280 m->revision);
2281 if (m->init != NULL)
2282 m->init(m->m, &fw.nfcache);
2283
2284 opts = merge_options(opts,
2285 m->extra_opts, &m->option_offset);
2286
2287 optind--;
2288 continue;
2289 }
2290
2291 if (!m)
2292 exit_error(PARAMETER_PROBLEM,
2293 "Unknown arg `%s'",
2294 argv[optind-1]);
2295 }
2296 }
2297 invert = FALSE;
2298 }
2299
2300 for (matchp = matches; matchp; matchp = matchp->next)
2301 matchp->match->final_check(matchp->match->mflags);
2302
2303 if (target)
2304 target->final_check(target->tflags);
2305
2306 /* Fix me: must put inverse options checking here --MN */
2307
2308 if (optind < argc)
2309 exit_error(PARAMETER_PROBLEM,
2310 "unknown arguments found on commandline");
2311 if (!command)
2312 exit_error(PARAMETER_PROBLEM, "no command specified");
2313 if (invert)
2314 exit_error(PARAMETER_PROBLEM,
2315 "nothing appropriate following !");
2316
2317 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
2318 if (!(options & OPT_DESTINATION))
2319 dhostnetworkmask = "::0/0";
2320 if (!(options & OPT_SOURCE))
2321 shostnetworkmask = "::0/0";
2322 }
2323
2324 if (shostnetworkmask)
2325 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2326 &(fw.ipv6.smsk), &nsaddrs);
2327
2328 if (dhostnetworkmask)
2329 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2330 &(fw.ipv6.dmsk), &ndaddrs);
2331
2332 if ((nsaddrs > 1 || ndaddrs > 1) &&
2333 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2334 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2335 " source or destination IP addresses");
2336
2337 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2338 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2339 "specify a unique address");
2340
2341 generic_opt_check(command, options);
2342
2343 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2344 exit_error(PARAMETER_PROBLEM,
2345 "chain name `%s' too long (must be under %i chars)",
2346 chain, IP6T_FUNCTION_MAXNAMELEN);
2347
2348 /* only allocate handle if we weren't called with a handle */
2349 if (!*handle)
2350 *handle = ip6tc_init(*table);
2351
2352 /* try to insmod the module if iptc_init failed */
2353 if (!*handle && load_ip6tables_ko(modprobe) != -1)
2354 *handle = ip6tc_init(*table);
2355
2356 if (!*handle)
2357 exit_error(VERSION_PROBLEM,
2358 "can't initialize ip6tables table `%s': %s",
2359 *table, ip6tc_strerror(errno));
2360
2361 if (command == CMD_APPEND
2362 || command == CMD_DELETE
2363 || command == CMD_INSERT
2364 || command == CMD_REPLACE) {
2365 if (strcmp(chain, "PREROUTING") == 0
2366 || strcmp(chain, "INPUT") == 0) {
2367 /* -o not valid with incoming packets. */
2368 if (options & OPT_VIANAMEOUT)
2369 exit_error(PARAMETER_PROBLEM,
2370 "Can't use -%c with %s\n",
2371 opt2char(OPT_VIANAMEOUT),
2372 chain);
2373 }
2374
2375 if (strcmp(chain, "POSTROUTING") == 0
2376 || strcmp(chain, "OUTPUT") == 0) {
2377 /* -i not valid with outgoing packets */
2378 if (options & OPT_VIANAMEIN)
2379 exit_error(PARAMETER_PROBLEM,
2380 "Can't use -%c with %s\n",
2381 opt2char(OPT_VIANAMEIN),
2382 chain);
2383 }
2384
2385 if (target && ip6tc_is_chain(jumpto, *handle)) {
2386 printf("Warning: using chain %s, not extension\n",
2387 jumpto);
2388
2389 if (target->t)
2390 free(target->t);
2391
2392 target = NULL;
2393 }
2394
2395 /* If they didn't specify a target, or it's a chain
2396 name, use standard. */
2397 if (!target
2398 && (strlen(jumpto) == 0
2399 || ip6tc_is_chain(jumpto, *handle))) {
2400 size_t size;
2401
2402 target = find_target(IP6T_STANDARD_TARGET,
2403 LOAD_MUST_SUCCEED);
2404
2405 size = sizeof(struct ip6t_entry_target)
2406 + target->size;
2407 target->t = fw_calloc(1, size);
2408 target->t->u.target_size = size;
2409 strcpy(target->t->u.user.name, jumpto);
2410 if (target->init != NULL)
2411 target->init(target->t, &fw.nfcache);
2412 }
2413
2414 if (!target) {
2415 /* it is no chain, and we can't load a plugin.
2416 * We cannot know if the plugin is corrupt, non
2417 * existant OR if the user just misspelled a
2418 * chain. */
2419 find_target(jumpto, LOAD_MUST_SUCCEED);
2420 } else {
2421 e = generate_entry(&fw, matches, target->t);
2422 free(target->t);
2423 }
2424 }
2425
2426 switch (command) {
2427 case CMD_APPEND:
2428 ret = append_entry(chain, e,
2429 nsaddrs, saddrs, ndaddrs, daddrs,
2430 options&OPT_VERBOSE,
2431 handle);
2432 break;
2433 case CMD_DELETE:
2434 ret = delete_entry(chain, e,
2435 nsaddrs, saddrs, ndaddrs, daddrs,
2436 options&OPT_VERBOSE,
2437 handle, matches);
2438 break;
2439 case CMD_DELETE_NUM:
2440 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2441 break;
2442 case CMD_REPLACE:
2443 ret = replace_entry(chain, e, rulenum - 1,
2444 saddrs, daddrs, options&OPT_VERBOSE,
2445 handle);
2446 break;
2447 case CMD_INSERT:
2448 ret = insert_entry(chain, e, rulenum - 1,
2449 nsaddrs, saddrs, ndaddrs, daddrs,
2450 options&OPT_VERBOSE,
2451 handle);
2452 break;
2453 case CMD_LIST:
2454 ret = list_entries(chain,
2455 options&OPT_VERBOSE,
2456 options&OPT_NUMERIC,
2457 options&OPT_EXPANDED,
2458 options&OPT_LINENUMBERS,
2459 handle);
2460 break;
2461 case CMD_FLUSH:
2462 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2463 break;
2464 case CMD_ZERO:
2465 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2466 break;
2467 case CMD_LIST|CMD_ZERO:
2468 ret = list_entries(chain,
2469 options&OPT_VERBOSE,
2470 options&OPT_NUMERIC,
2471 options&OPT_EXPANDED,
2472 options&OPT_LINENUMBERS,
2473 handle);
2474 if (ret)
2475 ret = zero_entries(chain,
2476 options&OPT_VERBOSE, handle);
2477 break;
2478 case CMD_NEW_CHAIN:
2479 ret = ip6tc_create_chain(chain, handle);
2480 break;
2481 case CMD_DELETE_CHAIN:
2482 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2483 break;
2484 case CMD_RENAME_CHAIN:
2485 ret = ip6tc_rename_chain(chain, newname, handle);
2486 break;
2487 case CMD_SET_POLICY:
2488 ret = ip6tc_set_policy(chain, policy, NULL, handle);
2489 break;
2490 default:
2491 /* We should never reach this... */
2492 exit_tryhelp(2);
2493 }
2494
2495 if (verbose > 1)
2496 dump_entries6(*handle);
2497
2498 clear_rule_matches(&matches);
2499
2500 if (e != NULL) {
2501 free(e);
2502 e = NULL;
2503 }
2504
2505 for (c = 0; c < nsaddrs; c++)
2506 free(&saddrs[c]);
2507
2508 for (c = 0; c < ndaddrs; c++)
2509 free(&daddrs[c]);
2510
2511 free_opts(1);
2512
2513 return ret;
2514 }
2515