blob: 22d636eb60e72402cc0b74ba6af1cd373a5e10ad [file] [log] [blame]
Rusty Russell5eed48a2000-06-02 20:12:24 +00001/* Code to take an iptables-style command line and do it. */
2
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <getopt.h>
22#include <string.h>
23#include <netdb.h>
24#include <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <ctype.h>
29#include <stdarg.h>
30#include <limits.h>
31#include <ip6tables.h>
32#include <arpa/inet.h>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000033#include <unistd.h>
34#include <fcntl.h>
35#include <sys/wait.h>
36#include <sys/types.h>
37#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000038
39#ifndef TRUE
40#define TRUE 1
41#endif
42#ifndef FALSE
43#define FALSE 0
44#endif
45
46#ifndef IP6T_LIB_DIR
47#define IP6T_LIB_DIR "/usr/local/lib/iptables"
48#endif
49
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000050#ifndef PROC_SYS_MODPROBE
51#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
52#endif
53
Rusty Russell5eed48a2000-06-02 20:12:24 +000054#define FMT_NUMERIC 0x0001
55#define FMT_NOCOUNTS 0x0002
56#define FMT_KILOMEGAGIGA 0x0004
57#define FMT_OPTIONS 0x0008
58#define FMT_NOTABLE 0x0010
59#define FMT_NOTARGET 0x0020
60#define FMT_VIA 0x0040
61#define FMT_NONEWLINE 0x0080
62#define FMT_LINENUMBERS 0x0100
63
64#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
65 | FMT_NUMERIC | FMT_NOTABLE)
66#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
67
68
69#define CMD_NONE 0x0000U
70#define CMD_INSERT 0x0001U
71#define CMD_DELETE 0x0002U
72#define CMD_DELETE_NUM 0x0004U
73#define CMD_REPLACE 0x0008U
74#define CMD_APPEND 0x0010U
75#define CMD_LIST 0x0020U
76#define CMD_FLUSH 0x0040U
77#define CMD_ZERO 0x0080U
78#define CMD_NEW_CHAIN 0x0100U
79#define CMD_DELETE_CHAIN 0x0200U
80#define CMD_SET_POLICY 0x0400U
81#define CMD_CHECK 0x0800U
82#define CMD_RENAME_CHAIN 0x1000U
83#define NUMBER_OF_CMD 13
84static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
85 'N', 'X', 'P', 'C', 'E' };
86
87#define OPTION_OFFSET 256
88
89#define OPT_NONE 0x00000U
90#define OPT_NUMERIC 0x00001U
91#define OPT_SOURCE 0x00002U
92#define OPT_DESTINATION 0x00004U
93#define OPT_PROTOCOL 0x00008U
94#define OPT_JUMP 0x00010U
95#define OPT_VERBOSE 0x00020U
96#define OPT_EXPANDED 0x00040U
97#define OPT_VIANAMEIN 0x00080U
98#define OPT_VIANAMEOUT 0x00100U
99#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000100#define OPT_COUNTERS 0x00400U
101#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000102static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000103= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104
105static struct option original_opts[] = {
106 { "append", 1, 0, 'A' },
107 { "delete", 1, 0, 'D' },
108 { "insert", 1, 0, 'I' },
109 { "replace", 1, 0, 'R' },
110 { "list", 2, 0, 'L' },
111 { "flush", 2, 0, 'F' },
112 { "zero", 2, 0, 'Z' },
113 { "check", 1, 0, 'C' },
114 { "new-chain", 1, 0, 'N' },
115 { "delete-chain", 2, 0, 'X' },
116 { "rename-chain", 2, 0, 'E' },
117 { "policy", 1, 0, 'P' },
118 { "source", 1, 0, 's' },
119 { "destination", 1, 0, 'd' },
120 { "src", 1, 0, 's' }, /* synonym */
121 { "dst", 1, 0, 'd' }, /* synonym */
122 { "protocol", 1, 0, 'p' },
123 { "in-interface", 1, 0, 'i' },
124 { "jump", 1, 0, 'j' },
125 { "table", 1, 0, 't' },
126 { "match", 1, 0, 'm' },
127 { "numeric", 0, 0, 'n' },
128 { "out-interface", 1, 0, 'o' },
129 { "verbose", 0, 0, 'v' },
130 { "exact", 0, 0, 'x' },
131 { "version", 0, 0, 'V' },
132 { "help", 2, 0, 'h' },
133 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000134 { "modprobe", 1, 0, 'M' },
135 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000136 { 0 }
137};
138
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000139#ifndef __OPTIMIZE__
140struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000141ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000142{
143 return (void *)e + e->target_offset;
144}
145#endif
146
Rusty Russell5eed48a2000-06-02 20:12:24 +0000147static struct option *opts = original_opts;
148static unsigned int global_option_offset = 0;
149
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
157 */
158
159static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
160/* Well, it's better than "Re: Linux vs FreeBSD" */
161{
162 /* -n -s -d -p -j -v -x -i -o --line */
163/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
164/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
165/*DELETE_NUM*/{'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',' '},
169/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
170/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
171/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
172/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
173/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000174/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
176};
177
178static int inverse_for_options[NUMBER_OF_OPT] =
179{
180/* -n */ 0,
181/* -s */ IP6T_INV_SRCIP,
182/* -d */ IP6T_INV_DSTIP,
183/* -p */ IP6T_INV_PROTO,
184/* -j */ 0,
185/* -v */ 0,
186/* -x */ 0,
187/* -i */ IP6T_INV_VIA_IN,
188/* -o */ IP6T_INV_VIA_OUT,
189/*--line*/ 0
190};
191
192const char *program_version;
193const char *program_name;
194
195/* Keeping track of external matches and targets: linked lists. */
196struct ip6tables_match *ip6tables_matches = NULL;
197struct ip6tables_target *ip6tables_targets = NULL;
198
199/* Extra debugging from libiptc */
200extern void dump_entries6(const ip6tc_handle_t handle);
201
202/* A few hardcoded protocols for 'all' and in case the user has no
203 /etc/protocols */
204struct pprot {
205 char *name;
206 u_int8_t num;
207};
208
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000209/* Primitive headers... */
210/* defined in netinet/in.h */
211#if 0
212#ifndef IPPROTO_ESP
213#define IPPROTO_ESP 50
214#endif
215#ifndef IPPROTO_AH
216#define IPPROTO_AH 51
217#endif
218#endif
219
Rusty Russell5eed48a2000-06-02 20:12:24 +0000220static const struct pprot chain_protos[] = {
221 { "tcp", IPPROTO_TCP },
222 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000223 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000224 { "esp", IPPROTO_ESP },
225 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000226 { "all", 0 },
227};
228
229static char *
230proto_to_name(u_int8_t proto, int nolookup)
231{
232 unsigned int i;
233
234 if (proto && !nolookup) {
235 struct protoent *pent = getprotobynumber(proto);
236 if (pent)
237 return pent->p_name;
238 }
239
240 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
241 if (chain_protos[i].num == proto)
242 return chain_protos[i].name;
243
244 return NULL;
245}
246
247static void
248in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
249{
250 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000251 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000252}
253
254void
255exit_error(enum exittype status, char *msg, ...)
256{
257 va_list args;
258
259 va_start(args, msg);
260 fprintf(stderr, "%s v%s: ", program_name, program_version);
261 vfprintf(stderr, msg, args);
262 va_end(args);
263 fprintf(stderr, "\n");
264 if (status == PARAMETER_PROBLEM)
265 exit_tryhelp(status);
266 if (status == VERSION_PROBLEM)
267 fprintf(stderr,
268 "Perhaps iptables or your kernel needs to be upgraded.\n");
269 exit(status);
270}
271
272void
273exit_tryhelp(int status)
274{
275 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
276 program_name, program_name );
277 exit(status);
278}
279
280void
281exit_printhelp(void)
282{
283 struct ip6tables_match *m = NULL;
284 struct ip6tables_target *t = NULL;
285
286 printf("%s v%s\n\n"
287"Usage: %s -[ADC] chain rule-specification [options]\n"
288" %s -[RI] chain rulenum rule-specification [options]\n"
289" %s -D chain rulenum [options]\n"
290" %s -[LFZ] [chain] [options]\n"
291" %s -[NX] chain\n"
292" %s -E old-chain-name new-chain-name\n"
293" %s -P chain target [options]\n"
294" %s -h (print this help information)\n\n",
295 program_name, program_version, program_name, program_name,
296 program_name, program_name, program_name, program_name,
297 program_name, program_name);
298
299 printf(
300"Commands:\n"
301"Either long or short options are allowed.\n"
302" --append -A chain Append to chain\n"
303" --delete -D chain Delete matching rule from chain\n"
304" --delete -D chain rulenum\n"
305" Delete rule rulenum (1 = first) from chain\n"
306" --insert -I chain [rulenum]\n"
307" Insert in chain as rulenum (default 1=first)\n"
308" --replace -R chain rulenum\n"
309" Replace rule rulenum (1 = first) in chain\n"
310" --list -L [chain] List the rules in a chain or all chains\n"
311" --flush -F [chain] Delete all rules in chain or all chains\n"
312" --zero -Z [chain] Zero counters in chain or all chains\n"
313" --check -C chain Test this packet on chain\n"
314" --new -N chain Create a new user-defined chain\n"
315" --delete-chain\n"
316" -X [chain] Delete a user-defined chain\n"
317" --policy -P chain target\n"
318" Change policy on chain to target\n"
319" --rename-chain\n"
320" -E old-chain new-chain\n"
321" Change chain name, (moving any references)\n"
322
323"Options:\n"
324" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
325" --source -s [!] address[/mask]\n"
326" source specification\n"
327" --destination -d [!] address[/mask]\n"
328" destination specification\n"
329" --in-interface -i [!] input name[+]\n"
330" network interface name ([+] for wildcard)\n"
331" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000332" target for rule (may load target extension)\n"
333" --match -m match\n"
334" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000335" --numeric -n numeric output of addresses and ports\n"
336" --out-interface -o [!] output name[+]\n"
337" network interface name ([+] for wildcard)\n"
338" --table -t table table to manipulate (default: `filter')\n"
339" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000340" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000341" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000342/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000343" --modprobe=<command> try to insert modules using this command\n"
344" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000345"[!] --version -V print package version.\n");
346
347 /* Print out any special helps. A user might like to be able to add a --help
348 to the commandline, and see expected results. So we call help for all
349 matches & targets */
350 for (t=ip6tables_targets;t;t=t->next) {
351 printf("\n");
352 t->help();
353 }
354 for (m=ip6tables_matches;m;m=m->next) {
355 printf("\n");
356 m->help();
357 }
358 exit(0);
359}
360
361static void
362generic_opt_check(int command, int options)
363{
364 int i, j, legal = 0;
365
366 /* Check that commands are valid with options. Complicated by the
367 * fact that if an option is legal with *any* command given, it is
368 * legal overall (ie. -z and -l).
369 */
370 for (i = 0; i < NUMBER_OF_OPT; i++) {
371 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
372
373 for (j = 0; j < NUMBER_OF_CMD; j++) {
374 if (!(command & (1<<j)))
375 continue;
376
377 if (!(options & (1<<i))) {
378 if (commands_v_options[j][i] == '+')
379 exit_error(PARAMETER_PROBLEM,
380 "You need to supply the `-%c' "
381 "option for this command\n",
382 optflags[i]);
383 } else {
384 if (commands_v_options[j][i] != 'x')
385 legal = 1;
386 else if (legal == 0)
387 legal = -1;
388 }
389 }
390 if (legal == -1)
391 exit_error(PARAMETER_PROBLEM,
392 "Illegal option `-%c' with this command\n",
393 optflags[i]);
394 }
395}
396
397static char
398opt2char(int option)
399{
400 const char *ptr;
401 for (ptr = optflags; option > 1; option >>= 1, ptr++);
402
403 return *ptr;
404}
405
406static char
407cmd2char(int option)
408{
409 const char *ptr;
410 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
411
412 return *ptr;
413}
414
415static void
416add_command(int *cmd, const int newcmd, const int othercmds, int invert)
417{
418 if (invert)
419 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
420 if (*cmd & (~othercmds))
421 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
422 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
423 *cmd |= newcmd;
424}
425
426int
427check_inverse(const char option[], int *invert)
428{
429 if (option && strcmp(option, "!") == 0) {
430 if (*invert)
431 exit_error(PARAMETER_PROBLEM,
432 "Multiple `!' flags not allowed");
433
434 *invert = TRUE;
435 return TRUE;
436 }
437 return FALSE;
438}
439
440static void *
441fw_calloc(size_t count, size_t size)
442{
443 void *p;
444
445 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000446 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000447 exit(1);
448 }
449 return p;
450}
451
452static void *
453fw_malloc(size_t size)
454{
455 void *p;
456
457 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000458 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000459 exit(1);
460 }
461 return p;
462}
463
Rusty Russell5eed48a2000-06-02 20:12:24 +0000464static char *
465addr_to_numeric(const struct in6_addr *addrp)
466{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000467 /* 0000:0000:0000:0000:0000:000.000.000.000
468 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000469 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000470 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000471}
472
473static struct in6_addr *
474numeric_to_addr(const char *num)
475{
476 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000477 int err;
478 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000479 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000480#ifdef DEBUG
481 fprintf(stderr, "\nnumeric2addr: %d\n", err);
482#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483 return (struct in6_addr *)NULL;
484}
485
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000486
487static struct in6_addr *
488host_to_addr(const char *name, unsigned int *naddr)
489{
490 struct addrinfo hints;
491 struct addrinfo *res;
492 static struct in6_addr *addr;
493 int err;
494
495 memset(&hints, 0, sizeof(hints));
496 hints.ai_flags=AI_CANONNAME;
497 hints.ai_family=AF_INET6;
498 hints.ai_socktype=SOCK_RAW;
499 hints.ai_protocol=41;
500 hints.ai_next=NULL;
501
502 *naddr = 0;
503 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
504#ifdef DEBUG
505 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
506#endif
507 return (struct in6_addr *) NULL;
508 } else {
509 if (res->ai_family != AF_INET6 ||
510 res->ai_addrlen != sizeof(struct sockaddr_in6))
511 return (struct in6_addr *) NULL;
512
513#ifdef DEBUG
514 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
515 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
516#endif
517 /* Get the first element of the address-chain */
518 addr = fw_calloc(1, sizeof(struct in6_addr));
519 in6addrcpy(addr, (struct in6_addr *)
520 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
521 freeaddrinfo(res);
522 *naddr = 1;
523 return addr;
524 }
525
526 return (struct in6_addr *) NULL;
527}
528
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000529static char *
530addr_to_host(const struct in6_addr *addr)
531{
532 struct sockaddr_in6 saddr;
533 int err;
534 static char hostname[NI_MAXHOST];
535
536 memset(&saddr, 0, sizeof(struct sockaddr_in6));
537 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000538 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000539
540 if ( (err=getnameinfo((struct sockaddr *)&saddr,
541 sizeof(struct sockaddr_in6),
542 hostname, sizeof(hostname)-1,
543 NULL, 0, 0)) != 0 ){
544#ifdef DEBUG
545 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
546#endif
547 return (char *) NULL;
548 } else {
549#ifdef DEBUG
550 fprintf (stderr, "\naddr2host: %s\n", hostname);
551#endif
552
553 return hostname;
554 }
555
556 return (char *) NULL;
557}
558
Rusty Russell5eed48a2000-06-02 20:12:24 +0000559static char *
560mask_to_numeric(const struct in6_addr *addrp)
561{
562 static char buf[20];
563 int l = ipv6_prefix_length(addrp);
564 if (l == -1)
565 return addr_to_numeric(addrp);
Harald Welte43ac1912002-03-03 09:43:07 +0000566 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000567 return buf;
568}
569
570static struct in6_addr *
571network_to_addr(const char *name)
572{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000573 /* abort();*/
574 /* TODO: not implemented yet, but the exception breaks the
575 * name resolvation */
576 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000577}
578
579static char *
580addr_to_anyname(const struct in6_addr *addr)
581{
582 char *name;
583
584 if ((name = addr_to_host(addr)) != NULL)
585 return name;
586
587 return addr_to_numeric(addr);
588}
589
590/*
591 * All functions starting with "parse" should succeed, otherwise
592 * the program fails.
593 * Most routines return pointers to static data that may change
594 * between calls to the same or other routines with a few exceptions:
595 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
596 * return global static data.
597*/
598
599static struct in6_addr *
600parse_hostnetwork(const char *name, unsigned int *naddrs)
601{
602 struct in6_addr *addrp, *addrptmp;
603
604 if ((addrptmp = numeric_to_addr(name)) != NULL ||
605 (addrptmp = network_to_addr(name)) != NULL) {
606 addrp = fw_malloc(sizeof(struct in6_addr));
607 in6addrcpy(addrp, addrptmp);
608 *naddrs = 1;
609 return addrp;
610 }
611 if ((addrp = host_to_addr(name, naddrs)) != NULL)
612 return addrp;
613
614 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
615}
616
617static struct in6_addr *
618parse_mask(char *mask)
619{
620 static struct in6_addr maskaddr;
621 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000622 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000623
624 if (mask == NULL) {
625 /* no mask at all defaults to 128 bits */
626 memset(&maskaddr, 0xff, sizeof maskaddr);
627 return &maskaddr;
628 }
629 if ((addrp = numeric_to_addr(mask)) != NULL)
630 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000631 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000632 exit_error(PARAMETER_PROBLEM,
633 "invalid mask `%s' specified", mask);
634 if (bits != 0) {
635 char *p = (char *)&maskaddr;
636 memset(p, 0xff, bits / 8);
637 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
638 p[bits / 8] = 0xff << (8 - (bits & 7));
639 return &maskaddr;
640 }
641
642 memset(&maskaddr, 0, sizeof maskaddr);
643 return &maskaddr;
644}
645
Harald Welte43ac1912002-03-03 09:43:07 +0000646void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000647parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
648 struct in6_addr *maskp, unsigned int *naddrs)
649{
650 struct in6_addr *addrp;
651 char buf[256];
652 char *p;
653 int i, j, n;
654
655 strncpy(buf, name, sizeof(buf) - 1);
656 if ((p = strrchr(buf, '/')) != NULL) {
657 *p = '\0';
658 addrp = parse_mask(p + 1);
659 } else
660 addrp = parse_mask(NULL);
661 in6addrcpy(maskp, addrp);
662
663 /* if a null mask is given, the name is ignored, like in "any/0" */
664 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
665 strcpy(buf, "::");
666
667 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
668 n = *naddrs;
669 for (i = 0, j = 0; i < n; i++) {
670 int k;
671 for (k = 0; k < 4; k++)
672 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
673 j++;
674 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000675 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000676 (*naddrs)--;
677 j--;
678 break;
679 }
680 }
681 }
682}
683
684struct ip6tables_match *
685find_match(const char *name, enum ip6t_tryload tryload)
686{
687 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000688 int icmphack = 0;
689
690 /* This is ugly as hell. Nonetheless, there is no way of changing
691 * this without hurting backwards compatibility */
692 if ( (strcmp(name,"icmpv6") == 0) ||
693 (strcmp(name,"ipv6-icmp") == 0) ||
694 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
695
696 if (!icmphack) {
697 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
698 if (strcmp(name, ptr->name) == 0)
699 break;
700 }
701 } else {
702 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
703 if (strcmp("icmp6", ptr->name) == 0)
704 break;
705 }
706 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000707
Harald Welte3efb6ea2001-08-06 18:50:21 +0000708#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000709 if (!ptr && tryload != DONT_LOAD) {
710 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
711 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000712 if (!icmphack)
713 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
714 else
715 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000716 if (dlopen(path, RTLD_NOW)) {
717 /* Found library. If it didn't register itself,
718 maybe they specified target as match. */
719 ptr = find_match(name, DONT_LOAD);
720
721 if (!ptr)
722 exit_error(PARAMETER_PROBLEM,
723 "Couldn't load match `%s'\n",
724 name);
725 } else if (tryload == LOAD_MUST_SUCCEED)
726 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000727 "Couldn't load match `%s':%s\n",
728 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000729 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000730#else
731 if (ptr && !ptr->loaded) {
732 if (tryload != DONT_LOAD)
733 ptr->loaded = 1;
734 else
735 ptr = NULL;
736 }
737#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738
Harald Welte43ac1912002-03-03 09:43:07 +0000739 if (ptr)
740 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000741
742
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743 return ptr;
744}
745
746/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
747static struct ip6tables_match *
748find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup)
749{
Harald Welteed498492001-07-23 01:24:22 +0000750 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000751
Harald Welte43ac1912002-03-03 09:43:07 +0000752 if (string_to_number(pname, 0, 255, &proto) != -1) {
753 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000754
Harald Welte43ac1912002-03-03 09:43:07 +0000755 if (protoname)
756 return find_match(protoname, tryload);
757 } else
758 return find_match(pname, tryload);
759
760 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000761}
762
Harald Welte43ac1912002-03-03 09:43:07 +0000763u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000764parse_protocol(const char *s)
765{
Harald Welteed498492001-07-23 01:24:22 +0000766 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000767
Harald Welteed498492001-07-23 01:24:22 +0000768 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769 struct protoent *pent;
770
771 if ((pent = getprotobyname(s)))
772 proto = pent->p_proto;
773 else {
774 unsigned int i;
775 for (i = 0;
776 i < sizeof(chain_protos)/sizeof(struct pprot);
777 i++) {
778 if (strcmp(s, chain_protos[i].name) == 0) {
779 proto = chain_protos[i].num;
780 break;
781 }
782 }
783 if (i == sizeof(chain_protos)/sizeof(struct pprot))
784 exit_error(PARAMETER_PROBLEM,
785 "unknown protocol `%s' specified",
786 s);
787 }
788 }
789
790 return (u_int16_t)proto;
791}
792
793static void
794parse_interface(const char *arg, char *vianame, unsigned char *mask)
795{
796 int vialen = strlen(arg);
797 unsigned int i;
798
799 memset(mask, 0, IFNAMSIZ);
800 memset(vianame, 0, IFNAMSIZ);
801
802 if (vialen + 1 > IFNAMSIZ)
803 exit_error(PARAMETER_PROBLEM,
804 "interface name `%s' must be shorter than IFNAMSIZ"
805 " (%i)", arg, IFNAMSIZ-1);
806
807 strcpy(vianame, arg);
808 if (vialen == 0)
809 memset(mask, 0, IFNAMSIZ);
810 else if (vianame[vialen - 1] == '+') {
811 memset(mask, 0xFF, vialen - 1);
812 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000813 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000814 } else {
815 /* Include nul-terminator in match */
816 memset(mask, 0xFF, vialen + 1);
817 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000818 for (i = 0; vianame[i]; i++) {
819 if (!isalnum(vianame[i])
820 && vianame[i] != '_'
821 && vianame[i] != '.') {
822 printf("Warning: wierd character in interface"
823 " `%s' (No aliases, :, ! or *).\n",
824 vianame);
825 break;
826 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000827 }
828 }
829}
830
831/* Can't be zero. */
832static int
833parse_rulenumber(const char *rule)
834{
Harald Welte43ac1912002-03-03 09:43:07 +0000835 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000836
Harald Welte43ac1912002-03-03 09:43:07 +0000837 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000838 exit_error(PARAMETER_PROBLEM,
839 "Invalid rule number `%s'", rule);
840
841 return rulenum;
842}
843
844static const char *
845parse_target(const char *targetname)
846{
847 const char *ptr;
848
849 if (strlen(targetname) < 1)
850 exit_error(PARAMETER_PROBLEM,
851 "Invalid target name (too short)");
852
853 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
854 exit_error(PARAMETER_PROBLEM,
855 "Invalid target name `%s' (%i chars max)",
856 targetname, sizeof(ip6t_chainlabel)-1);
857
858 for (ptr = targetname; *ptr; ptr++)
859 if (isspace(*ptr))
860 exit_error(PARAMETER_PROBLEM,
861 "Invalid target name `%s'", targetname);
862 return targetname;
863}
864
865int
Harald Welteed498492001-07-23 01:24:22 +0000866string_to_number(const char *s, unsigned int min, unsigned int max,
867 unsigned int *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000868{
Harald Welteed498492001-07-23 01:24:22 +0000869 long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000870 char *end;
871
872 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000873 errno = 0;
874 number = strtol(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875 if (*end == '\0' && end != s) {
876 /* we parsed a number, let's see if we want this */
Harald Welte43ac1912002-03-03 09:43:07 +0000877 if (errno != ERANGE && min <= number && number <= max) {
Harald Welteed498492001-07-23 01:24:22 +0000878 *ret = number;
879 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000880 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000881 }
882 return -1;
883}
884
885static void
886set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
887 int invert)
888{
889 if (*options & option)
890 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
891 opt2char(option));
892 *options |= option;
893
894 if (invert) {
895 unsigned int i;
896 for (i = 0; 1 << i != option; i++);
897
898 if (!inverse_for_options[i])
899 exit_error(PARAMETER_PROBLEM,
900 "cannot have ! before -%c",
901 opt2char(option));
902 *invflg |= inverse_for_options[i];
903 }
904}
905
906struct ip6tables_target *
907find_target(const char *name, enum ip6t_tryload tryload)
908{
909 struct ip6tables_target *ptr;
910
911 /* Standard target? */
912 if (strcmp(name, "") == 0
913 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
914 || strcmp(name, IP6TC_LABEL_DROP) == 0
915 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
916 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
917 name = "standard";
918
919 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
920 if (strcmp(name, ptr->name) == 0)
921 break;
922 }
923
Harald Welte3efb6ea2001-08-06 18:50:21 +0000924#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000925 if (!ptr && tryload != DONT_LOAD) {
926 char path[sizeof(IP6T_LIB_DIR) + sizeof("/libip6t_.so")
927 + strlen(name)];
928 sprintf(path, IP6T_LIB_DIR "/libip6t_%s.so", name);
929 if (dlopen(path, RTLD_NOW)) {
930 /* Found library. If it didn't register itself,
931 maybe they specified match as a target. */
932 ptr = find_target(name, DONT_LOAD);
933 if (!ptr)
934 exit_error(PARAMETER_PROBLEM,
935 "Couldn't load target `%s'\n",
936 name);
937 } else if (tryload == LOAD_MUST_SUCCEED)
938 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000939 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000940 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000941 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000942#else
943 if (ptr && !ptr->loaded) {
944 if (tryload != DONT_LOAD)
945 ptr->loaded = 1;
946 else
947 ptr = NULL;
948 }
949#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000950
Harald Welte43ac1912002-03-03 09:43:07 +0000951 if (ptr)
952 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000953
Rusty Russell5eed48a2000-06-02 20:12:24 +0000954 return ptr;
955}
956
957static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +0000958merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000959 unsigned int *option_offset)
960{
961 unsigned int num_old, num_new, i;
962 struct option *merge;
963
964 for (num_old = 0; oldopts[num_old].name; num_old++);
965 for (num_new = 0; newopts[num_new].name; num_new++);
966
967 global_option_offset += OPTION_OFFSET;
968 *option_offset = global_option_offset;
969
970 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
971 memcpy(merge, oldopts, num_old * sizeof(struct option));
972 for (i = 0; i < num_new; i++) {
973 merge[num_old + i] = newopts[i];
974 merge[num_old + i].val += *option_offset;
975 }
976 memset(merge + num_old + num_new, 0, sizeof(struct option));
977
978 return merge;
979}
980
981void
982register_match6(struct ip6tables_match *me)
983{
Harald Welte43ac1912002-03-03 09:43:07 +0000984 struct ip6tables_match **i;
985
Rusty Russell5eed48a2000-06-02 20:12:24 +0000986 if (strcmp(me->version, program_version) != 0) {
987 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
988 program_name, me->name, me->version, program_version);
989 exit(1);
990 }
991
992 if (find_match(me->name, DONT_LOAD)) {
993 fprintf(stderr, "%s: match `%s' already registered.\n",
994 program_name, me->name);
995 exit(1);
996 }
997
Harald Welte43ac1912002-03-03 09:43:07 +0000998 if (me->size != IP6T_ALIGN(me->size)) {
999 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
1000 program_name, me->name, me->size);
1001 exit(1);
1002 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001003
Harald Welte43ac1912002-03-03 09:43:07 +00001004 /* Append to list. */
1005 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1006 me->next = NULL;
1007 *i = me;
1008
Rusty Russell5eed48a2000-06-02 20:12:24 +00001009 me->m = NULL;
1010 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001011}
1012
1013void
1014register_target6(struct ip6tables_target *me)
1015{
1016 if (strcmp(me->version, program_version) != 0) {
1017 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1018 program_name, me->name, me->version, program_version);
1019 exit(1);
1020 }
1021
1022 if (find_target(me->name, DONT_LOAD)) {
1023 fprintf(stderr, "%s: target `%s' already registered.\n",
1024 program_name, me->name);
1025 exit(1);
1026 }
1027
Harald Welte43ac1912002-03-03 09:43:07 +00001028 if (me->size != IP6T_ALIGN(me->size)) {
1029 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
1030 program_name, me->name, me->size);
1031 exit(1);
1032 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001033
Rusty Russell5eed48a2000-06-02 20:12:24 +00001034 /* Prepend to list. */
1035 me->next = ip6tables_targets;
1036 ip6tables_targets = me;
1037 me->t = NULL;
1038 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001039}
1040
1041static void
1042print_num(u_int64_t number, unsigned int format)
1043{
Harald Welte43ac1912002-03-03 09:43:07 +00001044 if (format & FMT_KILOMEGAGIGA) {
1045 if (number > 99999) {
1046 number = (number + 500) / 1000;
1047 if (number > 9999) {
1048 number = (number + 500) / 1000;
1049 if (number > 9999) {
1050 number = (number + 500) / 1000;
1051 if (number > 9999) {
1052 number = (number + 500) / 1000;
1053 printf(FMT("%4lluT ","%lluT "), number);
1054 }
1055 else printf(FMT("%4lluG ","%lluG "), number);
1056 }
1057 else printf(FMT("%4lluM ","%lluM "), number);
1058 } else
1059 printf(FMT("%4lluK ","%lluK "), number);
1060 } else
1061 printf(FMT("%5llu ","%llu "), number);
1062 } else
1063 printf(FMT("%8llu ","%llu "), number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001064}
1065
Harald Welte43ac1912002-03-03 09:43:07 +00001066
Rusty Russell5eed48a2000-06-02 20:12:24 +00001067static void
1068print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1069{
1070 struct ip6t_counters counters;
1071 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1072 printf("Chain %s", chain);
1073 if (pol) {
1074 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001075 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001076 fputc(' ', stdout);
1077 print_num(counters.pcnt, (format|FMT_NOTABLE));
1078 fputs("packets, ", stdout);
1079 print_num(counters.bcnt, (format|FMT_NOTABLE));
1080 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001081 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001082 printf(")\n");
1083 } else {
1084 unsigned int refs;
1085 if (!ip6tc_get_references(&refs, chain, handle))
1086 printf(" (ERROR obtaining refs)\n");
1087 else
1088 printf(" (%u references)\n", refs);
1089 }
1090
1091 if (format & FMT_LINENUMBERS)
1092 printf(FMT("%-4s ", "%s "), "num");
1093 if (!(format & FMT_NOCOUNTS)) {
1094 if (format & FMT_KILOMEGAGIGA) {
1095 printf(FMT("%5s ","%s "), "pkts");
1096 printf(FMT("%5s ","%s "), "bytes");
1097 } else {
1098 printf(FMT("%8s ","%s "), "pkts");
1099 printf(FMT("%10s ","%s "), "bytes");
1100 }
1101 }
1102 if (!(format & FMT_NOTARGET))
1103 printf(FMT("%-9s ","%s "), "target");
1104 fputs(" prot ", stdout);
1105 if (format & FMT_OPTIONS)
1106 fputs("opt", stdout);
1107 if (format & FMT_VIA) {
1108 printf(FMT(" %-6s ","%s "), "in");
1109 printf(FMT("%-6s ","%s "), "out");
1110 }
1111 printf(FMT(" %-19s ","%s "), "source");
1112 printf(FMT(" %-19s "," %s "), "destination");
1113 printf("\n");
1114}
1115
Harald Welte43ac1912002-03-03 09:43:07 +00001116
Rusty Russell5eed48a2000-06-02 20:12:24 +00001117static int
1118print_match(const struct ip6t_entry_match *m,
1119 const struct ip6t_ip6 *ip,
1120 int numeric)
1121{
1122 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD);
1123
1124 if (match) {
1125 if (match->print)
1126 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001127 else
1128 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001129 } else {
1130 if (m->u.user.name[0])
1131 printf("UNKNOWN match `%s' ", m->u.user.name);
1132 }
1133 /* Don't stop iterating. */
1134 return 0;
1135}
1136
1137/* e is called `fw' here for hysterical raisins */
1138static void
1139print_firewall(const struct ip6t_entry *fw,
1140 const char *targname,
1141 unsigned int num,
1142 unsigned int format,
1143 const ip6tc_handle_t handle)
1144{
1145 struct ip6tables_target *target = NULL;
1146 const struct ip6t_entry_target *t;
1147 u_int8_t flags;
1148 char buf[BUFSIZ];
1149
1150 /* User creates a chain called "REJECT": this overrides the
1151 `REJECT' target module. Keep feeding them rope until the
1152 revolution... Bwahahahahah */
1153 if (!ip6tc_is_chain(targname, handle))
1154 target = find_target(targname, TRY_LOAD);
1155 else
1156 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1157
1158 t = ip6t_get_target((struct ip6t_entry *)fw);
1159 flags = fw->ipv6.flags;
1160
1161 if (format & FMT_LINENUMBERS)
1162 printf(FMT("%-4u ", "%u "), num+1);
1163
1164 if (!(format & FMT_NOCOUNTS)) {
1165 print_num(fw->counters.pcnt, format);
1166 print_num(fw->counters.bcnt, format);
1167 }
1168
1169 if (!(format & FMT_NOTARGET))
1170 printf(FMT("%-9s ", "%s "), targname);
1171
1172 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1173 {
Harald Welte43ac1912002-03-03 09:43:07 +00001174 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001175 if (pname)
1176 printf(FMT("%-5s", "%s "), pname);
1177 else
1178 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1179 }
1180
1181 if (format & FMT_OPTIONS) {
1182 if (format & FMT_NOTABLE)
1183 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001184 fputc(' ', stdout); /* Invert flag of FRAG */
1185 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001186 fputc(' ', stdout);
1187 }
1188
1189 if (format & FMT_VIA) {
1190 char iface[IFNAMSIZ+2];
1191
1192 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1193 iface[0] = '!';
1194 iface[1] = '\0';
1195 }
1196 else iface[0] = '\0';
1197
1198 if (fw->ipv6.iniface[0] != '\0') {
1199 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001200 }
1201 else if (format & FMT_NUMERIC) strcat(iface, "*");
1202 else strcat(iface, "any");
1203 printf(FMT(" %-6s ","in %s "), iface);
1204
1205 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1206 iface[0] = '!';
1207 iface[1] = '\0';
1208 }
1209 else iface[0] = '\0';
1210
1211 if (fw->ipv6.outiface[0] != '\0') {
1212 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001213 }
1214 else if (format & FMT_NUMERIC) strcat(iface, "*");
1215 else strcat(iface, "any");
1216 printf(FMT("%-6s ","out %s "), iface);
1217 }
1218
1219 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1220 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1221 && !(format & FMT_NUMERIC))
1222 printf(FMT("%-19s ","%s "), "anywhere");
1223 else {
1224 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001225 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001226 else
Harald Welte43ac1912002-03-03 09:43:07 +00001227 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001228 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1229 printf(FMT("%-19s ","%s "), buf);
1230 }
1231
1232 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1233 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1234 && !(format & FMT_NUMERIC))
1235 printf(FMT("%-19s","-> %s"), "anywhere");
1236 else {
1237 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001238 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001239 else
Harald Welte43ac1912002-03-03 09:43:07 +00001240 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1242 printf(FMT("%-19s","-> %s"), buf);
1243 }
1244
1245 if (format & FMT_NOTABLE)
1246 fputs(" ", stdout);
1247
1248 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1249
1250 if (target) {
1251 if (target->print)
1252 /* Print the target information. */
1253 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1254 } else if (t->u.target_size != sizeof(*t))
1255 printf("[%u bytes of unknown target data] ",
1256 t->u.target_size - sizeof(*t));
1257
1258 if (!(format & FMT_NONEWLINE))
1259 fputc('\n', stdout);
1260}
1261
1262static void
1263print_firewall_line(const struct ip6t_entry *fw,
1264 const ip6tc_handle_t h)
1265{
1266 struct ip6t_entry_target *t;
1267
1268 t = ip6t_get_target((struct ip6t_entry *)fw);
1269 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1270}
1271
1272static int
1273append_entry(const ip6t_chainlabel chain,
1274 struct ip6t_entry *fw,
1275 unsigned int nsaddrs,
1276 const struct in6_addr saddrs[],
1277 unsigned int ndaddrs,
1278 const struct in6_addr daddrs[],
1279 int verbose,
1280 ip6tc_handle_t *handle)
1281{
1282 unsigned int i, j;
1283 int ret = 1;
1284
1285 for (i = 0; i < nsaddrs; i++) {
1286 fw->ipv6.src = saddrs[i];
1287 for (j = 0; j < ndaddrs; j++) {
1288 fw->ipv6.dst = daddrs[j];
1289 if (verbose)
1290 print_firewall_line(fw, *handle);
1291 ret &= ip6tc_append_entry(chain, fw, handle);
1292 }
1293 }
1294
1295 return ret;
1296}
1297
1298static int
1299replace_entry(const ip6t_chainlabel chain,
1300 struct ip6t_entry *fw,
1301 unsigned int rulenum,
1302 const struct in6_addr *saddr,
1303 const struct in6_addr *daddr,
1304 int verbose,
1305 ip6tc_handle_t *handle)
1306{
1307 fw->ipv6.src = *saddr;
1308 fw->ipv6.dst = *daddr;
1309
1310 if (verbose)
1311 print_firewall_line(fw, *handle);
1312 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1313}
1314
1315static int
1316insert_entry(const ip6t_chainlabel chain,
1317 struct ip6t_entry *fw,
1318 unsigned int rulenum,
1319 unsigned int nsaddrs,
1320 const struct in6_addr saddrs[],
1321 unsigned int ndaddrs,
1322 const struct in6_addr daddrs[],
1323 int verbose,
1324 ip6tc_handle_t *handle)
1325{
1326 unsigned int i, j;
1327 int ret = 1;
1328
1329 for (i = 0; i < nsaddrs; i++) {
1330 fw->ipv6.src = saddrs[i];
1331 for (j = 0; j < ndaddrs; j++) {
1332 fw->ipv6.dst = daddrs[j];
1333 if (verbose)
1334 print_firewall_line(fw, *handle);
1335 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1336 }
1337 }
1338
1339 return ret;
1340}
1341
1342static unsigned char *
1343make_delete_mask(struct ip6t_entry *fw)
1344{
1345 /* Establish mask for comparison */
1346 unsigned int size;
1347 struct ip6tables_match *m;
1348 unsigned char *mask, *mptr;
1349
1350 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001351 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001352 if (!m->used)
1353 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001354
1355 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
1356 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001357
1358 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001359 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001360 + ip6tables_targets->size);
1361
1362 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1363 mptr = mask + sizeof(struct ip6t_entry);
1364
1365 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001366 if (!m->used)
1367 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001368
Rusty Russell5eed48a2000-06-02 20:12:24 +00001369 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001370 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1371 + m->userspacesize);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001372 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001373 }
1374
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001375 memset(mptr, 0xFF,
1376 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1377 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378
1379 return mask;
1380}
1381
1382static int
1383delete_entry(const ip6t_chainlabel chain,
1384 struct ip6t_entry *fw,
1385 unsigned int nsaddrs,
1386 const struct in6_addr saddrs[],
1387 unsigned int ndaddrs,
1388 const struct in6_addr daddrs[],
1389 int verbose,
1390 ip6tc_handle_t *handle)
1391{
1392 unsigned int i, j;
1393 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001394 unsigned char *mask;
1395
1396 mask = make_delete_mask(fw);
1397 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001398 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001399 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001400 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001401 if (verbose)
1402 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001403 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001404 }
1405 }
1406 return ret;
1407}
1408
1409static int
1410check_packet(const ip6t_chainlabel chain,
1411 struct ip6t_entry *fw,
1412 unsigned int nsaddrs,
1413 const struct in6_addr saddrs[],
1414 unsigned int ndaddrs,
1415 const struct in6_addr daddrs[],
1416 int verbose,
1417 ip6tc_handle_t *handle)
1418{
1419 int ret = 1;
1420 unsigned int i, j;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001421 const char *msg;
1422
1423 for (i = 0; i < nsaddrs; i++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001424 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001425 for (j = 0; j < ndaddrs; j++) {
Harald Welte43ac1912002-03-03 09:43:07 +00001426 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 if (verbose)
1428 print_firewall_line(fw, *handle);
Harald Welte43ac1912002-03-03 09:43:07 +00001429 msg = ip6tc_check_packet(chain, fw, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001430 if (!msg) ret = 0;
1431 else printf("%s\n", msg);
1432 }
1433 }
1434
1435 return ret;
1436}
1437
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001438/*static int*/
András Kis-Szabó764316a2001-02-26 17:31:20 +00001439int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001440for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1441 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1442{
1443 int ret = 1;
1444 const char *chain;
1445 char *chains;
1446 unsigned int i, chaincount = 0;
1447
1448 chain = ip6tc_first_chain(handle);
1449 while (chain) {
1450 chaincount++;
1451 chain = ip6tc_next_chain(handle);
1452 }
1453
1454 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1455 i = 0;
1456 chain = ip6tc_first_chain(handle);
1457 while (chain) {
1458 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1459 i++;
1460 chain = ip6tc_next_chain(handle);
1461 }
1462
1463 for (i = 0; i < chaincount; i++) {
1464 if (!builtinstoo
1465 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
1466 *handle))
1467 continue;
1468 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1469 }
1470
1471 free(chains);
1472 return ret;
1473}
1474
András Kis-Szabó764316a2001-02-26 17:31:20 +00001475int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001476flush_entries(const ip6t_chainlabel chain, int verbose,
1477 ip6tc_handle_t *handle)
1478{
1479 if (!chain)
1480 return for_each_chain(flush_entries, verbose, 1, handle);
1481
1482 if (verbose)
1483 fprintf(stdout, "Flushing chain `%s'\n", chain);
1484 return ip6tc_flush_entries(chain, handle);
1485}
1486
1487static int
1488zero_entries(const ip6t_chainlabel chain, int verbose,
1489 ip6tc_handle_t *handle)
1490{
1491 if (!chain)
1492 return for_each_chain(zero_entries, verbose, 1, handle);
1493
1494 if (verbose)
1495 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1496 return ip6tc_zero_entries(chain, handle);
1497}
1498
András Kis-Szabó764316a2001-02-26 17:31:20 +00001499int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500delete_chain(const ip6t_chainlabel chain, int verbose,
1501 ip6tc_handle_t *handle)
1502{
1503 if (!chain)
1504 return for_each_chain(delete_chain, verbose, 0, handle);
1505
1506 if (verbose)
1507 fprintf(stdout, "Deleting chain `%s'\n", chain);
1508 return ip6tc_delete_chain(chain, handle);
1509}
1510
1511static int
1512list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1513 int expanded, int linenumbers, ip6tc_handle_t *handle)
1514{
1515 int found = 0;
1516 unsigned int format;
1517 const char *this;
1518
1519 format = FMT_OPTIONS;
1520 if (!verbose)
1521 format |= FMT_NOCOUNTS;
1522 else
1523 format |= FMT_VIA;
1524
1525 if (numeric)
1526 format |= FMT_NUMERIC;
1527
1528 if (!expanded)
1529 format |= FMT_KILOMEGAGIGA;
1530
1531 if (linenumbers)
1532 format |= FMT_LINENUMBERS;
1533
1534 for (this = ip6tc_first_chain(handle);
1535 this;
1536 this = ip6tc_next_chain(handle)) {
1537 const struct ip6t_entry *i;
1538 unsigned int num;
1539
1540 if (chain && strcmp(chain, this) != 0)
1541 continue;
1542
1543 if (found) printf("\n");
1544
1545 print_header(format, this, handle);
1546 i = ip6tc_first_rule(this, handle);
1547
1548 num = 0;
1549 while (i) {
1550 print_firewall(i,
1551 ip6tc_get_target(i, handle),
1552 num++,
1553 format,
1554 *handle);
1555 i = ip6tc_next_rule(i, handle);
1556 }
1557 found = 1;
1558 }
1559
1560 errno = ENOENT;
1561 return found;
1562}
1563
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001564static char *get_modprobe(void)
1565{
Harald Welte43ac1912002-03-03 09:43:07 +00001566 int procfile;
1567 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001568
Harald Welte43ac1912002-03-03 09:43:07 +00001569 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1570 if (procfile < 0)
1571 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001572
Harald Welte43ac1912002-03-03 09:43:07 +00001573 ret = malloc(1024);
1574 if (ret) {
1575 switch (read(procfile, ret, 1024)) {
1576 case -1: goto fail;
1577 case 1024: goto fail; /* Partial read. Wierd */
1578 }
1579 if (ret[strlen(ret)-1]=='\n')
1580 ret[strlen(ret)-1]=0;
1581 close(procfile);
1582 return ret;
1583 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001584 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001585 free(ret);
1586 close(procfile);
1587 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001588}
1589
Harald Welte58918652001-06-16 18:25:25 +00001590int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001591{
Harald Welte43ac1912002-03-03 09:43:07 +00001592 char *buf = NULL;
1593 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001594
Harald Welte43ac1912002-03-03 09:43:07 +00001595 /* If they don't explicitly set it, read out of kernel */
1596 if (!modprobe) {
1597 buf = get_modprobe();
1598 if (!buf)
1599 return -1;
1600 modprobe = buf;
1601 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001602
Harald Welte43ac1912002-03-03 09:43:07 +00001603 switch (fork()) {
1604 case 0:
1605 argv[0] = (char *)modprobe;
1606 argv[1] = (char *)modname;
1607 argv[2] = NULL;
1608 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001609
Harald Welte43ac1912002-03-03 09:43:07 +00001610 /* not usually reached */
1611 exit(0);
1612 case -1:
1613 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001614
Harald Welte43ac1912002-03-03 09:43:07 +00001615 default: /* parent */
1616 wait(NULL);
1617 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001618
Harald Welte43ac1912002-03-03 09:43:07 +00001619 free(buf);
1620 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001621}
1622
Rusty Russell5eed48a2000-06-02 20:12:24 +00001623static struct ip6t_entry *
1624generate_entry(const struct ip6t_entry *fw,
1625 struct ip6tables_match *matches,
1626 struct ip6t_entry_target *target)
1627{
1628 unsigned int size;
1629 struct ip6tables_match *m;
1630 struct ip6t_entry *e;
1631
1632 size = sizeof(struct ip6t_entry);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001633 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001634 if (!m->used)
1635 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001636
Rusty Russell5eed48a2000-06-02 20:12:24 +00001637 size += m->m->u.match_size;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001638 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001639
1640 e = fw_malloc(size + target->u.target_size);
1641 *e = *fw;
1642 e->target_offset = size;
1643 e->next_offset = size + target->u.target_size;
1644
1645 size = 0;
1646 for (m = matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00001647 if (!m->used)
1648 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001649
Rusty Russell5eed48a2000-06-02 20:12:24 +00001650 memcpy(e->elems + size, m->m, m->m->u.match_size);
1651 size += m->m->u.match_size;
1652 }
1653 memcpy(e->elems + size, target, target->u.target_size);
1654
1655 return e;
1656}
1657
1658int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1659{
1660 struct ip6t_entry fw, *e = NULL;
1661 int invert = 0;
1662 unsigned int nsaddrs = 0, ndaddrs = 0;
1663 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1664
1665 int c, verbose = 0;
1666 const char *chain = NULL;
1667 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1668 const char *policy = NULL, *newname = NULL;
1669 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001670 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001671 int ret = 1;
1672 struct ip6tables_match *m;
1673 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001674 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001675 const char *jumpto = "";
1676 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001677 const char *modprobe = NULL;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001678 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001679
1680 memset(&fw, 0, sizeof(fw));
1681
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001682 opts = original_opts;
1683 global_option_offset = 0;
1684
Harald Welte43ac1912002-03-03 09:43:07 +00001685 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001686 * a second time */
1687 optind = 0;
1688
Harald Welte43ac1912002-03-03 09:43:07 +00001689 /* clear mflags in case do_command gets called a second time
1690 * (we clear the global list of all matches for security)*/
1691 for (m = ip6tables_matches; m; m = m->next) {
1692 m->mflags = 0;
1693 m->used = 0;
1694 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001695
Harald Welte43ac1912002-03-03 09:43:07 +00001696 for (t = ip6tables_targets; t; t = t->next) {
1697 t->tflags = 0;
1698 t->used = 0;
1699 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001700
Rusty Russell5eed48a2000-06-02 20:12:24 +00001701 /* Suppress error messages: we may add new options if we
1702 demand-load a protocol. */
1703 opterr = 0;
1704
1705 while ((c = getopt_long(argc, argv,
Harald Welte43ac1912002-03-03 09:43:07 +00001706 "-A:C:D:R:I:L::F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001707 opts, NULL)) != -1) {
1708 switch (c) {
1709 /*
1710 * Command selection
1711 */
1712 case 'A':
1713 add_command(&command, CMD_APPEND, CMD_NONE,
1714 invert);
1715 chain = optarg;
1716 break;
1717
1718 case 'D':
1719 add_command(&command, CMD_DELETE, CMD_NONE,
1720 invert);
1721 chain = optarg;
1722 if (optind < argc && argv[optind][0] != '-'
1723 && argv[optind][0] != '!') {
1724 rulenum = parse_rulenumber(argv[optind++]);
1725 command = CMD_DELETE_NUM;
1726 }
1727 break;
1728
1729 case 'C':
1730 add_command(&command, CMD_CHECK, CMD_NONE,
1731 invert);
1732 chain = optarg;
1733 break;
1734
1735 case 'R':
1736 add_command(&command, CMD_REPLACE, CMD_NONE,
1737 invert);
1738 chain = optarg;
1739 if (optind < argc && argv[optind][0] != '-'
1740 && argv[optind][0] != '!')
1741 rulenum = parse_rulenumber(argv[optind++]);
1742 else
1743 exit_error(PARAMETER_PROBLEM,
1744 "-%c requires a rule number",
1745 cmd2char(CMD_REPLACE));
1746 break;
1747
1748 case 'I':
1749 add_command(&command, CMD_INSERT, CMD_NONE,
1750 invert);
1751 chain = optarg;
1752 if (optind < argc && argv[optind][0] != '-'
1753 && argv[optind][0] != '!')
1754 rulenum = parse_rulenumber(argv[optind++]);
1755 else rulenum = 1;
1756 break;
1757
1758 case 'L':
1759 add_command(&command, CMD_LIST, CMD_ZERO,
1760 invert);
1761 if (optarg) chain = optarg;
1762 else if (optind < argc && argv[optind][0] != '-'
1763 && argv[optind][0] != '!')
1764 chain = argv[optind++];
1765 break;
1766
1767 case 'F':
1768 add_command(&command, CMD_FLUSH, CMD_NONE,
1769 invert);
1770 if (optarg) chain = optarg;
1771 else if (optind < argc && argv[optind][0] != '-'
1772 && argv[optind][0] != '!')
1773 chain = argv[optind++];
1774 break;
1775
1776 case 'Z':
1777 add_command(&command, CMD_ZERO, CMD_LIST,
1778 invert);
1779 if (optarg) chain = optarg;
1780 else if (optind < argc && argv[optind][0] != '-'
1781 && argv[optind][0] != '!')
1782 chain = argv[optind++];
1783 break;
1784
1785 case 'N':
1786 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1787 invert);
1788 chain = optarg;
1789 break;
1790
1791 case 'X':
1792 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1793 invert);
1794 if (optarg) chain = optarg;
1795 else if (optind < argc && argv[optind][0] != '-'
1796 && argv[optind][0] != '!')
1797 chain = argv[optind++];
1798 break;
1799
1800 case 'E':
1801 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1802 invert);
1803 chain = optarg;
1804 if (optind < argc && argv[optind][0] != '-'
1805 && argv[optind][0] != '!')
1806 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001807 else
1808 exit_error(PARAMETER_PROBLEM,
1809 "-%c requires old-chain-name and "
1810 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001811 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001812 break;
1813
1814 case 'P':
1815 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1816 invert);
1817 chain = optarg;
1818 if (optind < argc && argv[optind][0] != '-'
1819 && argv[optind][0] != '!')
1820 policy = argv[optind++];
1821 else
1822 exit_error(PARAMETER_PROBLEM,
1823 "-%c requires a chain and a policy",
1824 cmd2char(CMD_SET_POLICY));
1825 break;
1826
1827 case 'h':
1828 if (!optarg)
1829 optarg = argv[optind];
1830
1831 /* iptables -p icmp -h */
1832 if (!ip6tables_matches && protocol)
1833 find_match(protocol, TRY_LOAD);
1834
1835 exit_printhelp();
1836
1837 /*
1838 * Option selection
1839 */
1840 case 'p':
1841 if (check_inverse(optarg, &invert))
1842 optind++;
1843 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1844 invert);
1845
1846 /* Canonicalize into lower case */
1847 for (protocol = argv[optind-1]; *protocol; protocol++)
1848 *protocol = tolower(*protocol);
1849
1850 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001851 if ( strcmp(protocol,"ipv6-icmp") == 0)
1852 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001853 fw.ipv6.proto = parse_protocol(protocol);
1854 fw.ipv6.flags |= IP6T_F_PROTO;
1855
1856 if (fw.ipv6.proto == 0
1857 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1858 exit_error(PARAMETER_PROBLEM,
1859 "rule would never match protocol");
1860 fw.nfcache |= NFC_IP6_PROTO;
1861 break;
1862
1863 case 's':
1864 if (check_inverse(optarg, &invert))
1865 optind++;
1866 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1867 invert);
1868 shostnetworkmask = argv[optind-1];
1869 fw.nfcache |= NFC_IP6_SRC;
1870 break;
1871
1872 case 'd':
1873 if (check_inverse(optarg, &invert))
1874 optind++;
1875 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1876 invert);
1877 dhostnetworkmask = argv[optind-1];
1878 fw.nfcache |= NFC_IP6_DST;
1879 break;
1880
1881 case 'j':
1882 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1883 invert);
1884 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001885 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001886 target = find_target(jumpto, TRY_LOAD);
1887
1888 if (target) {
1889 size_t size;
1890
Harald Welte43ac1912002-03-03 09:43:07 +00001891 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1892 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001893
1894 target->t = fw_calloc(1, size);
1895 target->t->u.target_size = size;
1896 strcpy(target->t->u.user.name, jumpto);
1897 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001898 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001899 }
1900 break;
1901
1902
1903 case 'i':
1904 if (check_inverse(optarg, &invert))
1905 optind++;
1906 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1907 invert);
1908 parse_interface(argv[optind-1],
1909 fw.ipv6.iniface,
1910 fw.ipv6.iniface_mask);
1911 fw.nfcache |= NFC_IP6_IF_IN;
1912 break;
1913
1914 case 'o':
1915 if (check_inverse(optarg, &invert))
1916 optind++;
1917 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1918 invert);
1919 parse_interface(argv[optind-1],
1920 fw.ipv6.outiface,
1921 fw.ipv6.outiface_mask);
1922 fw.nfcache |= NFC_IP6_IF_OUT;
1923 break;
1924
1925 case 'v':
1926 if (!verbose)
1927 set_option(&options, OPT_VERBOSE,
1928 &fw.ipv6.invflags, invert);
1929 verbose++;
1930 break;
1931
1932 case 'm': {
1933 size_t size;
1934
1935 if (invert)
1936 exit_error(PARAMETER_PROBLEM,
1937 "unexpected ! flag before --match");
1938
1939 m = find_match(optarg, LOAD_MUST_SUCCEED);
Harald Welte43ac1912002-03-03 09:43:07 +00001940 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1941 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001942 m->m = fw_calloc(1, size);
1943 m->m->u.match_size = size;
1944 strcpy(m->m->u.user.name, m->name);
1945 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001946 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 }
1948 break;
1949
1950 case 'n':
1951 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1952 invert);
1953 break;
1954
1955 case 't':
1956 if (invert)
1957 exit_error(PARAMETER_PROBLEM,
1958 "unexpected ! flag before --table");
1959 *table = argv[optind-1];
1960 break;
1961
1962 case 'x':
1963 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
1964 invert);
1965 break;
1966
1967 case 'V':
1968 if (invert)
1969 printf("Not %s ;-)\n", program_version);
1970 else
1971 printf("%s v%s\n",
1972 program_name, program_version);
1973 exit(0);
1974
1975 case '0':
1976 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
1977 invert);
1978 break;
1979
Harald Welte43ac1912002-03-03 09:43:07 +00001980 case 'M':
1981 modprobe = optarg;
1982 break;
1983
1984 case 'c':
1985
1986 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
1987 invert);
1988 pcnt = optarg;
1989 if (optind < argc && argv[optind][0] != '-'
1990 && argv[optind][0] != '!')
1991 bcnt = argv[optind++];
1992 else
1993 exit_error(PARAMETER_PROBLEM,
1994 "-%c requires packet and byte counter",
1995 opt2char(OPT_COUNTERS));
1996
1997 if (sscanf(pcnt, "%llu", &fw.counters.pcnt) != 1)
1998 exit_error(PARAMETER_PROBLEM,
1999 "-%c packet counter not numeric",
2000 opt2char(OPT_COUNTERS));
2001
2002 if (sscanf(bcnt, "%llu", &fw.counters.bcnt) != 1)
2003 exit_error(PARAMETER_PROBLEM,
2004 "-%c byte counter not numeric",
2005 opt2char(OPT_COUNTERS));
2006
2007 break;
2008
2009
Rusty Russell5eed48a2000-06-02 20:12:24 +00002010 case 1: /* non option */
2011 if (optarg[0] == '!' && optarg[1] == '\0') {
2012 if (invert)
2013 exit_error(PARAMETER_PROBLEM,
2014 "multiple consecutive ! not"
2015 " allowed");
2016 invert = TRUE;
2017 optarg[0] = '\0';
2018 continue;
2019 }
2020 printf("Bad argument `%s'\n", optarg);
2021 exit_tryhelp(2);
2022
2023 default:
2024 /* FIXME: This scheme doesn't allow two of the same
2025 matches --RR */
2026 if (!target
2027 || !(target->parse(c - target->option_offset,
2028 argv, invert,
2029 &target->tflags,
2030 &fw, &target->t))) {
2031 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002032 if (!m->used)
2033 continue;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002034
Rusty Russell5eed48a2000-06-02 20:12:24 +00002035 if (m->parse(c - m->option_offset,
2036 argv, invert,
2037 &m->mflags,
2038 &fw,
2039 &fw.nfcache,
2040 &m->m))
2041 break;
2042 }
2043
2044 /* If you listen carefully, you can
2045 actually hear this code suck. */
2046 if (m == NULL
2047 && protocol
2048 && !find_proto(protocol, DONT_LOAD,
2049 options&OPT_NUMERIC)
2050 && (m = find_proto(protocol, TRY_LOAD,
2051 options&OPT_NUMERIC))) {
2052 /* Try loading protocol */
2053 size_t size;
2054
Harald Welte43ac1912002-03-03 09:43:07 +00002055 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2056 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002057
2058 m->m = fw_calloc(1, size);
2059 m->m->u.match_size = size;
2060 strcpy(m->m->u.user.name, m->name);
2061 m->init(m->m, &fw.nfcache);
2062
Harald Welte43ac1912002-03-03 09:43:07 +00002063 opts = merge_options(opts,
2064 m->extra_opts, &m->option_offset);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002065
Rusty Russell5eed48a2000-06-02 20:12:24 +00002066 optind--;
2067 continue;
2068 }
2069 if (!m)
2070 exit_error(PARAMETER_PROBLEM,
2071 "Unknown arg `%s'",
2072 argv[optind-1]);
2073 }
2074 }
2075 invert = FALSE;
2076 }
2077
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002078 for (m = ip6tables_matches; m; m = m->next) {
Harald Welte43ac1912002-03-03 09:43:07 +00002079 if (!m->used)
2080 continue;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002081
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002082 m->final_check(m->mflags);
2083 }
2084
Harald Welte43ac1912002-03-03 09:43:07 +00002085 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002086 target->final_check(target->tflags);
2087
2088 /* Fix me: must put inverse options checking here --MN */
2089
2090 if (optind < argc)
2091 exit_error(PARAMETER_PROBLEM,
2092 "unknown arguments found on commandline");
2093 if (!command)
2094 exit_error(PARAMETER_PROBLEM, "no command specified");
2095 if (invert)
2096 exit_error(PARAMETER_PROBLEM,
2097 "nothing appropriate following !");
2098
2099 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND |
2100 CMD_CHECK)) {
2101 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002102 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002103 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002104 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002105 }
2106
2107 if (shostnetworkmask)
2108 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2109 &(fw.ipv6.smsk), &nsaddrs);
2110
2111 if (dhostnetworkmask)
2112 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2113 &(fw.ipv6.dmsk), &ndaddrs);
2114
2115 if ((nsaddrs > 1 || ndaddrs > 1) &&
2116 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2117 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2118 " source or destination IP addresses");
2119
2120 if (command == CMD_CHECK && fw.ipv6.invflags != 0)
2121 exit_error(PARAMETER_PROBLEM, "! not allowed with -%c",
2122 cmd2char(CMD_CHECK));
2123
2124 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2125 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2126 "specify a unique address");
2127
2128 generic_opt_check(command, options);
2129
2130 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2131 exit_error(PARAMETER_PROBLEM,
2132 "chain name `%s' too long (must be under %i chars)",
2133 chain, IP6T_FUNCTION_MAXNAMELEN);
2134
Harald Welte43ac1912002-03-03 09:43:07 +00002135 /* only allocate handle if we weren't called with a handle */
2136 if (!*handle)
2137 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002138
Harald Welte43ac1912002-03-03 09:43:07 +00002139 if (!*handle) {
2140 /* try to insmod the module if iptc_init failed */
2141 ip6tables_insmod("ip6_tables", modprobe);
2142 *handle = ip6tc_init(*table);
2143 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002144
Harald Welte43ac1912002-03-03 09:43:07 +00002145 if (!*handle)
2146 exit_error(VERSION_PROBLEM,
2147 "can't initialize ip6tables table `%s': %s",
2148 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002149
2150 if (command == CMD_CHECK
2151 || command == CMD_APPEND
2152 || command == CMD_DELETE
2153 || command == CMD_INSERT
2154 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002155 if (strcmp(chain, "PREROUTING") == 0
2156 || strcmp(chain, "INPUT") == 0) {
2157 /* -o not valid with incoming packets. */
2158 if (options & OPT_VIANAMEOUT)
2159 exit_error(PARAMETER_PROBLEM,
2160 "Can't use -%c with %s\n",
2161 opt2char(OPT_VIANAMEOUT),
2162 chain);
2163 /* -i required with -C */
2164 if (command == CMD_CHECK && !(options & OPT_VIANAMEIN))
2165 exit_error(PARAMETER_PROBLEM,
2166 "Need -%c with %s\n",
2167 opt2char(OPT_VIANAMEIN),
2168 chain);
2169 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002170
Harald Welte43ac1912002-03-03 09:43:07 +00002171 if (strcmp(chain, "POSTROUTING") == 0
2172 || strcmp(chain, "OUTPUT") == 0) {
2173 /* -i not valid with outgoing packets */
2174 if (options & OPT_VIANAMEIN)
2175 exit_error(PARAMETER_PROBLEM,
2176 "Can't use -%c with %s\n",
2177 opt2char(OPT_VIANAMEIN),
2178 chain);
2179 /* -o required with -C */
2180 if (command == CMD_CHECK && !(options&OPT_VIANAMEOUT))
2181 exit_error(PARAMETER_PROBLEM,
2182 "Need -%c with %s\n",
2183 opt2char(OPT_VIANAMEOUT),
2184 chain);
2185 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002186
2187 if (target && ip6tc_is_chain(jumpto, *handle)) {
2188 printf("Warning: using chain %s, not extension\n",
2189 jumpto);
2190
2191 target = NULL;
2192 }
2193
2194 /* If they didn't specify a target, or it's a chain
2195 name, use standard. */
2196 if (!target
2197 && (strlen(jumpto) == 0
2198 || ip6tc_is_chain(jumpto, *handle))) {
2199 size_t size;
2200
2201 target = find_target(IP6T_STANDARD_TARGET,
2202 LOAD_MUST_SUCCEED);
2203
2204 size = sizeof(struct ip6t_entry_target)
2205 + target->size;
2206 target->t = fw_calloc(1, size);
2207 target->t->u.target_size = size;
2208 strcpy(target->t->u.user.name, jumpto);
2209 target->init(target->t, &fw.nfcache);
2210 }
2211
2212 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002213 /* it is no chain, and we can't load a plugin.
2214 * We cannot know if the plugin is corrupt, non
2215 * existant OR if the user just misspelled a
2216 * chain. */
2217 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002218 } else {
2219 e = generate_entry(&fw, ip6tables_matches, target->t);
2220 }
2221 }
2222
2223 switch (command) {
2224 case CMD_APPEND:
2225 ret = append_entry(chain, e,
2226 nsaddrs, saddrs, ndaddrs, daddrs,
2227 options&OPT_VERBOSE,
2228 handle);
2229 break;
2230 case CMD_CHECK:
2231 ret = check_packet(chain, e,
2232 nsaddrs, saddrs, ndaddrs, daddrs,
2233 options&OPT_VERBOSE, handle);
2234 break;
2235 case CMD_DELETE:
2236 ret = delete_entry(chain, e,
2237 nsaddrs, saddrs, ndaddrs, daddrs,
2238 options&OPT_VERBOSE,
2239 handle);
2240 break;
2241 case CMD_DELETE_NUM:
2242 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2243 break;
2244 case CMD_REPLACE:
2245 ret = replace_entry(chain, e, rulenum - 1,
2246 saddrs, daddrs, options&OPT_VERBOSE,
2247 handle);
2248 break;
2249 case CMD_INSERT:
2250 ret = insert_entry(chain, e, rulenum - 1,
2251 nsaddrs, saddrs, ndaddrs, daddrs,
2252 options&OPT_VERBOSE,
2253 handle);
2254 break;
2255 case CMD_LIST:
2256 ret = list_entries(chain,
2257 options&OPT_VERBOSE,
2258 options&OPT_NUMERIC,
2259 options&OPT_EXPANDED,
2260 options&OPT_LINENUMBERS,
2261 handle);
2262 break;
2263 case CMD_FLUSH:
2264 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2265 break;
2266 case CMD_ZERO:
2267 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2268 break;
2269 case CMD_LIST|CMD_ZERO:
2270 ret = list_entries(chain,
2271 options&OPT_VERBOSE,
2272 options&OPT_NUMERIC,
2273 options&OPT_EXPANDED,
2274 options&OPT_LINENUMBERS,
2275 handle);
2276 if (ret)
2277 ret = zero_entries(chain,
2278 options&OPT_VERBOSE, handle);
2279 break;
2280 case CMD_NEW_CHAIN:
2281 ret = ip6tc_create_chain(chain, handle);
2282 break;
2283 case CMD_DELETE_CHAIN:
2284 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2285 break;
2286 case CMD_RENAME_CHAIN:
2287 ret = ip6tc_rename_chain(chain, newname, handle);
2288 break;
2289 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002290 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002291 break;
2292 default:
2293 /* We should never reach this... */
2294 exit_tryhelp(2);
2295 }
2296
2297 if (verbose > 1)
2298 dump_entries6(*handle);
2299
2300 return ret;
2301}