blob: 9bfe1d3831923ab98f48c2dc3202cc9a3ea0eac0 [file] [log] [blame]
Jonas Berlin1b91e592005-04-01 06:58:38 +00001/* Code to take an ip6tables-style command line and do it. */
Rusty Russell5eed48a2000-06-02 20:12:24 +00002
3/*
4 * Author: Paul.Russell@rustcorp.com.au and mneuling@radlogic.com.au
5 *
Harald Welte10a907f2002-08-07 09:07:41 +00006 * (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 *
Rusty Russell5eed48a2000-06-02 20:12:24 +000013 * 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>
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000040#include <unistd.h>
41#include <fcntl.h>
42#include <sys/wait.h>
43#include <sys/types.h>
44#include <sys/socket.h>
Rusty Russell5eed48a2000-06-02 20:12:24 +000045
46#ifndef TRUE
47#define TRUE 1
48#endif
49#ifndef FALSE
50#define FALSE 0
51#endif
52
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000053#ifndef PROC_SYS_MODPROBE
54#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
55#endif
56
Rusty Russell5eed48a2000-06-02 20:12:24 +000057#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_CHECK 0x0800U
85#define CMD_RENAME_CHAIN 0x1000U
86#define NUMBER_OF_CMD 13
87static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000088 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000089
90#define OPTION_OFFSET 256
91
92#define OPT_NONE 0x00000U
93#define OPT_NUMERIC 0x00001U
94#define OPT_SOURCE 0x00002U
95#define OPT_DESTINATION 0x00004U
96#define OPT_PROTOCOL 0x00008U
97#define OPT_JUMP 0x00010U
98#define OPT_VERBOSE 0x00020U
99#define OPT_EXPANDED 0x00040U
100#define OPT_VIANAMEIN 0x00080U
101#define OPT_VIANAMEOUT 0x00100U
102#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000103#define OPT_COUNTERS 0x00400U
104#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000105static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000106= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000107
108static struct option original_opts[] = {
109 { "append", 1, 0, 'A' },
110 { "delete", 1, 0, 'D' },
111 { "insert", 1, 0, 'I' },
112 { "replace", 1, 0, 'R' },
113 { "list", 2, 0, 'L' },
114 { "flush", 2, 0, 'F' },
115 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000116 { "new-chain", 1, 0, 'N' },
117 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000118 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000119 { "policy", 1, 0, 'P' },
120 { "source", 1, 0, 's' },
121 { "destination", 1, 0, 'd' },
122 { "src", 1, 0, 's' }, /* synonym */
123 { "dst", 1, 0, 'd' }, /* synonym */
124 { "protocol", 1, 0, 'p' },
125 { "in-interface", 1, 0, 'i' },
126 { "jump", 1, 0, 'j' },
127 { "table", 1, 0, 't' },
128 { "match", 1, 0, 'm' },
129 { "numeric", 0, 0, 'n' },
130 { "out-interface", 1, 0, 'o' },
131 { "verbose", 0, 0, 'v' },
132 { "exact", 0, 0, 'x' },
133 { "version", 0, 0, 'V' },
134 { "help", 2, 0, 'h' },
135 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000136 { "modprobe", 1, 0, 'M' },
137 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000138 { 0 }
139};
140
Harald Weltea8658ca2003-03-05 07:46:15 +0000141/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
142 * current line of the input file, in order to give a more precise error
143 * message. ip6tables itself doesn't need this, so it is initialized to the
144 * magic number of -1 */
145int line = -1;
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;
Martin Josefsson357d59d2004-12-27 19:49:28 +0000194char *lib_dir;
Rusty Russell208d42e2004-12-20 05:29:52 +0000195
Rusty Russell5eed48a2000-06-02 20:12:24 +0000196/* Keeping track of external matches and targets: linked lists. */
197struct ip6tables_match *ip6tables_matches = NULL;
198struct ip6tables_target *ip6tables_targets = NULL;
199
200/* Extra debugging from libiptc */
201extern void dump_entries6(const ip6tc_handle_t handle);
202
203/* A few hardcoded protocols for 'all' and in case the user has no
204 /etc/protocols */
205struct pprot {
206 char *name;
207 u_int8_t num;
208};
209
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000210/* Primitive headers... */
211/* defined in netinet/in.h */
212#if 0
213#ifndef IPPROTO_ESP
214#define IPPROTO_ESP 50
215#endif
216#ifndef IPPROTO_AH
217#define IPPROTO_AH 51
218#endif
219#endif
220
Rusty Russell5eed48a2000-06-02 20:12:24 +0000221static const struct pprot chain_protos[] = {
222 { "tcp", IPPROTO_TCP },
223 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000224 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000225 { "esp", IPPROTO_ESP },
226 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000227 { "all", 0 },
228};
229
230static char *
231proto_to_name(u_int8_t proto, int nolookup)
232{
233 unsigned int i;
234
235 if (proto && !nolookup) {
236 struct protoent *pent = getprotobynumber(proto);
237 if (pent)
238 return pent->p_name;
239 }
240
241 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
242 if (chain_protos[i].num == proto)
243 return chain_protos[i].name;
244
245 return NULL;
246}
247
248static void
249in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
250{
251 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000252 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000253}
254
Pablo Neiradfdcd642005-05-29 19:05:23 +0000255static void free_opts(int reset_offset)
256{
257 if (opts != original_opts) {
258 free(opts);
259 opts = original_opts;
260 if (reset_offset)
261 global_option_offset = 0;
262 }
263}
264
Rusty Russell5eed48a2000-06-02 20:12:24 +0000265void
266exit_error(enum exittype status, char *msg, ...)
267{
268 va_list args;
269
270 va_start(args, msg);
271 fprintf(stderr, "%s v%s: ", program_name, program_version);
272 vfprintf(stderr, msg, args);
273 va_end(args);
274 fprintf(stderr, "\n");
275 if (status == PARAMETER_PROBLEM)
276 exit_tryhelp(status);
277 if (status == VERSION_PROBLEM)
278 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000279 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000280 /* On error paths, make sure that we don't leak memory */
281 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000282 exit(status);
283}
284
285void
286exit_tryhelp(int status)
287{
Harald Weltea8658ca2003-03-05 07:46:15 +0000288 if (line != -1)
289 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000290 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
291 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000292 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000293 exit(status);
294}
295
296void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000297exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000298{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000299 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000300 struct ip6tables_target *t = NULL;
301
302 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000303"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000304" %s -[RI] chain rulenum rule-specification [options]\n"
305" %s -D chain rulenum [options]\n"
306" %s -[LFZ] [chain] [options]\n"
307" %s -[NX] chain\n"
308" %s -E old-chain-name new-chain-name\n"
309" %s -P chain target [options]\n"
310" %s -h (print this help information)\n\n",
311 program_name, program_version, program_name, program_name,
312 program_name, program_name, program_name, program_name,
313 program_name, program_name);
314
315 printf(
316"Commands:\n"
317"Either long or short options are allowed.\n"
318" --append -A chain Append to chain\n"
319" --delete -D chain Delete matching rule from chain\n"
320" --delete -D chain rulenum\n"
321" Delete rule rulenum (1 = first) from chain\n"
322" --insert -I chain [rulenum]\n"
323" Insert in chain as rulenum (default 1=first)\n"
324" --replace -R chain rulenum\n"
325" Replace rule rulenum (1 = first) in chain\n"
326" --list -L [chain] List the rules in a chain or all chains\n"
327" --flush -F [chain] Delete all rules in chain or all chains\n"
328" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000329" --new -N chain Create a new user-defined chain\n"
330" --delete-chain\n"
331" -X [chain] Delete a user-defined chain\n"
332" --policy -P chain target\n"
333" Change policy on chain to target\n"
334" --rename-chain\n"
335" -E old-chain new-chain\n"
336" Change chain name, (moving any references)\n"
337
338"Options:\n"
339" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
340" --source -s [!] address[/mask]\n"
341" source specification\n"
342" --destination -d [!] address[/mask]\n"
343" destination specification\n"
344" --in-interface -i [!] input name[+]\n"
345" network interface name ([+] for wildcard)\n"
346" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000347" target for rule (may load target extension)\n"
348" --match -m match\n"
349" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000350" --numeric -n numeric output of addresses and ports\n"
351" --out-interface -o [!] output name[+]\n"
352" network interface name ([+] for wildcard)\n"
353" --table -t table table to manipulate (default: `filter')\n"
354" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000355" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000356" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000357/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000358" --modprobe=<command> try to insert modules using this command\n"
359" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000360"[!] --version -V print package version.\n");
361
362 /* Print out any special helps. A user might like to be able to add a --help
363 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000364 specified matches & targets */
365 for (t = ip6tables_targets; t; t = t->next) {
366 if (t->used) {
367 printf("\n");
368 t->help();
369 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000370 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000371 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000372 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000373 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000374 }
375 exit(0);
376}
377
378static void
379generic_opt_check(int command, int options)
380{
381 int i, j, legal = 0;
382
383 /* Check that commands are valid with options. Complicated by the
384 * fact that if an option is legal with *any* command given, it is
385 * legal overall (ie. -z and -l).
386 */
387 for (i = 0; i < NUMBER_OF_OPT; i++) {
388 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
389
390 for (j = 0; j < NUMBER_OF_CMD; j++) {
391 if (!(command & (1<<j)))
392 continue;
393
394 if (!(options & (1<<i))) {
395 if (commands_v_options[j][i] == '+')
396 exit_error(PARAMETER_PROBLEM,
397 "You need to supply the `-%c' "
398 "option for this command\n",
399 optflags[i]);
400 } else {
401 if (commands_v_options[j][i] != 'x')
402 legal = 1;
403 else if (legal == 0)
404 legal = -1;
405 }
406 }
407 if (legal == -1)
408 exit_error(PARAMETER_PROBLEM,
409 "Illegal option `-%c' with this command\n",
410 optflags[i]);
411 }
412}
413
414static char
415opt2char(int option)
416{
417 const char *ptr;
418 for (ptr = optflags; option > 1; option >>= 1, ptr++);
419
420 return *ptr;
421}
422
423static char
424cmd2char(int option)
425{
426 const char *ptr;
427 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
428
429 return *ptr;
430}
431
432static void
433add_command(int *cmd, const int newcmd, const int othercmds, int invert)
434{
435 if (invert)
436 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
437 if (*cmd & (~othercmds))
438 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
439 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
440 *cmd |= newcmd;
441}
442
443int
Harald Welteb77f1da2002-03-14 11:35:58 +0000444check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000445{
446 if (option && strcmp(option, "!") == 0) {
447 if (*invert)
448 exit_error(PARAMETER_PROBLEM,
449 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000450 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000451 if (optind) {
452 *optind = *optind+1;
453 if (argc && *optind > argc)
454 exit_error(PARAMETER_PROBLEM,
455 "no argument following `!'");
456 }
457
Rusty Russell5eed48a2000-06-02 20:12:24 +0000458 return TRUE;
459 }
460 return FALSE;
461}
462
463static void *
464fw_calloc(size_t count, size_t size)
465{
466 void *p;
467
468 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000469 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470 exit(1);
471 }
472 return p;
473}
474
475static void *
476fw_malloc(size_t size)
477{
478 void *p;
479
480 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000481 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000482 exit(1);
483 }
484 return p;
485}
486
Rusty Russell5eed48a2000-06-02 20:12:24 +0000487static char *
488addr_to_numeric(const struct in6_addr *addrp)
489{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000490 /* 0000:0000:0000:0000:0000:000.000.000.000
491 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000492 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000493 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000494}
495
496static struct in6_addr *
497numeric_to_addr(const char *num)
498{
499 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000500 int err;
501 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000502 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000503#ifdef DEBUG
504 fprintf(stderr, "\nnumeric2addr: %d\n", err);
505#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000506 return (struct in6_addr *)NULL;
507}
508
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000509
510static struct in6_addr *
511host_to_addr(const char *name, unsigned int *naddr)
512{
513 struct addrinfo hints;
514 struct addrinfo *res;
515 static struct in6_addr *addr;
516 int err;
517
518 memset(&hints, 0, sizeof(hints));
519 hints.ai_flags=AI_CANONNAME;
520 hints.ai_family=AF_INET6;
521 hints.ai_socktype=SOCK_RAW;
522 hints.ai_protocol=41;
523 hints.ai_next=NULL;
524
525 *naddr = 0;
526 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
527#ifdef DEBUG
528 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
529#endif
530 return (struct in6_addr *) NULL;
531 } else {
532 if (res->ai_family != AF_INET6 ||
533 res->ai_addrlen != sizeof(struct sockaddr_in6))
534 return (struct in6_addr *) NULL;
535
536#ifdef DEBUG
537 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
538 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
539#endif
540 /* Get the first element of the address-chain */
541 addr = fw_calloc(1, sizeof(struct in6_addr));
542 in6addrcpy(addr, (struct in6_addr *)
543 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
544 freeaddrinfo(res);
545 *naddr = 1;
546 return addr;
547 }
548
549 return (struct in6_addr *) NULL;
550}
551
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000552static char *
553addr_to_host(const struct in6_addr *addr)
554{
555 struct sockaddr_in6 saddr;
556 int err;
557 static char hostname[NI_MAXHOST];
558
559 memset(&saddr, 0, sizeof(struct sockaddr_in6));
560 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000561 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000562
563 if ( (err=getnameinfo((struct sockaddr *)&saddr,
564 sizeof(struct sockaddr_in6),
565 hostname, sizeof(hostname)-1,
566 NULL, 0, 0)) != 0 ){
567#ifdef DEBUG
568 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
569#endif
570 return (char *) NULL;
571 } else {
572#ifdef DEBUG
573 fprintf (stderr, "\naddr2host: %s\n", hostname);
574#endif
575
576 return hostname;
577 }
578
579 return (char *) NULL;
580}
581
Rusty Russell5eed48a2000-06-02 20:12:24 +0000582static char *
583mask_to_numeric(const struct in6_addr *addrp)
584{
Harald Welte8a50ab82003-06-24 18:15:59 +0000585 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000586 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000587 if (l == -1) {
588 strcpy(buf, "/");
589 strcat(buf, addr_to_numeric(addrp));
590 return buf;
591 }
Harald Welte43ac1912002-03-03 09:43:07 +0000592 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000593 return buf;
594}
595
596static struct in6_addr *
597network_to_addr(const char *name)
598{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000599 /* abort();*/
600 /* TODO: not implemented yet, but the exception breaks the
601 * name resolvation */
602 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000603}
604
605static char *
606addr_to_anyname(const struct in6_addr *addr)
607{
608 char *name;
609
610 if ((name = addr_to_host(addr)) != NULL)
611 return name;
612
613 return addr_to_numeric(addr);
614}
615
616/*
617 * All functions starting with "parse" should succeed, otherwise
618 * the program fails.
619 * Most routines return pointers to static data that may change
620 * between calls to the same or other routines with a few exceptions:
621 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
622 * return global static data.
623*/
624
625static struct in6_addr *
626parse_hostnetwork(const char *name, unsigned int *naddrs)
627{
628 struct in6_addr *addrp, *addrptmp;
629
630 if ((addrptmp = numeric_to_addr(name)) != NULL ||
631 (addrptmp = network_to_addr(name)) != NULL) {
632 addrp = fw_malloc(sizeof(struct in6_addr));
633 in6addrcpy(addrp, addrptmp);
634 *naddrs = 1;
635 return addrp;
636 }
637 if ((addrp = host_to_addr(name, naddrs)) != NULL)
638 return addrp;
639
640 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
641}
642
643static struct in6_addr *
644parse_mask(char *mask)
645{
646 static struct in6_addr maskaddr;
647 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000648 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000649
650 if (mask == NULL) {
651 /* no mask at all defaults to 128 bits */
652 memset(&maskaddr, 0xff, sizeof maskaddr);
653 return &maskaddr;
654 }
655 if ((addrp = numeric_to_addr(mask)) != NULL)
656 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000657 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000658 exit_error(PARAMETER_PROBLEM,
659 "invalid mask `%s' specified", mask);
660 if (bits != 0) {
661 char *p = (char *)&maskaddr;
662 memset(p, 0xff, bits / 8);
663 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
664 p[bits / 8] = 0xff << (8 - (bits & 7));
665 return &maskaddr;
666 }
667
668 memset(&maskaddr, 0, sizeof maskaddr);
669 return &maskaddr;
670}
671
Harald Welte43ac1912002-03-03 09:43:07 +0000672void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000673parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
674 struct in6_addr *maskp, unsigned int *naddrs)
675{
676 struct in6_addr *addrp;
677 char buf[256];
678 char *p;
679 int i, j, n;
680
681 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000682 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000683 if ((p = strrchr(buf, '/')) != NULL) {
684 *p = '\0';
685 addrp = parse_mask(p + 1);
686 } else
687 addrp = parse_mask(NULL);
688 in6addrcpy(maskp, addrp);
689
690 /* if a null mask is given, the name is ignored, like in "any/0" */
691 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
692 strcpy(buf, "::");
693
694 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
695 n = *naddrs;
696 for (i = 0, j = 0; i < n; i++) {
697 int k;
698 for (k = 0; k < 4; k++)
699 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
700 j++;
701 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000702 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000703 (*naddrs)--;
704 j--;
705 break;
706 }
707 }
708 }
709}
710
711struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000712find_match(const char *name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000713{
714 struct ip6tables_match *ptr;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000715 int icmphack = 0;
716
717 /* This is ugly as hell. Nonetheless, there is no way of changing
718 * this without hurting backwards compatibility */
719 if ( (strcmp(name,"icmpv6") == 0) ||
720 (strcmp(name,"ipv6-icmp") == 0) ||
721 (strcmp(name,"icmp6") == 0) ) icmphack = 1;
722
723 if (!icmphack) {
724 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
725 if (strcmp(name, ptr->name) == 0)
726 break;
727 }
728 } else {
729 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
730 if (strcmp("icmp6", ptr->name) == 0)
731 break;
732 }
733 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000734
Harald Welte3efb6ea2001-08-06 18:50:21 +0000735#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000736 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000737 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738 + strlen(name)];
Harald Weltecfaed1f2001-10-04 08:11:44 +0000739 if (!icmphack)
Rusty Russell208d42e2004-12-20 05:29:52 +0000740 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Harald Weltecfaed1f2001-10-04 08:11:44 +0000741 else
Rusty Russell208d42e2004-12-20 05:29:52 +0000742 sprintf(path, "%s/libip6t_%s.so", lib_dir, "icmpv6");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000743 if (dlopen(path, RTLD_NOW)) {
744 /* Found library. If it didn't register itself,
745 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000746 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000747
748 if (!ptr)
749 exit_error(PARAMETER_PROBLEM,
750 "Couldn't load match `%s'\n",
751 name);
752 } else if (tryload == LOAD_MUST_SUCCEED)
753 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000754 "Couldn't load match `%s':%s\n",
755 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000756 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000757#else
758 if (ptr && !ptr->loaded) {
759 if (tryload != DONT_LOAD)
760 ptr->loaded = 1;
761 else
762 ptr = NULL;
763 }
Marc Boucher067477b2002-03-24 15:09:31 +0000764 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
765 exit_error(PARAMETER_PROBLEM,
766 "Couldn't find match `%s'\n", name);
767 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000768#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000769
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000770 if (ptr && matches) {
771 struct ip6tables_rule_match **i;
772 struct ip6tables_rule_match *newentry;
773
774 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
775
776 for (i = matches; *i; i = &(*i)->next);
777 newentry->match = ptr;
778 newentry->next = NULL;
779 *i = newentry;
780 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000781
Rusty Russell5eed48a2000-06-02 20:12:24 +0000782 return ptr;
783}
784
785/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
786static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000787find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000788{
Harald Welteed498492001-07-23 01:24:22 +0000789 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790
Harald Welte43ac1912002-03-03 09:43:07 +0000791 if (string_to_number(pname, 0, 255, &proto) != -1) {
792 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000793
Harald Welte43ac1912002-03-03 09:43:07 +0000794 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000795 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000796 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000797 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000798
799 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000800}
801
Harald Welte43ac1912002-03-03 09:43:07 +0000802u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803parse_protocol(const char *s)
804{
Harald Welteed498492001-07-23 01:24:22 +0000805 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000806
Harald Welteed498492001-07-23 01:24:22 +0000807 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000808 struct protoent *pent;
809
810 if ((pent = getprotobyname(s)))
811 proto = pent->p_proto;
812 else {
813 unsigned int i;
814 for (i = 0;
815 i < sizeof(chain_protos)/sizeof(struct pprot);
816 i++) {
817 if (strcmp(s, chain_protos[i].name) == 0) {
818 proto = chain_protos[i].num;
819 break;
820 }
821 }
822 if (i == sizeof(chain_protos)/sizeof(struct pprot))
823 exit_error(PARAMETER_PROBLEM,
824 "unknown protocol `%s' specified",
825 s);
826 }
827 }
828
829 return (u_int16_t)proto;
830}
831
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000832void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000833{
834 int vialen = strlen(arg);
835 unsigned int i;
836
837 memset(mask, 0, IFNAMSIZ);
838 memset(vianame, 0, IFNAMSIZ);
839
840 if (vialen + 1 > IFNAMSIZ)
841 exit_error(PARAMETER_PROBLEM,
842 "interface name `%s' must be shorter than IFNAMSIZ"
843 " (%i)", arg, IFNAMSIZ-1);
844
845 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000846 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000847 memset(mask, 0, IFNAMSIZ);
848 else if (vianame[vialen - 1] == '+') {
849 memset(mask, 0xFF, vialen - 1);
850 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000851 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000852 } else {
853 /* Include nul-terminator in match */
854 memset(mask, 0xFF, vialen + 1);
855 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000856 for (i = 0; vianame[i]; i++) {
857 if (!isalnum(vianame[i])
858 && vianame[i] != '_'
859 && vianame[i] != '.') {
860 printf("Warning: wierd character in interface"
861 " `%s' (No aliases, :, ! or *).\n",
862 vianame);
863 break;
864 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000865 }
866 }
867}
868
869/* Can't be zero. */
870static int
871parse_rulenumber(const char *rule)
872{
Harald Welte43ac1912002-03-03 09:43:07 +0000873 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000874
Harald Welte43ac1912002-03-03 09:43:07 +0000875 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000876 exit_error(PARAMETER_PROBLEM,
877 "Invalid rule number `%s'", rule);
878
879 return rulenum;
880}
881
882static const char *
883parse_target(const char *targetname)
884{
885 const char *ptr;
886
887 if (strlen(targetname) < 1)
888 exit_error(PARAMETER_PROBLEM,
889 "Invalid target name (too short)");
890
891 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
892 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000893 "Invalid target name `%s' (%u chars max)",
894 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000895
896 for (ptr = targetname; *ptr; ptr++)
897 if (isspace(*ptr))
898 exit_error(PARAMETER_PROBLEM,
899 "Invalid target name `%s'", targetname);
900 return targetname;
901}
902
903int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000904string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
905 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000906{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000907 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 char *end;
909
910 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000911 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000912 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000913 if (*end == '\0' && end != s) {
914 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000915 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000916 *ret = number;
917 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000918 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000919 }
920 return -1;
921}
922
Martin Josefssonb105bc92004-05-26 15:54:49 +0000923int
924string_to_number_l(const char *s, unsigned long min, unsigned long max,
925 unsigned long *ret)
926{
927 int result;
928 unsigned long long number;
929
930 result = string_to_number_ll(s, min, max, &number);
931 *ret = (unsigned long)number;
932
933 return result;
934}
935
936int string_to_number(const char *s, unsigned int min, unsigned int max,
937 unsigned int *ret)
938{
939 int result;
940 unsigned long number;
941
942 result = string_to_number_l(s, min, max, &number);
943 *ret = (unsigned int)number;
944
945 return result;
946}
947
Rusty Russell5eed48a2000-06-02 20:12:24 +0000948static void
949set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
950 int invert)
951{
952 if (*options & option)
953 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
954 opt2char(option));
955 *options |= option;
956
957 if (invert) {
958 unsigned int i;
959 for (i = 0; 1 << i != option; i++);
960
961 if (!inverse_for_options[i])
962 exit_error(PARAMETER_PROBLEM,
963 "cannot have ! before -%c",
964 opt2char(option));
965 *invflg |= inverse_for_options[i];
966 }
967}
968
969struct ip6tables_target *
970find_target(const char *name, enum ip6t_tryload tryload)
971{
972 struct ip6tables_target *ptr;
973
974 /* Standard target? */
975 if (strcmp(name, "") == 0
976 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
977 || strcmp(name, IP6TC_LABEL_DROP) == 0
978 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
979 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
980 name = "standard";
981
982 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
983 if (strcmp(name, ptr->name) == 0)
984 break;
985 }
986
Harald Welte3efb6ea2001-08-06 18:50:21 +0000987#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000988 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000989 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000990 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000991 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000992 if (dlopen(path, RTLD_NOW)) {
993 /* Found library. If it didn't register itself,
994 maybe they specified match as a target. */
995 ptr = find_target(name, DONT_LOAD);
996 if (!ptr)
997 exit_error(PARAMETER_PROBLEM,
998 "Couldn't load target `%s'\n",
999 name);
1000 } else if (tryload == LOAD_MUST_SUCCEED)
1001 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001002 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001003 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001005#else
1006 if (ptr && !ptr->loaded) {
1007 if (tryload != DONT_LOAD)
1008 ptr->loaded = 1;
1009 else
1010 ptr = NULL;
1011 }
Marc Boucher067477b2002-03-24 15:09:31 +00001012 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1013 exit_error(PARAMETER_PROBLEM,
1014 "Couldn't find target `%s'\n", name);
1015 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001016#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001017
Harald Welte43ac1912002-03-03 09:43:07 +00001018 if (ptr)
1019 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001020
Rusty Russell5eed48a2000-06-02 20:12:24 +00001021 return ptr;
1022}
1023
1024static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001025merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001026 unsigned int *option_offset)
1027{
1028 unsigned int num_old, num_new, i;
1029 struct option *merge;
1030
Pablo Neiradfdcd642005-05-29 19:05:23 +00001031 /* Release previous options merged if any */
1032 free_opts(0);
1033
Rusty Russell5eed48a2000-06-02 20:12:24 +00001034 for (num_old = 0; oldopts[num_old].name; num_old++);
1035 for (num_new = 0; newopts[num_new].name; num_new++);
1036
1037 global_option_offset += OPTION_OFFSET;
1038 *option_offset = global_option_offset;
1039
1040 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1041 memcpy(merge, oldopts, num_old * sizeof(struct option));
1042 for (i = 0; i < num_new; i++) {
1043 merge[num_old + i] = newopts[i];
1044 merge[num_old + i].val += *option_offset;
1045 }
1046 memset(merge + num_old + num_new, 0, sizeof(struct option));
1047
1048 return merge;
1049}
1050
1051void
1052register_match6(struct ip6tables_match *me)
1053{
Harald Welte43ac1912002-03-03 09:43:07 +00001054 struct ip6tables_match **i;
1055
Rusty Russell5eed48a2000-06-02 20:12:24 +00001056 if (strcmp(me->version, program_version) != 0) {
1057 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1058 program_name, me->name, me->version, program_version);
1059 exit(1);
1060 }
1061
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001062 if (find_match(me->name, DONT_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001063 fprintf(stderr, "%s: match `%s' already registered.\n",
1064 program_name, me->name);
1065 exit(1);
1066 }
1067
Harald Welte43ac1912002-03-03 09:43:07 +00001068 if (me->size != IP6T_ALIGN(me->size)) {
1069 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001070 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001071 exit(1);
1072 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001073
Harald Welte43ac1912002-03-03 09:43:07 +00001074 /* Append to list. */
1075 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1076 me->next = NULL;
1077 *i = me;
1078
Rusty Russell5eed48a2000-06-02 20:12:24 +00001079 me->m = NULL;
1080 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001081}
1082
1083void
1084register_target6(struct ip6tables_target *me)
1085{
1086 if (strcmp(me->version, program_version) != 0) {
1087 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1088 program_name, me->name, me->version, program_version);
1089 exit(1);
1090 }
1091
1092 if (find_target(me->name, DONT_LOAD)) {
1093 fprintf(stderr, "%s: target `%s' already registered.\n",
1094 program_name, me->name);
1095 exit(1);
1096 }
1097
Harald Welte43ac1912002-03-03 09:43:07 +00001098 if (me->size != IP6T_ALIGN(me->size)) {
1099 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001100 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001101 exit(1);
1102 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001103
Rusty Russell5eed48a2000-06-02 20:12:24 +00001104 /* Prepend to list. */
1105 me->next = ip6tables_targets;
1106 ip6tables_targets = me;
1107 me->t = NULL;
1108 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001109}
1110
1111static void
1112print_num(u_int64_t number, unsigned int format)
1113{
Harald Welte43ac1912002-03-03 09:43:07 +00001114 if (format & FMT_KILOMEGAGIGA) {
1115 if (number > 99999) {
1116 number = (number + 500) / 1000;
1117 if (number > 9999) {
1118 number = (number + 500) / 1000;
1119 if (number > 9999) {
1120 number = (number + 500) / 1000;
1121 if (number > 9999) {
1122 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001123 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001124 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001125 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001126 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001127 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001128 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001129 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001130 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001131 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001132 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001133 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001134}
1135
Harald Welte43ac1912002-03-03 09:43:07 +00001136
Rusty Russell5eed48a2000-06-02 20:12:24 +00001137static void
1138print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1139{
1140 struct ip6t_counters counters;
1141 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1142 printf("Chain %s", chain);
1143 if (pol) {
1144 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001145 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001146 fputc(' ', stdout);
1147 print_num(counters.pcnt, (format|FMT_NOTABLE));
1148 fputs("packets, ", stdout);
1149 print_num(counters.bcnt, (format|FMT_NOTABLE));
1150 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001151 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001152 printf(")\n");
1153 } else {
1154 unsigned int refs;
1155 if (!ip6tc_get_references(&refs, chain, handle))
1156 printf(" (ERROR obtaining refs)\n");
1157 else
1158 printf(" (%u references)\n", refs);
1159 }
1160
1161 if (format & FMT_LINENUMBERS)
1162 printf(FMT("%-4s ", "%s "), "num");
1163 if (!(format & FMT_NOCOUNTS)) {
1164 if (format & FMT_KILOMEGAGIGA) {
1165 printf(FMT("%5s ","%s "), "pkts");
1166 printf(FMT("%5s ","%s "), "bytes");
1167 } else {
1168 printf(FMT("%8s ","%s "), "pkts");
1169 printf(FMT("%10s ","%s "), "bytes");
1170 }
1171 }
1172 if (!(format & FMT_NOTARGET))
1173 printf(FMT("%-9s ","%s "), "target");
1174 fputs(" prot ", stdout);
1175 if (format & FMT_OPTIONS)
1176 fputs("opt", stdout);
1177 if (format & FMT_VIA) {
1178 printf(FMT(" %-6s ","%s "), "in");
1179 printf(FMT("%-6s ","%s "), "out");
1180 }
1181 printf(FMT(" %-19s ","%s "), "source");
1182 printf(FMT(" %-19s "," %s "), "destination");
1183 printf("\n");
1184}
1185
Harald Welte43ac1912002-03-03 09:43:07 +00001186
Rusty Russell5eed48a2000-06-02 20:12:24 +00001187static int
1188print_match(const struct ip6t_entry_match *m,
1189 const struct ip6t_ip6 *ip,
1190 int numeric)
1191{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001192 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001193
1194 if (match) {
1195 if (match->print)
1196 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001197 else
1198 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001199 } else {
1200 if (m->u.user.name[0])
1201 printf("UNKNOWN match `%s' ", m->u.user.name);
1202 }
1203 /* Don't stop iterating. */
1204 return 0;
1205}
1206
1207/* e is called `fw' here for hysterical raisins */
1208static void
1209print_firewall(const struct ip6t_entry *fw,
1210 const char *targname,
1211 unsigned int num,
1212 unsigned int format,
1213 const ip6tc_handle_t handle)
1214{
1215 struct ip6tables_target *target = NULL;
1216 const struct ip6t_entry_target *t;
1217 u_int8_t flags;
1218 char buf[BUFSIZ];
1219
Rusty Russell5eed48a2000-06-02 20:12:24 +00001220 if (!ip6tc_is_chain(targname, handle))
1221 target = find_target(targname, TRY_LOAD);
1222 else
1223 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1224
1225 t = ip6t_get_target((struct ip6t_entry *)fw);
1226 flags = fw->ipv6.flags;
1227
1228 if (format & FMT_LINENUMBERS)
1229 printf(FMT("%-4u ", "%u "), num+1);
1230
1231 if (!(format & FMT_NOCOUNTS)) {
1232 print_num(fw->counters.pcnt, format);
1233 print_num(fw->counters.bcnt, format);
1234 }
1235
1236 if (!(format & FMT_NOTARGET))
1237 printf(FMT("%-9s ", "%s "), targname);
1238
1239 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1240 {
Harald Welte43ac1912002-03-03 09:43:07 +00001241 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001242 if (pname)
1243 printf(FMT("%-5s", "%s "), pname);
1244 else
1245 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1246 }
1247
1248 if (format & FMT_OPTIONS) {
1249 if (format & FMT_NOTABLE)
1250 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001251 fputc(' ', stdout); /* Invert flag of FRAG */
1252 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001253 fputc(' ', stdout);
1254 }
1255
1256 if (format & FMT_VIA) {
1257 char iface[IFNAMSIZ+2];
1258
1259 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1260 iface[0] = '!';
1261 iface[1] = '\0';
1262 }
1263 else iface[0] = '\0';
1264
1265 if (fw->ipv6.iniface[0] != '\0') {
1266 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001267 }
1268 else if (format & FMT_NUMERIC) strcat(iface, "*");
1269 else strcat(iface, "any");
1270 printf(FMT(" %-6s ","in %s "), iface);
1271
1272 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1273 iface[0] = '!';
1274 iface[1] = '\0';
1275 }
1276 else iface[0] = '\0';
1277
1278 if (fw->ipv6.outiface[0] != '\0') {
1279 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001280 }
1281 else if (format & FMT_NUMERIC) strcat(iface, "*");
1282 else strcat(iface, "any");
1283 printf(FMT("%-6s ","out %s "), iface);
1284 }
1285
1286 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1287 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1288 && !(format & FMT_NUMERIC))
1289 printf(FMT("%-19s ","%s "), "anywhere");
1290 else {
1291 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001292 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293 else
Harald Welte43ac1912002-03-03 09:43:07 +00001294 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001295 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1296 printf(FMT("%-19s ","%s "), buf);
1297 }
1298
1299 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1300 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1301 && !(format & FMT_NUMERIC))
1302 printf(FMT("%-19s","-> %s"), "anywhere");
1303 else {
1304 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001305 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001306 else
Harald Welte43ac1912002-03-03 09:43:07 +00001307 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001308 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1309 printf(FMT("%-19s","-> %s"), buf);
1310 }
1311
1312 if (format & FMT_NOTABLE)
1313 fputs(" ", stdout);
1314
1315 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1316
1317 if (target) {
1318 if (target->print)
1319 /* Print the target information. */
1320 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1321 } else if (t->u.target_size != sizeof(*t))
1322 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001323 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001324
1325 if (!(format & FMT_NONEWLINE))
1326 fputc('\n', stdout);
1327}
1328
1329static void
1330print_firewall_line(const struct ip6t_entry *fw,
1331 const ip6tc_handle_t h)
1332{
1333 struct ip6t_entry_target *t;
1334
1335 t = ip6t_get_target((struct ip6t_entry *)fw);
1336 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1337}
1338
1339static int
1340append_entry(const ip6t_chainlabel chain,
1341 struct ip6t_entry *fw,
1342 unsigned int nsaddrs,
1343 const struct in6_addr saddrs[],
1344 unsigned int ndaddrs,
1345 const struct in6_addr daddrs[],
1346 int verbose,
1347 ip6tc_handle_t *handle)
1348{
1349 unsigned int i, j;
1350 int ret = 1;
1351
1352 for (i = 0; i < nsaddrs; i++) {
1353 fw->ipv6.src = saddrs[i];
1354 for (j = 0; j < ndaddrs; j++) {
1355 fw->ipv6.dst = daddrs[j];
1356 if (verbose)
1357 print_firewall_line(fw, *handle);
1358 ret &= ip6tc_append_entry(chain, fw, handle);
1359 }
1360 }
1361
1362 return ret;
1363}
1364
1365static int
1366replace_entry(const ip6t_chainlabel chain,
1367 struct ip6t_entry *fw,
1368 unsigned int rulenum,
1369 const struct in6_addr *saddr,
1370 const struct in6_addr *daddr,
1371 int verbose,
1372 ip6tc_handle_t *handle)
1373{
1374 fw->ipv6.src = *saddr;
1375 fw->ipv6.dst = *daddr;
1376
1377 if (verbose)
1378 print_firewall_line(fw, *handle);
1379 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1380}
1381
1382static int
1383insert_entry(const ip6t_chainlabel chain,
1384 struct ip6t_entry *fw,
1385 unsigned int rulenum,
1386 unsigned int nsaddrs,
1387 const struct in6_addr saddrs[],
1388 unsigned int ndaddrs,
1389 const struct in6_addr daddrs[],
1390 int verbose,
1391 ip6tc_handle_t *handle)
1392{
1393 unsigned int i, j;
1394 int ret = 1;
1395
1396 for (i = 0; i < nsaddrs; i++) {
1397 fw->ipv6.src = saddrs[i];
1398 for (j = 0; j < ndaddrs; j++) {
1399 fw->ipv6.dst = daddrs[j];
1400 if (verbose)
1401 print_firewall_line(fw, *handle);
1402 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1403 }
1404 }
1405
1406 return ret;
1407}
1408
1409static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001410make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001411{
1412 /* Establish mask for comparison */
1413 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001414 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001415 unsigned char *mask, *mptr;
1416
1417 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001418 for (matchp = matches; matchp; matchp = matchp->next)
1419 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001420
1421 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001422 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001423 + ip6tables_targets->size);
1424
1425 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1426 mptr = mask + sizeof(struct ip6t_entry);
1427
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001428 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001429 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001430 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001431 + matchp->match->userspacesize);
1432 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001433 }
1434
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001435 memset(mptr, 0xFF,
1436 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1437 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001438
1439 return mask;
1440}
1441
1442static int
1443delete_entry(const ip6t_chainlabel chain,
1444 struct ip6t_entry *fw,
1445 unsigned int nsaddrs,
1446 const struct in6_addr saddrs[],
1447 unsigned int ndaddrs,
1448 const struct in6_addr daddrs[],
1449 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001450 ip6tc_handle_t *handle,
1451 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001452{
1453 unsigned int i, j;
1454 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001455 unsigned char *mask;
1456
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001457 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001459 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001460 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001461 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001462 if (verbose)
1463 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001464 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001465 }
1466 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001467 free(mask);
1468
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469 return ret;
1470}
1471
András Kis-Szabó764316a2001-02-26 17:31:20 +00001472int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001473for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1474 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1475{
1476 int ret = 1;
1477 const char *chain;
1478 char *chains;
1479 unsigned int i, chaincount = 0;
1480
1481 chain = ip6tc_first_chain(handle);
1482 while (chain) {
1483 chaincount++;
1484 chain = ip6tc_next_chain(handle);
1485 }
1486
1487 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1488 i = 0;
1489 chain = ip6tc_first_chain(handle);
1490 while (chain) {
1491 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1492 i++;
1493 chain = ip6tc_next_chain(handle);
1494 }
1495
1496 for (i = 0; i < chaincount; i++) {
1497 if (!builtinstoo
1498 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001499 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001500 continue;
1501 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1502 }
1503
1504 free(chains);
1505 return ret;
1506}
1507
András Kis-Szabó764316a2001-02-26 17:31:20 +00001508int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509flush_entries(const ip6t_chainlabel chain, int verbose,
1510 ip6tc_handle_t *handle)
1511{
1512 if (!chain)
1513 return for_each_chain(flush_entries, verbose, 1, handle);
1514
1515 if (verbose)
1516 fprintf(stdout, "Flushing chain `%s'\n", chain);
1517 return ip6tc_flush_entries(chain, handle);
1518}
1519
1520static int
1521zero_entries(const ip6t_chainlabel chain, int verbose,
1522 ip6tc_handle_t *handle)
1523{
1524 if (!chain)
1525 return for_each_chain(zero_entries, verbose, 1, handle);
1526
1527 if (verbose)
1528 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1529 return ip6tc_zero_entries(chain, handle);
1530}
1531
András Kis-Szabó764316a2001-02-26 17:31:20 +00001532int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001533delete_chain(const ip6t_chainlabel chain, int verbose,
1534 ip6tc_handle_t *handle)
1535{
1536 if (!chain)
1537 return for_each_chain(delete_chain, verbose, 0, handle);
1538
1539 if (verbose)
1540 fprintf(stdout, "Deleting chain `%s'\n", chain);
1541 return ip6tc_delete_chain(chain, handle);
1542}
1543
1544static int
1545list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1546 int expanded, int linenumbers, ip6tc_handle_t *handle)
1547{
1548 int found = 0;
1549 unsigned int format;
1550 const char *this;
1551
1552 format = FMT_OPTIONS;
1553 if (!verbose)
1554 format |= FMT_NOCOUNTS;
1555 else
1556 format |= FMT_VIA;
1557
1558 if (numeric)
1559 format |= FMT_NUMERIC;
1560
1561 if (!expanded)
1562 format |= FMT_KILOMEGAGIGA;
1563
1564 if (linenumbers)
1565 format |= FMT_LINENUMBERS;
1566
1567 for (this = ip6tc_first_chain(handle);
1568 this;
1569 this = ip6tc_next_chain(handle)) {
1570 const struct ip6t_entry *i;
1571 unsigned int num;
1572
1573 if (chain && strcmp(chain, this) != 0)
1574 continue;
1575
1576 if (found) printf("\n");
1577
1578 print_header(format, this, handle);
1579 i = ip6tc_first_rule(this, handle);
1580
1581 num = 0;
1582 while (i) {
1583 print_firewall(i,
1584 ip6tc_get_target(i, handle),
1585 num++,
1586 format,
1587 *handle);
1588 i = ip6tc_next_rule(i, handle);
1589 }
1590 found = 1;
1591 }
1592
1593 errno = ENOENT;
1594 return found;
1595}
1596
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001597static char *get_modprobe(void)
1598{
Harald Welte43ac1912002-03-03 09:43:07 +00001599 int procfile;
1600 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001601
Harald Welte10f7f142004-10-22 08:14:07 +00001602#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001603 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1604 if (procfile < 0)
1605 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001606
Harald Welte10f7f142004-10-22 08:14:07 +00001607 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001608 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001609 memset(ret, 0, PROCFILE_BUFSIZ);
1610 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001611 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001612 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001613 }
1614 if (ret[strlen(ret)-1]=='\n')
1615 ret[strlen(ret)-1]=0;
1616 close(procfile);
1617 return ret;
1618 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001619 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001620 free(ret);
1621 close(procfile);
1622 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001623}
1624
Harald Welte58918652001-06-16 18:25:25 +00001625int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001626{
Harald Welte43ac1912002-03-03 09:43:07 +00001627 char *buf = NULL;
1628 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001629 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001630
Harald Welte43ac1912002-03-03 09:43:07 +00001631 /* If they don't explicitly set it, read out of kernel */
1632 if (!modprobe) {
1633 buf = get_modprobe();
1634 if (!buf)
1635 return -1;
1636 modprobe = buf;
1637 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001638
Harald Welte43ac1912002-03-03 09:43:07 +00001639 switch (fork()) {
1640 case 0:
1641 argv[0] = (char *)modprobe;
1642 argv[1] = (char *)modname;
1643 argv[2] = NULL;
1644 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001645
Harald Welte43ac1912002-03-03 09:43:07 +00001646 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001647 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001648 case -1:
1649 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001650
Harald Welte43ac1912002-03-03 09:43:07 +00001651 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001652 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001653 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001654
Harald Welte43ac1912002-03-03 09:43:07 +00001655 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001656 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1657 return 0;
1658 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001659}
1660
Rusty Russell5eed48a2000-06-02 20:12:24 +00001661static struct ip6t_entry *
1662generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001663 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001664 struct ip6t_entry_target *target)
1665{
1666 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001667 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668 struct ip6t_entry *e;
1669
1670 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001671 for (matchp = matches; matchp; matchp = matchp->next)
1672 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001673
1674 e = fw_malloc(size + target->u.target_size);
1675 *e = *fw;
1676 e->target_offset = size;
1677 e->next_offset = size + target->u.target_size;
1678
1679 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001680 for (matchp = matches; matchp; matchp = matchp->next) {
1681 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1682 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001683 }
1684 memcpy(e->elems + size, target, target->u.target_size);
1685
1686 return e;
1687}
1688
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001689void clear_rule_matches(struct ip6tables_rule_match **matches)
1690{
1691 struct ip6tables_rule_match *matchp, *tmp;
1692
1693 for (matchp = *matches; matchp;) {
1694 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001695 if (matchp->match->m)
1696 free(matchp->match->m);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001697 free(matchp);
1698 matchp = tmp;
1699 }
1700
1701 *matches = NULL;
1702}
1703
Rusty Russell5eed48a2000-06-02 20:12:24 +00001704int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1705{
1706 struct ip6t_entry fw, *e = NULL;
1707 int invert = 0;
1708 unsigned int nsaddrs = 0, ndaddrs = 0;
1709 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1710
1711 int c, verbose = 0;
1712 const char *chain = NULL;
1713 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1714 const char *policy = NULL, *newname = NULL;
1715 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001716 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001717 int ret = 1;
1718 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001719 struct ip6tables_rule_match *matches = NULL;
1720 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001721 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001722 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001723 const char *jumpto = "";
1724 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001725 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001726 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001727 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001728
1729 memset(&fw, 0, sizeof(fw));
1730
Harald Welte43ac1912002-03-03 09:43:07 +00001731 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001732 * a second time */
1733 optind = 0;
1734
Harald Welte43ac1912002-03-03 09:43:07 +00001735 /* clear mflags in case do_command gets called a second time
1736 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001737 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001738 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001739
Harald Welte43ac1912002-03-03 09:43:07 +00001740 for (t = ip6tables_targets; t; t = t->next) {
1741 t->tflags = 0;
1742 t->used = 0;
1743 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001744
Rusty Russell5eed48a2000-06-02 20:12:24 +00001745 /* Suppress error messages: we may add new options if we
1746 demand-load a protocol. */
1747 opterr = 0;
1748
1749 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001750 "-A:D:R:I:L::M:F::Z::N:X::E:P:Vh::o:p:s:d:j:i:bvnt:m:xc:",
Rusty Russell5eed48a2000-06-02 20:12:24 +00001751 opts, NULL)) != -1) {
1752 switch (c) {
1753 /*
1754 * Command selection
1755 */
1756 case 'A':
1757 add_command(&command, CMD_APPEND, CMD_NONE,
1758 invert);
1759 chain = optarg;
1760 break;
1761
1762 case 'D':
1763 add_command(&command, CMD_DELETE, CMD_NONE,
1764 invert);
1765 chain = optarg;
1766 if (optind < argc && argv[optind][0] != '-'
1767 && argv[optind][0] != '!') {
1768 rulenum = parse_rulenumber(argv[optind++]);
1769 command = CMD_DELETE_NUM;
1770 }
1771 break;
1772
Rusty Russell5eed48a2000-06-02 20:12:24 +00001773 case 'R':
1774 add_command(&command, CMD_REPLACE, CMD_NONE,
1775 invert);
1776 chain = optarg;
1777 if (optind < argc && argv[optind][0] != '-'
1778 && argv[optind][0] != '!')
1779 rulenum = parse_rulenumber(argv[optind++]);
1780 else
1781 exit_error(PARAMETER_PROBLEM,
1782 "-%c requires a rule number",
1783 cmd2char(CMD_REPLACE));
1784 break;
1785
1786 case 'I':
1787 add_command(&command, CMD_INSERT, CMD_NONE,
1788 invert);
1789 chain = optarg;
1790 if (optind < argc && argv[optind][0] != '-'
1791 && argv[optind][0] != '!')
1792 rulenum = parse_rulenumber(argv[optind++]);
1793 else rulenum = 1;
1794 break;
1795
1796 case 'L':
1797 add_command(&command, CMD_LIST, CMD_ZERO,
1798 invert);
1799 if (optarg) chain = optarg;
1800 else if (optind < argc && argv[optind][0] != '-'
1801 && argv[optind][0] != '!')
1802 chain = argv[optind++];
1803 break;
1804
1805 case 'F':
1806 add_command(&command, CMD_FLUSH, CMD_NONE,
1807 invert);
1808 if (optarg) chain = optarg;
1809 else if (optind < argc && argv[optind][0] != '-'
1810 && argv[optind][0] != '!')
1811 chain = argv[optind++];
1812 break;
1813
1814 case 'Z':
1815 add_command(&command, CMD_ZERO, CMD_LIST,
1816 invert);
1817 if (optarg) chain = optarg;
1818 else if (optind < argc && argv[optind][0] != '-'
1819 && argv[optind][0] != '!')
1820 chain = argv[optind++];
1821 break;
1822
1823 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001824 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001825 exit_error(PARAMETER_PROBLEM,
1826 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001827 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001828 if (find_target(optarg, TRY_LOAD))
1829 exit_error(PARAMETER_PROBLEM,
1830 "chain name may not clash "
1831 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1833 invert);
1834 chain = optarg;
1835 break;
1836
1837 case 'X':
1838 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1839 invert);
1840 if (optarg) chain = optarg;
1841 else if (optind < argc && argv[optind][0] != '-'
1842 && argv[optind][0] != '!')
1843 chain = argv[optind++];
1844 break;
1845
1846 case 'E':
1847 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1848 invert);
1849 chain = optarg;
1850 if (optind < argc && argv[optind][0] != '-'
1851 && argv[optind][0] != '!')
1852 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001853 else
1854 exit_error(PARAMETER_PROBLEM,
1855 "-%c requires old-chain-name and "
1856 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001857 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001858 break;
1859
1860 case 'P':
1861 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1862 invert);
1863 chain = optarg;
1864 if (optind < argc && argv[optind][0] != '-'
1865 && argv[optind][0] != '!')
1866 policy = argv[optind++];
1867 else
1868 exit_error(PARAMETER_PROBLEM,
1869 "-%c requires a chain and a policy",
1870 cmd2char(CMD_SET_POLICY));
1871 break;
1872
1873 case 'h':
1874 if (!optarg)
1875 optarg = argv[optind];
1876
Jonas Berlin1b91e592005-04-01 06:58:38 +00001877 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001878 if (!matches && protocol)
1879 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001880
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001881 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001882
1883 /*
1884 * Option selection
1885 */
1886 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001887 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001888 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1889 invert);
1890
1891 /* Canonicalize into lower case */
1892 for (protocol = argv[optind-1]; *protocol; protocol++)
1893 *protocol = tolower(*protocol);
1894
1895 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001896 if ( strcmp(protocol,"ipv6-icmp") == 0)
1897 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001898 fw.ipv6.proto = parse_protocol(protocol);
1899 fw.ipv6.flags |= IP6T_F_PROTO;
1900
1901 if (fw.ipv6.proto == 0
1902 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1903 exit_error(PARAMETER_PROBLEM,
1904 "rule would never match protocol");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001905 break;
1906
1907 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001908 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001909 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1910 invert);
1911 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001912 break;
1913
1914 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001915 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001916 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1917 invert);
1918 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001919 break;
1920
1921 case 'j':
1922 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1923 invert);
1924 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001925 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001926 target = find_target(jumpto, TRY_LOAD);
1927
1928 if (target) {
1929 size_t size;
1930
Harald Welte43ac1912002-03-03 09:43:07 +00001931 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1932 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001933
1934 target->t = fw_calloc(1, size);
1935 target->t->u.target_size = size;
1936 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001937 if (target->init != NULL)
1938 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001939 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001940 }
1941 break;
1942
1943
1944 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001945 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001946 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1947 invert);
1948 parse_interface(argv[optind-1],
1949 fw.ipv6.iniface,
1950 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001951 break;
1952
1953 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001954 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001955 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1956 invert);
1957 parse_interface(argv[optind-1],
1958 fw.ipv6.outiface,
1959 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001960 break;
1961
1962 case 'v':
1963 if (!verbose)
1964 set_option(&options, OPT_VERBOSE,
1965 &fw.ipv6.invflags, invert);
1966 verbose++;
1967 break;
1968
1969 case 'm': {
1970 size_t size;
1971
1972 if (invert)
1973 exit_error(PARAMETER_PROBLEM,
1974 "unexpected ! flag before --match");
1975
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001976 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001977 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1978 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001979 m->m = fw_calloc(1, size);
1980 m->m->u.match_size = size;
1981 strcpy(m->m->u.user.name, m->name);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001982 if (m->init != NULL)
1983 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001984 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001985 }
1986 break;
1987
1988 case 'n':
1989 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1990 invert);
1991 break;
1992
1993 case 't':
1994 if (invert)
1995 exit_error(PARAMETER_PROBLEM,
1996 "unexpected ! flag before --table");
1997 *table = argv[optind-1];
1998 break;
1999
2000 case 'x':
2001 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2002 invert);
2003 break;
2004
2005 case 'V':
2006 if (invert)
2007 printf("Not %s ;-)\n", program_version);
2008 else
2009 printf("%s v%s\n",
2010 program_name, program_version);
2011 exit(0);
2012
2013 case '0':
2014 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2015 invert);
2016 break;
2017
Harald Welte43ac1912002-03-03 09:43:07 +00002018 case 'M':
2019 modprobe = optarg;
2020 break;
2021
2022 case 'c':
2023
2024 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2025 invert);
2026 pcnt = optarg;
2027 if (optind < argc && argv[optind][0] != '-'
2028 && argv[optind][0] != '!')
2029 bcnt = argv[optind++];
2030 else
2031 exit_error(PARAMETER_PROBLEM,
2032 "-%c requires packet and byte counter",
2033 opt2char(OPT_COUNTERS));
2034
Martin Josefssona28d4952004-05-26 16:04:48 +00002035 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002036 exit_error(PARAMETER_PROBLEM,
2037 "-%c packet counter not numeric",
2038 opt2char(OPT_COUNTERS));
2039
Martin Josefssona28d4952004-05-26 16:04:48 +00002040 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002041 exit_error(PARAMETER_PROBLEM,
2042 "-%c byte counter not numeric",
2043 opt2char(OPT_COUNTERS));
2044
2045 break;
2046
2047
Rusty Russell5eed48a2000-06-02 20:12:24 +00002048 case 1: /* non option */
2049 if (optarg[0] == '!' && optarg[1] == '\0') {
2050 if (invert)
2051 exit_error(PARAMETER_PROBLEM,
2052 "multiple consecutive ! not"
2053 " allowed");
2054 invert = TRUE;
2055 optarg[0] = '\0';
2056 continue;
2057 }
2058 printf("Bad argument `%s'\n", optarg);
2059 exit_tryhelp(2);
2060
2061 default:
2062 /* FIXME: This scheme doesn't allow two of the same
2063 matches --RR */
2064 if (!target
2065 || !(target->parse(c - target->option_offset,
2066 argv, invert,
2067 &target->tflags,
2068 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002069 for (matchp = matches; matchp; matchp = matchp->next) {
2070 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002071 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002072 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073 &fw,
2074 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002075 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002076 break;
2077 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002078 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002079
2080 /* If you listen carefully, you can
2081 actually hear this code suck. */
2082
2083 /* some explanations (after four different bugs
2084 * in 3 different releases): If we encountere a
2085 * parameter, that has not been parsed yet,
2086 * it's not an option of an explicitly loaded
2087 * match or a target. However, we support
2088 * implicit loading of the protocol match
2089 * extension. '-p tcp' means 'l4 proto 6' and
2090 * at the same time 'load tcp protocol match on
2091 * demand if we specify --dport'.
2092 *
2093 * To make this work, we need to make sure:
2094 * - the parameter has not been parsed by
2095 * a match (m above)
2096 * - a protocol has been specified
2097 * - the protocol extension has not been
2098 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002099 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002100 * - the protocol extension can be successively
2101 * loaded
2102 */
2103 if (m == NULL
2104 && protocol
2105 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002106 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002107 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002108 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002109 && (proto_used == 0))
2110 )
2111 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002112 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002113 /* Try loading protocol */
2114 size_t size;
2115
2116 proto_used = 1;
2117
2118 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2119 + m->size;
2120
2121 m->m = fw_calloc(1, size);
2122 m->m->u.match_size = size;
2123 strcpy(m->m->u.user.name, m->name);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002124 if (m->init != NULL)
2125 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002126
2127 opts = merge_options(opts,
2128 m->extra_opts, &m->option_offset);
2129
2130 optind--;
2131 continue;
2132 }
2133
Rusty Russell5eed48a2000-06-02 20:12:24 +00002134 if (!m)
2135 exit_error(PARAMETER_PROBLEM,
2136 "Unknown arg `%s'",
2137 argv[optind-1]);
2138 }
2139 }
2140 invert = FALSE;
2141 }
2142
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002143 for (matchp = matches; matchp; matchp = matchp->next)
2144 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002145
Harald Welte43ac1912002-03-03 09:43:07 +00002146 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002147 target->final_check(target->tflags);
2148
2149 /* Fix me: must put inverse options checking here --MN */
2150
2151 if (optind < argc)
2152 exit_error(PARAMETER_PROBLEM,
2153 "unknown arguments found on commandline");
2154 if (!command)
2155 exit_error(PARAMETER_PROBLEM, "no command specified");
2156 if (invert)
2157 exit_error(PARAMETER_PROBLEM,
2158 "nothing appropriate following !");
2159
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002160 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002161 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002162 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002163 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002164 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002165 }
2166
2167 if (shostnetworkmask)
2168 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2169 &(fw.ipv6.smsk), &nsaddrs);
2170
2171 if (dhostnetworkmask)
2172 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2173 &(fw.ipv6.dmsk), &ndaddrs);
2174
2175 if ((nsaddrs > 1 || ndaddrs > 1) &&
2176 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2177 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2178 " source or destination IP addresses");
2179
Rusty Russell5eed48a2000-06-02 20:12:24 +00002180 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2181 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2182 "specify a unique address");
2183
2184 generic_opt_check(command, options);
2185
2186 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2187 exit_error(PARAMETER_PROBLEM,
2188 "chain name `%s' too long (must be under %i chars)",
2189 chain, IP6T_FUNCTION_MAXNAMELEN);
2190
Harald Welte43ac1912002-03-03 09:43:07 +00002191 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002192 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002193 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002194
Rusty Russell8beb0492004-12-22 00:37:10 +00002195 /* try to insmod the module if iptc_init failed */
2196 if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002197 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002198
Harald Welte43ac1912002-03-03 09:43:07 +00002199 if (!*handle)
2200 exit_error(VERSION_PROBLEM,
2201 "can't initialize ip6tables table `%s': %s",
2202 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002203
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002204 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002205 || command == CMD_DELETE
2206 || command == CMD_INSERT
2207 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002208 if (strcmp(chain, "PREROUTING") == 0
2209 || strcmp(chain, "INPUT") == 0) {
2210 /* -o not valid with incoming packets. */
2211 if (options & OPT_VIANAMEOUT)
2212 exit_error(PARAMETER_PROBLEM,
2213 "Can't use -%c with %s\n",
2214 opt2char(OPT_VIANAMEOUT),
2215 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002216 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002217
Harald Welte43ac1912002-03-03 09:43:07 +00002218 if (strcmp(chain, "POSTROUTING") == 0
2219 || strcmp(chain, "OUTPUT") == 0) {
2220 /* -i not valid with outgoing packets */
2221 if (options & OPT_VIANAMEIN)
2222 exit_error(PARAMETER_PROBLEM,
2223 "Can't use -%c with %s\n",
2224 opt2char(OPT_VIANAMEIN),
2225 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002226 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002227
2228 if (target && ip6tc_is_chain(jumpto, *handle)) {
2229 printf("Warning: using chain %s, not extension\n",
2230 jumpto);
2231
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002232 if (target->t)
2233 free(target->t);
2234
Rusty Russell5eed48a2000-06-02 20:12:24 +00002235 target = NULL;
2236 }
2237
2238 /* If they didn't specify a target, or it's a chain
2239 name, use standard. */
2240 if (!target
2241 && (strlen(jumpto) == 0
2242 || ip6tc_is_chain(jumpto, *handle))) {
2243 size_t size;
2244
2245 target = find_target(IP6T_STANDARD_TARGET,
2246 LOAD_MUST_SUCCEED);
2247
2248 size = sizeof(struct ip6t_entry_target)
2249 + target->size;
2250 target->t = fw_calloc(1, size);
2251 target->t->u.target_size = size;
2252 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002253 if (target->init != NULL)
2254 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002255 }
2256
2257 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002258 /* it is no chain, and we can't load a plugin.
2259 * We cannot know if the plugin is corrupt, non
2260 * existant OR if the user just misspelled a
2261 * chain. */
2262 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002263 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002264 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002265 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002266 }
2267 }
2268
2269 switch (command) {
2270 case CMD_APPEND:
2271 ret = append_entry(chain, e,
2272 nsaddrs, saddrs, ndaddrs, daddrs,
2273 options&OPT_VERBOSE,
2274 handle);
2275 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002276 case CMD_DELETE:
2277 ret = delete_entry(chain, e,
2278 nsaddrs, saddrs, ndaddrs, daddrs,
2279 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002280 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002281 break;
2282 case CMD_DELETE_NUM:
2283 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2284 break;
2285 case CMD_REPLACE:
2286 ret = replace_entry(chain, e, rulenum - 1,
2287 saddrs, daddrs, options&OPT_VERBOSE,
2288 handle);
2289 break;
2290 case CMD_INSERT:
2291 ret = insert_entry(chain, e, rulenum - 1,
2292 nsaddrs, saddrs, ndaddrs, daddrs,
2293 options&OPT_VERBOSE,
2294 handle);
2295 break;
2296 case CMD_LIST:
2297 ret = list_entries(chain,
2298 options&OPT_VERBOSE,
2299 options&OPT_NUMERIC,
2300 options&OPT_EXPANDED,
2301 options&OPT_LINENUMBERS,
2302 handle);
2303 break;
2304 case CMD_FLUSH:
2305 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2306 break;
2307 case CMD_ZERO:
2308 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2309 break;
2310 case CMD_LIST|CMD_ZERO:
2311 ret = list_entries(chain,
2312 options&OPT_VERBOSE,
2313 options&OPT_NUMERIC,
2314 options&OPT_EXPANDED,
2315 options&OPT_LINENUMBERS,
2316 handle);
2317 if (ret)
2318 ret = zero_entries(chain,
2319 options&OPT_VERBOSE, handle);
2320 break;
2321 case CMD_NEW_CHAIN:
2322 ret = ip6tc_create_chain(chain, handle);
2323 break;
2324 case CMD_DELETE_CHAIN:
2325 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2326 break;
2327 case CMD_RENAME_CHAIN:
2328 ret = ip6tc_rename_chain(chain, newname, handle);
2329 break;
2330 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002331 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002332 break;
2333 default:
2334 /* We should never reach this... */
2335 exit_tryhelp(2);
2336 }
2337
2338 if (verbose > 1)
2339 dump_entries6(*handle);
2340
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002341 clear_rule_matches(&matches);
2342
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002343 if (e != NULL) {
2344 free(e);
2345 e = NULL;
2346 }
2347
2348 for (c = 0; c < nsaddrs; c++)
2349 free(&saddrs[c]);
2350
2351 for (c = 0; c < ndaddrs; c++)
2352 free(&daddrs[c]);
2353
Pablo Neiradfdcd642005-05-29 19:05:23 +00002354 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002355
Rusty Russell5eed48a2000-06-02 20:12:24 +00002356 return ret;
2357}