blob: ef7e36536475502d3f6875aea2c19f9c9808d289 [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
Harald Welte0eca33f2006-04-21 11:56:30 +000084#define CMD_RENAME_CHAIN 0x0800U
Rusty Russell5eed48a2000-06-02 20:12:24 +000085#define NUMBER_OF_CMD 13
86static const char cmdflags[] = { 'I', 'D', 'D', 'R', 'A', 'L', 'F', 'Z',
András Kis-Szabó0c4188f2002-08-14 11:40:41 +000087 'N', 'X', 'P', 'E' };
Rusty Russell5eed48a2000-06-02 20:12:24 +000088
89#define OPTION_OFFSET 256
90
91#define OPT_NONE 0x00000U
92#define OPT_NUMERIC 0x00001U
93#define OPT_SOURCE 0x00002U
94#define OPT_DESTINATION 0x00004U
95#define OPT_PROTOCOL 0x00008U
96#define OPT_JUMP 0x00010U
97#define OPT_VERBOSE 0x00020U
98#define OPT_EXPANDED 0x00040U
99#define OPT_VIANAMEIN 0x00080U
100#define OPT_VIANAMEOUT 0x00100U
101#define OPT_LINENUMBERS 0x00200U
Harald Welte43ac1912002-03-03 09:43:07 +0000102#define OPT_COUNTERS 0x00400U
103#define NUMBER_OF_OPT 11
Rusty Russell5eed48a2000-06-02 20:12:24 +0000104static const char optflags[NUMBER_OF_OPT]
Jonas Berlin4a06cf02005-04-01 06:38:25 +0000105= { 'n', 's', 'd', 'p', 'j', 'v', 'x', 'i', 'o', '0', 'c'};
Rusty Russell5eed48a2000-06-02 20:12:24 +0000106
107static struct option original_opts[] = {
108 { "append", 1, 0, 'A' },
109 { "delete", 1, 0, 'D' },
110 { "insert", 1, 0, 'I' },
111 { "replace", 1, 0, 'R' },
112 { "list", 2, 0, 'L' },
113 { "flush", 2, 0, 'F' },
114 { "zero", 2, 0, 'Z' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000115 { "new-chain", 1, 0, 'N' },
116 { "delete-chain", 2, 0, 'X' },
Harald Welte68ec9c72002-11-02 14:48:17 +0000117 { "rename-chain", 1, 0, 'E' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000118 { "policy", 1, 0, 'P' },
119 { "source", 1, 0, 's' },
120 { "destination", 1, 0, 'd' },
121 { "src", 1, 0, 's' }, /* synonym */
122 { "dst", 1, 0, 'd' }, /* synonym */
123 { "protocol", 1, 0, 'p' },
124 { "in-interface", 1, 0, 'i' },
125 { "jump", 1, 0, 'j' },
126 { "table", 1, 0, 't' },
127 { "match", 1, 0, 'm' },
128 { "numeric", 0, 0, 'n' },
129 { "out-interface", 1, 0, 'o' },
130 { "verbose", 0, 0, 'v' },
131 { "exact", 0, 0, 'x' },
132 { "version", 0, 0, 'V' },
133 { "help", 2, 0, 'h' },
134 { "line-numbers", 0, 0, '0' },
Harald Welte43ac1912002-03-03 09:43:07 +0000135 { "modprobe", 1, 0, 'M' },
136 { "set-counters", 1, 0, 'c' },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000137 { 0 }
138};
139
Harald Weltea8658ca2003-03-05 07:46:15 +0000140/* we need this for ip6tables-restore. ip6tables-restore.c sets line to the
141 * current line of the input file, in order to give a more precise error
142 * message. ip6tables itself doesn't need this, so it is initialized to the
143 * magic number of -1 */
144int line = -1;
145
Rusty Russell5eed48a2000-06-02 20:12:24 +0000146static struct option *opts = original_opts;
147static unsigned int global_option_offset = 0;
148
149/* Table of legal combinations of commands and options. If any of the
150 * given commands make an option legal, that option is legal (applies to
151 * CMD_LIST and CMD_ZERO only).
152 * Key:
153 * + compulsory
154 * x illegal
155 * optional
156 */
157
158static char commands_v_options[NUMBER_OF_CMD][NUMBER_OF_OPT] =
159/* Well, it's better than "Re: Linux vs FreeBSD" */
160{
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000161 /* -n -s -d -p -j -v -x -i -o --line -c */
162/*INSERT*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
163/*DELETE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x','x'},
164/*DELETE_NUM*/{'x','x','x','x','x',' ','x','x','x','x','x'},
165/*REPLACE*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
166/*APPEND*/ {'x',' ',' ',' ',' ',' ','x',' ',' ','x',' '},
167/*LIST*/ {' ','x','x','x','x',' ',' ','x','x',' ','x'},
168/*FLUSH*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
169/*ZERO*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
170/*NEW_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
171/*DEL_CHAIN*/ {'x','x','x','x','x',' ','x','x','x','x','x'},
172/*SET_POLICY*/{'x','x','x','x','x',' ','x','x','x','x','x'},
173/*CHECK*/ {'x','+','+','+','x',' ','x',' ',' ','x','x'},
174/*RENAME*/ {'x','x','x','x','x',' ','x','x','x','x','x'}
Rusty Russell5eed48a2000-06-02 20:12:24 +0000175};
176
177static int inverse_for_options[NUMBER_OF_OPT] =
178{
179/* -n */ 0,
180/* -s */ IP6T_INV_SRCIP,
181/* -d */ IP6T_INV_DSTIP,
182/* -p */ IP6T_INV_PROTO,
183/* -j */ 0,
184/* -v */ 0,
185/* -x */ 0,
186/* -i */ IP6T_INV_VIA_IN,
187/* -o */ IP6T_INV_VIA_OUT,
Patrick McHardyHarald Welte2cfbd9f2006-04-22 02:08:12 +0000188/*--line*/ 0,
189/* -c */ 0,
Rusty Russell5eed48a2000-06-02 20:12:24 +0000190};
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 },
Yasuyuki KOZAKAI85872c82006-04-15 03:09:37 +0000225 { "ipv6-icmp", IPPROTO_ICMPV6 },
András Kis-Szabó764316a2001-02-26 17:31:20 +0000226 { "esp", IPPROTO_ESP },
227 { "ah", IPPROTO_AH },
Rusty Russell5eed48a2000-06-02 20:12:24 +0000228};
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
Phil Oester58179b12006-07-20 17:00:19 +0000248int
249service_to_port(const char *name, const char *proto)
250{
251 struct servent *service;
252
253 if ((service = getservbyname(name, proto)) != NULL)
254 return ntohs((unsigned short) service->s_port);
255
256 return -1;
257}
258
Phil Oesterdbac8ad2006-07-20 17:01:54 +0000259u_int16_t
260parse_port(const char *port, const char *proto)
261{
262 unsigned int portnum;
263
264 if ((string_to_number(port, 0, 65535, &portnum)) != -1 ||
265 (portnum = service_to_port(port, proto)) != -1)
266 return (u_int16_t)portnum;
267
268 exit_error(PARAMETER_PROBLEM,
269 "invalid port/service `%s' specified", port);
270}
271
Rusty Russell5eed48a2000-06-02 20:12:24 +0000272static void
273in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
274{
275 memcpy(dst, src, sizeof(struct in6_addr));
Harald Welte43ac1912002-03-03 09:43:07 +0000276 /* dst->s6_addr = src->s6_addr; */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000277}
278
Pablo Neiradfdcd642005-05-29 19:05:23 +0000279static void free_opts(int reset_offset)
280{
281 if (opts != original_opts) {
282 free(opts);
283 opts = original_opts;
284 if (reset_offset)
285 global_option_offset = 0;
286 }
287}
288
Rusty Russell5eed48a2000-06-02 20:12:24 +0000289void
290exit_error(enum exittype status, char *msg, ...)
291{
292 va_list args;
293
294 va_start(args, msg);
295 fprintf(stderr, "%s v%s: ", program_name, program_version);
296 vfprintf(stderr, msg, args);
297 va_end(args);
298 fprintf(stderr, "\n");
299 if (status == PARAMETER_PROBLEM)
300 exit_tryhelp(status);
301 if (status == VERSION_PROBLEM)
302 fprintf(stderr,
Jonas Berlin1b91e592005-04-01 06:58:38 +0000303 "Perhaps ip6tables or your kernel needs to be upgraded.\n");
Pablo Neiradfdcd642005-05-29 19:05:23 +0000304 /* On error paths, make sure that we don't leak memory */
305 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000306 exit(status);
307}
308
309void
310exit_tryhelp(int status)
311{
Harald Weltea8658ca2003-03-05 07:46:15 +0000312 if (line != -1)
313 fprintf(stderr, "Error occurred at line: %d\n", line);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000314 fprintf(stderr, "Try `%s -h' or '%s --help' for more information.\n",
315 program_name, program_name );
Pablo Neiradfdcd642005-05-29 19:05:23 +0000316 free_opts(1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000317 exit(status);
318}
319
320void
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000321exit_printhelp(struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000322{
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000323 struct ip6tables_rule_match *matchp = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000324 struct ip6tables_target *t = NULL;
325
326 printf("%s v%s\n\n"
András Kis-Szabó0c4188f2002-08-14 11:40:41 +0000327"Usage: %s -[AD] chain rule-specification [options]\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000328" %s -[RI] chain rulenum rule-specification [options]\n"
329" %s -D chain rulenum [options]\n"
330" %s -[LFZ] [chain] [options]\n"
331" %s -[NX] chain\n"
332" %s -E old-chain-name new-chain-name\n"
333" %s -P chain target [options]\n"
334" %s -h (print this help information)\n\n",
335 program_name, program_version, program_name, program_name,
336 program_name, program_name, program_name, program_name,
337 program_name, program_name);
338
339 printf(
340"Commands:\n"
341"Either long or short options are allowed.\n"
342" --append -A chain Append to chain\n"
343" --delete -D chain Delete matching rule from chain\n"
344" --delete -D chain rulenum\n"
345" Delete rule rulenum (1 = first) from chain\n"
346" --insert -I chain [rulenum]\n"
347" Insert in chain as rulenum (default 1=first)\n"
348" --replace -R chain rulenum\n"
349" Replace rule rulenum (1 = first) in chain\n"
350" --list -L [chain] List the rules in a chain or all chains\n"
351" --flush -F [chain] Delete all rules in chain or all chains\n"
352" --zero -Z [chain] Zero counters in chain or all chains\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000353" --new -N chain Create a new user-defined chain\n"
354" --delete-chain\n"
355" -X [chain] Delete a user-defined chain\n"
356" --policy -P chain target\n"
357" Change policy on chain to target\n"
358" --rename-chain\n"
359" -E old-chain new-chain\n"
360" Change chain name, (moving any references)\n"
361
362"Options:\n"
363" --proto -p [!] proto protocol: by number or name, eg. `tcp'\n"
364" --source -s [!] address[/mask]\n"
365" source specification\n"
366" --destination -d [!] address[/mask]\n"
367" destination specification\n"
368" --in-interface -i [!] input name[+]\n"
369" network interface name ([+] for wildcard)\n"
370" --jump -j target\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000371" target for rule (may load target extension)\n"
372" --match -m match\n"
373" extended match (may load extension)\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000374" --numeric -n numeric output of addresses and ports\n"
375" --out-interface -o [!] output name[+]\n"
376" network interface name ([+] for wildcard)\n"
377" --table -t table table to manipulate (default: `filter')\n"
378" --verbose -v verbose mode\n"
Harald Welte43ac1912002-03-03 09:43:07 +0000379" --line-numbers print line numbers when listing\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000380" --exact -x expand numbers (display exact values)\n"
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000381/*"[!] --fragment -f match second or further fragments only\n"*/
Harald Welte43ac1912002-03-03 09:43:07 +0000382" --modprobe=<command> try to insert modules using this command\n"
383" --set-counters PKTS BYTES set the counter during insert/append\n"
Rusty Russell5eed48a2000-06-02 20:12:24 +0000384"[!] --version -V print package version.\n");
385
386 /* Print out any special helps. A user might like to be able to add a --help
387 to the commandline, and see expected results. So we call help for all
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000388 specified matches & targets */
389 for (t = ip6tables_targets; t; t = t->next) {
390 if (t->used) {
391 printf("\n");
392 t->help();
393 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000394 }
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000395 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000396 printf("\n");
Martin Josefsson66aea6f2004-05-26 15:41:54 +0000397 matchp->match->help();
Rusty Russell5eed48a2000-06-02 20:12:24 +0000398 }
399 exit(0);
400}
401
402static void
403generic_opt_check(int command, int options)
404{
405 int i, j, legal = 0;
406
407 /* Check that commands are valid with options. Complicated by the
408 * fact that if an option is legal with *any* command given, it is
409 * legal overall (ie. -z and -l).
410 */
411 for (i = 0; i < NUMBER_OF_OPT; i++) {
412 legal = 0; /* -1 => illegal, 1 => legal, 0 => undecided. */
413
414 for (j = 0; j < NUMBER_OF_CMD; j++) {
415 if (!(command & (1<<j)))
416 continue;
417
418 if (!(options & (1<<i))) {
419 if (commands_v_options[j][i] == '+')
420 exit_error(PARAMETER_PROBLEM,
421 "You need to supply the `-%c' "
422 "option for this command\n",
423 optflags[i]);
424 } else {
425 if (commands_v_options[j][i] != 'x')
426 legal = 1;
427 else if (legal == 0)
428 legal = -1;
429 }
430 }
431 if (legal == -1)
432 exit_error(PARAMETER_PROBLEM,
433 "Illegal option `-%c' with this command\n",
434 optflags[i]);
435 }
436}
437
438static char
439opt2char(int option)
440{
441 const char *ptr;
442 for (ptr = optflags; option > 1; option >>= 1, ptr++);
443
444 return *ptr;
445}
446
447static char
448cmd2char(int option)
449{
450 const char *ptr;
451 for (ptr = cmdflags; option > 1; option >>= 1, ptr++);
452
453 return *ptr;
454}
455
456static void
Harald Welteefa8fc22005-07-19 22:03:49 +0000457add_command(unsigned int *cmd, const int newcmd, const int othercmds,
458 int invert)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000459{
460 if (invert)
461 exit_error(PARAMETER_PROBLEM, "unexpected ! flag");
462 if (*cmd & (~othercmds))
463 exit_error(PARAMETER_PROBLEM, "Can't use -%c with -%c\n",
464 cmd2char(newcmd), cmd2char(*cmd & (~othercmds)));
465 *cmd |= newcmd;
466}
467
468int
Harald Welteb77f1da2002-03-14 11:35:58 +0000469check_inverse(const char option[], int *invert, int *optind, int argc)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000470{
471 if (option && strcmp(option, "!") == 0) {
472 if (*invert)
473 exit_error(PARAMETER_PROBLEM,
474 "Multiple `!' flags not allowed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000475 *invert = TRUE;
Harald Welteb77f1da2002-03-14 11:35:58 +0000476 if (optind) {
477 *optind = *optind+1;
478 if (argc && *optind > argc)
479 exit_error(PARAMETER_PROBLEM,
480 "no argument following `!'");
481 }
482
Rusty Russell5eed48a2000-06-02 20:12:24 +0000483 return TRUE;
484 }
485 return FALSE;
486}
487
488static void *
489fw_calloc(size_t count, size_t size)
490{
491 void *p;
492
493 if ((p = calloc(count, size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000494 perror("ip6tables: calloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000495 exit(1);
496 }
497 return p;
498}
499
500static void *
501fw_malloc(size_t size)
502{
503 void *p;
504
505 if ((p = malloc(size)) == NULL) {
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000506 perror("ip6tables: malloc failed");
Rusty Russell5eed48a2000-06-02 20:12:24 +0000507 exit(1);
508 }
509 return p;
510}
511
Rusty Russell5eed48a2000-06-02 20:12:24 +0000512static char *
513addr_to_numeric(const struct in6_addr *addrp)
514{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000515 /* 0000:0000:0000:0000:0000:000.000.000.000
516 * 0000:0000:0000:0000:0000:0000:0000:0000 */
András Kis-Szabó764316a2001-02-26 17:31:20 +0000517 static char buf[50+1];
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000518 return (char *)inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
Rusty Russell5eed48a2000-06-02 20:12:24 +0000519}
520
521static struct in6_addr *
522numeric_to_addr(const char *num)
523{
524 static struct in6_addr ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000525 int err;
526 if ((err=inet_pton(AF_INET6, num, &ap)) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000527 return &ap;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000528#ifdef DEBUG
529 fprintf(stderr, "\nnumeric2addr: %d\n", err);
530#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000531 return (struct in6_addr *)NULL;
532}
533
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000534
535static struct in6_addr *
536host_to_addr(const char *name, unsigned int *naddr)
537{
538 struct addrinfo hints;
539 struct addrinfo *res;
540 static struct in6_addr *addr;
541 int err;
542
543 memset(&hints, 0, sizeof(hints));
544 hints.ai_flags=AI_CANONNAME;
545 hints.ai_family=AF_INET6;
546 hints.ai_socktype=SOCK_RAW;
547 hints.ai_protocol=41;
548 hints.ai_next=NULL;
549
550 *naddr = 0;
551 if ( (err=getaddrinfo(name, NULL, &hints, &res)) != 0 ){
552#ifdef DEBUG
553 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
554#endif
555 return (struct in6_addr *) NULL;
556 } else {
557 if (res->ai_family != AF_INET6 ||
558 res->ai_addrlen != sizeof(struct sockaddr_in6))
559 return (struct in6_addr *) NULL;
560
561#ifdef DEBUG
562 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
563 addr_to_numeric(&(((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)));
564#endif
565 /* Get the first element of the address-chain */
566 addr = fw_calloc(1, sizeof(struct in6_addr));
567 in6addrcpy(addr, (struct in6_addr *)
568 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr);
569 freeaddrinfo(res);
570 *naddr = 1;
571 return addr;
572 }
573
574 return (struct in6_addr *) NULL;
575}
576
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000577static char *
578addr_to_host(const struct in6_addr *addr)
579{
580 struct sockaddr_in6 saddr;
581 int err;
582 static char hostname[NI_MAXHOST];
583
584 memset(&saddr, 0, sizeof(struct sockaddr_in6));
585 in6addrcpy(&(saddr.sin6_addr),(struct in6_addr *)addr);
András Kis-Szabóed44b832001-06-27 02:21:45 +0000586 saddr.sin6_family = AF_INET6;
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000587
588 if ( (err=getnameinfo((struct sockaddr *)&saddr,
589 sizeof(struct sockaddr_in6),
590 hostname, sizeof(hostname)-1,
591 NULL, 0, 0)) != 0 ){
592#ifdef DEBUG
593 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
594#endif
595 return (char *) NULL;
596 } else {
597#ifdef DEBUG
598 fprintf (stderr, "\naddr2host: %s\n", hostname);
599#endif
600
601 return hostname;
602 }
603
604 return (char *) NULL;
605}
606
Rusty Russell5eed48a2000-06-02 20:12:24 +0000607static char *
608mask_to_numeric(const struct in6_addr *addrp)
609{
Harald Welte8a50ab82003-06-24 18:15:59 +0000610 static char buf[50+2];
Rusty Russell5eed48a2000-06-02 20:12:24 +0000611 int l = ipv6_prefix_length(addrp);
Harald Welte8a50ab82003-06-24 18:15:59 +0000612 if (l == -1) {
613 strcpy(buf, "/");
614 strcat(buf, addr_to_numeric(addrp));
615 return buf;
616 }
Harald Welte43ac1912002-03-03 09:43:07 +0000617 sprintf(buf, "/%d", l);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000618 return buf;
619}
620
621static struct in6_addr *
622network_to_addr(const char *name)
623{
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000624 /* abort();*/
625 /* TODO: not implemented yet, but the exception breaks the
626 * name resolvation */
627 return (struct in6_addr *)NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000628}
629
630static char *
631addr_to_anyname(const struct in6_addr *addr)
632{
633 char *name;
634
635 if ((name = addr_to_host(addr)) != NULL)
636 return name;
637
638 return addr_to_numeric(addr);
639}
640
641/*
642 * All functions starting with "parse" should succeed, otherwise
643 * the program fails.
644 * Most routines return pointers to static data that may change
645 * between calls to the same or other routines with a few exceptions:
646 * "host_to_addr", "parse_hostnetwork", and "parse_hostnetworkmask"
647 * return global static data.
648*/
649
650static struct in6_addr *
651parse_hostnetwork(const char *name, unsigned int *naddrs)
652{
653 struct in6_addr *addrp, *addrptmp;
654
655 if ((addrptmp = numeric_to_addr(name)) != NULL ||
656 (addrptmp = network_to_addr(name)) != NULL) {
657 addrp = fw_malloc(sizeof(struct in6_addr));
658 in6addrcpy(addrp, addrptmp);
659 *naddrs = 1;
660 return addrp;
661 }
662 if ((addrp = host_to_addr(name, naddrs)) != NULL)
663 return addrp;
664
665 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name);
666}
667
668static struct in6_addr *
669parse_mask(char *mask)
670{
671 static struct in6_addr maskaddr;
672 struct in6_addr *addrp;
Harald Welteed498492001-07-23 01:24:22 +0000673 unsigned int bits;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000674
675 if (mask == NULL) {
676 /* no mask at all defaults to 128 bits */
677 memset(&maskaddr, 0xff, sizeof maskaddr);
678 return &maskaddr;
679 }
680 if ((addrp = numeric_to_addr(mask)) != NULL)
681 return addrp;
Harald Welteed498492001-07-23 01:24:22 +0000682 if (string_to_number(mask, 0, 128, &bits) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000683 exit_error(PARAMETER_PROBLEM,
684 "invalid mask `%s' specified", mask);
685 if (bits != 0) {
686 char *p = (char *)&maskaddr;
687 memset(p, 0xff, bits / 8);
688 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
689 p[bits / 8] = 0xff << (8 - (bits & 7));
690 return &maskaddr;
691 }
692
693 memset(&maskaddr, 0, sizeof maskaddr);
694 return &maskaddr;
695}
696
Harald Welte43ac1912002-03-03 09:43:07 +0000697void
Rusty Russell5eed48a2000-06-02 20:12:24 +0000698parse_hostnetworkmask(const char *name, struct in6_addr **addrpp,
699 struct in6_addr *maskp, unsigned int *naddrs)
700{
701 struct in6_addr *addrp;
702 char buf[256];
703 char *p;
704 int i, j, n;
705
706 strncpy(buf, name, sizeof(buf) - 1);
Karsten Desler073df8f2004-01-31 15:33:55 +0000707 buf[sizeof(buf) - 1] = '\0';
Rusty Russell5eed48a2000-06-02 20:12:24 +0000708 if ((p = strrchr(buf, '/')) != NULL) {
709 *p = '\0';
710 addrp = parse_mask(p + 1);
711 } else
712 addrp = parse_mask(NULL);
713 in6addrcpy(maskp, addrp);
714
715 /* if a null mask is given, the name is ignored, like in "any/0" */
716 if (!memcmp(maskp, &in6addr_any, sizeof(in6addr_any)))
717 strcpy(buf, "::");
718
719 addrp = *addrpp = parse_hostnetwork(buf, naddrs);
720 n = *naddrs;
721 for (i = 0, j = 0; i < n; i++) {
722 int k;
723 for (k = 0; k < 4; k++)
724 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k];
725 j++;
726 for (k = 0; k < j - 1; k++) {
András Kis-Szabó058eaad2001-06-16 15:42:17 +0000727 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000728 (*naddrs)--;
729 j--;
730 break;
731 }
732 }
733 }
734}
735
736struct ip6tables_match *
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000737find_match(const char *match_name, enum ip6t_tryload tryload, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000738{
739 struct ip6tables_match *ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000740 const char *icmp6 = "icmp6";
741 const char *name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000742
743 /* This is ugly as hell. Nonetheless, there is no way of changing
744 * this without hurting backwards compatibility */
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000745 if ( (strcmp(match_name,"icmpv6") == 0) ||
746 (strcmp(match_name,"ipv6-icmp") == 0) ||
747 (strcmp(match_name,"icmp6") == 0) )
748 name = icmp6;
749 else
750 name = match_name;
Harald Weltecfaed1f2001-10-04 08:11:44 +0000751
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000752 for (ptr = ip6tables_matches; ptr; ptr = ptr->next) {
753 if (strcmp(name, ptr->name) == 0) {
754 struct ip6tables_match *clone;
755
756 /* First match of this type: */
757 if (ptr->m == NULL)
758 break;
759
760 /* Second and subsequent clones */
761 clone = fw_malloc(sizeof(struct ip6tables_match));
762 memcpy(clone, ptr, sizeof(struct ip6tables_match));
763 clone->mflags = 0;
764 /* This is a clone: */
765 clone->next = clone;
766
767 ptr = clone;
768 break;
769 }
770 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000771
Harald Welte3efb6ea2001-08-06 18:50:21 +0000772#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +0000773 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +0000774 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +0000775 + strlen(name)];
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000776 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000777 if (dlopen(path, RTLD_NOW)) {
778 /* Found library. If it didn't register itself,
779 maybe they specified target as match. */
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000780 ptr = find_match(name, DONT_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000781
782 if (!ptr)
783 exit_error(PARAMETER_PROBLEM,
784 "Couldn't load match `%s'\n",
785 name);
786 } else if (tryload == LOAD_MUST_SUCCEED)
787 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +0000788 "Couldn't load match `%s':%s\n",
789 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +0000790 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000791#else
792 if (ptr && !ptr->loaded) {
793 if (tryload != DONT_LOAD)
794 ptr->loaded = 1;
795 else
796 ptr = NULL;
797 }
Marc Boucher067477b2002-03-24 15:09:31 +0000798 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
799 exit_error(PARAMETER_PROBLEM,
800 "Couldn't find match `%s'\n", name);
801 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000802#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +0000803
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000804 if (ptr && matches) {
805 struct ip6tables_rule_match **i;
806 struct ip6tables_rule_match *newentry;
807
808 newentry = fw_malloc(sizeof(struct ip6tables_rule_match));
809
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000810 for (i = matches; *i; i = &(*i)->next) {
811 if (strcmp(name, (*i)->match->name) == 0)
812 (*i)->completed = 1;
813 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000814 newentry->match = ptr;
Joszef Kadlecsika258ad72006-03-03 09:36:50 +0000815 newentry->completed = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000816 newentry->next = NULL;
817 *i = newentry;
818 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +0000819
Rusty Russell5eed48a2000-06-02 20:12:24 +0000820 return ptr;
821}
822
823/* Christophe Burki wants `-p 6' to imply `-m tcp'. */
824static struct ip6tables_match *
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000825find_proto(const char *pname, enum ip6t_tryload tryload, int nolookup, struct ip6tables_rule_match **matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000826{
Harald Welteed498492001-07-23 01:24:22 +0000827 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000828
Harald Welte43ac1912002-03-03 09:43:07 +0000829 if (string_to_number(pname, 0, 255, &proto) != -1) {
830 char *protoname = proto_to_name(proto, nolookup);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000831
Harald Welte43ac1912002-03-03 09:43:07 +0000832 if (protoname)
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000833 return find_match(protoname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000834 } else
Martin Josefsson69ac0e02004-02-02 20:02:10 +0000835 return find_match(pname, tryload, matches);
Harald Welte43ac1912002-03-03 09:43:07 +0000836
837 return NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000838}
839
Harald Welte43ac1912002-03-03 09:43:07 +0000840u_int16_t
Rusty Russell5eed48a2000-06-02 20:12:24 +0000841parse_protocol(const char *s)
842{
Harald Welteed498492001-07-23 01:24:22 +0000843 unsigned int proto;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000844
Harald Welteed498492001-07-23 01:24:22 +0000845 if (string_to_number(s, 0, 255, &proto) == -1) {
Rusty Russell5eed48a2000-06-02 20:12:24 +0000846 struct protoent *pent;
847
Harald Weltecbe1ec72006-02-11 09:50:11 +0000848 /* first deal with the special case of 'all' to prevent
849 * people from being able to redefine 'all' in nsswitch
850 * and/or provoke expensive [not working] ldap/nis/...
851 * lookups */
852 if (!strcmp(s, "all"))
853 return 0;
854
Rusty Russell5eed48a2000-06-02 20:12:24 +0000855 if ((pent = getprotobyname(s)))
856 proto = pent->p_proto;
857 else {
858 unsigned int i;
859 for (i = 0;
860 i < sizeof(chain_protos)/sizeof(struct pprot);
861 i++) {
862 if (strcmp(s, chain_protos[i].name) == 0) {
863 proto = chain_protos[i].num;
864 break;
865 }
866 }
867 if (i == sizeof(chain_protos)/sizeof(struct pprot))
868 exit_error(PARAMETER_PROBLEM,
869 "unknown protocol `%s' specified",
870 s);
871 }
872 }
873
874 return (u_int16_t)proto;
875}
876
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +0000877/* proto means IPv6 extension header ? */
878static int is_exthdr(u_int16_t proto)
879{
880 return (proto == IPPROTO_HOPOPTS ||
881 proto == IPPROTO_ROUTING ||
882 proto == IPPROTO_FRAGMENT ||
883 proto == IPPROTO_ESP ||
884 proto == IPPROTO_AH ||
885 proto == IPPROTO_DSTOPTS);
886}
887
Yasuyuki KOZAKAI9867e812005-06-22 12:24:21 +0000888void parse_interface(const char *arg, char *vianame, unsigned char *mask)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000889{
890 int vialen = strlen(arg);
891 unsigned int i;
892
893 memset(mask, 0, IFNAMSIZ);
894 memset(vianame, 0, IFNAMSIZ);
895
896 if (vialen + 1 > IFNAMSIZ)
897 exit_error(PARAMETER_PROBLEM,
898 "interface name `%s' must be shorter than IFNAMSIZ"
899 " (%i)", arg, IFNAMSIZ-1);
900
901 strcpy(vianame, arg);
Ozgur AKAN3610deb2004-04-07 09:36:29 +0000902 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+'))
Rusty Russell5eed48a2000-06-02 20:12:24 +0000903 memset(mask, 0, IFNAMSIZ);
904 else if (vianame[vialen - 1] == '+') {
905 memset(mask, 0xFF, vialen - 1);
906 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000907 /* Don't remove `+' here! -HW */
Rusty Russell5eed48a2000-06-02 20:12:24 +0000908 } else {
909 /* Include nul-terminator in match */
910 memset(mask, 0xFF, vialen + 1);
911 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1);
Harald Welte43ac1912002-03-03 09:43:07 +0000912 for (i = 0; vianame[i]; i++) {
913 if (!isalnum(vianame[i])
914 && vianame[i] != '_'
915 && vianame[i] != '.') {
916 printf("Warning: wierd character in interface"
917 " `%s' (No aliases, :, ! or *).\n",
918 vianame);
919 break;
920 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000921 }
922 }
923}
924
925/* Can't be zero. */
926static int
927parse_rulenumber(const char *rule)
928{
Harald Welte43ac1912002-03-03 09:43:07 +0000929 unsigned int rulenum;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000930
Harald Welte43ac1912002-03-03 09:43:07 +0000931 if (string_to_number(rule, 1, INT_MAX, &rulenum) == -1)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000932 exit_error(PARAMETER_PROBLEM,
933 "Invalid rule number `%s'", rule);
934
935 return rulenum;
936}
937
938static const char *
939parse_target(const char *targetname)
940{
941 const char *ptr;
942
943 if (strlen(targetname) < 1)
944 exit_error(PARAMETER_PROBLEM,
945 "Invalid target name (too short)");
946
947 if (strlen(targetname)+1 > sizeof(ip6t_chainlabel))
948 exit_error(PARAMETER_PROBLEM,
Martin Josefssona28d4952004-05-26 16:04:48 +0000949 "Invalid target name `%s' (%u chars max)",
950 targetname, (unsigned int)sizeof(ip6t_chainlabel)-1);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000951
952 for (ptr = targetname; *ptr; ptr++)
953 if (isspace(*ptr))
954 exit_error(PARAMETER_PROBLEM,
955 "Invalid target name `%s'", targetname);
956 return targetname;
957}
958
959int
Martin Josefssonb105bc92004-05-26 15:54:49 +0000960string_to_number_ll(const char *s, unsigned long long min, unsigned long long max,
961 unsigned long long *ret)
Rusty Russell5eed48a2000-06-02 20:12:24 +0000962{
Martin Josefssonb105bc92004-05-26 15:54:49 +0000963 unsigned long long number;
Rusty Russell5eed48a2000-06-02 20:12:24 +0000964 char *end;
965
966 /* Handle hex, octal, etc. */
Harald Welteed498492001-07-23 01:24:22 +0000967 errno = 0;
Martin Josefssonb105bc92004-05-26 15:54:49 +0000968 number = strtoull(s, &end, 0);
Rusty Russell5eed48a2000-06-02 20:12:24 +0000969 if (*end == '\0' && end != s) {
970 /* we parsed a number, let's see if we want this */
Martin Josefssonb105bc92004-05-26 15:54:49 +0000971 if (errno != ERANGE && min <= number && (!max || number <= max)) {
Harald Welteed498492001-07-23 01:24:22 +0000972 *ret = number;
973 return 0;
Harald Welte43ac1912002-03-03 09:43:07 +0000974 }
Rusty Russell5eed48a2000-06-02 20:12:24 +0000975 }
976 return -1;
977}
978
Martin Josefssonb105bc92004-05-26 15:54:49 +0000979int
980string_to_number_l(const char *s, unsigned long min, unsigned long max,
981 unsigned long *ret)
982{
983 int result;
984 unsigned long long number;
985
986 result = string_to_number_ll(s, min, max, &number);
987 *ret = (unsigned long)number;
988
989 return result;
990}
991
992int string_to_number(const char *s, unsigned int min, unsigned int max,
993 unsigned int *ret)
994{
995 int result;
996 unsigned long number;
997
998 result = string_to_number_l(s, min, max, &number);
999 *ret = (unsigned int)number;
1000
1001 return result;
1002}
1003
Rusty Russell5eed48a2000-06-02 20:12:24 +00001004static void
1005set_option(unsigned int *options, unsigned int option, u_int8_t *invflg,
1006 int invert)
1007{
1008 if (*options & option)
1009 exit_error(PARAMETER_PROBLEM, "multiple -%c flags not allowed",
1010 opt2char(option));
1011 *options |= option;
1012
1013 if (invert) {
1014 unsigned int i;
1015 for (i = 0; 1 << i != option; i++);
1016
1017 if (!inverse_for_options[i])
1018 exit_error(PARAMETER_PROBLEM,
1019 "cannot have ! before -%c",
1020 opt2char(option));
1021 *invflg |= inverse_for_options[i];
1022 }
1023}
1024
1025struct ip6tables_target *
1026find_target(const char *name, enum ip6t_tryload tryload)
1027{
1028 struct ip6tables_target *ptr;
1029
1030 /* Standard target? */
1031 if (strcmp(name, "") == 0
1032 || strcmp(name, IP6TC_LABEL_ACCEPT) == 0
1033 || strcmp(name, IP6TC_LABEL_DROP) == 0
1034 || strcmp(name, IP6TC_LABEL_QUEUE) == 0
1035 || strcmp(name, IP6TC_LABEL_RETURN) == 0)
1036 name = "standard";
1037
1038 for (ptr = ip6tables_targets; ptr; ptr = ptr->next) {
1039 if (strcmp(name, ptr->name) == 0)
1040 break;
1041 }
1042
Harald Welte3efb6ea2001-08-06 18:50:21 +00001043#ifndef NO_SHARED_LIBS
Jones Desougif5b86e62005-12-22 03:33:50 +00001044 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) {
Rusty Russell208d42e2004-12-20 05:29:52 +00001045 char path[strlen(lib_dir) + sizeof("/libip6t_.so")
Rusty Russell5eed48a2000-06-02 20:12:24 +00001046 + strlen(name)];
Rusty Russell208d42e2004-12-20 05:29:52 +00001047 sprintf(path, "%s/libip6t_%s.so", lib_dir, name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001048 if (dlopen(path, RTLD_NOW)) {
1049 /* Found library. If it didn't register itself,
1050 maybe they specified match as a target. */
1051 ptr = find_target(name, DONT_LOAD);
1052 if (!ptr)
1053 exit_error(PARAMETER_PROBLEM,
1054 "Couldn't load target `%s'\n",
1055 name);
1056 } else if (tryload == LOAD_MUST_SUCCEED)
1057 exit_error(PARAMETER_PROBLEM,
Harald Welte43ac1912002-03-03 09:43:07 +00001058 "Couldn't load target `%s':%s\n",
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001059 name, dlerror());
Rusty Russell5eed48a2000-06-02 20:12:24 +00001060 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001061#else
1062 if (ptr && !ptr->loaded) {
1063 if (tryload != DONT_LOAD)
1064 ptr->loaded = 1;
1065 else
1066 ptr = NULL;
1067 }
Marc Boucher067477b2002-03-24 15:09:31 +00001068 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) {
1069 exit_error(PARAMETER_PROBLEM,
1070 "Couldn't find target `%s'\n", name);
1071 }
Harald Welte3efb6ea2001-08-06 18:50:21 +00001072#endif
Rusty Russell5eed48a2000-06-02 20:12:24 +00001073
Harald Welte43ac1912002-03-03 09:43:07 +00001074 if (ptr)
1075 ptr->used = 1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001076
Rusty Russell5eed48a2000-06-02 20:12:24 +00001077 return ptr;
1078}
1079
1080static struct option *
Jan Echternachaf8fe9e2000-08-27 07:41:39 +00001081merge_options(struct option *oldopts, const struct option *newopts,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001082 unsigned int *option_offset)
1083{
1084 unsigned int num_old, num_new, i;
1085 struct option *merge;
1086
1087 for (num_old = 0; oldopts[num_old].name; num_old++);
1088 for (num_new = 0; newopts[num_new].name; num_new++);
1089
1090 global_option_offset += OPTION_OFFSET;
1091 *option_offset = global_option_offset;
1092
1093 merge = malloc(sizeof(struct option) * (num_new + num_old + 1));
1094 memcpy(merge, oldopts, num_old * sizeof(struct option));
Marcus Sundbergd91ed752005-07-29 13:26:35 +00001095 free_opts(0); /* Release previous options merged if any */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001096 for (i = 0; i < num_new; i++) {
1097 merge[num_old + i] = newopts[i];
1098 merge[num_old + i].val += *option_offset;
1099 }
1100 memset(merge + num_old + num_new, 0, sizeof(struct option));
1101
1102 return merge;
1103}
1104
1105void
1106register_match6(struct ip6tables_match *me)
1107{
Harald Welte43ac1912002-03-03 09:43:07 +00001108 struct ip6tables_match **i;
1109
Rusty Russell5eed48a2000-06-02 20:12:24 +00001110 if (strcmp(me->version, program_version) != 0) {
1111 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n",
1112 program_name, me->name, me->version, program_version);
1113 exit(1);
1114 }
1115
Jones Desougif5b86e62005-12-22 03:33:50 +00001116 if (find_match(me->name, DURING_LOAD, NULL)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001117 fprintf(stderr, "%s: match `%s' already registered.\n",
1118 program_name, me->name);
1119 exit(1);
1120 }
1121
Harald Welte43ac1912002-03-03 09:43:07 +00001122 if (me->size != IP6T_ALIGN(me->size)) {
1123 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001124 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001125 exit(1);
1126 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001127
Harald Welte43ac1912002-03-03 09:43:07 +00001128 /* Append to list. */
1129 for (i = &ip6tables_matches; *i; i = &(*i)->next);
1130 me->next = NULL;
1131 *i = me;
1132
Rusty Russell5eed48a2000-06-02 20:12:24 +00001133 me->m = NULL;
1134 me->mflags = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001135}
1136
1137void
1138register_target6(struct ip6tables_target *me)
1139{
1140 if (strcmp(me->version, program_version) != 0) {
1141 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n",
1142 program_name, me->name, me->version, program_version);
1143 exit(1);
1144 }
1145
Jones Desougif5b86e62005-12-22 03:33:50 +00001146 if (find_target(me->name, DURING_LOAD)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001147 fprintf(stderr, "%s: target `%s' already registered.\n",
1148 program_name, me->name);
1149 exit(1);
1150 }
1151
Harald Welte43ac1912002-03-03 09:43:07 +00001152 if (me->size != IP6T_ALIGN(me->size)) {
1153 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Martin Josefssona28d4952004-05-26 16:04:48 +00001154 program_name, me->name, (unsigned int)me->size);
Harald Welte43ac1912002-03-03 09:43:07 +00001155 exit(1);
1156 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001157
Rusty Russell5eed48a2000-06-02 20:12:24 +00001158 /* Prepend to list. */
1159 me->next = ip6tables_targets;
1160 ip6tables_targets = me;
1161 me->t = NULL;
1162 me->tflags = 0;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001163}
1164
1165static void
1166print_num(u_int64_t number, unsigned int format)
1167{
Harald Welte43ac1912002-03-03 09:43:07 +00001168 if (format & FMT_KILOMEGAGIGA) {
1169 if (number > 99999) {
1170 number = (number + 500) / 1000;
1171 if (number > 9999) {
1172 number = (number + 500) / 1000;
1173 if (number > 9999) {
1174 number = (number + 500) / 1000;
1175 if (number > 9999) {
1176 number = (number + 500) / 1000;
Martin Josefssona28d4952004-05-26 16:04:48 +00001177 printf(FMT("%4lluT ","%lluT "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001178 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001179 else printf(FMT("%4lluG ","%lluG "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001180 }
Martin Josefssona28d4952004-05-26 16:04:48 +00001181 else printf(FMT("%4lluM ","%lluM "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001182 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001183 printf(FMT("%4lluK ","%lluK "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001184 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001185 printf(FMT("%5llu ","%llu "), (unsigned long long)number);
Harald Welte43ac1912002-03-03 09:43:07 +00001186 } else
Martin Josefssona28d4952004-05-26 16:04:48 +00001187 printf(FMT("%8llu ","%llu "), (unsigned long long)number);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001188}
1189
Harald Welte43ac1912002-03-03 09:43:07 +00001190
Rusty Russell5eed48a2000-06-02 20:12:24 +00001191static void
1192print_header(unsigned int format, const char *chain, ip6tc_handle_t *handle)
1193{
1194 struct ip6t_counters counters;
1195 const char *pol = ip6tc_get_policy(chain, &counters, handle);
1196 printf("Chain %s", chain);
1197 if (pol) {
1198 printf(" (policy %s", pol);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001199 if (!(format & FMT_NOCOUNTS)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001200 fputc(' ', stdout);
1201 print_num(counters.pcnt, (format|FMT_NOTABLE));
1202 fputs("packets, ", stdout);
1203 print_num(counters.bcnt, (format|FMT_NOTABLE));
1204 fputs("bytes", stdout);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001205 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00001206 printf(")\n");
1207 } else {
1208 unsigned int refs;
1209 if (!ip6tc_get_references(&refs, chain, handle))
1210 printf(" (ERROR obtaining refs)\n");
1211 else
1212 printf(" (%u references)\n", refs);
1213 }
1214
1215 if (format & FMT_LINENUMBERS)
1216 printf(FMT("%-4s ", "%s "), "num");
1217 if (!(format & FMT_NOCOUNTS)) {
1218 if (format & FMT_KILOMEGAGIGA) {
1219 printf(FMT("%5s ","%s "), "pkts");
1220 printf(FMT("%5s ","%s "), "bytes");
1221 } else {
1222 printf(FMT("%8s ","%s "), "pkts");
1223 printf(FMT("%10s ","%s "), "bytes");
1224 }
1225 }
1226 if (!(format & FMT_NOTARGET))
1227 printf(FMT("%-9s ","%s "), "target");
1228 fputs(" prot ", stdout);
1229 if (format & FMT_OPTIONS)
1230 fputs("opt", stdout);
1231 if (format & FMT_VIA) {
1232 printf(FMT(" %-6s ","%s "), "in");
1233 printf(FMT("%-6s ","%s "), "out");
1234 }
1235 printf(FMT(" %-19s ","%s "), "source");
1236 printf(FMT(" %-19s "," %s "), "destination");
1237 printf("\n");
1238}
1239
Harald Welte43ac1912002-03-03 09:43:07 +00001240
Rusty Russell5eed48a2000-06-02 20:12:24 +00001241static int
1242print_match(const struct ip6t_entry_match *m,
1243 const struct ip6t_ip6 *ip,
1244 int numeric)
1245{
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001246 struct ip6tables_match *match = find_match(m->u.user.name, TRY_LOAD, NULL);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001247
1248 if (match) {
1249 if (match->print)
1250 match->print(ip, m, numeric);
Harald Welte43ac1912002-03-03 09:43:07 +00001251 else
1252 printf("%s ", match->name);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001253 } else {
1254 if (m->u.user.name[0])
1255 printf("UNKNOWN match `%s' ", m->u.user.name);
1256 }
1257 /* Don't stop iterating. */
1258 return 0;
1259}
1260
1261/* e is called `fw' here for hysterical raisins */
1262static void
1263print_firewall(const struct ip6t_entry *fw,
1264 const char *targname,
1265 unsigned int num,
1266 unsigned int format,
1267 const ip6tc_handle_t handle)
1268{
1269 struct ip6tables_target *target = NULL;
1270 const struct ip6t_entry_target *t;
1271 u_int8_t flags;
1272 char buf[BUFSIZ];
1273
Rusty Russell5eed48a2000-06-02 20:12:24 +00001274 if (!ip6tc_is_chain(targname, handle))
1275 target = find_target(targname, TRY_LOAD);
1276 else
1277 target = find_target(IP6T_STANDARD_TARGET, LOAD_MUST_SUCCEED);
1278
1279 t = ip6t_get_target((struct ip6t_entry *)fw);
1280 flags = fw->ipv6.flags;
1281
1282 if (format & FMT_LINENUMBERS)
1283 printf(FMT("%-4u ", "%u "), num+1);
1284
1285 if (!(format & FMT_NOCOUNTS)) {
1286 print_num(fw->counters.pcnt, format);
1287 print_num(fw->counters.bcnt, format);
1288 }
1289
1290 if (!(format & FMT_NOTARGET))
1291 printf(FMT("%-9s ", "%s "), targname);
1292
1293 fputc(fw->ipv6.invflags & IP6T_INV_PROTO ? '!' : ' ', stdout);
1294 {
Harald Welte43ac1912002-03-03 09:43:07 +00001295 char *pname = proto_to_name(fw->ipv6.proto, format&FMT_NUMERIC);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001296 if (pname)
1297 printf(FMT("%-5s", "%s "), pname);
1298 else
1299 printf(FMT("%-5hu", "%hu "), fw->ipv6.proto);
1300 }
1301
1302 if (format & FMT_OPTIONS) {
1303 if (format & FMT_NOTABLE)
1304 fputs("opt ", stdout);
Harald Welte43ac1912002-03-03 09:43:07 +00001305 fputc(' ', stdout); /* Invert flag of FRAG */
1306 fputc(' ', stdout); /* -f */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001307 fputc(' ', stdout);
1308 }
1309
1310 if (format & FMT_VIA) {
1311 char iface[IFNAMSIZ+2];
1312
1313 if (fw->ipv6.invflags & IP6T_INV_VIA_IN) {
1314 iface[0] = '!';
1315 iface[1] = '\0';
1316 }
1317 else iface[0] = '\0';
1318
1319 if (fw->ipv6.iniface[0] != '\0') {
1320 strcat(iface, fw->ipv6.iniface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001321 }
1322 else if (format & FMT_NUMERIC) strcat(iface, "*");
1323 else strcat(iface, "any");
1324 printf(FMT(" %-6s ","in %s "), iface);
1325
1326 if (fw->ipv6.invflags & IP6T_INV_VIA_OUT) {
1327 iface[0] = '!';
1328 iface[1] = '\0';
1329 }
1330 else iface[0] = '\0';
1331
1332 if (fw->ipv6.outiface[0] != '\0') {
1333 strcat(iface, fw->ipv6.outiface);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001334 }
1335 else if (format & FMT_NUMERIC) strcat(iface, "*");
1336 else strcat(iface, "any");
1337 printf(FMT("%-6s ","out %s "), iface);
1338 }
1339
1340 fputc(fw->ipv6.invflags & IP6T_INV_SRCIP ? '!' : ' ', stdout);
1341 if (!memcmp(&fw->ipv6.smsk, &in6addr_any, sizeof in6addr_any)
1342 && !(format & FMT_NUMERIC))
1343 printf(FMT("%-19s ","%s "), "anywhere");
1344 else {
1345 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001346 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001347 else
Harald Welte43ac1912002-03-03 09:43:07 +00001348 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.src)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001349 strcat(buf, mask_to_numeric(&(fw->ipv6.smsk)));
1350 printf(FMT("%-19s ","%s "), buf);
1351 }
1352
1353 fputc(fw->ipv6.invflags & IP6T_INV_DSTIP ? '!' : ' ', stdout);
1354 if (!memcmp(&fw->ipv6.dmsk, &in6addr_any, sizeof in6addr_any)
1355 && !(format & FMT_NUMERIC))
1356 printf(FMT("%-19s","-> %s"), "anywhere");
1357 else {
1358 if (format & FMT_NUMERIC)
Harald Welte43ac1912002-03-03 09:43:07 +00001359 sprintf(buf, "%s", addr_to_numeric(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001360 else
Harald Welte43ac1912002-03-03 09:43:07 +00001361 sprintf(buf, "%s", addr_to_anyname(&(fw->ipv6.dst)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001362 strcat(buf, mask_to_numeric(&(fw->ipv6.dmsk)));
1363 printf(FMT("%-19s","-> %s"), buf);
1364 }
1365
1366 if (format & FMT_NOTABLE)
1367 fputs(" ", stdout);
1368
1369 IP6T_MATCH_ITERATE(fw, print_match, &fw->ipv6, format & FMT_NUMERIC);
1370
1371 if (target) {
1372 if (target->print)
1373 /* Print the target information. */
1374 target->print(&fw->ipv6, t, format & FMT_NUMERIC);
1375 } else if (t->u.target_size != sizeof(*t))
1376 printf("[%u bytes of unknown target data] ",
Martin Josefssona28d4952004-05-26 16:04:48 +00001377 (unsigned int)(t->u.target_size - sizeof(*t)));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001378
1379 if (!(format & FMT_NONEWLINE))
1380 fputc('\n', stdout);
1381}
1382
1383static void
1384print_firewall_line(const struct ip6t_entry *fw,
1385 const ip6tc_handle_t h)
1386{
1387 struct ip6t_entry_target *t;
1388
1389 t = ip6t_get_target((struct ip6t_entry *)fw);
1390 print_firewall(fw, t->u.user.name, 0, FMT_PRINT_RULE, h);
1391}
1392
1393static int
1394append_entry(const ip6t_chainlabel chain,
1395 struct ip6t_entry *fw,
1396 unsigned int nsaddrs,
1397 const struct in6_addr saddrs[],
1398 unsigned int ndaddrs,
1399 const struct in6_addr daddrs[],
1400 int verbose,
1401 ip6tc_handle_t *handle)
1402{
1403 unsigned int i, j;
1404 int ret = 1;
1405
1406 for (i = 0; i < nsaddrs; i++) {
1407 fw->ipv6.src = saddrs[i];
1408 for (j = 0; j < ndaddrs; j++) {
1409 fw->ipv6.dst = daddrs[j];
1410 if (verbose)
1411 print_firewall_line(fw, *handle);
1412 ret &= ip6tc_append_entry(chain, fw, handle);
1413 }
1414 }
1415
1416 return ret;
1417}
1418
1419static int
1420replace_entry(const ip6t_chainlabel chain,
1421 struct ip6t_entry *fw,
1422 unsigned int rulenum,
1423 const struct in6_addr *saddr,
1424 const struct in6_addr *daddr,
1425 int verbose,
1426 ip6tc_handle_t *handle)
1427{
1428 fw->ipv6.src = *saddr;
1429 fw->ipv6.dst = *daddr;
1430
1431 if (verbose)
1432 print_firewall_line(fw, *handle);
1433 return ip6tc_replace_entry(chain, fw, rulenum, handle);
1434}
1435
1436static int
1437insert_entry(const ip6t_chainlabel chain,
1438 struct ip6t_entry *fw,
1439 unsigned int rulenum,
1440 unsigned int nsaddrs,
1441 const struct in6_addr saddrs[],
1442 unsigned int ndaddrs,
1443 const struct in6_addr daddrs[],
1444 int verbose,
1445 ip6tc_handle_t *handle)
1446{
1447 unsigned int i, j;
1448 int ret = 1;
1449
1450 for (i = 0; i < nsaddrs; i++) {
1451 fw->ipv6.src = saddrs[i];
1452 for (j = 0; j < ndaddrs; j++) {
1453 fw->ipv6.dst = daddrs[j];
1454 if (verbose)
1455 print_firewall_line(fw, *handle);
1456 ret &= ip6tc_insert_entry(chain, fw, rulenum, handle);
1457 }
1458 }
1459
1460 return ret;
1461}
1462
1463static unsigned char *
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001464make_delete_mask(struct ip6t_entry *fw, struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001465{
1466 /* Establish mask for comparison */
1467 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001468 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001469 unsigned char *mask, *mptr;
1470
1471 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001472 for (matchp = matches; matchp; matchp = matchp->next)
1473 size += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001474
1475 mask = fw_calloc(1, size
Harald Welte43ac1912002-03-03 09:43:07 +00001476 + IP6T_ALIGN(sizeof(struct ip6t_entry_target))
Rusty Russell5eed48a2000-06-02 20:12:24 +00001477 + ip6tables_targets->size);
1478
1479 memset(mask, 0xFF, sizeof(struct ip6t_entry));
1480 mptr = mask + sizeof(struct ip6t_entry);
1481
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001482 for (matchp = matches; matchp; matchp = matchp->next) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00001483 memset(mptr, 0xFF,
Harald Welte43ac1912002-03-03 09:43:07 +00001484 IP6T_ALIGN(sizeof(struct ip6t_entry_match))
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001485 + matchp->match->userspacesize);
1486 mptr += IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + matchp->match->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001487 }
1488
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001489 memset(mptr, 0xFF,
1490 IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1491 + ip6tables_targets->userspacesize);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001492
1493 return mask;
1494}
1495
1496static int
1497delete_entry(const ip6t_chainlabel chain,
1498 struct ip6t_entry *fw,
1499 unsigned int nsaddrs,
1500 const struct in6_addr saddrs[],
1501 unsigned int ndaddrs,
1502 const struct in6_addr daddrs[],
1503 int verbose,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001504 ip6tc_handle_t *handle,
1505 struct ip6tables_rule_match *matches)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001506{
1507 unsigned int i, j;
1508 int ret = 1;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001509 unsigned char *mask;
1510
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001511 mask = make_delete_mask(fw, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001512 for (i = 0; i < nsaddrs; i++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001513 fw->ipv6.src = saddrs[i];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001514 for (j = 0; j < ndaddrs; j++) {
Philip Blundell57e07af2000-06-04 17:25:33 +00001515 fw->ipv6.dst = daddrs[j];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001516 if (verbose)
1517 print_firewall_line(fw, *handle);
Philip Blundell57e07af2000-06-04 17:25:33 +00001518 ret &= ip6tc_delete_entry(chain, fw, mask, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001519 }
1520 }
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001521 free(mask);
1522
Rusty Russell5eed48a2000-06-02 20:12:24 +00001523 return ret;
1524}
1525
András Kis-Szabó764316a2001-02-26 17:31:20 +00001526int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001527for_each_chain(int (*fn)(const ip6t_chainlabel, int, ip6tc_handle_t *),
1528 int verbose, int builtinstoo, ip6tc_handle_t *handle)
1529{
1530 int ret = 1;
1531 const char *chain;
1532 char *chains;
1533 unsigned int i, chaincount = 0;
1534
1535 chain = ip6tc_first_chain(handle);
1536 while (chain) {
1537 chaincount++;
1538 chain = ip6tc_next_chain(handle);
1539 }
1540
1541 chains = fw_malloc(sizeof(ip6t_chainlabel) * chaincount);
1542 i = 0;
1543 chain = ip6tc_first_chain(handle);
1544 while (chain) {
1545 strcpy(chains + i*sizeof(ip6t_chainlabel), chain);
1546 i++;
1547 chain = ip6tc_next_chain(handle);
1548 }
1549
1550 for (i = 0; i < chaincount; i++) {
1551 if (!builtinstoo
1552 && ip6tc_builtin(chains + i*sizeof(ip6t_chainlabel),
Harald Weltedf1c71c2004-08-30 16:00:32 +00001553 *handle) == 1)
Rusty Russell5eed48a2000-06-02 20:12:24 +00001554 continue;
1555 ret &= fn(chains + i*sizeof(ip6t_chainlabel), verbose, handle);
1556 }
1557
1558 free(chains);
1559 return ret;
1560}
1561
András Kis-Szabó764316a2001-02-26 17:31:20 +00001562int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001563flush_entries(const ip6t_chainlabel chain, int verbose,
1564 ip6tc_handle_t *handle)
1565{
1566 if (!chain)
1567 return for_each_chain(flush_entries, verbose, 1, handle);
1568
1569 if (verbose)
1570 fprintf(stdout, "Flushing chain `%s'\n", chain);
1571 return ip6tc_flush_entries(chain, handle);
1572}
1573
1574static int
1575zero_entries(const ip6t_chainlabel chain, int verbose,
1576 ip6tc_handle_t *handle)
1577{
1578 if (!chain)
1579 return for_each_chain(zero_entries, verbose, 1, handle);
1580
1581 if (verbose)
1582 fprintf(stdout, "Zeroing chain `%s'\n", chain);
1583 return ip6tc_zero_entries(chain, handle);
1584}
1585
András Kis-Szabó764316a2001-02-26 17:31:20 +00001586int
Rusty Russell5eed48a2000-06-02 20:12:24 +00001587delete_chain(const ip6t_chainlabel chain, int verbose,
1588 ip6tc_handle_t *handle)
1589{
1590 if (!chain)
1591 return for_each_chain(delete_chain, verbose, 0, handle);
1592
1593 if (verbose)
1594 fprintf(stdout, "Deleting chain `%s'\n", chain);
1595 return ip6tc_delete_chain(chain, handle);
1596}
1597
1598static int
1599list_entries(const ip6t_chainlabel chain, int verbose, int numeric,
1600 int expanded, int linenumbers, ip6tc_handle_t *handle)
1601{
1602 int found = 0;
1603 unsigned int format;
1604 const char *this;
1605
1606 format = FMT_OPTIONS;
1607 if (!verbose)
1608 format |= FMT_NOCOUNTS;
1609 else
1610 format |= FMT_VIA;
1611
1612 if (numeric)
1613 format |= FMT_NUMERIC;
1614
1615 if (!expanded)
1616 format |= FMT_KILOMEGAGIGA;
1617
1618 if (linenumbers)
1619 format |= FMT_LINENUMBERS;
1620
1621 for (this = ip6tc_first_chain(handle);
1622 this;
1623 this = ip6tc_next_chain(handle)) {
1624 const struct ip6t_entry *i;
1625 unsigned int num;
1626
1627 if (chain && strcmp(chain, this) != 0)
1628 continue;
1629
1630 if (found) printf("\n");
1631
1632 print_header(format, this, handle);
1633 i = ip6tc_first_rule(this, handle);
1634
1635 num = 0;
1636 while (i) {
1637 print_firewall(i,
1638 ip6tc_get_target(i, handle),
1639 num++,
1640 format,
1641 *handle);
1642 i = ip6tc_next_rule(i, handle);
1643 }
1644 found = 1;
1645 }
1646
1647 errno = ENOENT;
1648 return found;
1649}
1650
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001651static char *get_modprobe(void)
1652{
Harald Welte43ac1912002-03-03 09:43:07 +00001653 int procfile;
1654 char *ret;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001655
Harald Welte10f7f142004-10-22 08:14:07 +00001656#define PROCFILE_BUFSIZ 1024
Harald Welte43ac1912002-03-03 09:43:07 +00001657 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
1658 if (procfile < 0)
1659 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001660
Harald Welte10f7f142004-10-22 08:14:07 +00001661 ret = malloc(PROCFILE_BUFSIZ);
Harald Welte43ac1912002-03-03 09:43:07 +00001662 if (ret) {
Harald Welte10f7f142004-10-22 08:14:07 +00001663 memset(ret, 0, PROCFILE_BUFSIZ);
1664 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
Harald Welte43ac1912002-03-03 09:43:07 +00001665 case -1: goto fail;
Harald Welte10f7f142004-10-22 08:14:07 +00001666 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
Harald Welte43ac1912002-03-03 09:43:07 +00001667 }
1668 if (ret[strlen(ret)-1]=='\n')
1669 ret[strlen(ret)-1]=0;
1670 close(procfile);
1671 return ret;
1672 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001673 fail:
Harald Welte43ac1912002-03-03 09:43:07 +00001674 free(ret);
1675 close(procfile);
1676 return NULL;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001677}
1678
Harald Welte58918652001-06-16 18:25:25 +00001679int ip6tables_insmod(const char *modname, const char *modprobe)
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001680{
Harald Welte43ac1912002-03-03 09:43:07 +00001681 char *buf = NULL;
1682 char *argv[3];
Rusty Russell8beb0492004-12-22 00:37:10 +00001683 int status;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001684
Harald Welte43ac1912002-03-03 09:43:07 +00001685 /* If they don't explicitly set it, read out of kernel */
1686 if (!modprobe) {
1687 buf = get_modprobe();
1688 if (!buf)
1689 return -1;
1690 modprobe = buf;
1691 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001692
Harald Welte43ac1912002-03-03 09:43:07 +00001693 switch (fork()) {
1694 case 0:
1695 argv[0] = (char *)modprobe;
1696 argv[1] = (char *)modname;
1697 argv[2] = NULL;
1698 execv(argv[0], argv);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001699
Harald Welte43ac1912002-03-03 09:43:07 +00001700 /* not usually reached */
Rusty Russell8beb0492004-12-22 00:37:10 +00001701 exit(1);
Harald Welte43ac1912002-03-03 09:43:07 +00001702 case -1:
1703 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001704
Harald Welte43ac1912002-03-03 09:43:07 +00001705 default: /* parent */
Rusty Russell8beb0492004-12-22 00:37:10 +00001706 wait(&status);
Harald Welte43ac1912002-03-03 09:43:07 +00001707 }
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001708
Harald Welte43ac1912002-03-03 09:43:07 +00001709 free(buf);
Rusty Russell8beb0492004-12-22 00:37:10 +00001710 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
1711 return 0;
1712 return -1;
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00001713}
1714
Rusty Russell5eed48a2000-06-02 20:12:24 +00001715static struct ip6t_entry *
1716generate_entry(const struct ip6t_entry *fw,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001717 struct ip6tables_rule_match *matches,
Rusty Russell5eed48a2000-06-02 20:12:24 +00001718 struct ip6t_entry_target *target)
1719{
1720 unsigned int size;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001721 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001722 struct ip6t_entry *e;
1723
1724 size = sizeof(struct ip6t_entry);
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001725 for (matchp = matches; matchp; matchp = matchp->next)
1726 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001727
1728 e = fw_malloc(size + target->u.target_size);
1729 *e = *fw;
1730 e->target_offset = size;
1731 e->next_offset = size + target->u.target_size;
1732
1733 size = 0;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001734 for (matchp = matches; matchp; matchp = matchp->next) {
1735 memcpy(e->elems + size, matchp->match->m, matchp->match->m->u.match_size);
1736 size += matchp->match->m->u.match_size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001737 }
1738 memcpy(e->elems + size, target, target->u.target_size);
1739
1740 return e;
1741}
1742
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001743void clear_rule_matches(struct ip6tables_rule_match **matches)
1744{
1745 struct ip6tables_rule_match *matchp, *tmp;
1746
1747 for (matchp = *matches; matchp;) {
1748 tmp = matchp->next;
Harald Welted6bc6082006-02-11 09:34:16 +00001749 if (matchp->match->m) {
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00001750 free(matchp->match->m);
Harald Welted6bc6082006-02-11 09:34:16 +00001751 matchp->match->m = NULL;
1752 }
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00001753 if (matchp->match == matchp->match->next) {
1754 free(matchp->match);
1755 matchp->match = NULL;
1756 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001757 free(matchp);
1758 matchp = tmp;
1759 }
1760
1761 *matches = NULL;
1762}
1763
Rusty Russell5eed48a2000-06-02 20:12:24 +00001764int do_command6(int argc, char *argv[], char **table, ip6tc_handle_t *handle)
1765{
1766 struct ip6t_entry fw, *e = NULL;
1767 int invert = 0;
1768 unsigned int nsaddrs = 0, ndaddrs = 0;
1769 struct in6_addr *saddrs = NULL, *daddrs = NULL;
1770
1771 int c, verbose = 0;
1772 const char *chain = NULL;
1773 const char *shostnetworkmask = NULL, *dhostnetworkmask = NULL;
1774 const char *policy = NULL, *newname = NULL;
1775 unsigned int rulenum = 0, options = 0, command = 0;
Harald Welte43ac1912002-03-03 09:43:07 +00001776 const char *pcnt = NULL, *bcnt = NULL;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001777 int ret = 1;
1778 struct ip6tables_match *m;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001779 struct ip6tables_rule_match *matches = NULL;
1780 struct ip6tables_rule_match *matchp;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001781 struct ip6tables_target *target = NULL;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001782 struct ip6tables_target *t;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001783 const char *jumpto = "";
1784 char *protocol = NULL;
Harald Welte43ac1912002-03-03 09:43:07 +00001785 const char *modprobe = NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00001786 int proto_used = 0;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001787
1788 memset(&fw, 0, sizeof(fw));
1789
Harald Welte43ac1912002-03-03 09:43:07 +00001790 /* re-set optind to 0 in case do_command gets called
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001791 * a second time */
1792 optind = 0;
1793
Harald Welte43ac1912002-03-03 09:43:07 +00001794 /* clear mflags in case do_command gets called a second time
1795 * (we clear the global list of all matches for security)*/
Martin Josefsson69ac0e02004-02-02 20:02:10 +00001796 for (m = ip6tables_matches; m; m = m->next)
Harald Welte43ac1912002-03-03 09:43:07 +00001797 m->mflags = 0;
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001798
Harald Welte43ac1912002-03-03 09:43:07 +00001799 for (t = ip6tables_targets; t; t = t->next) {
1800 t->tflags = 0;
1801 t->used = 0;
1802 }
András Kis-Szabó3aa62872001-05-03 01:01:41 +00001803
Rusty Russell5eed48a2000-06-02 20:12:24 +00001804 /* Suppress error messages: we may add new options if we
1805 demand-load a protocol. */
1806 opterr = 0;
1807
1808 while ((c = getopt_long(argc, argv,
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00001809 "-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 +00001810 opts, NULL)) != -1) {
1811 switch (c) {
1812 /*
1813 * Command selection
1814 */
1815 case 'A':
1816 add_command(&command, CMD_APPEND, CMD_NONE,
1817 invert);
1818 chain = optarg;
1819 break;
1820
1821 case 'D':
1822 add_command(&command, CMD_DELETE, CMD_NONE,
1823 invert);
1824 chain = optarg;
1825 if (optind < argc && argv[optind][0] != '-'
1826 && argv[optind][0] != '!') {
1827 rulenum = parse_rulenumber(argv[optind++]);
1828 command = CMD_DELETE_NUM;
1829 }
1830 break;
1831
Rusty Russell5eed48a2000-06-02 20:12:24 +00001832 case 'R':
1833 add_command(&command, CMD_REPLACE, CMD_NONE,
1834 invert);
1835 chain = optarg;
1836 if (optind < argc && argv[optind][0] != '-'
1837 && argv[optind][0] != '!')
1838 rulenum = parse_rulenumber(argv[optind++]);
1839 else
1840 exit_error(PARAMETER_PROBLEM,
1841 "-%c requires a rule number",
1842 cmd2char(CMD_REPLACE));
1843 break;
1844
1845 case 'I':
1846 add_command(&command, CMD_INSERT, CMD_NONE,
1847 invert);
1848 chain = optarg;
1849 if (optind < argc && argv[optind][0] != '-'
1850 && argv[optind][0] != '!')
1851 rulenum = parse_rulenumber(argv[optind++]);
1852 else rulenum = 1;
1853 break;
1854
1855 case 'L':
1856 add_command(&command, CMD_LIST, CMD_ZERO,
1857 invert);
1858 if (optarg) chain = optarg;
1859 else if (optind < argc && argv[optind][0] != '-'
1860 && argv[optind][0] != '!')
1861 chain = argv[optind++];
1862 break;
1863
1864 case 'F':
1865 add_command(&command, CMD_FLUSH, CMD_NONE,
1866 invert);
1867 if (optarg) chain = optarg;
1868 else if (optind < argc && argv[optind][0] != '-'
1869 && argv[optind][0] != '!')
1870 chain = argv[optind++];
1871 break;
1872
1873 case 'Z':
1874 add_command(&command, CMD_ZERO, CMD_LIST,
1875 invert);
1876 if (optarg) chain = optarg;
1877 else if (optind < argc && argv[optind][0] != '-'
1878 && argv[optind][0] != '!')
1879 chain = argv[optind++];
1880 break;
1881
1882 case 'N':
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001883 if (optarg && (*optarg == '-' || *optarg == '!'))
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001884 exit_error(PARAMETER_PROBLEM,
1885 "chain name not allowed to start "
Yasuyuki KOZAKAI8d8c8ea2005-06-13 01:06:10 +00001886 "with `%c'\n", *optarg);
Joszef Kadlecsik08f15272002-06-24 12:37:29 +00001887 if (find_target(optarg, TRY_LOAD))
1888 exit_error(PARAMETER_PROBLEM,
1889 "chain name may not clash "
1890 "with target name\n");
Rusty Russell5eed48a2000-06-02 20:12:24 +00001891 add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
1892 invert);
1893 chain = optarg;
1894 break;
1895
1896 case 'X':
1897 add_command(&command, CMD_DELETE_CHAIN, CMD_NONE,
1898 invert);
1899 if (optarg) chain = optarg;
1900 else if (optind < argc && argv[optind][0] != '-'
1901 && argv[optind][0] != '!')
1902 chain = argv[optind++];
1903 break;
1904
1905 case 'E':
1906 add_command(&command, CMD_RENAME_CHAIN, CMD_NONE,
1907 invert);
1908 chain = optarg;
1909 if (optind < argc && argv[optind][0] != '-'
1910 && argv[optind][0] != '!')
1911 newname = argv[optind++];
M.P.Anand Babuc9f20d32000-06-09 09:22:38 +00001912 else
1913 exit_error(PARAMETER_PROBLEM,
1914 "-%c requires old-chain-name and "
1915 "new-chain-name",
Harald Welte43ac1912002-03-03 09:43:07 +00001916 cmd2char(CMD_RENAME_CHAIN));
Rusty Russell5eed48a2000-06-02 20:12:24 +00001917 break;
1918
1919 case 'P':
1920 add_command(&command, CMD_SET_POLICY, CMD_NONE,
1921 invert);
1922 chain = optarg;
1923 if (optind < argc && argv[optind][0] != '-'
1924 && argv[optind][0] != '!')
1925 policy = argv[optind++];
1926 else
1927 exit_error(PARAMETER_PROBLEM,
1928 "-%c requires a chain and a policy",
1929 cmd2char(CMD_SET_POLICY));
1930 break;
1931
1932 case 'h':
1933 if (!optarg)
1934 optarg = argv[optind];
1935
Jonas Berlin1b91e592005-04-01 06:58:38 +00001936 /* ip6tables -p icmp -h */
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001937 if (!matches && protocol)
1938 find_match(protocol, TRY_LOAD, &matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001939
Martin Josefsson66aea6f2004-05-26 15:41:54 +00001940 exit_printhelp(matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001941
1942 /*
1943 * Option selection
1944 */
1945 case 'p':
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_PROTOCOL, &fw.ipv6.invflags,
1948 invert);
1949
1950 /* Canonicalize into lower case */
1951 for (protocol = argv[optind-1]; *protocol; protocol++)
1952 *protocol = tolower(*protocol);
1953
1954 protocol = argv[optind-1];
1955 fw.ipv6.proto = parse_protocol(protocol);
1956 fw.ipv6.flags |= IP6T_F_PROTO;
1957
1958 if (fw.ipv6.proto == 0
1959 && (fw.ipv6.invflags & IP6T_INV_PROTO))
1960 exit_error(PARAMETER_PROBLEM,
1961 "rule would never match protocol");
Yasuyuki KOZAKAI78716a92006-03-29 09:24:43 +00001962
1963 if (fw.ipv6.proto != IPPROTO_ESP &&
1964 is_exthdr(fw.ipv6.proto))
1965 printf("Warning: never matched protocol: %s. "
1966 "use exension match instead.", protocol);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001967 break;
1968
1969 case 's':
Harald Welteb77f1da2002-03-14 11:35:58 +00001970 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001971 set_option(&options, OPT_SOURCE, &fw.ipv6.invflags,
1972 invert);
1973 shostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001974 break;
1975
1976 case 'd':
Harald Welteb77f1da2002-03-14 11:35:58 +00001977 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00001978 set_option(&options, OPT_DESTINATION, &fw.ipv6.invflags,
1979 invert);
1980 dhostnetworkmask = argv[optind-1];
Rusty Russell5eed48a2000-06-02 20:12:24 +00001981 break;
1982
1983 case 'j':
1984 set_option(&options, OPT_JUMP, &fw.ipv6.invflags,
1985 invert);
1986 jumpto = parse_target(optarg);
Harald Welte43ac1912002-03-03 09:43:07 +00001987 /* TRY_LOAD (may be chain name) */
Rusty Russell5eed48a2000-06-02 20:12:24 +00001988 target = find_target(jumpto, TRY_LOAD);
1989
1990 if (target) {
1991 size_t size;
1992
Harald Welte43ac1912002-03-03 09:43:07 +00001993 size = IP6T_ALIGN(sizeof(struct ip6t_entry_target))
1994 + target->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00001995
1996 target->t = fw_calloc(1, size);
1997 target->t->u.target_size = size;
1998 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00001999 if (target->init != NULL)
2000 target->init(target->t, &fw.nfcache);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002001 opts = merge_options(opts, target->extra_opts, &target->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002002 }
2003 break;
2004
2005
2006 case 'i':
Harald Welteb77f1da2002-03-14 11:35:58 +00002007 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002008 set_option(&options, OPT_VIANAMEIN, &fw.ipv6.invflags,
2009 invert);
2010 parse_interface(argv[optind-1],
2011 fw.ipv6.iniface,
2012 fw.ipv6.iniface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002013 break;
2014
2015 case 'o':
Harald Welteb77f1da2002-03-14 11:35:58 +00002016 check_inverse(optarg, &invert, &optind, argc);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002017 set_option(&options, OPT_VIANAMEOUT, &fw.ipv6.invflags,
2018 invert);
2019 parse_interface(argv[optind-1],
2020 fw.ipv6.outiface,
2021 fw.ipv6.outiface_mask);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002022 break;
2023
2024 case 'v':
2025 if (!verbose)
2026 set_option(&options, OPT_VERBOSE,
2027 &fw.ipv6.invflags, invert);
2028 verbose++;
2029 break;
2030
2031 case 'm': {
2032 size_t size;
2033
2034 if (invert)
2035 exit_error(PARAMETER_PROBLEM,
2036 "unexpected ! flag before --match");
2037
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002038 m = find_match(optarg, LOAD_MUST_SUCCEED, &matches);
Harald Welte43ac1912002-03-03 09:43:07 +00002039 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2040 + m->size;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002041 m->m = fw_calloc(1, size);
2042 m->m->u.match_size = size;
2043 strcpy(m->m->u.user.name, m->name);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002044 if (m->init != NULL)
2045 m->init(m->m, &fw.nfcache);
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002046 if (m != m->next)
2047 /* Merge options for non-cloned matches */
2048 opts = merge_options(opts, m->extra_opts, &m->option_offset);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002049 }
2050 break;
2051
2052 case 'n':
2053 set_option(&options, OPT_NUMERIC, &fw.ipv6.invflags,
2054 invert);
2055 break;
2056
2057 case 't':
2058 if (invert)
2059 exit_error(PARAMETER_PROBLEM,
2060 "unexpected ! flag before --table");
2061 *table = argv[optind-1];
2062 break;
2063
2064 case 'x':
2065 set_option(&options, OPT_EXPANDED, &fw.ipv6.invflags,
2066 invert);
2067 break;
2068
2069 case 'V':
2070 if (invert)
2071 printf("Not %s ;-)\n", program_version);
2072 else
2073 printf("%s v%s\n",
2074 program_name, program_version);
2075 exit(0);
2076
2077 case '0':
2078 set_option(&options, OPT_LINENUMBERS, &fw.ipv6.invflags,
2079 invert);
2080 break;
2081
Harald Welte43ac1912002-03-03 09:43:07 +00002082 case 'M':
2083 modprobe = optarg;
2084 break;
2085
2086 case 'c':
2087
2088 set_option(&options, OPT_COUNTERS, &fw.ipv6.invflags,
2089 invert);
2090 pcnt = optarg;
2091 if (optind < argc && argv[optind][0] != '-'
2092 && argv[optind][0] != '!')
2093 bcnt = argv[optind++];
2094 else
2095 exit_error(PARAMETER_PROBLEM,
2096 "-%c requires packet and byte counter",
2097 opt2char(OPT_COUNTERS));
2098
Martin Josefssona28d4952004-05-26 16:04:48 +00002099 if (sscanf(pcnt, "%llu", (unsigned long long *)&fw.counters.pcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002100 exit_error(PARAMETER_PROBLEM,
2101 "-%c packet counter not numeric",
2102 opt2char(OPT_COUNTERS));
2103
Martin Josefssona28d4952004-05-26 16:04:48 +00002104 if (sscanf(bcnt, "%llu", (unsigned long long *)&fw.counters.bcnt) != 1)
Harald Welte43ac1912002-03-03 09:43:07 +00002105 exit_error(PARAMETER_PROBLEM,
2106 "-%c byte counter not numeric",
2107 opt2char(OPT_COUNTERS));
2108
2109 break;
2110
2111
Rusty Russell5eed48a2000-06-02 20:12:24 +00002112 case 1: /* non option */
2113 if (optarg[0] == '!' && optarg[1] == '\0') {
2114 if (invert)
2115 exit_error(PARAMETER_PROBLEM,
2116 "multiple consecutive ! not"
2117 " allowed");
2118 invert = TRUE;
2119 optarg[0] = '\0';
2120 continue;
2121 }
2122 printf("Bad argument `%s'\n", optarg);
2123 exit_tryhelp(2);
2124
2125 default:
Rusty Russell5eed48a2000-06-02 20:12:24 +00002126 if (!target
2127 || !(target->parse(c - target->option_offset,
2128 argv, invert,
2129 &target->tflags,
2130 &fw, &target->t))) {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002131 for (matchp = matches; matchp; matchp = matchp->next) {
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002132 if (matchp->completed)
2133 continue;
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002134 if (matchp->match->parse(c - matchp->match->option_offset,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002135 argv, invert,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002136 &matchp->match->mflags,
Rusty Russell5eed48a2000-06-02 20:12:24 +00002137 &fw,
2138 &fw.nfcache,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002139 &matchp->match->m))
Rusty Russell5eed48a2000-06-02 20:12:24 +00002140 break;
2141 }
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002142 m = matchp ? matchp->match : NULL;
Harald Welte00f5a9c2002-08-26 14:37:35 +00002143
2144 /* If you listen carefully, you can
2145 actually hear this code suck. */
2146
2147 /* some explanations (after four different bugs
Joszef Kadlecsika258ad72006-03-03 09:36:50 +00002148 * in 3 different releases): If we encounter a
Harald Welte00f5a9c2002-08-26 14:37:35 +00002149 * parameter, that has not been parsed yet,
2150 * it's not an option of an explicitly loaded
2151 * match or a target. However, we support
2152 * implicit loading of the protocol match
2153 * extension. '-p tcp' means 'l4 proto 6' and
2154 * at the same time 'load tcp protocol match on
2155 * demand if we specify --dport'.
2156 *
2157 * To make this work, we need to make sure:
2158 * - the parameter has not been parsed by
2159 * a match (m above)
2160 * - a protocol has been specified
2161 * - the protocol extension has not been
2162 * loaded yet, or is loaded and unused
Jonas Berlin1b91e592005-04-01 06:58:38 +00002163 * [think of ip6tables-restore!]
Harald Welte00f5a9c2002-08-26 14:37:35 +00002164 * - the protocol extension can be successively
2165 * loaded
2166 */
2167 if (m == NULL
2168 && protocol
2169 && (!find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002170 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002171 || (find_proto(protocol, DONT_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002172 options&OPT_NUMERIC, NULL)
Harald Welte00f5a9c2002-08-26 14:37:35 +00002173 && (proto_used == 0))
2174 )
2175 && (m = find_proto(protocol, TRY_LOAD,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002176 options&OPT_NUMERIC, &matches))) {
Harald Welte00f5a9c2002-08-26 14:37:35 +00002177 /* Try loading protocol */
2178 size_t size;
2179
2180 proto_used = 1;
2181
2182 size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
2183 + m->size;
2184
2185 m->m = fw_calloc(1, size);
2186 m->m->u.match_size = size;
2187 strcpy(m->m->u.user.name, m->name);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002188 if (m->init != NULL)
2189 m->init(m->m, &fw.nfcache);
Harald Welte00f5a9c2002-08-26 14:37:35 +00002190
2191 opts = merge_options(opts,
2192 m->extra_opts, &m->option_offset);
2193
2194 optind--;
2195 continue;
2196 }
2197
Rusty Russell5eed48a2000-06-02 20:12:24 +00002198 if (!m)
2199 exit_error(PARAMETER_PROBLEM,
2200 "Unknown arg `%s'",
2201 argv[optind-1]);
2202 }
2203 }
2204 invert = FALSE;
2205 }
2206
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002207 for (matchp = matches; matchp; matchp = matchp->next)
2208 matchp->match->final_check(matchp->match->mflags);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002209
Harald Welte43ac1912002-03-03 09:43:07 +00002210 if (target)
Rusty Russell5eed48a2000-06-02 20:12:24 +00002211 target->final_check(target->tflags);
2212
2213 /* Fix me: must put inverse options checking here --MN */
2214
2215 if (optind < argc)
2216 exit_error(PARAMETER_PROBLEM,
2217 "unknown arguments found on commandline");
2218 if (!command)
2219 exit_error(PARAMETER_PROBLEM, "no command specified");
2220 if (invert)
2221 exit_error(PARAMETER_PROBLEM,
2222 "nothing appropriate following !");
2223
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002224 if (command & (CMD_REPLACE | CMD_INSERT | CMD_DELETE | CMD_APPEND)) {
Rusty Russell5eed48a2000-06-02 20:12:24 +00002225 if (!(options & OPT_DESTINATION))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002226 dhostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002227 if (!(options & OPT_SOURCE))
András Kis-Szabó764316a2001-02-26 17:31:20 +00002228 shostnetworkmask = "::0/0";
Rusty Russell5eed48a2000-06-02 20:12:24 +00002229 }
2230
2231 if (shostnetworkmask)
2232 parse_hostnetworkmask(shostnetworkmask, &saddrs,
2233 &(fw.ipv6.smsk), &nsaddrs);
2234
2235 if (dhostnetworkmask)
2236 parse_hostnetworkmask(dhostnetworkmask, &daddrs,
2237 &(fw.ipv6.dmsk), &ndaddrs);
2238
2239 if ((nsaddrs > 1 || ndaddrs > 1) &&
2240 (fw.ipv6.invflags & (IP6T_INV_SRCIP | IP6T_INV_DSTIP)))
2241 exit_error(PARAMETER_PROBLEM, "! not allowed with multiple"
2242 " source or destination IP addresses");
2243
Rusty Russell5eed48a2000-06-02 20:12:24 +00002244 if (command == CMD_REPLACE && (nsaddrs != 1 || ndaddrs != 1))
2245 exit_error(PARAMETER_PROBLEM, "Replacement rule does not "
2246 "specify a unique address");
2247
2248 generic_opt_check(command, options);
2249
2250 if (chain && strlen(chain) > IP6T_FUNCTION_MAXNAMELEN)
2251 exit_error(PARAMETER_PROBLEM,
2252 "chain name `%s' too long (must be under %i chars)",
2253 chain, IP6T_FUNCTION_MAXNAMELEN);
2254
Harald Welte43ac1912002-03-03 09:43:07 +00002255 /* only allocate handle if we weren't called with a handle */
Martin Josefsson8371e152003-05-05 19:33:40 +00002256 if (!*handle)
Harald Welte43ac1912002-03-03 09:43:07 +00002257 *handle = ip6tc_init(*table);
András Kis-Szabó3aa62872001-05-03 01:01:41 +00002258
Rusty Russell8beb0492004-12-22 00:37:10 +00002259 /* try to insmod the module if iptc_init failed */
2260 if (!*handle && ip6tables_insmod("ip6_tables", modprobe) != -1)
Harald Welte43ac1912002-03-03 09:43:07 +00002261 *handle = ip6tc_init(*table);
Fabrice MARIE8a5eb6d2001-05-05 21:37:47 +00002262
Harald Welte43ac1912002-03-03 09:43:07 +00002263 if (!*handle)
2264 exit_error(VERSION_PROBLEM,
2265 "can't initialize ip6tables table `%s': %s",
2266 *table, ip6tc_strerror(errno));
Rusty Russell5eed48a2000-06-02 20:12:24 +00002267
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00002268 if (command == CMD_APPEND
Rusty Russell5eed48a2000-06-02 20:12:24 +00002269 || command == CMD_DELETE
2270 || command == CMD_INSERT
2271 || command == CMD_REPLACE) {
Harald Welte43ac1912002-03-03 09:43:07 +00002272 if (strcmp(chain, "PREROUTING") == 0
2273 || strcmp(chain, "INPUT") == 0) {
2274 /* -o not valid with incoming packets. */
2275 if (options & OPT_VIANAMEOUT)
2276 exit_error(PARAMETER_PROBLEM,
2277 "Can't use -%c with %s\n",
2278 opt2char(OPT_VIANAMEOUT),
2279 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002280 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002281
Harald Welte43ac1912002-03-03 09:43:07 +00002282 if (strcmp(chain, "POSTROUTING") == 0
2283 || strcmp(chain, "OUTPUT") == 0) {
2284 /* -i not valid with outgoing packets */
2285 if (options & OPT_VIANAMEIN)
2286 exit_error(PARAMETER_PROBLEM,
2287 "Can't use -%c with %s\n",
2288 opt2char(OPT_VIANAMEIN),
2289 chain);
Harald Welte43ac1912002-03-03 09:43:07 +00002290 }
Rusty Russell5eed48a2000-06-02 20:12:24 +00002291
2292 if (target && ip6tc_is_chain(jumpto, *handle)) {
2293 printf("Warning: using chain %s, not extension\n",
2294 jumpto);
2295
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002296 if (target->t)
2297 free(target->t);
2298
Rusty Russell5eed48a2000-06-02 20:12:24 +00002299 target = NULL;
2300 }
2301
2302 /* If they didn't specify a target, or it's a chain
2303 name, use standard. */
2304 if (!target
2305 && (strlen(jumpto) == 0
2306 || ip6tc_is_chain(jumpto, *handle))) {
2307 size_t size;
2308
2309 target = find_target(IP6T_STANDARD_TARGET,
2310 LOAD_MUST_SUCCEED);
2311
2312 size = sizeof(struct ip6t_entry_target)
2313 + target->size;
2314 target->t = fw_calloc(1, size);
2315 target->t->u.target_size = size;
2316 strcpy(target->t->u.user.name, jumpto);
Jonas Berlin1b91e592005-04-01 06:58:38 +00002317 if (target->init != NULL)
2318 target->init(target->t, &fw.nfcache);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002319 }
2320
2321 if (!target) {
Harald Welte43ac1912002-03-03 09:43:07 +00002322 /* it is no chain, and we can't load a plugin.
2323 * We cannot know if the plugin is corrupt, non
2324 * existant OR if the user just misspelled a
2325 * chain. */
2326 find_target(jumpto, LOAD_MUST_SUCCEED);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002327 } else {
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002328 e = generate_entry(&fw, matches, target->t);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002329 free(target->t);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002330 }
2331 }
2332
2333 switch (command) {
2334 case CMD_APPEND:
2335 ret = append_entry(chain, e,
2336 nsaddrs, saddrs, ndaddrs, daddrs,
2337 options&OPT_VERBOSE,
2338 handle);
2339 break;
Rusty Russell5eed48a2000-06-02 20:12:24 +00002340 case CMD_DELETE:
2341 ret = delete_entry(chain, e,
2342 nsaddrs, saddrs, ndaddrs, daddrs,
2343 options&OPT_VERBOSE,
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002344 handle, matches);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002345 break;
2346 case CMD_DELETE_NUM:
2347 ret = ip6tc_delete_num_entry(chain, rulenum - 1, handle);
2348 break;
2349 case CMD_REPLACE:
2350 ret = replace_entry(chain, e, rulenum - 1,
2351 saddrs, daddrs, options&OPT_VERBOSE,
2352 handle);
2353 break;
2354 case CMD_INSERT:
2355 ret = insert_entry(chain, e, rulenum - 1,
2356 nsaddrs, saddrs, ndaddrs, daddrs,
2357 options&OPT_VERBOSE,
2358 handle);
2359 break;
2360 case CMD_LIST:
2361 ret = list_entries(chain,
2362 options&OPT_VERBOSE,
2363 options&OPT_NUMERIC,
2364 options&OPT_EXPANDED,
2365 options&OPT_LINENUMBERS,
2366 handle);
2367 break;
2368 case CMD_FLUSH:
2369 ret = flush_entries(chain, options&OPT_VERBOSE, handle);
2370 break;
2371 case CMD_ZERO:
2372 ret = zero_entries(chain, options&OPT_VERBOSE, handle);
2373 break;
2374 case CMD_LIST|CMD_ZERO:
2375 ret = list_entries(chain,
2376 options&OPT_VERBOSE,
2377 options&OPT_NUMERIC,
2378 options&OPT_EXPANDED,
2379 options&OPT_LINENUMBERS,
2380 handle);
2381 if (ret)
2382 ret = zero_entries(chain,
2383 options&OPT_VERBOSE, handle);
2384 break;
2385 case CMD_NEW_CHAIN:
2386 ret = ip6tc_create_chain(chain, handle);
2387 break;
2388 case CMD_DELETE_CHAIN:
2389 ret = delete_chain(chain, options&OPT_VERBOSE, handle);
2390 break;
2391 case CMD_RENAME_CHAIN:
2392 ret = ip6tc_rename_chain(chain, newname, handle);
2393 break;
2394 case CMD_SET_POLICY:
Harald Welted8e65632001-01-05 15:20:07 +00002395 ret = ip6tc_set_policy(chain, policy, NULL, handle);
Rusty Russell5eed48a2000-06-02 20:12:24 +00002396 break;
2397 default:
2398 /* We should never reach this... */
2399 exit_tryhelp(2);
2400 }
2401
2402 if (verbose > 1)
2403 dump_entries6(*handle);
2404
Martin Josefsson69ac0e02004-02-02 20:02:10 +00002405 clear_rule_matches(&matches);
2406
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002407 if (e != NULL) {
2408 free(e);
2409 e = NULL;
2410 }
2411
2412 for (c = 0; c < nsaddrs; c++)
2413 free(&saddrs[c]);
2414
2415 for (c = 0; c < ndaddrs; c++)
2416 free(&daddrs[c]);
2417
Pablo Neiradfdcd642005-05-29 19:05:23 +00002418 free_opts(1);
Martin Josefsson4dd5fed2004-05-18 18:09:43 +00002419
Rusty Russell5eed48a2000-06-02 20:12:24 +00002420 return ret;
2421}