blob: ced26a558c5412effdefe363fe0e6409e5c42374 [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 *
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
53#ifndef IP6T_LIB_DIR
54#define IP6T_LIB_DIR "/usr/local/lib/iptables"
55#endif
56
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +000057#ifndef PROC_SYS_MODPROBE
58#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
59#endif
60
Rusty Russell5eed48a2000-06-02 20:12:24 +000061#define FMT_NUMERIC 0x0001
62#define FMT_NOCOUNTS 0x0002
63#define FMT_KILOMEGAGIGA 0x0004
64#define FMT_OPTIONS 0x0008
65#define FMT_NOTABLE 0x0010
66#define FMT_NOTARGET 0x0020
67#define FMT_VIA 0x0040
68#define FMT_NONEWLINE 0x0080
69#define FMT_LINENUMBERS 0x0100
70
71#define FMT_PRINT_RULE (FMT_NOCOUNTS | FMT_OPTIONS | FMT_VIA \
72 | FMT_NUMERIC | FMT_NOTABLE)
73#define FMT(tab,notab) ((format) & FMT_NOTABLE ? (notab) : (tab))
74
75
76#define CMD_NONE 0x0000U
77#define CMD_INSERT 0x0001U
78#define CMD_DELETE 0x0002U
79#define CMD_DELETE_NUM 0x0004U
80#define CMD_REPLACE 0x0008U
81#define CMD_APPEND 0x0010U
82#define CMD_LIST 0x0020U
83#define CMD_FLUSH 0x0040U
84#define CMD_ZERO 0x0080U
85#define CMD_NEW_CHAIN 0x0100U
86#define CMD_DELETE_CHAIN 0x0200U
87#define CMD_SET_POLICY 0x0400U
88#define CMD_CHECK 0x0800U
89#define CMD_RENAME_CHAIN 0x1000U
90#define NUMBER_OF_CMD 13
91static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000092 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000093
94#define OPTION_OFFSET 256
95
96#define OPT_NONE 0x00000U
97#define OPT_NUMERIC 0x00001U
98#define OPT_SOURCE 0x00002U
99#define OPT_DESTINATION 0x00004U
100#define OPT_PROTOCOL 0x00008U
101#define OPT_JUMP 0x00010U
102#define OPT_VERBOSE 0x00020U
103#define OPT_EXPANDED 0x00040U
104#define OPT_VIANAMEIN 0x00080U
105#define OPT_VIANAMEOUT 0x00100U
106#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000107#define OPT_COUNTERS 0x00400U
108#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000109static const char optflags[NUMBER_OF_OPT]
Harald Welte43ac1912002-03-03 09:43:07 +0000110= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '3', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000111
112static struct option original_opts[] = {
113 { "append", 1, 0, 'A' },
114 { "delete", 1, 0, 'D' },
115 { "insert", 1, 0, 'I' },
116 { "replace", 1, 0, 'R' },
117 { "list", 2, 0, 'L' },
118 { "flush", 2, 0, 'F' },
119 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000120 { "new-chain", 1, 0, 'N' },
121 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000122 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000123 { "policy", 1, 0, 'P' },
124 { "source", 1, 0, 's' },
125 { "destination", 1, 0, 'd' },
126 { "src", 1, 0, 's' }, /* synonym */
127 { "dst", 1, 0, 'd' }, /* synonym */
128 { "protocol", 1, 0, 'p' },
129 { "in-interface", 1, 0, 'i' },
130 { "jump", 1, 0, 'j' },
131 { "table", 1, 0, 't' },
132 { "match", 1, 0, 'm' },
133 { "numeric", 0, 0, 'n' },
134 { "out-interface", 1, 0, 'o' },
135 { "verbose", 0, 0, 'v' },
136 { "exact", 0, 0, 'x' },
137 { "version", 0, 0, 'V' },
138 { "help", 2, 0, 'h' },
139 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000140 { "modprobe", 1, 0, 'M' },
141 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000142 { 0 }
143};
144
Harald Weltea8658ca2003-03-05 07:46:15 +0000145/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
146 * current line of the input file, in order to give a more precise error
147 * message. ip6tables itself doesn't need this, so it is initialized to the
148 * magic number of -1 */
149int line = -1;
150
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000151#ifndef __OPTIMIZE__
152struct ip6t_entry_target *
Harald Weltef7eca1c2001-05-28 19:48:14 +0000153ip6t_get_target(struct ip6t_entry *e)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000154{
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000155 return (void *)e + e->target_offset;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000156}
157#endif
158
Rusty Russell5eed48a2000-06-02 20:12:24 +0000159static struct option *opts = original_opts;
160static unsigned int global_option_offset = 0;
161
162/* Table of legal combinations of commands and options. If any of the
163 * given commands make an option legal, that option is legal (applies to
164 * CMD_LIST and CMD_ZERO only).
165 * Key:
166 * + compulsory
167 * x illegal
168 * optional
169 */
170
171static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
172/* Well, it's better than "Re: Linux vs FreeBSD" */
173{
174 /* -n -s -d -p -j -v -x -i -o --line */
175/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
176/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
177/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x'},
178/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
179/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x'},
180/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' '},
181/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x'},
182/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x'},
183/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
184/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x'},
185/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x'},
Harald Welte43ac1912002-03-03 09:43:07 +0000186/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x'},
Rusty Russell5eed48a2000-06-02 20:12:24 +0000187/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x'}
188};
189
190static int inverse_for_options[NUMBER_OF_OPT] =
191{
192/* -n */ 0,
193/* -s */ IP6T_INV_SRCIP,
194/* -d */ IP6T_INV_DSTIP,
195/* -p */ IP6T_INV_PROTO,
196/* -j */ 0,
197/* -v */ 0,
198/* -x */ 0,
199/* -i */ IP6T_INV_VIA_IN,
200/* -o */ IP6T_INV_VIA_OUT,
201/*--line*/ 0
202};
203
204const char *program_version;
205const char *program_name;
206
Rusty Russell208d42e2004-12-20 05:29:52 +0000207static char *lib_dir;
208
Rusty Russell5eed48a2000-06-02 20:12:24 +0000209/* Keeping track of external matches and targets: linked lists. */
210struct ip6tables_match *ip6tables_matches = NULL;
211struct ip6tables_target *ip6tables_targets = NULL;
212
213/* Extra debugging from libiptc */
214extern void dump_entries6(const ip6tc_handle_t handle);
215
216/* A few hardcoded protocols for 'all' and in case the user has no
217 /etc/protocols */
218struct pprot {
219 char *name;
220 u_int8_t num;
221};
222
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000223/* Primitive headers... */
224/* defined in netinet/in.h */
225#if 0
226#ifndef IPPROTO_ESP
227#define IPPROTO_ESP 50
228#endif
229#ifndef IPPROTO_AH
230#define IPPROTO_AH 51
231#endif
232#endif
233
Rusty Russell5eed48a2000-06-02 20:12:24 +0000234static const struct pprot chain_protos[] = {
235 { "tcp", IPPROTO_TCP },
236 { "udp", IPPROTO_UDP },
Harald Weltede8e5fa2000-11-13 14:34:34 +0000237 { "icmpv6", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000238 { "esp", IPPROTO_ESP },
239 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000240 { "all", 0 },
241};
242
243static char *
244proto_to_name(u_int8_t proto, int nolookup)
245{
246 unsigned int i;
247
248 if (proto && !nolookup) {
249 struct protoent *pent = getprotobynumber(proto);
250 if (pent)
251 return pent->p_name;
252 }
253
254 for (i = 0; i < sizeof(chain_protos)/sizeof(struct pprot); i++)
255 if (chain_protos[i].num == proto)
256 return chain_protos[i].name;
257
258 return NULL;
259}
260
261static void
262in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
263{
264 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000265 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000266}
267
268void
269exit_error(enum exittype status, char *msg, ...)
270{
271 va_list args;
272
273 va_start(args, msg);
274 fprintf(stderr, "%s v%s: ", program_name, program_version);
275 vfprintf(stderr, msg, args);
276 va_end(args);
277 fprintf(stderr, "\n");
278 if (status == PARAMETER_PROBLEM)
279 exit_tryhelp(status);
280 if (status == VERSION_PROBLEM)
281 fprintf(stderr,
282 "Perhaps iptables or your kernel needs to be upgraded.\n");
283 exit(status);
284}
285
286void
287exit_tryhelp(int status)
288{
Harald Weltea8658ca2003-03-05 07:46:15 +0000289 if (line != -1)
290 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000291 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
292 program_name, program_name );
293 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
832static void
833parse_interface(const char *arg, char *vianame, unsigned char *mask)
834{
835 int vialen = strlen(arg);
836 unsigned int i;
837
838 memset(mask, 0, IFNAMSIZ);
839 memset(vianame, 0, IFNAMSIZ);
840
841 if (vialen + 1 > IFNAMSIZ)
842 exit_error(PARAMETER_PROBLEM,
843 "interface name `%s' must be shorter than IFNAMSIZ"
844 " (%i)", arg, IFNAMSIZ-1);
845
846 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000847 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000848 memset(mask, 0, IFNAMSIZ);
849 else if (vianame[vialen - 1] == '+') {
850 memset(mask, 0xFF, vialen - 1);
851 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000852 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000853 } else {
854 /* Include nul-terminator in match */
855 memset(mask, 0xFF, vialen + 1);
856 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000857 for (i = 0; vianame[i]; i++) {
858 if (!isalnum(vianame[i])
859 && vianame[i] != '_'
860 && vianame[i] != '.') {
861 printf("Warning: wierd character in interface"
862 " `%s' (No aliases, :, ! or *).\n",
863 vianame);
864 break;
865 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000866 }
867 }
868}
869
870/* Can't be zero. */
871static int
872parse_rulenumber(const char *rule)
873{
Harald Welte43ac1912002-03-03 09:43:07 +0000874 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000875
Harald Welte43ac1912002-03-03 09:43:07 +0000876 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000877 exit_error(PARAMETER_PROBLEM,
878 "Invalid rule number `%s'", rule);
879
880 return rulenum;
881}
882
883static const char *
884parse_target(const char *targetname)
885{
886 const char *ptr;
887
888 if (strlen(targetname) < 1)
889 exit_error(PARAMETER_PROBLEM,
890 "Invalid target name (too short)");
891
892 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
893 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000894 "Invalid target name `%s' (%u chars max)",
895 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000896
897 for (ptr = targetname; *ptr; ptr++)
898 if (isspace(*ptr))
899 exit_error(PARAMETER_PROBLEM,
900 "Invalid target name `%s'", targetname);
901 return targetname;
902}
903
904int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000905string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
906 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000907{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000908 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000909 char *end;
910
911 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000912 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000913 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000914 if (*end == '\0' && end != s) {
915 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000916 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000917 *ret = number;
918 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000919 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000920 }
921 return -1;
922}
923
Martin Josefssonb105bc92004-05-26 15:54:49 +0000924int
925string_to_number_l(const char *s, unsigned long min, unsigned long max,
926 unsigned long *ret)
927{
928 int result;
929 unsigned long long number;
930
931 result = string_to_number_ll(s, min, max, &number);
932 *ret = (unsigned long)number;
933
934 return result;
935}
936
937int string_to_number(const char *s, unsigned int min, unsigned int max,
938 unsigned int *ret)
939{
940 int result;
941 unsigned long number;
942
943 result = string_to_number_l(s, min, max, &number);
944 *ret = (unsigned int)number;
945
946 return result;
947}
948
Rusty Russell5eed48a2000-06-02 20:12:24 +0000949static void
950set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
951 int invert)
952{
953 if (*options & option)
954 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
955 opt2char(option));
956 *options |= option;
957
958 if (invert) {
959 unsigned int i;
960 for (i = 0; 1 << i != option; i++);
961
962 if (!inverse_for_options[i])
963 exit_error(PARAMETER_PROBLEM,
964 "cannot have ! before -%c",
965 opt2char(option));
966 *invflg |= inverse_for_options[i];
967 }
968}
969
970struct ip6tables_target *
971find_target(const char *name, enum ip6t_tryload tryload)
972{
973 struct ip6tables_target *ptr;
974
975 /* Standard target? */
976 if (strcmp(name, "") == 0
977 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
978 || strcmp(name, IP6TC_LABEL_DROP) == 0
979 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
980 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
981 name = "standard";
982
983 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
984 if (strcmp(name, ptr->name) == 0)
985 break;
986 }
987
Harald Welte3efb6ea2001-08-06 18:50:21 +0000988#ifndef NO_SHARED_LIBS
Rusty Russell5eed48a2000-06-02 20:12:24 +0000989 if (!ptr && tryload != DONT_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000990 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000991 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +0000992 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000993 if (dlopen(path, RTLD_NOW)) {
994 /* Found library. If it didn't register itself,
995 maybe they specified match as a target. */
996 ptr = find_target(name, DONT_LOAD);
997 if (!ptr)
998 exit_error(PARAMETER_PROBLEM,
999 "Couldn't load target `%s'\n",
1000 name);
1001 } else if (tryload == LOAD_MUST_SUCCEED)
1002 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001003 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001004 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001005 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001006#else
1007 if (ptr && !ptr->loaded) {
1008 if (tryload != DONT_LOAD)
1009 ptr->loaded = 1;
1010 else
1011 ptr = NULL;
1012 }
Marc Boucher067477b2002-03-24 15:09:31 +00001013 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1014 exit_error(PARAMETER_PROBLEM,
1015 "Couldn't find target `%s'\n", name);
1016 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001017#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001018
Harald Welte43ac1912002-03-03 09:43:07 +00001019 if (ptr)
1020 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001021
Rusty Russell5eed48a2000-06-02 20:12:24 +00001022 return ptr;
1023}
1024
1025static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001026merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001027 unsigned int *option_offset)
1028{
1029 unsigned int num_old, num_new, i;
1030 struct option *merge;
1031
1032 for (num_old = 0; oldopts[num_old].name; num_old++);
1033 for (num_new = 0; newopts[num_new].name; num_new++);
1034
1035 global_option_offset += OPTION_OFFSET;
1036 *option_offset = global_option_offset;
1037
1038 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1039 memcpy(merge, oldopts, num_old * sizeof(struct option));
1040 for (i = 0; i < num_new; i++) {
1041 merge[num_old + i] = newopts[i];
1042 merge[num_old + i].val += *option_offset;
1043 }
1044 memset(merge + num_old + num_new, 0, sizeof(struct option));
1045
1046 return merge;
1047}
1048
1049void
1050register_match6(struct ip6tables_match *me)
1051{
Harald Welte43ac1912002-03-03 09:43:07 +00001052 struct ip6tables_match **i;
1053
Rusty Russell5eed48a2000-06-02 20:12:24 +00001054 if (strcmp(me->version, program_version) != 0) {
1055 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1056 program_name, me->name, me->version, program_version);
1057 exit(1);
1058 }
1059
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001060 if (find_match(me->name, DONT_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001061 fprintf(stderr, "%s: match `%s' already registered.\n",
1062 program_name, me->name);
1063 exit(1);
1064 }
1065
Harald Welte43ac1912002-03-03 09:43:07 +00001066 if (me->size != IP6T_ALIGN(me->size)) {
1067 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001068 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001069 exit(1);
1070 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001071
Harald Welte43ac1912002-03-03 09:43:07 +00001072 /* Append to list. */
1073 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1074 me->next = NULL;
1075 *i = me;
1076
Rusty Russell5eed48a2000-06-02 20:12:24 +00001077 me->m = NULL;
1078 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001079}
1080
1081void
1082register_target6(struct ip6tables_target *me)
1083{
1084 if (strcmp(me->version, program_version) != 0) {
1085 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1086 program_name, me->name, me->version, program_version);
1087 exit(1);
1088 }
1089
1090 if (find_target(me->name, DONT_LOAD)) {
1091 fprintf(stderr, "%s: target `%s' already registered.\n",
1092 program_name, me->name);
1093 exit(1);
1094 }
1095
Harald Welte43ac1912002-03-03 09:43:07 +00001096 if (me->size != IP6T_ALIGN(me->size)) {
1097 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001098 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001099 exit(1);
1100 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001101
Rusty Russell5eed48a2000-06-02 20:12:24 +00001102 /* Prepend to list. */
1103 me->next = ip6tables_targets;
1104 ip6tables_targets = me;
1105 me->t = NULL;
1106 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001107}
1108
1109static void
1110print_num(u_int64_t number, unsigned int format)
1111{
Harald Welte43ac1912002-03-03 09:43:07 +00001112 if (format & FMT_KILOMEGAGIGA) {
1113 if (number > 99999) {
1114 number = (number + 500) / 1000;
1115 if (number > 9999) {
1116 number = (number + 500) / 1000;
1117 if (number > 9999) {
1118 number = (number + 500) / 1000;
1119 if (number > 9999) {
1120 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001121 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001122 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001123 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001124 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001125 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001126 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001127 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001128 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001129 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001130 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001131 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001132}
1133
Harald Welte43ac1912002-03-03 09:43:07 +00001134
Rusty Russell5eed48a2000-06-02 20:12:24 +00001135static void
1136print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1137{
1138 struct ip6t_counters counters;
1139 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1140 printf("Chain %s", chain);
1141 if (pol) {
1142 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001143 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001144 fputc(' ', stdout);
1145 print_num(counters.pcnt, (format|FMT_NOTABLE));
1146 fputs("packets, ", stdout);
1147 print_num(counters.bcnt, (format|FMT_NOTABLE));
1148 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001149 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001150 printf(")\n");
1151 } else {
1152 unsigned int refs;
1153 if (!ip6tc_get_references(&refs, chain, handle))
1154 printf(" (ERROR obtaining refs)\n");
1155 else
1156 printf(" (%u references)\n", refs);
1157 }
1158
1159 if (format & FMT_LINENUMBERS)
1160 printf(FMT("%-4s ", "%s "), "num");
1161 if (!(format & FMT_NOCOUNTS)) {
1162 if (format & FMT_KILOMEGAGIGA) {
1163 printf(FMT("%5s ","%s "), "pkts");
1164 printf(FMT("%5s ","%s "), "bytes");
1165 } else {
1166 printf(FMT("%8s ","%s "), "pkts");
1167 printf(FMT("%10s ","%s "), "bytes");
1168 }
1169 }
1170 if (!(format & FMT_NOTARGET))
1171 printf(FMT("%-9s ","%s "), "target");
1172 fputs(" prot ", stdout);
1173 if (format & FMT_OPTIONS)
1174 fputs("opt", stdout);
1175 if (format & FMT_VIA) {
1176 printf(FMT(" %-6s ","%s "), "in");
1177 printf(FMT("%-6s ","%s "), "out");
1178 }
1179 printf(FMT(" %-19s ","%s "), "source");
1180 printf(FMT(" %-19s "," %s "), "destination");
1181 printf("\n");
1182}
1183
Harald Welte43ac1912002-03-03 09:43:07 +00001184
Rusty Russell5eed48a2000-06-02 20:12:24 +00001185static int
1186print_match(const struct ip6t_entry_match *m,
1187 const struct ip6t_ip6 *ip,
1188 int numeric)
1189{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001190 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001191
1192 if (match) {
1193 if (match->print)
1194 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001195 else
1196 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001197 } else {
1198 if (m->u.user.name[0])
1199 printf("UNKNOWN match `%s' ", m->u.user.name);
1200 }
1201 /* Don't stop iterating. */
1202 return 0;
1203}
1204
1205/* e is called `fw' here for hysterical raisins */
1206static void
1207print_firewall(const struct ip6t_entry *fw,
1208 const char *targname,
1209 unsigned int num,
1210 unsigned int format,
1211 const ip6tc_handle_t handle)
1212{
1213 struct ip6tables_target *target = NULL;
1214 const struct ip6t_entry_target *t;
1215 u_int8_t flags;
1216 char buf[BUFSIZ];
1217
Rusty Russell5eed48a2000-06-02 20:12:24 +00001218 if (!ip6tc_is_chain(targname, handle))
1219 target = find_target(targname, TRY_LOAD);
1220 else
1221 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1222
1223 t = ip6t_get_target((struct ip6t_entry *)fw);
1224 flags = fw->ipv6.flags;
1225
1226 if (format & FMT_LINENUMBERS)
1227 printf(FMT("%-4u ", "%u "), num+1);
1228
1229 if (!(format & FMT_NOCOUNTS)) {
1230 print_num(fw->counters.pcnt, format);
1231 print_num(fw->counters.bcnt, format);
1232 }
1233
1234 if (!(format & FMT_NOTARGET))
1235 printf(FMT("%-9s ", "%s "), targname);
1236
1237 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1238 {
Harald Welte43ac1912002-03-03 09:43:07 +00001239 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001240 if (pname)
1241 printf(FMT("%-5s", "%s "), pname);
1242 else
1243 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1244 }
1245
1246 if (format & FMT_OPTIONS) {
1247 if (format & FMT_NOTABLE)
1248 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001249 fputc(' ', stdout); /* Invert flag of FRAG */
1250 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001251 fputc(' ', stdout);
1252 }
1253
1254 if (format & FMT_VIA) {
1255 char iface[IFNAMSIZ+2];
1256
1257 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1258 iface[0] = '!';
1259 iface[1] = '\0';
1260 }
1261 else iface[0] = '\0';
1262
1263 if (fw->ipv6.iniface[0] != '\0') {
1264 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001265 }
1266 else if (format & FMT_NUMERIC) strcat(iface, "*");
1267 else strcat(iface, "any");
1268 printf(FMT(" %-6s ","in %s "), iface);
1269
1270 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1271 iface[0] = '!';
1272 iface[1] = '\0';
1273 }
1274 else iface[0] = '\0';
1275
1276 if (fw->ipv6.outiface[0] != '\0') {
1277 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001278 }
1279 else if (format & FMT_NUMERIC) strcat(iface, "*");
1280 else strcat(iface, "any");
1281 printf(FMT("%-6s ","out %s "), iface);
1282 }
1283
1284 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1285 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1286 && !(format & FMT_NUMERIC))
1287 printf(FMT("%-19s ","%s "), "anywhere");
1288 else {
1289 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001290 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001291 else
Harald Welte43ac1912002-03-03 09:43:07 +00001292 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001293 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1294 printf(FMT("%-19s ","%s "), buf);
1295 }
1296
1297 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1298 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1299 && !(format & FMT_NUMERIC))
1300 printf(FMT("%-19s","-> %s"), "anywhere");
1301 else {
1302 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001303 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001304 else
Harald Welte43ac1912002-03-03 09:43:07 +00001305 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001306 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1307 printf(FMT("%-19s","-> %s"), buf);
1308 }
1309
1310 if (format & FMT_NOTABLE)
1311 fputs(" ", stdout);
1312
1313 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1314
1315 if (target) {
1316 if (target->print)
1317 /* Print the target information. */
1318 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1319 } else if (t->u.target_size != sizeof(*t))
1320 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001321 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001322
1323 if (!(format & FMT_NONEWLINE))
1324 fputc('\n', stdout);
1325}
1326
1327static void
1328print_firewall_line(const struct ip6t_entry *fw,
1329 const ip6tc_handle_t h)
1330{
1331 struct ip6t_entry_target *t;
1332
1333 t = ip6t_get_target((struct ip6t_entry *)fw);
1334 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1335}
1336
1337static int
1338append_entry(const ip6t_chainlabel chain,
1339 struct ip6t_entry *fw,
1340 unsigned int nsaddrs,
1341 const struct in6_addr saddrs[],
1342 unsigned int ndaddrs,
1343 const struct in6_addr daddrs[],
1344 int verbose,
1345 ip6tc_handle_t *handle)
1346{
1347 unsigned int i, j;
1348 int ret = 1;
1349
1350 for (i = 0; i < nsaddrs; i++) {
1351 fw->ipv6.src = saddrs[i];
1352 for (j = 0; j < ndaddrs; j++) {
1353 fw->ipv6.dst = daddrs[j];
1354 if (verbose)
1355 print_firewall_line(fw, *handle);
1356 ret &= ip6tc_append_entry(chain, fw, handle);
1357 }
1358 }
1359
1360 return ret;
1361}
1362
1363static int
1364replace_entry(const ip6t_chainlabel chain,
1365 struct ip6t_entry *fw,
1366 unsigned int rulenum,
1367 const struct in6_addr *saddr,
1368 const struct in6_addr *daddr,
1369 int verbose,
1370 ip6tc_handle_t *handle)
1371{
1372 fw->ipv6.src = *saddr;
1373 fw->ipv6.dst = *daddr;
1374
1375 if (verbose)
1376 print_firewall_line(fw, *handle);
1377 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1378}
1379
1380static int
1381insert_entry(const ip6t_chainlabel chain,
1382 struct ip6t_entry *fw,
1383 unsigned int rulenum,
1384 unsigned int nsaddrs,
1385 const struct in6_addr saddrs[],
1386 unsigned int ndaddrs,
1387 const struct in6_addr daddrs[],
1388 int verbose,
1389 ip6tc_handle_t *handle)
1390{
1391 unsigned int i, j;
1392 int ret = 1;
1393
1394 for (i = 0; i < nsaddrs; i++) {
1395 fw->ipv6.src = saddrs[i];
1396 for (j = 0; j < ndaddrs; j++) {
1397 fw->ipv6.dst = daddrs[j];
1398 if (verbose)
1399 print_firewall_line(fw, *handle);
1400 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1401 }
1402 }
1403
1404 return ret;
1405}
1406
1407static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001408make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001409{
1410 /* Establish mask for comparison */
1411 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001412 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001413 unsigned char *mask, *mptr;
1414
1415 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001416 for (matchp = matches; matchp; matchp = matchp->next)
1417 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001418
1419 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001420 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001421 + ip6tables_targets->size);
1422
1423 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1424 mptr = mask + sizeof(struct ip6t_entry);
1425
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001426 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001427 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001428 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001429 + matchp->match->userspacesize);
1430 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001431 }
1432
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001433 memset(mptr, 0xFF,
1434 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1435 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001436
1437 return mask;
1438}
1439
1440static int
1441delete_entry(const ip6t_chainlabel chain,
1442 struct ip6t_entry *fw,
1443 unsigned int nsaddrs,
1444 const struct in6_addr saddrs[],
1445 unsigned int ndaddrs,
1446 const struct in6_addr daddrs[],
1447 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001448 ip6tc_handle_t *handle,
1449 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001450{
1451 unsigned int i, j;
1452 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001453 unsigned char *mask;
1454
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001455 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001456 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001457 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001458 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001459 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001460 if (verbose)
1461 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001462 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001463 }
1464 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001465 free(mask);
1466
Rusty Russell5eed48a2000-06-02 20:12:24 +00001467 return ret;
1468}
1469
András Kis-Szabó764316a2001-02-26 17:31:20 +00001470int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001471for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1472 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1473{
1474 int ret = 1;
1475 const char *chain;
1476 char *chains;
1477 unsigned int i, chaincount = 0;
1478
1479 chain = ip6tc_first_chain(handle);
1480 while (chain) {
1481 chaincount++;
1482 chain = ip6tc_next_chain(handle);
1483 }
1484
1485 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1486 i = 0;
1487 chain = ip6tc_first_chain(handle);
1488 while (chain) {
1489 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1490 i++;
1491 chain = ip6tc_next_chain(handle);
1492 }
1493
1494 for (i = 0; i < chaincount; i++) {
1495 if (!builtinstoo
1496 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001497 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001498 continue;
1499 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1500 }
1501
1502 free(chains);
1503 return ret;
1504}
1505
András Kis-Szabó764316a2001-02-26 17:31:20 +00001506int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001507flush_entries(const ip6t_chainlabel chain, int verbose,
1508 ip6tc_handle_t *handle)
1509{
1510 if (!chain)
1511 return for_each_chain(flush_entries, verbose, 1, handle);
1512
1513 if (verbose)
1514 fprintf(stdout, "Flushing chain `%s'\n", chain);
1515 return ip6tc_flush_entries(chain, handle);
1516}
1517
1518static int
1519zero_entries(const ip6t_chainlabel chain, int verbose,
1520 ip6tc_handle_t *handle)
1521{
1522 if (!chain)
1523 return for_each_chain(zero_entries, verbose, 1, handle);
1524
1525 if (verbose)
1526 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1527 return ip6tc_zero_entries(chain, handle);
1528}
1529
András Kis-Szabó764316a2001-02-26 17:31:20 +00001530int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001531delete_chain(const ip6t_chainlabel chain, int verbose,
1532 ip6tc_handle_t *handle)
1533{
1534 if (!chain)
1535 return for_each_chain(delete_chain, verbose, 0, handle);
1536
1537 if (verbose)
1538 fprintf(stdout, "Deleting chain `%s'\n", chain);
1539 return ip6tc_delete_chain(chain, handle);
1540}
1541
1542static int
1543list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1544 int expanded, int linenumbers, ip6tc_handle_t *handle)
1545{
1546 int found = 0;
1547 unsigned int format;
1548 const char *this;
1549
1550 format = FMT_OPTIONS;
1551 if (!verbose)
1552 format |= FMT_NOCOUNTS;
1553 else
1554 format |= FMT_VIA;
1555
1556 if (numeric)
1557 format |= FMT_NUMERIC;
1558
1559 if (!expanded)
1560 format |= FMT_KILOMEGAGIGA;
1561
1562 if (linenumbers)
1563 format |= FMT_LINENUMBERS;
1564
1565 for (this = ip6tc_first_chain(handle);
1566 this;
1567 this = ip6tc_next_chain(handle)) {
1568 const struct ip6t_entry *i;
1569 unsigned int num;
1570
1571 if (chain && strcmp(chain, this) != 0)
1572 continue;
1573
1574 if (found) printf("\n");
1575
1576 print_header(format, this, handle);
1577 i = ip6tc_first_rule(this, handle);
1578
1579 num = 0;
1580 while (i) {
1581 print_firewall(i,
1582 ip6tc_get_target(i, handle),
1583 num++,
1584 format,
1585 *handle);
1586 i = ip6tc_next_rule(i, handle);
1587 }
1588 found = 1;
1589 }
1590
1591 errno = ENOENT;
1592 return found;
1593}
1594
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001595static char *get_modprobe(void)
1596{
Harald Welte43ac1912002-03-03 09:43:07 +00001597 int procfile;
1598 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001599
Harald Welte10f7f142004-10-22 08:14:07 +00001600#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001601 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1602 if (procfile < 0)
1603 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001604
Harald Welte10f7f142004-10-22 08:14:07 +00001605 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001606 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001607 memset(ret, 0, PROCFILE_BUFSIZ);
1608 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001609 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001610 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001611 }
1612 if (ret[strlen(ret)-1]=='\n')
1613 ret[strlen(ret)-1]=0;
1614 close(procfile);
1615 return ret;
1616 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001617 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001618 free(ret);
1619 close(procfile);
1620 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001621}
1622
Harald Welte58918652001-06-16 18:25:25 +00001623int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001624{
Harald Welte43ac1912002-03-03 09:43:07 +00001625 char *buf = NULL;
1626 char *argv[3];
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001627
Harald Welte43ac1912002-03-03 09:43:07 +00001628 /* If they don't explicitly set it, read out of kernel */
1629 if (!modprobe) {
1630 buf = get_modprobe();
1631 if (!buf)
1632 return -1;
1633 modprobe = buf;
1634 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001635
Harald Welte43ac1912002-03-03 09:43:07 +00001636 switch (fork()) {
1637 case 0:
1638 argv[0] = (char *)modprobe;
1639 argv[1] = (char *)modname;
1640 argv[2] = NULL;
1641 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001642
Harald Welte43ac1912002-03-03 09:43:07 +00001643 /* not usually reached */
1644 exit(0);
1645 case -1:
1646 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001647
Harald Welte43ac1912002-03-03 09:43:07 +00001648 default: /* parent */
1649 wait(NULL);
1650 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001651
Harald Welte43ac1912002-03-03 09:43:07 +00001652 free(buf);
1653 return 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001654}
1655
Rusty Russell5eed48a2000-06-02 20:12:24 +00001656static struct ip6t_entry *
1657generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001658 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001659 struct ip6t_entry_target *target)
1660{
1661 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001662 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001663 struct ip6t_entry *e;
1664
1665 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001666 for (matchp = matches; matchp; matchp = matchp->next)
1667 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001668
1669 e = fw_malloc(size + target->u.target_size);
1670 *e = *fw;
1671 e->target_offset = size;
1672 e->next_offset = size + target->u.target_size;
1673
1674 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001675 for (matchp = matches; matchp; matchp = matchp->next) {
1676 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1677 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001678 }
1679 memcpy(e->elems + size, target, target->u.target_size);
1680
1681 return e;
1682}
1683
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001684void clear_rule_matches(struct ip6tables_rule_match **matches)
1685{
1686 struct ip6tables_rule_match *matchp, *tmp;
1687
1688 for (matchp = *matches; matchp;) {
1689 tmp = matchp->next;
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001690 if (matchp->match->m)
1691 free(matchp->match->m);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001692 free(matchp);
1693 matchp = tmp;
1694 }
1695
1696 *matches = NULL;
1697}
1698
Rusty Russell5eed48a2000-06-02 20:12:24 +00001699int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1700{
1701 struct ip6t_entry fw, *e = NULL;
1702 int invert = 0;
1703 unsigned int nsaddrs = 0, ndaddrs = 0;
1704 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1705
1706 int c, verbose = 0;
1707 const char *chain = NULL;
1708 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1709 const char *policy = NULL, *newname = NULL;
1710 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001711 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001712 int ret = 1;
1713 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001714 struct ip6tables_rule_match *matches = NULL;
1715 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001716 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001717 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001718 const char *jumpto = "";
1719 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001720 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001721 int proto_used = 0;
Harald Weltecfaed1f2001-10-04 08:11:44 +00001722 char icmp6p[] = "icmpv6";
Rusty Russell5eed48a2000-06-02 20:12:24 +00001723
1724 memset(&fw, 0, sizeof(fw));
1725
Rusty Russell208d42e2004-12-20 05:29:52 +00001726 lib_dir = getenv("IP6TABLES_LIB_DIR");
1727 if (!lib_dir)
1728 lib_dir = IP6T_LIB_DIR;
1729
Harald Welte43ac1912002-03-03 09:43:07 +00001730 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001731 * a second time */
1732 optind = 0;
1733
Harald Welte43ac1912002-03-03 09:43:07 +00001734 /* clear mflags in case do_command gets called a second time
1735 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001736 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001737 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001738
Harald Welte43ac1912002-03-03 09:43:07 +00001739 for (t = ip6tables_targets; t; t = t->next) {
1740 t->tflags = 0;
1741 t->used = 0;
1742 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001743
Rusty Russell5eed48a2000-06-02 20:12:24 +00001744 /* Suppress error messages: we may add new options if we
1745 demand-load a protocol. */
1746 opterr = 0;
1747
1748 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001749 "-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 +00001750 opts, NULL)) != -1) {
1751 switch (c) {
1752 /*
1753 * Command selection
1754 */
1755 case 'A':
1756 add_command(&command, CMD_APPEND, CMD_NONE,
1757 invert);
1758 chain = optarg;
1759 break;
1760
1761 case 'D':
1762 add_command(&command, CMD_DELETE, CMD_NONE,
1763 invert);
1764 chain = optarg;
1765 if (optind < argc && argv[optind][0] != '-'
1766 && argv[optind][0] != '!') {
1767 rulenum = parse_rulenumber(argv[optind++]);
1768 command = CMD_DELETE_NUM;
1769 }
1770 break;
1771
Rusty Russell5eed48a2000-06-02 20:12:24 +00001772 case 'R':
1773 add_command(&command, CMD_REPLACE, CMD_NONE,
1774 invert);
1775 chain = optarg;
1776 if (optind < argc && argv[optind][0] != '-'
1777 && argv[optind][0] != '!')
1778 rulenum = parse_rulenumber(argv[optind++]);
1779 else
1780 exit_error(PARAMETER_PROBLEM,
1781 "-%c requires a rule number",
1782 cmd2char(CMD_REPLACE));
1783 break;
1784
1785 case 'I':
1786 add_command(&command, CMD_INSERT, CMD_NONE,
1787 invert);
1788 chain = optarg;
1789 if (optind < argc && argv[optind][0] != '-'
1790 && argv[optind][0] != '!')
1791 rulenum = parse_rulenumber(argv[optind++]);
1792 else rulenum = 1;
1793 break;
1794
1795 case 'L':
1796 add_command(&command, CMD_LIST, CMD_ZERO,
1797 invert);
1798 if (optarg) chain = optarg;
1799 else if (optind < argc && argv[optind][0] != '-'
1800 && argv[optind][0] != '!')
1801 chain = argv[optind++];
1802 break;
1803
1804 case 'F':
1805 add_command(&command, CMD_FLUSH, CMD_NONE,
1806 invert);
1807 if (optarg) chain = optarg;
1808 else if (optind < argc && argv[optind][0] != '-'
1809 && argv[optind][0] != '!')
1810 chain = argv[optind++];
1811 break;
1812
1813 case 'Z':
1814 add_command(&command, CMD_ZERO, CMD_LIST,
1815 invert);
1816 if (optarg) chain = optarg;
1817 else if (optind < argc && argv[optind][0] != '-'
1818 && argv[optind][0] != '!')
1819 chain = argv[optind++];
1820 break;
1821
1822 case 'N':
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001823 if (optarg && *optarg == '-')
1824 exit_error(PARAMETER_PROBLEM,
1825 "chain name not allowed to start "
1826 "with `-'\n");
1827 if (find_target(optarg, TRY_LOAD))
1828 exit_error(PARAMETER_PROBLEM,
1829 "chain name may not clash "
1830 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001831 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1832 invert);
1833 chain = optarg;
1834 break;
1835
1836 case 'X':
1837 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1838 invert);
1839 if (optarg) chain = optarg;
1840 else if (optind < argc && argv[optind][0] != '-'
1841 && argv[optind][0] != '!')
1842 chain = argv[optind++];
1843 break;
1844
1845 case 'E':
1846 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1847 invert);
1848 chain = optarg;
1849 if (optind < argc && argv[optind][0] != '-'
1850 && argv[optind][0] != '!')
1851 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001852 else
1853 exit_error(PARAMETER_PROBLEM,
1854 "-%c requires old-chain-name and "
1855 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001856 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001857 break;
1858
1859 case 'P':
1860 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1861 invert);
1862 chain = optarg;
1863 if (optind < argc && argv[optind][0] != '-'
1864 && argv[optind][0] != '!')
1865 policy = argv[optind++];
1866 else
1867 exit_error(PARAMETER_PROBLEM,
1868 "-%c requires a chain and a policy",
1869 cmd2char(CMD_SET_POLICY));
1870 break;
1871
1872 case 'h':
1873 if (!optarg)
1874 optarg = argv[optind];
1875
1876 /* iptables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001877 if (!matches && protocol)
1878 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001879
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001880 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001881
1882 /*
1883 * Option selection
1884 */
1885 case 'p':
Harald Welteb77f1da2002-03-14 11:35:58 +00001886 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001887 set_option(&options, OPT_PROTOCOL, &fw.ipv6.invflags,
1888 invert);
1889
1890 /* Canonicalize into lower case */
1891 for (protocol = argv[optind-1]; *protocol; protocol++)
1892 *protocol = tolower(*protocol);
1893
1894 protocol = argv[optind-1];
Harald Weltecfaed1f2001-10-04 08:11:44 +00001895 if ( strcmp(protocol,"ipv6-icmp") == 0)
1896 protocol = icmp6p;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001897 fw.ipv6.proto = parse_protocol(protocol);
1898 fw.ipv6.flags |= IP6T_F_PROTO;
1899
1900 if (fw.ipv6.proto == 0
1901 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1902 exit_error(PARAMETER_PROBLEM,
1903 "rule would never match protocol");
1904 fw.nfcache |= NFC_IP6_PROTO;
1905 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];
1912 fw.nfcache |= NFC_IP6_SRC;
1913 break;
1914
1915 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001916 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001917 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1918 invert);
1919 dhostnetworkmask = argv[optind-1];
1920 fw.nfcache |= NFC_IP6_DST;
1921 break;
1922
1923 case 'j':
1924 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1925 invert);
1926 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001927 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001928 target = find_target(jumpto, TRY_LOAD);
1929
1930 if (target) {
1931 size_t size;
1932
Harald Welte43ac1912002-03-03 09:43:07 +00001933 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1934 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001935
1936 target->t = fw_calloc(1, size);
1937 target->t->u.target_size = size;
1938 strcpy(target->t->u.user.name, jumpto);
1939 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001940 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941 }
1942 break;
1943
1944
1945 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00001946 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001947 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
1948 invert);
1949 parse_interface(argv[optind-1],
1950 fw.ipv6.iniface,
1951 fw.ipv6.iniface_mask);
1952 fw.nfcache |= NFC_IP6_IF_IN;
1953 break;
1954
1955 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00001956 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001957 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
1958 invert);
1959 parse_interface(argv[optind-1],
1960 fw.ipv6.outiface,
1961 fw.ipv6.outiface_mask);
1962 fw.nfcache |= NFC_IP6_IF_OUT;
1963 break;
1964
1965 case 'v':
1966 if (!verbose)
1967 set_option(&options, OPT_VERBOSE,
1968 &fw.ipv6.invflags, invert);
1969 verbose++;
1970 break;
1971
1972 case 'm': {
1973 size_t size;
1974
1975 if (invert)
1976 exit_error(PARAMETER_PROBLEM,
1977 "unexpected ! flag before --match");
1978
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001979 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00001980 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
1981 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001982 m->m = fw_calloc(1, size);
1983 m->m->u.match_size = size;
1984 strcpy(m->m->u.user.name, m->name);
1985 m->init(m->m, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001986 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001987 }
1988 break;
1989
1990 case 'n':
1991 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
1992 invert);
1993 break;
1994
1995 case 't':
1996 if (invert)
1997 exit_error(PARAMETER_PROBLEM,
1998 "unexpected ! flag before --table");
1999 *table = argv[optind-1];
2000 break;
2001
2002 case 'x':
2003 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2004 invert);
2005 break;
2006
2007 case 'V':
2008 if (invert)
2009 printf("Not %s ;-)\n", program_version);
2010 else
2011 printf("%s v%s\n",
2012 program_name, program_version);
2013 exit(0);
2014
2015 case '0':
2016 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2017 invert);
2018 break;
2019
Harald Welte43ac1912002-03-03 09:43:07 +00002020 case 'M':
2021 modprobe = optarg;
2022 break;
2023
2024 case 'c':
2025
2026 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2027 invert);
2028 pcnt = optarg;
2029 if (optind < argc && argv[optind][0] != '-'
2030 && argv[optind][0] != '!')
2031 bcnt = argv[optind++];
2032 else
2033 exit_error(PARAMETER_PROBLEM,
2034 "-%c requires packet and byte counter",
2035 opt2char(OPT_COUNTERS));
2036
Martin Josefssona28d4952004-05-26 16:04:48 +00002037 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002038 exit_error(PARAMETER_PROBLEM,
2039 "-%c packet counter not numeric",
2040 opt2char(OPT_COUNTERS));
2041
Martin Josefssona28d4952004-05-26 16:04:48 +00002042 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002043 exit_error(PARAMETER_PROBLEM,
2044 "-%c byte counter not numeric",
2045 opt2char(OPT_COUNTERS));
2046
2047 break;
2048
2049
Rusty Russell5eed48a2000-06-02 20:12:24 +00002050 case 1: /* non option */
2051 if (optarg[0] == '!' && optarg[1] == '\0') {
2052 if (invert)
2053 exit_error(PARAMETER_PROBLEM,
2054 "multiple consecutive ! not"
2055 " allowed");
2056 invert = TRUE;
2057 optarg[0] = '\0';
2058 continue;
2059 }
2060 printf("Bad argument `%s'\n", optarg);
2061 exit_tryhelp(2);
2062
2063 default:
2064 /* FIXME: This scheme doesn't allow two of the same
2065 matches --RR */
2066 if (!target
2067 || !(target->parse(c - target->option_offset,
2068 argv, invert,
2069 &target->tflags,
2070 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002071 for (matchp = matches; matchp; matchp = matchp->next) {
2072 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002073 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002074 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002075 &fw,
2076 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002077 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002078 break;
2079 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002080 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002081
2082 /* If you listen carefully, you can
2083 actually hear this code suck. */
2084
2085 /* some explanations (after four different bugs
2086 * in 3 different releases): If we encountere a
2087 * parameter, that has not been parsed yet,
2088 * it's not an option of an explicitly loaded
2089 * match or a target. However, we support
2090 * implicit loading of the protocol match
2091 * extension. '-p tcp' means 'l4 proto 6' and
2092 * at the same time 'load tcp protocol match on
2093 * demand if we specify --dport'.
2094 *
2095 * To make this work, we need to make sure:
2096 * - the parameter has not been parsed by
2097 * a match (m above)
2098 * - a protocol has been specified
2099 * - the protocol extension has not been
2100 * loaded yet, or is loaded and unused
2101 * [think of iptables-restore!]
2102 * - the protocol extension can be successively
2103 * loaded
2104 */
2105 if (m == NULL
2106 && protocol
2107 && (!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 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002110 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002111 && (proto_used == 0))
2112 )
2113 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002114 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002115 /* Try loading protocol */
2116 size_t size;
2117
2118 proto_used = 1;
2119
2120 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2121 + m->size;
2122
2123 m->m = fw_calloc(1, size);
2124 m->m->u.match_size = size;
2125 strcpy(m->m->u.user.name, m->name);
2126 m->init(m->m, &fw.nfcache);
2127
2128 opts = merge_options(opts,
2129 m->extra_opts, &m->option_offset);
2130
2131 optind--;
2132 continue;
2133 }
2134
Rusty Russell5eed48a2000-06-02 20:12:24 +00002135 if (!m)
2136 exit_error(PARAMETER_PROBLEM,
2137 "Unknown arg `%s'",
2138 argv[optind-1]);
2139 }
2140 }
2141 invert = FALSE;
2142 }
2143
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002144 for (matchp = matches; matchp; matchp = matchp->next)
2145 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002146
Harald Welte43ac1912002-03-03 09:43:07 +00002147 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002148 target->final_check(target->tflags);
2149
2150 /* Fix me: must put inverse options checking here --MN */
2151
2152 if (optind < argc)
2153 exit_error(PARAMETER_PROBLEM,
2154 "unknown arguments found on commandline");
2155 if (!command)
2156 exit_error(PARAMETER_PROBLEM, "no command specified");
2157 if (invert)
2158 exit_error(PARAMETER_PROBLEM,
2159 "nothing appropriate following !");
2160
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002161 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002162 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002163 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002164 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002165 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002166 }
2167
2168 if (shostnetworkmask)
2169 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2170 &(fw.ipv6.smsk), &nsaddrs);
2171
2172 if (dhostnetworkmask)
2173 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2174 &(fw.ipv6.dmsk), &ndaddrs);
2175
2176 if ((nsaddrs > 1 || ndaddrs > 1) &&
2177 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2178 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2179 " source or destination IP addresses");
2180
Rusty Russell5eed48a2000-06-02 20:12:24 +00002181 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2182 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2183 "specify a unique address");
2184
2185 generic_opt_check(command, options);
2186
2187 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2188 exit_error(PARAMETER_PROBLEM,
2189 "chain name `%s' too long (must be under %i chars)",
2190 chain, IP6T_FUNCTION_MAXNAMELEN);
2191
Harald Welte43ac1912002-03-03 09:43:07 +00002192 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002193 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002194 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002195
Harald Welte43ac1912002-03-03 09:43:07 +00002196 if (!*handle) {
2197 /* try to insmod the module if iptc_init failed */
2198 ip6tables_insmod("ip6_tables", modprobe);
2199 *handle = ip6tc_init(*table);
2200 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002201
Harald Welte43ac1912002-03-03 09:43:07 +00002202 if (!*handle)
2203 exit_error(VERSION_PROBLEM,
2204 "can't initialize ip6tables table `%s': %s",
2205 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002206
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002207 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002208 || command == CMD_DELETE
2209 || command == CMD_INSERT
2210 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002211 if (strcmp(chain, "PREROUTING") == 0
2212 || strcmp(chain, "INPUT") == 0) {
2213 /* -o not valid with incoming packets. */
2214 if (options & OPT_VIANAMEOUT)
2215 exit_error(PARAMETER_PROBLEM,
2216 "Can't use -%c with %s\n",
2217 opt2char(OPT_VIANAMEOUT),
2218 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002219 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002220
Harald Welte43ac1912002-03-03 09:43:07 +00002221 if (strcmp(chain, "POSTROUTING") == 0
2222 || strcmp(chain, "OUTPUT") == 0) {
2223 /* -i not valid with outgoing packets */
2224 if (options & OPT_VIANAMEIN)
2225 exit_error(PARAMETER_PROBLEM,
2226 "Can't use -%c with %s\n",
2227 opt2char(OPT_VIANAMEIN),
2228 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002229 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002230
2231 if (target && ip6tc_is_chain(jumpto, *handle)) {
2232 printf("Warning: using chain %s, not extension\n",
2233 jumpto);
2234
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002235 if (target->t)
2236 free(target->t);
2237
Rusty Russell5eed48a2000-06-02 20:12:24 +00002238 target = NULL;
2239 }
2240
2241 /* If they didn't specify a target, or it's a chain
2242 name, use standard. */
2243 if (!target
2244 && (strlen(jumpto) == 0
2245 || ip6tc_is_chain(jumpto, *handle))) {
2246 size_t size;
2247
2248 target = find_target(IP6T_STANDARD_TARGET,
2249 LOAD_MUST_SUCCEED);
2250
2251 size = sizeof(struct ip6t_entry_target)
2252 + target->size;
2253 target->t = fw_calloc(1, size);
2254 target->t->u.target_size = size;
2255 strcpy(target->t->u.user.name, jumpto);
2256 target->init(target->t, &fw.nfcache);
2257 }
2258
2259 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002260 /* it is no chain, and we can't load a plugin.
2261 * We cannot know if the plugin is corrupt, non
2262 * existant OR if the user just misspelled a
2263 * chain. */
2264 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002265 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002266 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002267 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002268 }
2269 }
2270
2271 switch (command) {
2272 case CMD_APPEND:
2273 ret = append_entry(chain, e,
2274 nsaddrs, saddrs, ndaddrs, daddrs,
2275 options&OPT_VERBOSE,
2276 handle);
2277 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002278 case CMD_DELETE:
2279 ret = delete_entry(chain, e,
2280 nsaddrs, saddrs, ndaddrs, daddrs,
2281 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002282 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002283 break;
2284 case CMD_DELETE_NUM:
2285 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2286 break;
2287 case CMD_REPLACE:
2288 ret = replace_entry(chain, e, rulenum - 1,
2289 saddrs, daddrs, options&OPT_VERBOSE,
2290 handle);
2291 break;
2292 case CMD_INSERT:
2293 ret = insert_entry(chain, e, rulenum - 1,
2294 nsaddrs, saddrs, ndaddrs, daddrs,
2295 options&OPT_VERBOSE,
2296 handle);
2297 break;
2298 case CMD_LIST:
2299 ret = list_entries(chain,
2300 options&OPT_VERBOSE,
2301 options&OPT_NUMERIC,
2302 options&OPT_EXPANDED,
2303 options&OPT_LINENUMBERS,
2304 handle);
2305 break;
2306 case CMD_FLUSH:
2307 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2308 break;
2309 case CMD_ZERO:
2310 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2311 break;
2312 case CMD_LIST|CMD_ZERO:
2313 ret = list_entries(chain,
2314 options&OPT_VERBOSE,
2315 options&OPT_NUMERIC,
2316 options&OPT_EXPANDED,
2317 options&OPT_LINENUMBERS,
2318 handle);
2319 if (ret)
2320 ret = zero_entries(chain,
2321 options&OPT_VERBOSE, handle);
2322 break;
2323 case CMD_NEW_CHAIN:
2324 ret = ip6tc_create_chain(chain, handle);
2325 break;
2326 case CMD_DELETE_CHAIN:
2327 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2328 break;
2329 case CMD_RENAME_CHAIN:
2330 ret = ip6tc_rename_chain(chain, newname, handle);
2331 break;
2332 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002333 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002334 break;
2335 default:
2336 /* We should never reach this... */
2337 exit_tryhelp(2);
2338 }
2339
2340 if (verbose > 1)
2341 dump_entries6(*handle);
2342
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002343 clear_rule_matches(&matches);
2344
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002345 if (e != NULL) {
2346 free(e);
2347 e = NULL;
2348 }
2349
2350 for (c = 0; c < nsaddrs; c++)
2351 free(&saddrs[c]);
2352
2353 for (c = 0; c < ndaddrs; c++)
2354 free(&daddrs[c]);
2355
2356 if (opts != original_opts) {
2357 free(opts);
2358 opts = original_opts;
2359 global_option_offset = 0;
2360 }
2361
Rusty Russell5eed48a2000-06-02 20:12:24 +00002362 return ret;
2363}